1 /* Copyright (C) 2000-2006  Thomas Bopp, Thorsten Hampel, Ludger Merkens
     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.
     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.
    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
    17  * $Id: log.pike,v 1.1 2008/03/31 13:39:57 exodusd Exp $
    19 inherit "/kernel/module";
    23 #include <attributes.h>
    24 class log : public module{
    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 ]);
    47  mapping mBacktraceLevels;
    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,
    67     mBacktraceLevels = ([ ]);
    69     foreach ( indices(mLogFiles), string logname ) 
    78     mMemory[t] = _Server->debug_memory();
    79     mObjects[t] = master()->get_in_memory();
    80     mRequests[t] = iRequests;
    81     call_out(debug_mem, 60);
    94 mapping get_request_map()
   110 void add_download(int bytes)
   120 void log_init(string logfile)
   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")];
   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")];
   130       mBacktraceLevels[logfile] = LOG_LEVEL_NONE;
   135   return indices( mLogFiles );
   138 void set_log_level(string logfile, int level)
   140     mLogLevels[logfile] = level;
   143 int get_log_level(string logfile)
   145   return mLogLevels[logfile];
   148 void set_backtrace_level(string logfile, int level)
   150   mBacktraceLevels[logfile] = level;
   153 int get_backtrace_level(string logfile)
   155   return mBacktraceLevels[logfile];
   158 void log_error(string logfile, string msg, mixed ... args)
   160   log( logfile, LOG_LEVEL_ERROR, "["+Calendar.now()->format_time()+"] ERROR: "+msg+"\n", @args );
   163 void log_warning(string logfile, string msg, mixed ... args)
   165   log( logfile, LOG_LEVEL_WARNING, "["+Calendar.now()->format_time()+"] WARNING: "+msg+"\n", @args );
   168 void log_info(string logfile, string msg, mixed ... args)
   170   log( logfile, LOG_LEVEL_INFO, "["+Calendar.now()->format_time()+"] INFO: "+msg+"\n", @args );
   173 void log_debug(string logfile, string msg, mixed ... args)
   175   log( logfile, LOG_LEVEL_DEBUG, "["+Calendar.now()->format_time()+"] DEBUG: "+msg+"\n", @args );
   178 void log(string logfile, int level, string msg, mixed ... args) 
   180   if ( mLogLevels[logfile] < level && mBacktraceLevels[logfile] < level ) {
   183   Stdio.File log = mLogFiles[logfile];
   185     steam_error("No such Logfile " + logfile);
   188   if ( arrayp(args) && sizeof(args) > 0 )
   189     what = sprintf(what, @args);
   191   log->write(what+"\n");
   193   if ( mBacktraceLevels[logfile] >= level )
   194     log->write( describe_backtrace( backtrace() ) );
   197 void log_security(string str, mixed ... args)
   199   log("security", LOG_LEVEL_DEBUG, str, @args);
   203 void set_debug(int on)
   208 string get_identifier() { return "log"; }