PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/code/classes/pinetd/Logger.class.php

https://github.com/blekkzor/pinetd2
PHP | 130 lines | 44 code | 12 blank | 74 comment | 5 complexity | 20dd8196bae03d4a88485ed0e026ebc6 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /* Portable INET daemon v2 in PHP
  3. * Copyright (C) 2007 Mark Karpeles <mark@kinoko.fr>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. /**
  20. * \file Logger.class.php
  21. * \brief Logger class, a fork-aware logger
  22. * \namespace pinetd
  23. */
  24. namespace pinetd;
  25. /**
  26. * \class Logger
  27. * \brief Logger class, for easy logging
  28. *
  29. * This class main function, Logger::log, is used to add a message to the main log.
  30. */
  31. class Logger {
  32. /**
  33. * \brief $self as we are a singleton
  34. */
  35. private static $self = null;
  36. /**
  37. * \brief Reference to the IPC class
  38. */
  39. private $IPC = null;
  40. const LOG_DEBUG= 'DEBUG'; /*!< Signals a DEBUG log message */
  41. const LOG_INFO = 'INFO'; /*!< A simple log message, with no real importance */
  42. const LOG_WARN = 'WARN'; /*!< Something went bad, but it's not fatal. Should be fixed if possible */
  43. const LOG_ERR = 'ERR'; /*!< A fatal error occured, and the daemon had to stop its current operation */
  44. const LOG_CRIT = 'CRIT'; /*!< A fatal error occured, causing the daemon to stop */
  45. /**
  46. * \brief Invokes Logger singleton, and returns it
  47. * \return a Logger (unique) instance
  48. */
  49. private function invoke() {
  50. if (is_null(self::$self)) self::$self = new self();
  51. return self::$self;
  52. }
  53. /**
  54. * \brief Log data to the main log
  55. * \param $level one of Logger::LOG_DEBUG Logger;;LOG_INFO Logger::WARN Logger::LOG_ERR Logger::LOG_CRIT
  56. * \param $msg string to log
  57. * \return bool Logging successful?
  58. */
  59. public static function log($level, $msg) {
  60. $logger = self::invoke();
  61. return $logger->do_log(getmypid(), time(), $level, $msg);
  62. }
  63. /**
  64. * \brief Log with full attributes definition
  65. * \param $pid int Pid originating the log
  66. * \param $stamp int Timestamp associated with log entry
  67. * \param $level See $level parameter of Logger::log
  68. * \param $msg string to log
  69. * \return bool Logging successful?
  70. *
  71. * This function is identiqual to Logger::log() but provides more control over
  72. * logged data. This function SHOULD NOT be used directly.
  73. */
  74. public static function log_full($pid, $stamp, $level, $msg) {
  75. $logger = self::invoke();
  76. return $logger->do_log($pid, $stamp, $level, $msg);
  77. }
  78. /**
  79. * \brief real function behind logging
  80. * \internal
  81. * \param $pid int Pid originating the log
  82. * \param $stamp int Timestamp associated with log entry
  83. * \param $level See $level parameter of Logger::log
  84. * \param $msg string to log
  85. * \return bool Logging successful?
  86. */
  87. public function do_log($pid, $stamp, $level, $msg) {
  88. if (!is_null($this->IPC)) {
  89. return $this->IPC->log($pid, $stamp, $level, $msg);
  90. }
  91. // TODO: allow to override this in config
  92. $logdir = PINETD_ROOT . '/logs';
  93. if (!is_dir($logdir)) mkdir($logdir);
  94. $fp = fopen($logdir . '/system_'.date('Y-m').'.log', 'a');
  95. if (!$fp) {
  96. echo '['.date('Y-m-d H:i:s').':'.$pid.'] '.$level.': '.$msg."\n";
  97. } else {
  98. fwrite($fp, '['.date('Y-m-d H:i:s').':'.$pid.'] '.$level.': '.$msg."\n");
  99. fclose($fp);
  100. }
  101. }
  102. /**
  103. * \brief Define IPC used by logger
  104. * \param $IPC New IPC class (after a fork for example)
  105. */
  106. public static function setIPC(&$IPC) {
  107. $logger = self::invoke();
  108. return $logger->real_setIPC($IPC);
  109. }
  110. /**
  111. * \brief Define IPC used by logger, internal use only
  112. * \param $IPC New IPC class (after a fork for example)
  113. * \internal
  114. */
  115. public function real_setIPC(&$IPC) {
  116. $this->IPC = &$IPC;
  117. }
  118. }