Log.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * XMPPHP: The PHP XMPP Library
  4. * Copyright (C) 2008 Nathanael C. Fritz
  5. * This file is part of SleekXMPP.
  6. *
  7. * XMPPHP is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * XMPPHP is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with XMPPHP; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * @category xmpphp
  22. * @package XMPPHP
  23. * @author Nathanael C. Fritz <JID: fritzy@netflint.net>
  24. * @author Stephan Wentz <JID: stephan@jabber.wentz.it>
  25. * @author Michael Garvin <JID: gar@netflint.net>
  26. * @author Alexander Birkner (https://github.com/BirknerAlex)
  27. * @author zorn-v (https://github.com/zorn-v/xmpphp/)
  28. * @author GNU social
  29. * @copyright 2008 Nathanael C. Fritz
  30. */
  31. namespace XMPPHP;
  32. /**
  33. * XMPPHP Log
  34. *
  35. * @package XMPPHP
  36. * @author Nathanael C. Fritz <JID: fritzy@netflint.net>
  37. * @author Stephan Wentz <JID: stephan@jabber.wentz.it>
  38. * @author Michael Garvin <JID: gar@netflint.net>
  39. * @copyright 2008 Nathanael C. Fritz
  40. * @version $Id$
  41. */
  42. class Log
  43. {
  44. const LEVEL_ERROR = 0;
  45. const LEVEL_WARNING = 1;
  46. const LEVEL_INFO = 2;
  47. const LEVEL_DEBUG = 3;
  48. const LEVEL_VERBOSE = 4;
  49. /**
  50. * @var array
  51. */
  52. protected $data = array();
  53. /**
  54. * @var array
  55. */
  56. protected $names = array('ERROR', 'WARNING', 'INFO', 'DEBUG', 'VERBOSE');
  57. /**
  58. * @var integer
  59. */
  60. protected $runlevel;
  61. /**
  62. * @var boolean
  63. */
  64. protected $printout;
  65. /**
  66. * Constructor
  67. *
  68. * @param boolean $printout
  69. * @param int $runlevel
  70. */
  71. public function __construct($printout = false, $runlevel = self::LEVEL_INFO)
  72. {
  73. $this->printout = (boolean)$printout;
  74. $this->runlevel = (int)$runlevel;
  75. }
  76. /**
  77. * Add a message to the log data array
  78. * If printout in this instance is set to true, directly output the message
  79. *
  80. * @param string $msg
  81. * @param integer $runlevel
  82. */
  83. public function log($msg, $runlevel = self::LEVEL_INFO)
  84. {
  85. $time = time();
  86. #$this->data[] = array($this->runlevel, $msg, $time);
  87. if ($this->printout and $runlevel <= $this->runlevel) {
  88. $this->writeLine($msg, $runlevel, $time);
  89. }
  90. }
  91. protected function writeLine($msg, $runlevel, $time)
  92. {
  93. //echo date('Y-m-d H:i:s', $time)." [".$this->names[$runlevel]."]: ".$msg."\n";
  94. echo $time . " [" . $this->names[$runlevel] . "]: " . $msg . "\n";
  95. flush();
  96. }
  97. /**
  98. * Output the complete log.
  99. * Log will be cleared if $clear = true
  100. *
  101. * @param boolean $clear
  102. * @param integer $runlevel
  103. */
  104. public function printout($clear = true, $runlevel = null)
  105. {
  106. if ($runlevel === null) {
  107. $runlevel = $this->runlevel;
  108. }
  109. foreach ($this->data as $data) {
  110. if ($runlevel <= $data[0]) {
  111. $this->writeLine($data[1], $runlevel, $data[2]);
  112. }
  113. }
  114. if ($clear) {
  115. $this->data = array();
  116. }
  117. }
  118. }