/**
  * This method is called to allow this class to provide values for the perf
  * variables it caters to.
  * In this case, it actually gets called by self::filter().
  * To avoid unnecessary overhead, it cheats a little bit, and it does not provide
  * values for ALL variables it supports, but only for the ones it knows will
  * be logged.
  * @param string $output
  * @param $returnCode
  * @return array
  */
 public static function measure($output, $returnCode = null)
 {
     global $scriptStartTime;
     // This var we want to save as it is used for logs even when not present in TrackVariables.
     // Also using ga / piwik logs do alter $output, making length calculation in doLog() unreliable
     /// @todo this way of passing data around is not really beautiful...
     self::$outputSize = strlen($output);
     if ($returnCode !== null) {
         self::$returnCode = (int) $returnCode;
     } else {
         // for cli scripts, set default response status to 0 instead of 200
         if (eZSys::isShellExecution()) {
             self::$returnCode = 0;
         }
     }
     $out = array();
     $vars = eZPerfLoggerINI::variable('GeneralSettings', 'TrackVariables');
     foreach ($vars as $var) {
         switch ($var) {
             case 'output_size':
                 // some bugs persist forever...
             // some bugs persist forever...
             case 'ouput_size':
                 $out[$var] = self::$outputSize;
                 break;
             case 'execution_time':
                 // This global var does not exist anymore in eZP LS 5.0.
                 // We prefer using it when available as it is slightly more accurate
                 if ($scriptStartTime == 0) {
                     $debug = eZDebug::instance();
                     $scriptStartTime = $debug->ScriptStart;
                 }
                 $out[$var] = round(microtime(true) - $scriptStartTime, 3);
                 break;
             case 'mem_usage':
                 $out[$var] = round(memory_get_peak_usage(true), -3);
                 break;
             case 'db_queries':
                 // (nb: only works when debug is enabled.
                 // Also does most likely not work when logging is done directly from the eZ5 stack
                 /// @todo fix to run from eZ5 context
                 $dbini = eZINI::instance();
                 // we cannot use $db->databasename() because we get the same for mysql and mysqli
                 $type = preg_replace('/^ez/', '', $dbini->variable('DatabaseSettings', 'DatabaseImplementation'));
                 $type .= '_query';
                 // read accumulator
                 $debug = eZDebug::instance();
                 if (isset($debug->TimeAccumulatorList[$type])) {
                     $queries = $debug->TimeAccumulatorList[$type]['count'];
                 } else {
                     // NB: to tell difference between 0 db reqs per page and no debug we could look for ezdebug::isenabled,
                     // but what if it was enabled at some point and later disabled?...
                     $queries = "0";
                 }
                 $out[$var] = $queries;
                 break;
             case 'xhkprof_runs':
                 $out[$var] = implode(',', eZXHProfLogger::runs());
                 break;
             case 'user_id':
                 $out[$var] = eZUser::currentUser()->attribute('contentobject_id');
                 break;
             case 'unique_id':
                 $out[$var] = $_SERVER['UNIQUE_ID'];
                 break;
                 //case 'content/nodeid':
                 //    $out[$var] = self::$nodeId;
                 //    break;
             //case 'content/nodeid':
             //    $out[$var] = self::$nodeId;
             //    break;
             default:
                 // wildcard-based naming:
                 // content-info things, useful to help group/filter recorded data
                 if (strpos($var, 'content_info/') === 0 || strpos($var, 'module_result/') === 0) {
                     $out[$var] = self::getModuleResultData($var);
                     break;
                 }
                 // standard accumulators
                 /// @todo fix to run from eZ5 context
                 if (strpos($var, 'accumulators/') === 0) {
                     $parts = explode('/', $var, 3);
                     $type = $parts[1];
                     $debug = eZDebug::instance();
                     if (isset($debug->TimeAccumulatorList[$type])) {
                         if (@$parts[2] === 'count') {
                             $out[$var] = $debug->TimeAccumulatorList[$type]['count'];
                         } else {
                             $out[$var] = round($debug->TimeAccumulatorList[$type]['time'], 3);
                         }
                     } else {
                         $out[$var] = -1;
                     }
                     break;
                 }
                 // everything in $_SERVER
                 if (strpos($var, '_server/') === 0) {
                     $parts = explode('/', $var, 2);
                     $val = @$_SERVER[$parts[1]];
                     $out[$var] = $val;
                     break;
                 }
         }
     }
     return $out;
 }