You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

debug_logger.php 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /**
  3. * Debug Logger
  4. *
  5. * Enhanced logging for debugging purposes. It is not recommened
  6. * to be enabled on production systems without testing because of
  7. * the somewhat increased memory, cpu and disk i/o overhead.
  8. *
  9. * Debug Logger listens for existing console("message") calls and
  10. * introduces start and end tags as well as free form tagging
  11. * which can redirect messages to files. The resulting log files
  12. * provide timing and tag quantity results.
  13. *
  14. * Enable the plugin in config.inc.php and add your desired
  15. * log types and files.
  16. *
  17. * @version @package_version@
  18. * @author Ziba Scott
  19. * @website http://roundcube.net
  20. *
  21. * Example:
  22. *
  23. * config.inc.php:
  24. *
  25. * // $config['debug_logger'][type of logging] = name of file in log_dir
  26. * // The 'master' log includes timing information
  27. * $config['debug_logger']['master'] = 'master';
  28. * // If you want sql messages to also go into a separate file
  29. * $config['debug_logger']['sql'] = 'sql';
  30. *
  31. * index.php (just after $RCMAIL->plugins->init()):
  32. *
  33. * console("my test","start");
  34. * console("my message");
  35. * console("my sql calls","start");
  36. * console("cp -r * /dev/null","shell exec");
  37. * console("select * from example","sql");
  38. * console("select * from example","sql");
  39. * console("select * from example","sql");
  40. * console("end");
  41. * console("end");
  42. *
  43. *
  44. * logs/master (after reloading the main page):
  45. *
  46. * [17-Feb-2009 16:51:37 -0500] start: Task: mail.
  47. * [17-Feb-2009 16:51:37 -0500] start: my test
  48. * [17-Feb-2009 16:51:37 -0500] my message
  49. * [17-Feb-2009 16:51:37 -0500] shell exec: cp -r * /dev/null
  50. * [17-Feb-2009 16:51:37 -0500] start: my sql calls
  51. * [17-Feb-2009 16:51:37 -0500] sql: select * from example
  52. * [17-Feb-2009 16:51:37 -0500] sql: select * from example
  53. * [17-Feb-2009 16:51:37 -0500] sql: select * from example
  54. * [17-Feb-2009 16:51:37 -0500] end: my sql calls - 0.0018 seconds shell exec: 1, sql: 3,
  55. * [17-Feb-2009 16:51:37 -0500] end: my test - 0.0055 seconds shell exec: 1, sql: 3,
  56. * [17-Feb-2009 16:51:38 -0500] end: Task: mail. - 0.8854 seconds shell exec: 1, sql: 3,
  57. *
  58. * logs/sql (after reloading the main page):
  59. *
  60. * [17-Feb-2009 16:51:37 -0500] sql: select * from example
  61. * [17-Feb-2009 16:51:37 -0500] sql: select * from example
  62. * [17-Feb-2009 16:51:37 -0500] sql: select * from example
  63. */
  64. class debug_logger extends rcube_plugin
  65. {
  66. function init()
  67. {
  68. require_once(__DIR__ . '/runlog/runlog.php');
  69. $this->runlog = new runlog();
  70. if(!rcmail::get_instance()->config->get('log_dir')){
  71. rcmail::get_instance()->config->set('log_dir',INSTALL_PATH.'logs');
  72. }
  73. $log_config = rcmail::get_instance()->config->get('debug_logger',array());
  74. foreach($log_config as $type=>$file){
  75. $this->runlog->set_file(rcmail::get_instance()->config->get('log_dir').'/'.$file, $type);
  76. }
  77. $start_string = "";
  78. $action = rcmail::get_instance()->action;
  79. $task = rcmail::get_instance()->task;
  80. if($action){
  81. $start_string .= "Action: ".$action.". ";
  82. }
  83. if($task){
  84. $start_string .= "Task: ".$task.". ";
  85. }
  86. $this->runlog->start($start_string);
  87. $this->add_hook('console', array($this, 'console'));
  88. $this->add_hook('authenticate', array($this, 'authenticate'));
  89. }
  90. function authenticate($args){
  91. $this->runlog->note('Authenticating '.$args['user'].'@'.$args['host']);
  92. return $args;
  93. }
  94. function console($args){
  95. $note = $args[0];
  96. $type = $args[1];
  97. if(!isset($args[1])){
  98. // This could be extended to detect types based on the
  99. // file which called console. For now only rcube_imap/rcube_storage is supported
  100. $bt = debug_backtrace();
  101. $file = $bt[3]['file'];
  102. switch(basename($file)){
  103. case 'rcube_imap.php':
  104. $type = 'imap';
  105. break;
  106. case 'rcube_storage.php':
  107. $type = 'storage';
  108. break;
  109. default:
  110. $type = FALSE;
  111. break;
  112. }
  113. }
  114. switch($note){
  115. case 'end':
  116. $type = 'end';
  117. break;
  118. }
  119. switch($type){
  120. case 'start':
  121. $this->runlog->start($note);
  122. break;
  123. case 'end':
  124. $this->runlog->end();
  125. break;
  126. default:
  127. $this->runlog->note($note, $type);
  128. break;
  129. }
  130. return $args;
  131. }
  132. function __destruct()
  133. {
  134. if ($this->runlog)
  135. $this->runlog->end();
  136. }
  137. }