log.class.inc.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. // Copyright (C) 2010-2012 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. /**
  19. * File logging
  20. *
  21. * @copyright Copyright (C) 2010-2012 Combodo SARL
  22. * @license http://opensource.org/licenses/AGPL-3.0
  23. */
  24. class FileLog
  25. {
  26. protected $m_sFile = ''; // log is disabled if this is empty
  27. public function __construct($sFileName = '')
  28. {
  29. $this->m_sFile = $sFileName;
  30. }
  31. public function Error($sText)
  32. {
  33. self::Write("Error | ".$sText);
  34. }
  35. public function Warning($sText)
  36. {
  37. self::Write("Warning | ".$sText);
  38. }
  39. public function Info($sText)
  40. {
  41. self::Write("Info | ".$sText);
  42. }
  43. public function Ok($sText)
  44. {
  45. self::Write("Ok | ".$sText);
  46. }
  47. protected function Write($sText)
  48. {
  49. if (strlen($this->m_sFile) == 0) return;
  50. $hLogFile = @fopen($this->m_sFile, 'a');
  51. if ($hLogFile !== false)
  52. {
  53. flock($hLogFile, LOCK_EX);
  54. $sDate = date('Y-m-d H:i:s');
  55. fwrite($hLogFile, "$sDate | $sText\n");
  56. fflush($hLogFile);
  57. flock($hLogFile, LOCK_UN);
  58. fclose($hLogFile);
  59. }
  60. }
  61. }
  62. class SetupLog
  63. {
  64. protected static $m_oFileLog;
  65. public static function Enable($sTargetFile)
  66. {
  67. self::$m_oFileLog = new FileLog($sTargetFile);
  68. }
  69. public static function Error($sText)
  70. {
  71. self::$m_oFileLog->Error($sText);
  72. }
  73. public static function Warning($sText)
  74. {
  75. self::$m_oFileLog->Warning($sText);
  76. }
  77. public static function Info($sText)
  78. {
  79. self::$m_oFileLog->Info($sText);
  80. }
  81. public static function Ok($sText)
  82. {
  83. self::$m_oFileLog->Ok($sText);
  84. }
  85. }
  86. class IssueLog
  87. {
  88. protected static $m_oFileLog;
  89. public static function Enable($sTargetFile)
  90. {
  91. self::$m_oFileLog = new FileLog($sTargetFile);
  92. }
  93. public static function Error($sText)
  94. {
  95. self::$m_oFileLog->Error($sText);
  96. }
  97. public static function Warning($sText)
  98. {
  99. self::$m_oFileLog->Warning($sText);
  100. }
  101. public static function Info($sText)
  102. {
  103. self::$m_oFileLog->Info($sText);
  104. }
  105. public static function Ok($sText)
  106. {
  107. self::$m_oFileLog->Ok($sText);
  108. }
  109. }
  110. class ToolsLog
  111. {
  112. protected static $m_oFileLog;
  113. public static function Enable($sTargetFile)
  114. {
  115. self::$m_oFileLog = new FileLog($sTargetFile);
  116. }
  117. public static function Error($sText)
  118. {
  119. self::$m_oFileLog->Error($sText);
  120. }
  121. public static function Warning($sText)
  122. {
  123. self::$m_oFileLog->Warning($sText);
  124. }
  125. public static function Info($sText)
  126. {
  127. self::$m_oFileLog->Info($sText);
  128. }
  129. public static function Ok($sText)
  130. {
  131. self::$m_oFileLog->Ok($sText);
  132. }
  133. }
  134. ?>