log._pike
Go to the documentation of this file.
1 /* Copyright (C) 2000-2006 Thomas Bopp, Thorsten Hampel, Ludger Merkens
2  *
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2 of the License, or
6  * (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * $Id: log.pike,v 1.1 2008/03/31 13:39:57 exodusd Exp $
18  */
19 inherit "/kernel/module";
20 #include <database.h>
21 #include <config.h>
22 #include <macros.h>
23 #include <attributes.h>
24 class log : public module{
25 public:
26 
27 
28 
29 
30 
31 int iLogDebug=0;
32 
33 constant LOG_LEVEL = ([ "none":LOG_LEVEL_NONE,
34  "error":LOG_LEVEL_ERROR,
35  "fatal":LOG_LEVEL_ERROR,
36  "warning":LOG_LEVEL_WARNING,
37  "info":LOG_LEVEL_INFO,
38  "debug":LOG_LEVEL_DEBUG ]);
39 
40  int iRequests;
41  mapping mRequests;
42  int iDownload;
43  mapping mMemory;
44  mapping mObjects;
45  mapping mLogFiles;
46  mapping mLogLevels;
47  mapping mBacktraceLevels;
48  string logs;
49 
50 private:
51 void init_module()
52 {
53  iRequests = 0;
54  iDownload = 0;
55  mRequests = ([ ]);
56  mMemory = ([ ]);
57  mObjects = ([ ]);
58 
59  logs = _Server->get_config("logdir");
60  mLogFiles = ([ "security": LOG_LEVEL_ERROR,
61  "events": LOG_LEVEL_ERROR,
62  "http": LOG_LEVEL_ERROR,
63  "smtp":LOG_LEVEL_ERROR,
64  "slow_requests":LOG_LEVEL_INFO,
65  ]);
66  mLogLevels = ([ ]);
67  mBacktraceLevels = ([ ]);
68 
69  foreach ( indices(mLogFiles), string logname )
70  log_init(logname);
71 }
72 
73 public:
74 
75 void debug_mem()
76 {
77  int t = time();
78  mMemory[t] = _Server->debug_memory();
79  mObjects[t] = master()->get_in_memory();
80  mRequests[t] = iRequests;
81  call_out(debug_mem, 60);
82 }
83 
84 mapping get_memory()
85 {
86  return mMemory;
87 }
88 
89 mapping get_objects()
90 {
91  return mObjects;
92 }
93 
94 mapping get_request_map()
95 {
96  return mRequests;
97 }
98 
99 void add_request()
100 {
101  iRequests++;
102 }
103 
104 
105 int get_requests()
106 {
107  return iRequests;
108 }
109 
110 void add_download(int bytes)
111 {
112  iDownload += bytes;
113 }
114 
115 int get_download()
116 {
117  return iDownload;
118 }
119 
120 void log_init(string logfile)
121 {
122  mLogFiles[logfile] = Stdio.File(logs + logfile + ".log", "wct");
123  if ( !zero_type(_Server->get_config(logfile+"_log_level")) )
124  mLogLevels[logfile] = LOG_LEVEL[_Server->get_config(logfile+"_log_level")];
125  else
126  mLogLevels[logfile] = LOG_LEVEL_ERROR;
127  if ( !zero_type(_Server->get_config(logfile+"_backtrace_level")) )
128  mBacktraceLevels[logfile] = LOG_LEVEL[_Server->get_config(logfile+"_backtrace_level")];
129  else
130  mBacktraceLevels[logfile] = LOG_LEVEL_NONE;
131 }
132 
133 array get_logs()
134 {
135  return indices( mLogFiles );
136 }
137 
138 void set_log_level(string logfile, int level)
139 {
140  mLogLevels[logfile] = level;
141 }
142 
143 int get_log_level(string logfile)
144 {
145  return mLogLevels[logfile];
146 }
147 
148 void set_backtrace_level(string logfile, int level)
149 {
150  mBacktraceLevels[logfile] = level;
151 }
152 
153 int get_backtrace_level(string logfile)
154 {
155  return mBacktraceLevels[logfile];
156 }
157 
158 void log_error(string logfile, string msg, mixed ... args)
159 {
160  log( logfile, LOG_LEVEL_ERROR, "["+Calendar.now()->format_time()+"] ERROR: "+msg+"\n", @args );
161 }
162 
163 void log_warning(string logfile, string msg, mixed ... args)
164 {
165  log( logfile, LOG_LEVEL_WARNING, "["+Calendar.now()->format_time()+"] WARNING: "+msg+"\n", @args );
166 }
167 
168 void log_info(string logfile, string msg, mixed ... args)
169 {
170  log( logfile, LOG_LEVEL_INFO, "["+Calendar.now()->format_time()+"] INFO: "+msg+"\n", @args );
171 }
172 
173 void log_debug(string logfile, string msg, mixed ... args)
174 {
175  log( logfile, LOG_LEVEL_DEBUG, "["+Calendar.now()->format_time()+"] DEBUG: "+msg+"\n", @args );
176 }
177 
178 void log(string logfile, int level, string msg, mixed ... args)
179 {
180  if ( mLogLevels[logfile] < level && mBacktraceLevels[logfile] < level ) {
181  return;
182  }
183  Stdio.File log = mLogFiles[logfile];
184  if ( !objectp(log) )
185  steam_error("No such Logfile " + logfile);
186 
187  string what = msg;
188  if ( arrayp(args) && sizeof(args) > 0 )
189  what = sprintf(what, @args);
190 
191  log->write(what+"\n");
192 
193  if ( mBacktraceLevels[logfile] >= level )
194  log->write( describe_backtrace( backtrace() ) );
195 }
196 
197 void log_security(string str, mixed ... args)
198 {
199  log("security", LOG_LEVEL_DEBUG, str, @args);
200 }
201 
202 
203 void set_debug(int on)
204 {
205  iLogDebug = on;
206 }
207 
208 string get_identifier() { return "log"; }
209 
210 
211 };