log_base.h

Go to the documentation of this file.
00001 #ifndef LOG_BASE_CLASS
00002 #define LOG_BASE_CLASS
00003 
00004 /*****************************************************************************\
00005 *                                                                             *
00006 *  Name   : log_base                                                          *
00007 *  Author : Chris Koeritz                                                     *
00008 *                                                                             *
00009 *******************************************************************************
00010 * Copyright (c) 1996-$now By Author.  This program is free software; you can  *
00011 * redistribute it and/or modify it under the terms of the GNU General Public  *
00012 * License as published by the Free Software Foundation; either version 2 of   *
00013 * the License or (at your option) any later version.  This is online at:      *
00014 *     http://www.fsf.org/copyleft/gpl.html                                    *
00015 * Please send any updates to: fred@gruntose.com                               *
00016 \*****************************************************************************/
00017 
00018 #include "object_base.h"
00019 #include "program_wide_logger.h"
00020 #include "utility.h"
00021 
00023 
00030 class log_base : public virtual object_base
00031 {
00032 public:
00033   log_base();  
00034 
00035   log_base(const log_base &to_copy);  
00036 
00037   virtual ~log_base();  
00038 
00039   log_base &operator =(const log_base &to_copy); 
00040 
00042 
00048   enum KNOWN_FILTERS {
00049     // these two filters are predefined and their meaning cannot be changed.
00050     ALWAYS_PRINT = common::ALWAYS_PRINT,  
00051     NEVER_PRINT = common::NEVER_PRINT  
00052   };
00053 
00054   virtual void add_filter(int new_filter);
00056 
00059   virtual void remove_filter(int old_filter);
00061 
00062   virtual bool member(int filter_to_check);
00064 
00067   virtual void clear_filters();
00069 
00070   virtual int_set filter_set();
00072 
00074 
00088   enum line_ending {
00089     LF_AT_END = -15,  
00090     CRLF_AT_END,  
00091     NO_ENDING  
00092   };
00093 
00094   virtual line_ending eol();
00096   virtual void eol(line_ending to_set);
00098 
00099   virtual istring get_ending();
00101   virtual void get_ending(istring &to_end);
00103 
00104   // the real core of the log_base is the log() method, which all derived
00105   // classes MUST provide.
00106 
00107   virtual outcome log(const istring &info, int filter = ALWAYS_PRINT) = 0;
00109 
00118   outcome log(const char *info, int filter = ALWAYS_PRINT);
00120   inline log_base &operator <<(const istring &info) { log(info); return *this; }
00122   inline log_base &operator <<(const char *info) { log(info); return *this; }
00124 
00125   // helpful methods for dealing with line endings.
00126 
00127   static const char *string_for_ending(line_ending ending);
00129   static line_ending pick_ending_for_platform();
00131   static const char *platform_ending();
00133 
00134   // nitty-gritty special functions...
00135 
00136   void stuff_filter_set(const int_set &new_set);
00138 
00141 private:
00142   mutex *_lock;  
00143   int_set *_filter_set;  
00144   line_ending _ending;  
00145 };
00146 
00148 
00149 // these macros can assist in logging.  they rely on the base class for
00150 // logging services.  note that it is often convenient to customize these
00151 // to yield a simpler macro name per project, such as PRINT or LOG.
00152 
00154 
00157 #define FILTER_LOG(the_logger, to_log, filter) { \
00158   if (the_logger.member(filter)) { \
00159     the_logger.log(to_log, filter); \
00160   } \
00161 }
00162 
00164 #define EMERGENCY_LOG(the_logger, to_log) \
00165   FILTER_LOG(the_logger, to_log, log_base::ALWAYS_PRINT)
00166 
00168 #define STAMPED_FILTER_LOG(the_logger, to_log, filter) { \
00169   if (the_logger.member(filter)) { \
00170     istring temp_log = to_log; \
00171     if (temp_log.length()) \
00172       temp_log.insert(0, utility::timestamp(true, true)); \
00173     the_logger.log(temp_log, filter); \
00174   } \
00175 }
00177 #define STAMPED_EMERGENCY_LOG(the_logger, to_log) \
00178   STAMPED_FILTER_LOG(the_logger, to_log, log_base::ALWAYS_PRINT)
00179 
00181 /* These add a class and function name to the log entry. */
00182 #define CLASS_FILTER_LOG(the_logger, to_log, filter) { \
00183   update_current_stack_frame_line_number(__LINE__); \
00184   if (the_logger.member(filter)) { \
00185     istring temp_log = to_log; \
00186     if (temp_log.length()) { \
00187       temp_log.insert(0, utility::timestamp(true, true)); \
00188       BASE_FUNCTION(func); \
00189       temp_log += " ["; \
00190       temp_log += function_name; \
00191       temp_log += "]"; \
00192     } \
00193     the_logger.log(temp_log, filter); \
00194   } \
00195   update_current_stack_frame_line_number(__LINE__); \
00196 }
00198 #define CLASS_EMERGENCY_LOG(the_logger, to_log) \
00199   CLASS_FILTER_LOG(the_logger, to_log, log_base::ALWAYS_PRINT)
00200 
00202 
00204 #define INSTANCE_FILTER_LOG(the_logger, to_log, filter) { \
00205   update_current_stack_frame_line_number(__LINE__); \
00206   if (the_logger.member(filter)) { \
00207     istring temp_log = to_log; \
00208     if (temp_log.length()) { \
00209       temp_log.insert(0, utility::timestamp(true, true)); \
00210       BASE_INSTANCE_FUNCTION(func); \
00211       temp_log += " ["; \
00212       temp_log += function_name; \
00213       temp_log += "]"; \
00214     } \
00215     the_logger.log(temp_log, filter); \
00216     update_current_stack_frame_line_number(__LINE__); \
00217   } \
00218 }
00220 #define INSTANCE_EMERGENCY_LOG(the_logger, to_log) \
00221   INSTANCE_FILTER_LOG(the_logger, to_log, log_base::ALWAYS_PRINT)
00222 
00223 #endif
00224 

Generated on Fri Sep 5 04:28:36 2008 for HOOPLE Libraries by  doxygen 1.5.1