PageRenderTime 53ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/ext/anewt.new/logging/logging.lib.php

https://github.com/jijkoun/ssscrape
PHP | 429 lines | 162 code | 53 blank | 214 comment | 10 complexity | 64af534437fbc50d87910b8a1b42b1e6 MD5 | raw file
  1. <?php
  2. /*
  3. * Anewt, Almost No Effort Web Toolkit, logging module
  4. *
  5. * This code is copyrighted and distributed under the terms of the GNU LGPL.
  6. * See the README file for more information.
  7. */
  8. /* These constants are all for private use only */
  9. define('ANEWT_LOG_LEVEL_ERROR', 0);
  10. define('ANEWT_LOG_LEVEL_CRITICAL', 1);
  11. define('ANEWT_LOG_LEVEL_WARNING', 2);
  12. define('ANEWT_LOG_LEVEL_MESSAGE', 3);
  13. define('ANEWT_LOG_LEVEL_INFO', 4);
  14. define('ANEWT_LOG_LEVEL_DEBUG', 5);
  15. /**
  16. * \static
  17. *
  18. * The AnewtLog class provides static methods for logging.
  19. */
  20. class AnewtLog
  21. {
  22. /**
  23. * Constructor throws an error. Only static usage of this class is allowed.
  24. */
  25. function __construct()
  26. {
  27. trigger_error('You cannot create AnewtLog instances. Call
  28. AnewtLog::init() for initialization and use the static methods
  29. afterwards.', E_USER_ERROR);
  30. }
  31. /**
  32. * Initializes the AnewtLog module.
  33. *
  34. * \param $setup_default_handler
  35. * Whether to setup a default handler. If true, a new
  36. * AnewtLogHandlerDefault instance with default level filtering will be
  37. * initialized. This parameter defaults to false, which means a log
  38. * handler needs to be added manually before using the logging facilities.
  39. */
  40. static function init($setup_default_handler=false)
  41. {
  42. global $anewt_logging_handlers;
  43. global $anewt_logging_domain_stack;
  44. assert('is_bool($setup_default_handler)');
  45. $anewt_logging_handlers = array();
  46. $anewt_logging_domain_stack = array();
  47. /* Setup default handler */
  48. if ($setup_default_handler)
  49. {
  50. $handler = new AnewtLogHandlerDefault();
  51. AnewtLog::add_handler($handler);
  52. }
  53. }
  54. /**
  55. * Adds a log handler to the list of log handlers.
  56. *
  57. * \param $handler
  58. * A AnewtLogHandlerBase subclass instance
  59. *
  60. * \param $level
  61. * The minimum log level this handler should log messages for. The default
  62. * is ANEWT_LOG_LEVEL_MESSAGE, which means that normal messages, warnings,
  63. * critical warnings and errors are logged. Set to ANEWT_LOG_LEVEL_DEBUG
  64. * to enable logging of all messages, set to ANEWT_LOG_LEVEL_WARNING to
  65. * only log warnings and higher priority messages.
  66. */
  67. static function add_handler(&$handler, $level=ANEWT_LOG_LEVEL_MESSAGE)
  68. {
  69. assert('is_int($level) && ($level >= 0) && ($level <= 5)');
  70. assert('$handler instanceof AnewtLogHandlerBase');
  71. global $anewt_logging_handlers;
  72. $handler->level = $level;
  73. $anewt_logging_handlers[] = &$handler;
  74. }
  75. /**
  76. * \private
  77. *
  78. * Checks if the AnewtLog module was initialized.
  79. *
  80. * \return
  81. * True if initialized, false otherwise.
  82. */
  83. private static function _is_initialized()
  84. {
  85. global $anewt_logging_handlers;
  86. return is_array($anewt_logging_handlers);
  87. }
  88. /**
  89. * Sets the current log domain to the specified domain. Previous domain
  90. * names will be remembered (stack-based).
  91. *
  92. * \param $domain
  93. * A string with the new domain name.
  94. *
  95. * \see AnewtLog::reset_domain
  96. * \see AnewtLog::get_domain
  97. */
  98. static function set_domain($domain) {
  99. assert('is_string($domain)');
  100. global $anewt_logging_domain_stack;
  101. $anewt_logging_domain_stack[] = $domain;
  102. }
  103. /**
  104. * Resets the log domain to the previous domain.
  105. *
  106. * \return
  107. * The current log domain.
  108. *
  109. * \see AnewtLog::set_domain
  110. * \see AnewtLog::get_domain
  111. */
  112. static function reset_domain() {
  113. global $anewt_logging_domain_stack;
  114. return array_pop($anewt_logging_domain_stack);
  115. }
  116. /**
  117. * Retrieves the current log domain.
  118. *
  119. * \return
  120. * The current log domain.
  121. *
  122. * \see AnewtLog::set_domain
  123. * \see AnewtLog::reset_domain
  124. */
  125. static function get_domain() {
  126. global $anewt_logging_domain_stack;
  127. if (!$anewt_logging_domain_stack) return null;
  128. return $anewt_logging_domain_stack[count($anewt_logging_domain_stack)-1];
  129. }
  130. /**
  131. * Converts a log level integer to a string representation.
  132. *
  133. * \param $level
  134. * The log level to convert, eg. ANEWT_LOG_LEVEL_MESSAGE
  135. *
  136. * \return
  137. * The name for this log level.
  138. */
  139. static function loglevel_to_string($level) {
  140. assert('is_int($level) && ($level >= 0) && ($level <= 5)');
  141. switch ($level) {
  142. case ANEWT_LOG_LEVEL_ERROR: return 'error';
  143. case ANEWT_LOG_LEVEL_CRITICAL: return 'critical';
  144. case ANEWT_LOG_LEVEL_WARNING: return 'warning';
  145. case ANEWT_LOG_LEVEL_MESSAGE: return 'message';
  146. case ANEWT_LOG_LEVEL_INFO: return 'info';
  147. case ANEWT_LOG_LEVEL_DEBUG: return 'debug';
  148. /* This should never happen: */
  149. default: trigger_error(sprintf(
  150. '%s:%s(): unknown level. Please report a bug!',
  151. __CLASS__, __FUNCTION__), E_USER_ERROR);
  152. }
  153. }
  154. /**
  155. * Log a message to the output handlers.
  156. *
  157. * \param $level
  158. * The message log level. This should be one of the ANEWT_LOG_LEVEL_*
  159. * constants.
  160. *
  161. * \param $message
  162. * The message itself. sprintf-style placeholders can be specified.
  163. *
  164. * \param $args
  165. * An array with data (optional). These values will be substituted for the
  166. * placeholders in $message.
  167. */
  168. private static function _log($level, $message, $args=null) {
  169. if (!AnewtLog::_is_initialized())
  170. AnewtLog::init();
  171. global $anewt_logging_handlers;
  172. if (is_null($args))
  173. $args = array();
  174. assert('is_int($level) && ($level >= 0) && ($level <= 5)');
  175. assert('is_string($message)');
  176. assert('is_array($args)');
  177. /* Get the variable arguments list */
  178. while ((count($args) == 1) && is_array($args[0])) {
  179. $args = $args[0];
  180. }
  181. if ($args)
  182. $message = vsprintf($message, $args);
  183. $domain = AnewtLog::get_domain();
  184. foreach ($anewt_logging_handlers as $handler) {
  185. if ($handler->level >= $level)
  186. $handler->log($domain, $level, $message);
  187. }
  188. }
  189. /**
  190. * Log a message to the output handlers, temporarily overriding the log
  191. * domain.
  192. *
  193. * \param $domain
  194. * The temporary log domain
  195. *
  196. * \param $level
  197. * \param $message
  198. * \param $args
  199. *
  200. * \see AnewtLog::_log
  201. */
  202. private static function _log_with_domain($domain, $level, $message, $args) {
  203. AnewtLog::set_domain($domain);
  204. AnewtLog::_log($level, $message, $args);
  205. AnewtLog::reset_domain();
  206. }
  207. /* Errors */
  208. /**
  209. * Log an error.
  210. *
  211. * \param $message
  212. * The message to log, with optional sprintf-style placeholders.
  213. *
  214. * \param $args
  215. * One or more parameters (or a single array) with values that will be
  216. * substituted for the placeholders in $message.
  217. */
  218. static function error($message, $args=null) {
  219. $args = func_get_args();
  220. $message = array_shift($args);
  221. AnewtLog::_log(ANEWT_LOG_LEVEL_ERROR, $message, $args);
  222. }
  223. /**
  224. * Log an error with a custom domain. This is particularly useful for
  225. * library routines.
  226. *
  227. * \param $domain
  228. * The custom domain
  229. *
  230. * \param $message
  231. * \param $args
  232. * \see AnewtLog::error
  233. */
  234. static function error_with_domain($domain, $message, $args=null) {
  235. $args = func_get_args();
  236. $domain = array_shift($args);
  237. $message = array_shift($args);
  238. AnewtLog::_log_with_domain($domain, ANEWT_LOG_LEVEL_ERROR, $message, $args);
  239. }
  240. /* Critical warnings */
  241. /**
  242. * Log a critical warning.
  243. *
  244. * \param $message
  245. * \param $args
  246. * \see AnewtLog::error
  247. */
  248. static function critical($message, $args=null) {
  249. $args = func_get_args();
  250. $message = array_shift($args);
  251. AnewtLog::_log(ANEWT_LOG_LEVEL_CRITICAL, $message, $args);
  252. }
  253. /**
  254. * Log a critical warning with a custom domain.
  255. *
  256. * \param $domain
  257. * \param $message
  258. * \param $args
  259. * \see AnewtLog::error_with_domain
  260. */
  261. static function critical_with_domain($domain, $message, $args=null) {
  262. $args = func_get_args();
  263. $domain = array_shift($args);
  264. $message = array_shift($args);
  265. AnewtLog::_log_with_domain($domain, ANEWT_LOG_LEVEL_CRITICAL, $message, $args);
  266. }
  267. /* Warnings */
  268. /**
  269. * Log a warning.
  270. *
  271. * \param $message
  272. * \param $args
  273. * \see AnewtLog::error
  274. */
  275. static function warning($message, $args=null) {
  276. $args = func_get_args();
  277. $message = array_shift($args);
  278. AnewtLog::_log(ANEWT_LOG_LEVEL_WARNING, $message, $args);
  279. }
  280. /**
  281. * Log a warning with a custom domain.
  282. *
  283. * \param $domain
  284. * \param $message
  285. * \param $args
  286. * \see AnewtLog::error_with_domain
  287. */
  288. static function warning_with_domain($domain, $message, $args=null) {
  289. $args = func_get_args();
  290. $domain = array_shift($args);
  291. $message = array_shift($args);
  292. AnewtLog::_log_with_domain($domain, ANEWT_LOG_LEVEL_WARNING, $message, $args);
  293. }
  294. /* Normal messages */
  295. /**
  296. * Log a normal message.
  297. *
  298. * \param $message
  299. * \param $args
  300. * \see AnewtLog::error
  301. */
  302. static function message($message, $args=null) {
  303. $args = func_get_args();
  304. $message = array_shift($args);
  305. AnewtLog::_log(ANEWT_LOG_LEVEL_MESSAGE, $message, $args);
  306. }
  307. /**
  308. * Log a normal message with a custom domain.
  309. *
  310. * \param $domain
  311. * \param $message
  312. * \param $args
  313. * \see AnewtLog::error_with_domain
  314. */
  315. static function message_with_domain($domain, $message, $args=null) {
  316. $args = func_get_args();
  317. $domain = array_shift($args);
  318. $message = array_shift($args);
  319. AnewtLog::_log_with_domain($domain, ANEWT_LOG_LEVEL_MESSAGE, $message, $args);
  320. }
  321. /* Information messages */
  322. /**
  323. * Log an informational message.
  324. *
  325. * \param $message
  326. * \param $args
  327. * \see AnewtLog::error
  328. */
  329. static function info($message, $args=null) {
  330. $args = func_get_args();
  331. $message = array_shift($args);
  332. AnewtLog::_log(ANEWT_LOG_LEVEL_INFO, $message, $args);
  333. }
  334. /**
  335. * Log an informational message with a custom domain.
  336. *
  337. * \param $domain
  338. * \param $message
  339. * \param $args
  340. * \see AnewtLog::error_with_domain
  341. */
  342. static function info_with_domain($domain, $message, $args=null) {
  343. $args = func_get_args();
  344. $domain = array_shift($args);
  345. $message = array_shift($args);
  346. AnewtLog::_log_with_domain($domain, ANEWT_LOG_LEVEL_INFO, $message, $args);
  347. }
  348. /* Debug messages */
  349. /**
  350. * Log a debug message.
  351. *
  352. * \param $message
  353. * \param $args
  354. * \see AnewtLog::error
  355. */
  356. static function debug($message, $args=null) {
  357. $args = func_get_args();
  358. $message = array_shift($args);
  359. AnewtLog::_log(ANEWT_LOG_LEVEL_DEBUG, $message, $args);
  360. }
  361. /**
  362. * Log a debug message with a custom domain.
  363. *
  364. * \param $domain
  365. * \param $message
  366. * \param $args
  367. * \see AnewtLog::error_with_domain
  368. */
  369. static function debug_with_domain($domain, $message, $args=null) {
  370. $args = func_get_args();
  371. $domain = array_shift($args);
  372. $message = array_shift($args);
  373. AnewtLog::_log_with_domain($domain, ANEWT_LOG_LEVEL_DEBUG, $message, $args);
  374. }
  375. }
  376. ?>