PLearn 0.1
pl_log.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // pl_log.h
00004 //
00005 // Copyright (C) 2004-2006 Nicolas Chapados 
00006 // 
00007 // Redistribution and use in source and binary forms, with or without
00008 // modification, are permitted provided that the following conditions are met:
00009 // 
00010 //  1. Redistributions of source code must retain the above copyright
00011 //     notice, this list of conditions and the following disclaimer.
00012 // 
00013 //  2. Redistributions in binary form must reproduce the above copyright
00014 //     notice, this list of conditions and the following disclaimer in the
00015 //     documentation and/or other materials provided with the distribution.
00016 // 
00017 //  3. The name of the authors may not be used to endorse or promote
00018 //     products derived from this software without specific prior written
00019 //     permission.
00020 // 
00021 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
00022 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00023 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
00024 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00025 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00026 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00027 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00028 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00029 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00030 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031 // 
00032 // This file is part of the PLearn library. For more information on the PLearn
00033 // library, go to the PLearn Web site at www.plearn.org
00034 
00035 /* *******************************************************      
00036  * $Id: pl_log.h 6834 2007-04-05 01:35:37Z saintmlx $ 
00037  ******************************************************* */
00038 
00039 // Authors: Nicolas Chapados, Christian Dorion
00040 
00046 #ifndef pl_log_INC
00047 #define pl_log_INC
00048 
00049 // From C++ stdlib
00050 #include <vector>
00051 #include <deque>
00052 #include <map>
00053 #include <set>
00054 #include <string>
00055 
00056 // From Plearn
00057 #include "PStream.h"
00058 
00059 namespace PLearn {
00060 
00061 #ifndef PL_LOG_MAXVERBOSITY
00062 #define PL_LOG_MAXVERBOSITY 1000
00063 #endif
00064 
00065 #ifndef PL_LOG_MINVERBOSITY
00066 #define PL_LOG_MINVERBOSITY 1
00067 #endif
00068 
00069 #ifndef PL_LOG_VERBOSITY
00070 #define PL_LOG_VERBOSITY 499                 // block EXTREME by default
00071 #endif
00072 
00073 enum VerbosityLevel {
00074     VLEVEL_MAND     = 0,    // Mandatory
00075     VLEVEL_IMP      = 1,    // Important
00076     VLEVEL_NORMAL   = 5,    // Normal
00077     VLEVEL_DBG      = 10,   // Debug Info
00078     VLEVEL_EXTREME  = 500   // Extreme Verbosity
00079 }; 
00080 
00081 
00082 //#####  LogPlugin  ###########################################################
00083 
00092 class PL_LogPlugin : public PPointable
00093 {
00094 public:
00095     virtual ~PL_LogPlugin();
00096 
00097     virtual PStream& getStream(PStream::mode_t outmode, const string& module_name,
00098                                int requested_verbosity) = 0;
00099 };
00100 
00101 
00106 class PL_LogPluginPStream : public PL_LogPlugin
00107 {
00108 public:
00109     PL_LogPluginPStream(PStream pstream)
00110         : m_pstream(pstream)
00111     { }
00112     
00113     virtual PStream& getStream(PStream::mode_t outmode, const string& module_name,
00114                                int requested_verbosity);
00115 
00116 protected:
00117     PStream m_pstream;                       
00118 };
00119 
00120 
00124 class PL_LogPluginServer : public PL_LogPlugin
00125 {
00126 public:
00127     PL_LogPluginServer(PStream pstream)
00128         : m_pstream(pstream)
00129     { }
00130     
00131     virtual PStream& getStream(PStream::mode_t outmode, const string& module_name,
00132                                int requested_verbosity);
00133 
00134 protected:
00135     PStream m_pstream;                      
00136     PStream m_sstream;                      
00137 };
00138 
00139 
00140 class LogInterceptorPStreamBuf;              
00141 
00149 class PL_LogPluginInterceptor : public PL_LogPlugin
00150 {
00151     typedef PL_LogPlugin inherited;
00152     friend class LogInterceptorPStreamBuf;
00153     typedef map< string, deque<string> > LogMap;
00154 
00155 public:
00156     PL_LogPluginInterceptor(const set<string>& intercept_lognames,
00157                             PL_LogPlugin* previous);
00158 
00159     virtual PStream& getStream(PStream::mode_t outmode, const string& module_name,
00160                                int requested_verbosity);
00161 
00163     const deque<string>& logEntries(const string& logname) const;
00164 
00167     void clearLog(const string& logname);
00168 
00169 protected:
00171     void appendLog(const string& logname, const string& logentry);
00172     
00173 protected:
00175     set<string> m_intercept_lognames;
00176 
00178     PL_LogPlugin* m_previous;
00179 
00181     mutable LogMap m_log_entries;
00182 
00184     PP<LogInterceptorPStreamBuf> m_streambuf;
00185 
00187     PStream m_pstream;
00188 };
00189 
00190 
00191 //#####  Logging Class Per Se  ################################################
00192 
00193 class PL_Log
00194 {
00195 public:
00198     PL_Log(PP<PL_LogPlugin> plugin);
00199   
00203     void verbosity(int v)                     { runtime_verbosity = v; }
00204 
00206     int verbosity() const                     { return runtime_verbosity; }
00207 
00209     void outmode(PStream::mode_t outmode)     { m_outmode = outmode; }
00210 
00216     PStream& logger(int requested_verbosity);
00217 
00223     PStream& namedLogger(const string& module_name,
00224                          int requested_verbosity);
00225 
00232     void enableNamedLogging(const vector<string>& module_names);
00233 
00236     void enableNamedLogging(const string& comma_separated_module_names);
00237 
00239     vector<string> namedLogging() const;
00240 
00242     bool loggingEnabled(const string& module_name) const;
00243     
00245     long loggerCount() const                   { return logger_count; }
00246   
00248     PP<PL_LogPlugin> getCurrentPlugin()
00249     {
00250         return m_plugin;
00251     }
00252 
00254     void setPlugin(PP<PL_LogPlugin> plugin)
00255     {
00256         m_plugin = plugin;
00257     }
00258     
00260     static PL_Log& instance();
00261 
00263     struct Heading {
00264         Heading(string h_) : h(h_) {}
00265         string h;
00266     };
00267 
00272     static VerbosityLevel vlevelFromString(const string& v);
00273   
00274 protected:
00275     int runtime_verbosity;
00276     PP<PL_LogPlugin> m_plugin;      
00277     PStream null_stream;            
00278     PStream::mode_t m_outmode;      
00279     long logger_count;              
00280     set<string> enabled_modules;    
00281 
00282     enum {
00283         AllModules = 0,           
00284         NoModules = 1,            
00285         SomeModules = 2           
00286     } named_logging_kind;
00287 };
00288 
00289 
00290 //#####  Main interface to the logging system  ################################
00291 
00292 #define PL_LOG(v) if (v <= PL_LOG_VERBOSITY) PL_Log::instance().logger(v)
00293 #define MAND_LOG      PL_LOG(VLEVEL_MAND)
00294 #define IMP_LOG       PL_LOG(VLEVEL_IMP) 
00295 #define NORMAL_LOG    PL_LOG(VLEVEL_NORMAL)
00296 #define DBG_LOG       PL_LOG(VLEVEL_DBG)   
00297 #define EXTREME_LOG   PL_LOG(VLEVEL_EXTREME)
00298 
00299 
00301 #define PL_LEVEL_NAMED_LOG(name,level)                  \
00302     if (level <= PL_LOG_VERBOSITY)                      \
00303         PL_Log::instance().namedLogger(name, level)
00304 
00306 #define MAND_NAMED_LOG(name)      PL_LEVEL_NAMED_LOG(name, VLEVEL_MAND)
00307 #define IMP_NAMED_LOG(name)       PL_LEVEL_NAMED_LOG(name, VLEVEL_IMP) 
00308 #define NAMED_LOG(name)           PL_LEVEL_NAMED_LOG(name, VLEVEL_NORMAL)
00309 #define DBG_NAMED_LOG(name)       PL_LEVEL_NAMED_LOG(name, VLEVEL_DBG)   
00310 #define EXTREME_NAMED_LOG(name)   PL_LEVEL_NAMED_LOG(name, VLEVEL_EXTREME)
00311 
00312 
00327 #ifdef PL_LOG_MODULE_NAME
00328 # ifndef MODULE_LOG
00329 
00330    namespace {
00331        string pl_log_module_name_string(PL_LOG_MODULE_NAME);
00332    }
00333 
00334 #  define MAND_MODULE_LOG    PL_LEVEL_NAMED_LOG(pl_log_module_name_string, VLEVEL_MAND)
00335 #  define IMP_MODULE_LOG     PL_LEVEL_NAMED_LOG(pl_log_module_name_string, VLEVEL_IMP)
00336 #  define MODULE_LOG         PL_LEVEL_NAMED_LOG(pl_log_module_name_string, VLEVEL_NORMAL)
00337 #  define DBG_MODULE_LOG     PL_LEVEL_NAMED_LOG(pl_log_module_name_string, VLEVEL_DBG)
00338 #  define EXTREME_MODULE_LOG PL_LEVEL_NAMED_LOG(pl_log_module_name_string, VLEVEL_EXTREME)
00339 
00340 #endif // MODULE_LOG
00341 #endif // PL_LOG_MODULE_NAME
00342 
00343 
00344 //#####  Heading Support  #####################################################
00345 
00347 PStream& plsep(PStream&);
00348 
00350 inline PL_Log::Heading plhead(string s)
00351 {
00352     return PL_Log::Heading(s);
00353 }
00354 
00356 PStream& operator<<(PStream&, PL_Log::Heading);
00357 
00358 } // end of namespace PLearn
00359 
00360 #endif
00361 
00362 
00363 /*
00364   Local Variables:
00365   mode:c++
00366   c-basic-offset:4
00367   c-file-style:"stroustrup"
00368   c-file-offsets:((innamespace . 0)(inline-open . 0))
00369   indent-tabs-mode:nil
00370   fill-column:79
00371   End:
00372 */
00373 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines