/**
  * Code inspired by https://github.com/etsy/statsd/blob/master/examples/php-example.php
  */
 public static function doLog($logMethod, array $data, &$output)
 {
     list($host, $port, $types) = eZPerfLoggerINI::variableMulti('StatsdSettings', array('Host', 'Port', 'VariableTypes'));
     // Failures in any of this should be silently ignored
     try {
         $strings = array();
         foreach ($data as $varName => $value) {
             $type = isset($types[$varName]) && $types[$varName] != '' ? $types[$varName] : 'ms';
             $strings[] = static::transformVarName($varName) . ":{$value}|{$type}";
         }
         $fp = fsockopen("udp://{$host}", (int) $port, $errNo, $errStr);
         if (!$fp) {
             eZPerfLoggerDebug::writeWarning("Could not open udp socket to {$host}:{$port} - {$errStr}", __METHOD__);
             return;
         }
         if (eZPerfLoggerINI::variable('StatsdSettings', 'SendMetricsInSinglePacket') == 'enabled') {
             fwrite($fp, implode("\n", $strings));
         } else {
             foreach ($strings as $string) {
                 fwrite($fp, $string);
             }
         }
         fclose($fp);
     } catch (Exception $e) {
     }
 }
 /**
  * Stops XHProf profiling and saves profile data in var/log/xhprof
  *
  * @return mixed false|string (the run_id)|true (when nosave==true)
  */
 public static function stop($dosave = true)
 {
     if (!extension_loaded('xhprof')) {
         eZPerfLoggerDebug::writeWarning('Extension xhprof not loaded, can not stop profiling', __METHOD__);
         return false;
     }
     if (!self::$profilingRunning) {
         return false;
     }
     $xhprofData = xhprof_disable();
     self::$profilingRunning = false;
     if (!$dosave) {
         return true;
     }
     if (!is_dir(self::$logdir)) {
         mkdir(self::$logdir);
     }
     $logger = new XHProfRuns_Default(self::$logdir);
     $runId = $logger->save_run($xhprofData, "xhprof");
     if ($runId) {
         // beside profiling data, save extra info in another file to make it more useful later
         file_put_contents(self::$logdir . "/{$runId}.info", eZPerfLoggerApacheLogger::apacheLogLine('combined'));
         self::$runs[] = $runId;
     }
     return $runId;
 }
 /**
  * Reset accumulators values so far: either a single one or all of them.
  * NB: also clears the ezdebug accumulators
  */
 public static function accumulatorReset($val = null)
 {
     if ($val === null) {
         self::$timeAccumulatorList = array();
         if (eZPerfLoggerDebug::isDebugEnabled()) {
             $debug = eZDebug::instance();
             $debug->TimeAccumulatorList = array();
         }
     } else {
         unset(self::$timeAccumulatorList[$val]);
     }
 }
 /**
  * Rotates a log file, if it's bigger than  $maxSize bytes.
  * Rotated files get a .20120612_122359 stringa ppended to their name
  * If $maxFiles is > 0, only $maxFiles files are kept, other eliminated
  */
 public static function rotateLogs($dir, $filename, $maxSize = 0, $maxFiles = 0)
 {
     $filepath = "{$dir}/{$filename}";
     if (is_file($filepath) && filesize($filepath) > $maxSize) {
         if (!rename($filepath, $filepath . "." . strftime('%Y%m%d_%H%M%S'))) {
             eZPerfLoggerDebug::writeWarning("Could not rotate log file {$filepath}", __METHOD__);
         }
     }
     if ($maxFiles) {
         $files = array();
         foreach (scandir($dir) as $afile) {
             if (is_file("{$dir}/{$afile}") && strpos($afile, $filename) === 0) {
                 $ext = substr(strrchr($afile, "."), 1);
                 $files[$ext] = $afile;
             }
         }
         if (count($files) > $maxFiles) {
             ksort($files);
             $oldest = "{$dir}/" . reset($files);
             if (!unlink($oldest)) {
                 eZPerfLoggerDebug::writeWarning("Could not remove log file {$oldest}", __METHOD__);
             }
         }
     }
 }