Log.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 = [];
  53. /**
  54. * @var array
  55. */
  56. protected $names = ['ERROR', 'WARNING', 'INFO', 'DEBUG', 'VERBOSE'];
  57. /**
  58. * @var int
  59. */
  60. protected $runlevel;
  61. /**
  62. * @var bool
  63. */
  64. protected $printout;
  65. /**
  66. * Constructor
  67. *
  68. * @param boolean $printout
  69. * @param int $runlevel (optional)
  70. */
  71. public function __construct($printout = false, ?int $runlevel = self::LEVEL_INFO)
  72. {
  73. $this->printout = (bool) $printout;
  74. $this->runlevel = (int) ($runlevel ?? 0);
  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 int $runlevel (optional)
  82. */
  83. public function log($msg, $runlevel = self::LEVEL_INFO): void
  84. {
  85. $time = time();
  86. //$this->data[] = [$this->runlevel, $msg, $time];
  87. if ($this->printout and $runlevel <= $this->runlevel) {
  88. $this->writeLine($msg, $runlevel, $time);
  89. }
  90. }
  91. /**
  92. * @param string $msg
  93. * @param int $runlevel
  94. * @param int $time
  95. */
  96. protected function writeLine(string $msg, int $runlevel, int $time): void
  97. {
  98. //echo date('Y-m-d H:i:s', $time)." [".$this->names[$runlevel]."]: ".$msg."\n";
  99. echo $time . " [" . $this->names[$runlevel] . "]: " . $msg . "\n";
  100. flush();
  101. }
  102. /**
  103. * Output the complete log.
  104. * Log will be cleared if $clear = true
  105. *
  106. * @param bool $clear
  107. * @param int $runlevel
  108. */
  109. public function printout(bool $clear = true, int $runlevel = null): void
  110. {
  111. if ($runlevel === null) {
  112. $runlevel = $this->runlevel;
  113. }
  114. foreach ($this->data as $data) {
  115. if ($runlevel <= $data[0]) {
  116. $this->writeLine($data[1], $runlevel, $data[2]);
  117. }
  118. }
  119. if ($clear) {
  120. $this->data = [];
  121. }
  122. }
  123. }