Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. <?php
  2.  
  3. $config = $this->diContainer->get(CCS::CONFIG);
  4. $logger = $this->diContainer->get(CCS::LOGGER);
  5. $registry = $this->diContainer->get(CCS::REGISTRY);
  6. $utils = $this->diContainer->get(CCS::UTILS);
  7.  
  8. ini_set('display_errors', boolval(0 !== $registry->devmode));
  9. error_reporting(E_ALL);
  10.  
  11. set_error_handler(
  12. function ($exception) use ($logger) {
  13. if ($exception instanceof \Exception) {
  14. $logger->error($exception->__toString());
  15. } else {
  16. $logger->error(
  17. json_encode(debug_backtrace()) . json_encode($exception)
  18. );
  19. }
  20. }
  21. );
  22.  
  23. set_exception_handler(
  24. function () use ($logger) {
  25. $logger->error(json_encode(debug_backtrace()));
  26. }
  27. );
  28.  
  29. register_shutdown_function(
  30. function () use ($logger, $registry, $utils) {
  31. $memory_difference = (
  32. memory_get_usage() - $registry->metricsMemory
  33. );
  34. $executionDifference = (
  35. microtime(true) - $registry->metricsMxecution
  36. );
  37.  
  38. if (0 !== $registry->devmode) {
  39. $logger->info(
  40. sprintf(
  41. 'Shutdown completed [%s] - [%s]',
  42. $utils->timeToHuman($executionDifference),
  43. $utils->bytesToHuman($memoryDifference)
  44. )
  45. );
  46. }
  47. }
  48. );
  49.  
  50. $timezone = $config->get('app_timezone', 'US/Eastern');
  51. date_default_timezone_set($timezone);
  52.  
  53.  
  54.  
  55.  
  56. public function timeToHuman($microseconds, $precision = 3)
  57. {
  58. $units = ['μs', 'ns', 'ms', 's'];
  59. $micro = max($microseconds, 0);
  60. $pow = 0;
  61. if (1000 < $micro) {
  62. $pow = floor(($micro ? log($micro) : 0) / log(1000));
  63. $pow = min($pow, count($units) - 1);
  64. $micro /= (1 << (10 * $pow));
  65. }
  66.  
  67. return round($micro, $precision) . ' ' . $units[$pow];
  68. }
  69.  
  70.  
  71. public function bytesToHuman($bytes, $precision = 2)
  72. {
  73. $units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];
  74. $bytes = max($bytes, 0);
  75. $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
  76. $pow = min($pow, count($units) - 1);
  77. $bytes /= (1 << (10 * $pow));
  78.  
  79. return round($bytes, $precision) . ' ' . $units[$pow];
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement