Skip to content

Commit

Permalink
Using microtime properly for all Log-specific dates. Added new tool-m…
Browse files Browse the repository at this point in the history
…ethod called NotNowButRightNow. added parent path to request hash for clarity
  • Loading branch information
enobrev committed Sep 7, 2016
1 parent 5d19ad2 commit 46d3b52
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"files": [
"src/ErrorsToExceptions.php",
"src/Arrays.php",
"src/DateTime.php",
"src/Debugging.php",
"src/Headers.php",
"src/Strings.php"
Expand Down
4 changes: 2 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions src/DateTime.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
namespace Enobrev;

use DateTime;

/**
* Returns DateTime object with proper microtime
* @return DateTime
*/
function notNowByRightNow() {
$aMicroTime = explode(' ', microtime());
return new DateTime(date('Y-m-d H:i:s.' . $aMicroTime[0] * 1000000, $aMicroTime[1]));
}
15 changes: 10 additions & 5 deletions src/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ private static function getThreadHash() {
} else if (isset($_REQUEST['__t'])) {
self::$sThreadHash = $_REQUEST['__t'];
} else {
self::$sThreadHash = substr(hash('sha1', (new DateTime())->format('Y-m-d G:i:s u')), 0, 6);
self::$sThreadHash = substr(hash('sha1', notNowByRightNow()->format('Y-m-d G:i:s.u')), 0, 6);
}

return self::$sThreadHash;
Expand All @@ -232,6 +232,13 @@ private static function getParentHash() {
}
}

/**
* @return string
*/
private static function getParentPath() {
return implode('.', self::$aHashHistory);
}

/**
* @internal param bool $bForceReset
* @return string
Expand All @@ -241,10 +248,8 @@ private static function getRequestHash() {
$sIP = get_ip();
$sAgent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';

$aMicroTime = explode(' ', microtime());
$oNow = new DateTime(date('Y-m-d H:i:s.' . $aMicroTime[0] * 1000000, $aMicroTime[1]));
$aRequest = array(
'date' => $oNow->format('Y-m-d H:i:s.u')
'date' => notNowByRightNow()->format('Y-m-d H:i:s.u')
);

if (isset($_SERVER['HTTP_REFERER'])) { $aRequest['referrer'] = $_SERVER['HTTP_REFERER']; }
Expand All @@ -253,7 +258,7 @@ private static function getRequestHash() {
if (strlen($sAgent)) { $aRequest['agent'] = $sAgent; }
if ($sIP != 'unknown') { $aRequest['ip'] = $sIP; }

self::$sRequestHash = substr(hash('sha1', json_encode($aRequest)), 0, 8);
self::$sRequestHash = self::getParentPath() . '.' . substr(hash('sha1', json_encode($aRequest)), 0, 6);

$aMessage = array(
'action' => 'Log.Start',
Expand Down

0 comments on commit 46d3b52

Please sign in to comment.