Back to index

im-sdk  12.3.91
IMLog.hh
Go to the documentation of this file.
00001 #ifndef IMLOG_HH_
00002 #define IMLOG_HH_
00003 #include <stdarg.h>
00004 #include <stdio.h>
00005 #include "basictype.hh"
00006 
00007 class IMLog;
00008 
00009 /* singleton class */
00010 class IMLog
00011 {
00012   public:
00013     enum LOG_LEVEL {
00014            INVALID = -1,
00015            QUIET,
00016            ERROR,
00017            WARNING,
00018            NORMAL,
00019            INFO,
00020            VERBOSE,
00021            DEBUGLOG
00022     };
00023     enum LOG_DESTINATION {
00024            IMLOG_DEFAULT = -1,
00025            IMLOG_STDOUT,
00026            IMLOG_STDERR,
00027            SYSLOG_AUTHPRIV,
00028            SYSLOG_USER,
00029            SYSLOG_LOCAL0,
00030            SYSLOG_LOCAL1,
00031            SYSLOG_LOCAL2,
00032            SYSLOG_LOCAL3,
00033            SYSLOG_LOCAL4,
00034            SYSLOG_LOCAL5,
00035            SYSLOG_LOCAL6,
00036            SYSLOG_LOCAL7
00037     };
00038 
00039   private:
00040     static IMLog *plinst;
00041     enum LOG_LEVEL lv;
00042     enum LOG_DESTINATION dest;
00043     char *progname;
00044 
00045     void output_file(
00046        FILE *file,
00047        const char *mes
00048     );
00049     void output_syslog(
00050        int facility,
00051        int priority,
00052        const char *mes
00053     );
00054     int get_syslog_priority(
00055        enum LOG_LEVEL l
00056     );
00057 
00058     virtual void output(
00059        const char *mes,
00060        enum LOG_LEVEL lv = NORMAL,
00061        enum LOG_DESTINATION d = IMLOG_DEFAULT
00062     );
00063 
00064     IMLog(
00065        const char *progname
00066     );
00067     virtual ~IMLog();
00068   public:
00069     static IMLog* get_instance()
00070     { return plinst; }
00071     static IMLog* construct(
00072        const char* progname,
00073        enum LOG_LEVEL lv = NORMAL,
00074        enum LOG_DESTINATION d = IMLOG_DEFAULT
00075     );
00076     static void cleanup() 
00077     { 
00078        if (plinst) delete plinst;
00079        plinst = NULL;
00080     }
00081 
00082     bool check(
00083        enum LOG_LEVEL l
00084     ) { return (l <= lv); }
00085     void voutput(
00086        enum LOG_LEVEL lv,
00087        enum LOG_DESTINATION d,
00088        const char *fmt,
00089        va_list args
00090     );
00091     void set_log_level(
00092        enum LOG_LEVEL l
00093     ) { lv = l; }
00094     void set_default_destination(
00095        enum LOG_DESTINATION d
00096     ) { dest = d; }
00097 };
00098 
00099 inline void
00100 IMLog_output(
00101     enum IMLog::LOG_LEVEL lv,
00102     enum IMLog::LOG_DESTINATION d,
00103     const char *fmt,
00104     ...
00105 )
00106 {
00107     va_list va;
00108     IMLog *pimlog = IMLog::get_instance();
00109 
00110     if (!(pimlog->check(lv))) return;
00111     va_start(va, fmt);
00112     pimlog->voutput(lv, d, fmt, va);
00113     va_end(va);
00114 }
00115 
00116 #define IMLOG_DEFINE_FUNC(fn, lv, sl) \
00117 inline void fn(const char *fmt, ...) \
00118 { \
00119     va_list va; \
00120     IMLog *pimlog = IMLog::get_instance(); \
00121 \
00122     if (!(pimlog->check(lv))) return; \
00123     va_start(va, fmt); \
00124     pimlog->voutput((lv), IMLog::IMLOG_DEFAULT, fmt, va); \
00125     if (sl != IMLog::IMLOG_DEFAULT) pimlog->voutput((lv), (sl), fmt, va); \
00126     va_end(va); \
00127 }
00128 
00129 IMLOG_DEFINE_FUNC(LOG_CRITICAL, IMLog::ERROR, IMLog::SYSLOG_USER);
00130 IMLOG_DEFINE_FUNC(LOG_ERROR, IMLog::ERROR, IMLog::IMLOG_DEFAULT);
00131 IMLOG_DEFINE_FUNC(LOG_WARNING, IMLog::WARNING, IMLog::IMLOG_DEFAULT);
00132 IMLOG_DEFINE_FUNC(LOG_NORMAL, IMLog::NORMAL, IMLog::IMLOG_DEFAULT);
00133 IMLOG_DEFINE_FUNC(LOG_INFO, IMLog::INFO, IMLog::IMLOG_DEFAULT);
00134 IMLOG_DEFINE_FUNC(LOG_VERBOSE, IMLog::VERBOSE, IMLog::IMLOG_DEFAULT);
00135 IMLOG_DEFINE_FUNC(LOG_DEBUG, IMLog::DEBUGLOG, IMLog::IMLOG_DEFAULT);
00136 
00137 #endif /* not IMLOG_HH_ */
00138 
00139 /* Local Variables: */
00140 /* c-file-style: "iiim-project" */
00141 /* End: */