Aleksander Machniak
2015-11-22 9f1f754daf4b57a0d0d3aea95d2321716d218cf5
commit | author | age
48e9c1 1 <?php
T 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  *
461a30 15  * Enable the plugin in config.inc.php and add your desired
48e9c1 16  * log types and files.
T 17  *
18  * @version @package_version@
19  * @author Ziba Scott
20  * @website http://roundcube.net
9f1f75 21  *
48e9c1 22  * Example:
T 23  *
461a30 24  * config.inc.php:
48e9c1 25  *
bcedf0 26  *   // $config['debug_logger'][type of logging] = name of file in log_dir
48e9c1 27  *   // The 'master' log includes timing information
bcedf0 28  *   $config['debug_logger']['master'] = 'master';
48e9c1 29  *   // If you want sql messages to also go into a separate file 
bcedf0 30  *   $config['debug_logger']['sql'] = 'sql';
48e9c1 31  *
T 32  * index.php (just after $RCMAIL->plugins->init()):
33  *
34  *   console("my test","start");
35  *   console("my message");
36  *   console("my sql calls","start");
37  *   console("cp -r * /dev/null","shell exec");
38  *   console("select * from example","sql");
39  *   console("select * from example","sql");
40  *   console("select * from example","sql");
41  *   console("end");
42  *   console("end");
43  *
44  *
45  * logs/master (after reloading the main page):
46  *
47  *   [17-Feb-2009 16:51:37 -0500] start: Task: mail. 
48  *   [17-Feb-2009 16:51:37 -0500]   start: my test
49  *   [17-Feb-2009 16:51:37 -0500]     my message
50  *   [17-Feb-2009 16:51:37 -0500]     shell exec: cp -r * /dev/null
51  *   [17-Feb-2009 16:51:37 -0500]     start: my sql calls
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]       sql: select * from example
55  *   [17-Feb-2009 16:51:37 -0500]     end: my sql calls - 0.0018 seconds shell exec: 1, sql: 3, 
56  *   [17-Feb-2009 16:51:37 -0500]   end: my test - 0.0055 seconds shell exec: 1, sql: 3, 
57  *   [17-Feb-2009 16:51:38 -0500] end: Task: mail.  - 0.8854 seconds shell exec: 1, sql: 3, 
58  * 
59  * logs/sql (after reloading the main page):
60  *
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  *   [17-Feb-2009 16:51:37 -0500]       sql: select * from example
64  */
65 class debug_logger extends rcube_plugin
66 {
67     function init()
68     {
0ea079 69         require_once(__DIR__ . '/runlog/runlog.php');
AM 70         $this->runlog = new runlog();
48e9c1 71
T 72         if(!rcmail::get_instance()->config->get('log_dir')){
73             rcmail::get_instance()->config->set('log_dir',INSTALL_PATH.'logs');
74         }
75
76         $log_config = rcmail::get_instance()->config->get('debug_logger',array());
77
78         foreach($log_config as $type=>$file){
79             $this->runlog->set_file(rcmail::get_instance()->config->get('log_dir').'/'.$file, $type);
80         }
81
82         $start_string = "";
83         $action = rcmail::get_instance()->action;
84         $task = rcmail::get_instance()->task;
85         if($action){
0ea079 86                $start_string .= "Action: ".$action.". ";
48e9c1 87         }
T 88         if($task){
0ea079 89                $start_string .= "Task: ".$task.". ";
48e9c1 90         }
T 91         $this->runlog->start($start_string);
92
93         $this->add_hook('console', array($this, 'console'));
94         $this->add_hook('authenticate', array($this, 'authenticate'));
95     }
96
97     function authenticate($args){
98         $this->runlog->note('Authenticating '.$args['user'].'@'.$args['host']);
99         return $args;
100     }
101
102     function console($args){
103         $note = $args[0];
104         $type = $args[1];
105
106
107         if(!isset($args[1])){
108             // This could be extended to detect types based on the 
109             // file which called console. For now only rcube_imap/rcube_storage is supported
110             $bt = debug_backtrace();
111             $file  = $bt[3]['file'];
112             switch(basename($file)){
113                 case 'rcube_imap.php':
114                     $type = 'imap';
115                     break;
116                 case 'rcube_storage.php':
117                     $type = 'storage';
118                     break;
119                 default:
120                     $type = FALSE; 
121                     break; 
122             }
123         }
124         switch($note){
125             case 'end':
126                 $type = 'end';
127                 break;
128         }
129
130
131         switch($type){
132             case 'start':
133                 $this->runlog->start($note);
134                 break;
135             case 'end':
136                 $this->runlog->end();
137                 break;
138             default:
139                 $this->runlog->note($note, $type);
140                 break;
141         }
142         return $args;
143     }
144
b00592 145     function __destruct()
AM 146     {
147         if ($this->runlog)
148             $this->runlog->end();
48e9c1 149     }
T 150 }