/lib/OA/Upgrade/UpgradeLogger.php

https://bitbucket.org/valmy/openx · PHP · 197 lines · 106 code · 16 blank · 75 comment · 9 complexity · 057690345029d66f75975702c1bba004 MD5 · raw file

  1. <?php
  2. /*
  3. +---------------------------------------------------------------------------+
  4. | OpenX v2.8 |
  5. | ========== |
  6. | |
  7. | Copyright (c) 2003-2009 OpenX Limited |
  8. | For contact details, see: http://www.openx.org/ |
  9. | |
  10. | This program is free software; you can redistribute it and/or modify |
  11. | it under the terms of the GNU General Public License as published by |
  12. | the Free Software Foundation; either version 2 of the License, or |
  13. | (at your option) any later version. |
  14. | |
  15. | This program is distributed in the hope that it will be useful, |
  16. | but WITHOUT ANY WARRANTY; without even the implied warranty of |
  17. | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
  18. | GNU General Public License for more details. |
  19. | |
  20. | You should have received a copy of the GNU General Public License |
  21. | along with this program; if not, write to the Free Software |
  22. | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
  23. +---------------------------------------------------------------------------+
  24. $Id$
  25. */
  26. /**
  27. * OpenXUpgrade Class
  28. *
  29. * @author Monique Szpak <monique.szpak@openx.org>
  30. */
  31. class OA_UpgradeLogger
  32. {
  33. var $aErrors = array();
  34. var $aMessages = array();
  35. var $warningExists = false;
  36. var $errorExists = false;
  37. var $logFile;
  38. /**
  39. * php5 class constructor
  40. *
  41. * simpletest throws a BadGroupTest error
  42. * Redefining already defined constructor for class Openads_DB_Upgrade
  43. * when both constructors are present
  44. *
  45. */
  46. // function __construct()
  47. // {
  48. // }
  49. /**
  50. * php4 class constructor
  51. *
  52. * @return boolean
  53. */
  54. function OA_UpgradeLogger()
  55. {
  56. //this->__construct();
  57. $this->logFile = MAX_PATH."/var/install.log";
  58. }
  59. function setLogFile($logfile)
  60. {
  61. $this->logFile = MAX_PATH."/var/{$logfile}";
  62. if (file_exists($this->logFile))
  63. {
  64. unlink($this->logFile);
  65. }
  66. $this->_logWrite(date('Y-m-d h:i:s'));
  67. }
  68. /**
  69. * check for a Pear error
  70. * log the error
  71. * returns true if pear error, false if not
  72. *
  73. * @param mixed $result
  74. * @param string $message
  75. * @return boolean
  76. */
  77. function isPearError($result, $message='omg it all went PEAR shaped!')
  78. {
  79. if (PEAR::isError($result))
  80. {
  81. $this->logError($message.' '. $result->getUserInfo());
  82. return true;
  83. }
  84. return false;
  85. }
  86. function logClear()
  87. {
  88. $this->aMessages = array();
  89. $this->aErrors = array();
  90. $this->errorExists = false;
  91. $this->warningExists = false;
  92. }
  93. /**
  94. * write a message to the logfile
  95. *
  96. * @param string $message
  97. */
  98. function log($message)
  99. {
  100. $this->aMessages[] = $message;
  101. $this->_logWrite($message);
  102. }
  103. function logOnly($message)
  104. {
  105. $this->_logWrite($message);
  106. }
  107. /**
  108. * write a warning to the log file
  109. *
  110. * @param string $message
  111. */
  112. function logWarning($message)
  113. {
  114. $this->aMessages[] = "#> {$message}";
  115. $this->_logWrite("#> {$message}");
  116. $this->warningExists = true;
  117. }
  118. /**
  119. * write an error to the log file
  120. *
  121. * @param string $message
  122. */
  123. function logError($message)
  124. {
  125. $this->log("#! {$message}");
  126. $this->errorExists = true;
  127. }
  128. /**
  129. * Writes an error message to the log file if $message is not empty.
  130. *
  131. * @param string $message
  132. */
  133. function logErrorUnlessEmpty($message)
  134. {
  135. if (!empty($message)) {
  136. $this->logError($message);
  137. }
  138. }
  139. function _logWrite($message)
  140. {
  141. if (empty($this->logFile))
  142. {
  143. $this->logBuffer[] = $message;
  144. } else
  145. {
  146. $log = fopen($this->logFile, 'a');
  147. if (count($this->logBuffer))
  148. {
  149. $message = join("\n", $this->logBuffer);
  150. $this->logBuffer = array();
  151. }
  152. fwrite($log, "{$message}\n");
  153. fclose($log);
  154. }
  155. }
  156. function getLogfilesList()
  157. {
  158. $aFiles = array();
  159. $dh = opendir(MAX_PATH.'/var');
  160. if ($dh) {
  161. while (false !== ($file = readdir($dh)))
  162. {
  163. $aMatches = array();
  164. if (preg_match('/openads_upgrade_[\w\W\d]+\.log/', $file, $aMatches))
  165. {
  166. $aFiles[] = basename($file);
  167. }
  168. }
  169. }
  170. closedir($dh);
  171. return $aFiles;
  172. }
  173. function deleteLogFile()
  174. {
  175. if (file_exists($this->logFile))
  176. {
  177. unlink($this->logFile);
  178. }
  179. }
  180. }
  181. ?>