comparison plugins/debug_logger/debug_logger.php @ 0:1e000243b222

vanilla 1.3.3 distro, I hope
author Charlie Root
date Thu, 04 Jan 2018 15:50:29 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:1e000243b222
1 <?php
2
3 /**
4 * Debug Logger
5 *
6 * Enhanced logging for debugging purposes. It is not recommened
7 * to be enabled on production systems without testing because of
8 * the somewhat increased memory, cpu and disk i/o overhead.
9 *
10 * Debug Logger listens for existing console("message") calls and
11 * introduces start and end tags as well as free form tagging
12 * which can redirect messages to files. The resulting log files
13 * provide timing and tag quantity results.
14 *
15 * Enable the plugin in config.inc.php and add your desired
16 * log types and files.
17 *
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
71 if(!rcmail::get_instance()->config->get('log_dir')){
72 rcmail::get_instance()->config->set('log_dir',INSTALL_PATH.'logs');
73 }
74
75 $log_config = rcmail::get_instance()->config->get('debug_logger',array());
76
77 foreach ($log_config as $type => $file){
78 $this->runlog->set_file(rcmail::get_instance()->config->get('log_dir').'/'.$file, $type);
79 }
80
81 $start_string = "";
82 $action = rcmail::get_instance()->action;
83 $task = rcmail::get_instance()->task;
84 if($action){
85 $start_string .= "Action: ".$action.". ";
86 }
87 if($task){
88 $start_string .= "Task: ".$task.". ";
89 }
90 $this->runlog->start($start_string);
91
92 $this->add_hook('console', array($this, 'console'));
93 $this->add_hook('authenticate', array($this, 'authenticate'));
94 }
95
96 function authenticate($args){
97 $this->runlog->note('Authenticating '.$args['user'].'@'.$args['host']);
98 return $args;
99 }
100
101 function console($args){
102 $note = $args[0];
103 $type = $args[1];
104
105
106 if(!isset($args[1])){
107 // This could be extended to detect types based on the
108 // file which called console. For now only rcube_imap/rcube_storage is supported
109 $bt = debug_backtrace();
110 $file = $bt[3]['file'];
111 switch(basename($file)){
112 case 'rcube_imap.php':
113 $type = 'imap';
114 break;
115 case 'rcube_storage.php':
116 $type = 'storage';
117 break;
118 default:
119 $type = FALSE;
120 break;
121 }
122 }
123 switch($note){
124 case 'end':
125 $type = 'end';
126 break;
127 }
128
129
130 switch($type){
131 case 'start':
132 $this->runlog->start($note);
133 break;
134 case 'end':
135 $this->runlog->end();
136 break;
137 default:
138 $this->runlog->note($note, $type);
139 break;
140 }
141 return $args;
142 }
143
144 function __destruct()
145 {
146 if ($this->runlog)
147 $this->runlog->end();
148 }
149 }