class.swpm-log.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. class SwpmLog {
  3. private $error;
  4. private $warn;
  5. private $notice;
  6. private static $intance;
  7. private function __construct() {
  8. $this->error = array();
  9. $this->warn = array();
  10. $this->notice = array();
  11. }
  12. public static function get_logger($context = ''){
  13. $context = empty($context)? 'default': $context;
  14. if (!isset(self::$intance[$context])){
  15. self::$intance[$context] = new SwpmLog();
  16. }
  17. return self::$intance[$context];
  18. }
  19. public function error($msg){
  20. $this->error[] = $msg;
  21. }
  22. public function warn($msg){
  23. $this->warn[] = $msg;
  24. }
  25. public function debug($msg){
  26. $this->notice[] = $msg;
  27. }
  28. public function get($to_screen = false){
  29. $msg = '';
  30. foreach ($this->error as $error ){
  31. $msg .= 'ERROR: ' . $error . ($to_screen?"<br/>":"\n");
  32. }
  33. foreach($this->warn as $warn){
  34. $msg .= 'WARN: ' . $warn . ($to_screen?"<br/>":"\n");
  35. }
  36. foreach ($this->notice as $notice){
  37. $msg = 'NOTICE: ' . $notice . ($to_screen?"<br/>":"\n");
  38. }
  39. return $msg;
  40. }
  41. public static function writeall($path = ''){
  42. if (empty($path)) {$path = SIMPLE_WP_MEMBERSHIP_PATH . 'log.txt';}
  43. $fp = fopen($path, 'a');
  44. $date = current_time('mysql');
  45. fwrite($fp, strtoupper($date) . ":\n");
  46. fwrite($fp, str_repeat('-=', (strlen($date)+1.0)/2.0) . "\n");
  47. foreach (self::$intance as $context=>$intance){
  48. fwrite($fp, strtoupper($context) . ":\n");
  49. fwrite($fp, str_repeat('=', strlen($context)+1) . "\n");
  50. fwrite($fp, $intance->get());
  51. }
  52. fclose($fp);
  53. }
  54. public static function log_simple_debug($message, $success, $end = false) {
  55. $settings = SwpmSettings::get_instance();
  56. $debug_enabled = $settings->get_value('enable-debug');
  57. if (empty($debug_enabled)) {//Debug is not enabled
  58. return;
  59. }
  60. //Lets write to the log file
  61. $debug_log_file_name = SIMPLE_WP_MEMBERSHIP_PATH . 'log.txt';
  62. // Timestamp
  63. $log_timestamp = SwpmUtils::get_current_timestamp_for_debug_log();
  64. $text = '[' . $log_timestamp . '] - ' . (($success) ? 'SUCCESS: ' : 'FAILURE: ') . $message . "\n";
  65. if ($end) {
  66. $text .= "\n------------------------------------------------------------------\n\n";
  67. }
  68. // Write to log
  69. $fp = fopen($debug_log_file_name, 'a');
  70. fwrite($fp, $text);
  71. fclose($fp); // close file
  72. }
  73. public static function log_array_data_to_debug($array_to_write, $success, $end = false) {
  74. $settings = SwpmSettings::get_instance();
  75. $debug_enabled = $settings->get_value('enable-debug');
  76. if (empty($debug_enabled)) {//Debug is not enabled
  77. return;
  78. }
  79. //Lets write to the log file
  80. $debug_log_file_name = SIMPLE_WP_MEMBERSHIP_PATH . 'log.txt';
  81. // Timestamp
  82. $log_timestamp = SwpmUtils::get_current_timestamp_for_debug_log();
  83. $text = '[' . $log_timestamp . '] - ' . (($success) ? 'SUCCESS: ' : 'FAILURE: ') . "\n";
  84. ob_start();
  85. print_r($array_to_write);
  86. $var = ob_get_contents();
  87. ob_end_clean();
  88. $text .= $var;
  89. if ($end) {
  90. $text .= "\n------------------------------------------------------------------\n\n";
  91. }
  92. // Write to log
  93. $fp = fopen($debug_log_file_name, 'a');
  94. fwrite($fp, $text);
  95. fclose($fp); // close file
  96. }
  97. public static function log_auth_debug($message, $success, $end = false) {
  98. $settings = SwpmSettings::get_instance();
  99. $debug_enabled = $settings->get_value('enable-debug');
  100. if (empty($debug_enabled)) {//Debug is not enabled
  101. return;
  102. }
  103. //Lets write to the log file
  104. $debug_log_file_name = SIMPLE_WP_MEMBERSHIP_PATH . 'log-auth.txt';
  105. // Timestamp
  106. $log_timestamp = SwpmUtils::get_current_timestamp_for_debug_log();
  107. $text = '[' . $log_timestamp . '] - ' . (($success) ? 'SUCCESS: ' : 'FAILURE: ') . $message . "\n";
  108. if ($end) {
  109. $text .= "\n------------------------------------------------------------------\n\n";
  110. }
  111. // Write to log
  112. $fp = fopen($debug_log_file_name, 'a');
  113. fwrite($fp, $text);
  114. fclose($fp); // close file
  115. }
  116. public static function reset_swmp_log_files() {
  117. $log_reset = true;
  118. $logfile_list = array(
  119. SIMPLE_WP_MEMBERSHIP_PATH . '/log.txt',
  120. SIMPLE_WP_MEMBERSHIP_PATH . '/log-auth.txt',
  121. );
  122. foreach ($logfile_list as $logfile) {
  123. if (empty($logfile)) {
  124. continue;
  125. }
  126. $log_timestamp = SwpmUtils::get_current_timestamp_for_debug_log();
  127. $text = '[' . $log_timestamp . '] - SUCCESS: Log file reset';
  128. $text .= "\n------------------------------------------------------------------\n\n";
  129. $fp = fopen($logfile, 'w');
  130. if ($fp != FALSE) {
  131. @fwrite($fp, $text);
  132. @fclose($fp);
  133. } else {
  134. $log_reset = false;
  135. }
  136. }
  137. return $log_reset;
  138. }
  139. }