PageRenderTime 37ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/administrator/components/com_joomlapack/includes/CJPLogger.php

https://bitbucket.org/dgough/annamaria-daneswood-25102012
PHP | 128 lines | 90 code | 9 blank | 29 comment | 7 complexity | df5f60b1d61da70573144eff06515a32 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * @package JoomlaPack
  4. * @subpackage BaseClasses
  5. * @copyright Copyright (C) 2006-2008 JoomlaPack Developers. All rights reserved.
  6. * @version $Id$
  7. * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
  8. *
  9. * JoomlaPack is free software. This version may have been modified pursuant
  10. * to the GNU General Public License, and as distributed it includes or
  11. * is derivative of works licensed under the GNU General Public License or
  12. * other free or open source software licenses.
  13. */
  14. // ensure this file is being included by a parent file - Joomla! 1.0.x and 1.5 compatible
  15. (defined( '_VALID_MOS' ) || defined('_JEXEC')) or die( 'Direct Access to this location is not allowed.' );
  16. // Log levels
  17. define("_JP_LOG_NONE", 0);
  18. define("_JP_LOG_ERROR", 1);
  19. define("_JP_LOG_WARNING", 2);
  20. define("_JP_LOG_INFO", 3);
  21. define("_JP_LOG_DEBUG", 4);
  22. class CJPLogger
  23. {
  24. /**
  25. * Clears the logfile
  26. */
  27. function ResetLog() {
  28. $logName = CJPLogger::logName();
  29. @unlink( $logName );
  30. touch( $logName );
  31. }
  32. /**
  33. * Writes a line to the log, if the log level is high enough
  34. *
  35. * @param integer $level The log level (_JP_LOG_XX constants)
  36. * @param string $message The message to write to the log
  37. */
  38. function WriteLog( $level, $message )
  39. {
  40. $configuration =& CConfiguration::getInstance();
  41. if( $configuration->logLevel >= $level and $configuration->logLevel != 0)
  42. {
  43. $logName = CJPLogger::logName();
  44. $message = str_replace( JPSiteRoot, "<root>", $message );
  45. $message = str_replace( "\n", '\n', $message ); // Fix 1.1.1 - Handle (error) messages containing newlines (by nicholas)
  46. switch( $level )
  47. {
  48. case _JP_LOG_ERROR:
  49. $string = "ERROR |";
  50. break;
  51. case _JP_LOG_WARNING:
  52. $string = "WARNING |";
  53. break;
  54. case _JP_LOG_INFO:
  55. $string = "INFO |";
  56. break;
  57. default:
  58. $string = "DEBUG |";
  59. break;
  60. }
  61. $string .= strftime( "%y%m%d %R" ) . "|$message\n";
  62. $fp = fopen( $logName, "at" );
  63. if (!($fp === FALSE))
  64. {
  65. fwrite( $fp, $string );
  66. fclose( $fp );
  67. }
  68. }
  69. }
  70. /**
  71. * Parses the log file and outputs formatted HTML to the standard output
  72. */
  73. function VisualizeLogDirect()
  74. {
  75. $logName = CJPLogger::logName();
  76. if(!file_exists($logName)) return false; //joostina pach
  77. $fp = fopen( $logName, "rt" );
  78. if ($fp === FALSE) return false;
  79. while( !feof($fp) )
  80. {
  81. $line = fgets( $fp );
  82. if(!$line) return;
  83. $exploded = explode( "|", $line, 3 );
  84. unset( $line );
  85. switch( trim($exploded[0]) )
  86. {
  87. case "ERROR":
  88. $fmtString = "<span style=\"color: red; font-weight: bold;\">[";
  89. break;
  90. case "WARNING":
  91. $fmtString = "<span style=\"color: #D8AD00; font-weight: bold;\">[";
  92. break;
  93. case "INFO":
  94. $fmtString = "<span style=\"color: black;\">[";
  95. break;
  96. case "DEBUG":
  97. $fmtString = "<span style=\"color: #666666; font-size: small;\">[";
  98. break;
  99. default:
  100. $fmtString = "<span style=\"font-size: small;\">[";
  101. break;
  102. }
  103. $fmtString .= $exploded[1] . "] " . htmlspecialchars($exploded[2]) . "</span><br/>\n";
  104. unset( $exploded );
  105. echo $fmtString;
  106. unset( $fmtString );
  107. }
  108. ob_flush();
  109. }
  110. /**
  111. * Calculates the absolute path to the log file
  112. */
  113. function logName()
  114. {
  115. $configuration =& CConfiguration::getInstance();
  116. return CJVAbstract::TranslateWinPath( $configuration->OutputDirectory . "/joomlapack.log" );
  117. }
  118. }
  119. ?>