PageRenderTime 47ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/jabber/XMPP/Log.php

https://bitbucket.org/moodle/moodle
PHP | 119 lines | 39 code | 10 blank | 70 comment | 4 complexity | 49e2d5209df8b3f1d509d7bcdb850225 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-3.0
  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. * @copyright 2008 Nathanael C. Fritz
  27. */
  28. /**
  29. * XMPPHP Log
  30. *
  31. * @package XMPPHP
  32. * @author Nathanael C. Fritz <JID: fritzy@netflint.net>
  33. * @author Stephan Wentz <JID: stephan@jabber.wentz.it>
  34. * @author Michael Garvin <JID: gar@netflint.net>
  35. * @copyright 2008 Nathanael C. Fritz
  36. * @version $Id$
  37. */
  38. class XMPPHP_Log {
  39. const LEVEL_ERROR = 0;
  40. const LEVEL_WARNING = 1;
  41. const LEVEL_INFO = 2;
  42. const LEVEL_DEBUG = 3;
  43. const LEVEL_VERBOSE = 4;
  44. /**
  45. * @var array
  46. */
  47. protected $data = array();
  48. /**
  49. * @var array
  50. */
  51. protected $names = array('ERROR', 'WARNING', 'INFO', 'DEBUG', 'VERBOSE');
  52. /**
  53. * @var integer
  54. */
  55. protected $runlevel;
  56. /**
  57. * @var boolean
  58. */
  59. protected $printout;
  60. /**
  61. * Constructor
  62. *
  63. * @param boolean $printout
  64. * @param string $runlevel
  65. */
  66. public function __construct($printout = false, $runlevel = self::LEVEL_INFO) {
  67. $this->printout = (boolean)$printout;
  68. $this->runlevel = (int)$runlevel;
  69. }
  70. /**
  71. * Add a message to the log data array
  72. * If printout in this instance is set to true, directly output the message
  73. *
  74. * @param string $msg
  75. * @param integer $runlevel
  76. */
  77. public function log($msg, $runlevel = self::LEVEL_INFO) {
  78. $time = time();
  79. #$this->data[] = array($this->runlevel, $msg, $time);
  80. if($this->printout and $runlevel <= $this->runlevel) {
  81. $this->writeLine($msg, $runlevel, $time);
  82. }
  83. }
  84. /**
  85. * Output the complete log.
  86. * Log will be cleared if $clear = true
  87. *
  88. * @param boolean $clear
  89. * @param integer $runlevel
  90. */
  91. public function printout($clear = true, $runlevel = null) {
  92. if($runlevel === null) {
  93. $runlevel = $this->runlevel;
  94. }
  95. foreach($this->data as $data) {
  96. if($runlevel <= $data[0]) {
  97. $this->writeLine($data[1], $runlevel, $data[2]);
  98. }
  99. }
  100. if($clear) {
  101. $this->data = array();
  102. }
  103. }
  104. protected function writeLine($msg, $runlevel, $time) {
  105. //echo date('Y-m-d H:i:s', $time)." [".$this->names[$runlevel]."]: ".$msg."\n";
  106. echo $time." [".$this->names[$runlevel]."]: ".$msg."\n";
  107. flush();
  108. }
  109. }