Back to index

im-sdk  12.3.91
IMLog.cpp
Go to the documentation of this file.
00001 #include <config.h>
00002 #include <IMLog.hh>
00003 
00004 #ifdef HAVE_SYSLOG_H
00005 #include <syslog.h>
00006 #endif
00007 
00008 IMLog* IMLog::plinst = NULL;
00009 const int IMLOG_MAX_SIZE = 4096;
00010 
00011 void
00012 IMLog::output(
00013     const char *mes,
00014     enum LOG_LEVEL lvl,
00015     enum LOG_DESTINATION d
00016 )
00017 {
00018     if (d == IMLOG_DEFAULT) d = dest;
00019     switch (d) {
00020       case IMLOG_STDOUT:
00021        output_file(stdout, mes);
00022       case IMLOG_STDERR:
00023        output_file(stderr, mes);
00024        break;
00025 #ifdef HAVE_SYSLOG_H
00026       case SYSLOG_AUTHPRIV:
00027 #ifdef LOG_AUTHPRIV
00028        output_syslog(LOG_AUTHPRIV, get_syslog_priority(lvl), mes);
00029 #elif defined(LOG_AUTH)
00030        output_syslog(LOG_AUTH, get_syslog_priority(lvl), mes);
00031 #endif
00032        break;
00033       case SYSLOG_USER:
00034        output_syslog(LOG_USER, get_syslog_priority(lvl), mes);
00035        break;
00036       case SYSLOG_LOCAL0:
00037        output_syslog(LOG_LOCAL0, get_syslog_priority(lvl), mes);
00038        break;
00039       case SYSLOG_LOCAL1:
00040        output_syslog(LOG_LOCAL1, get_syslog_priority(lvl), mes);
00041        break;
00042       case SYSLOG_LOCAL2:
00043        output_syslog(LOG_LOCAL2, get_syslog_priority(lvl), mes);
00044        break;
00045       case SYSLOG_LOCAL3:
00046        output_syslog(LOG_LOCAL3, get_syslog_priority(lvl), mes);
00047        break;
00048       case SYSLOG_LOCAL4:
00049        output_syslog(LOG_LOCAL4, get_syslog_priority(lvl), mes);
00050        break;
00051       case SYSLOG_LOCAL5:
00052        output_syslog(LOG_LOCAL5, get_syslog_priority(lvl), mes);
00053        break;
00054       case SYSLOG_LOCAL6:
00055        output_syslog(LOG_LOCAL6, get_syslog_priority(lvl), mes);
00056        break;
00057       case SYSLOG_LOCAL7:
00058        output_syslog(LOG_LOCAL7, get_syslog_priority(lvl), mes);
00059        break;
00060 #endif
00061       default:
00062        output_file(stderr, mes);
00063        break;
00064     }
00065 }
00066 
00067 /*
00068   logging interface.
00069  */
00070 
00071 void
00072 IMLog::voutput(
00073     enum LOG_LEVEL l,
00074     enum LOG_DESTINATION d,
00075     const char *fmt,
00076     va_list args
00077 )
00078 {
00079     if (!(lv >= l)) return;
00080     char buf[IMLOG_MAX_SIZE];
00081 
00082     memset(buf, 0, sizeof(buf));
00083 
00084     int len = vsnprintf(buf, sizeof(buf), fmt, args);
00085     output(buf, lv, d);
00086 }
00087 
00088 /*
00089   output physical implementations.
00090  */
00091 
00092 void
00093 IMLog::output_file(
00094     FILE *file,
00095     const char *mes
00096 )
00097 {
00098     fputs(mes, file);
00099     fputc('\n', file);
00100 }
00101 
00102 void
00103 IMLog::output_syslog(
00104     int facility,
00105     int priority,
00106     const char *mes
00107 )
00108 {
00109 #ifdef HAVE_SYSLOG_H
00110     openlog(progname, LOG_PID, facility);
00111     syslog(priority, "%s", mes);
00112     closelog();
00113 #endif
00114 }
00115 
00116 int
00117 IMLog::get_syslog_priority(
00118     enum LOG_LEVEL l
00119 )
00120 {
00121 #ifdef HAVE_SYSLOG_H
00122     if (l == QUIET) {
00123        return LOG_ALERT;
00124     } else if (l == ERROR) {
00125        return LOG_ERR;
00126     } else if (l == WARNING) {
00127        return LOG_WARNING;
00128     } else if (l == NORMAL) {
00129        return LOG_NOTICE;
00130     } else if (l == INFO) {
00131        return LOG_INFO;
00132     } else if (l == VERBOSE) {
00133        return LOG_DEBUG;
00134     } else if (l == DEBUGLOG) {
00135        return LOG_DEBUG;
00136     }
00137     return LOG_NOTICE;
00138 #else
00139     return 0;
00140 #endif
00141 }
00142 
00143 /*
00144   management functions.
00145  */
00146 
00147 IMLog*
00148 IMLog::construct(
00149     const char* progname,
00150     enum LOG_LEVEL lv,
00151     enum LOG_DESTINATION d
00152 )
00153 {
00154     if (plinst) return plinst;
00155     plinst = new IMLog(progname);
00156     plinst->set_log_level(lv);
00157     plinst->set_default_destination(d);
00158 
00159     return plinst;
00160 }
00161 
00162 IMLog::IMLog(
00163     const char *x_progname
00164 )
00165 {
00166     if (x_progname) {
00167        int len;
00168        const char *spos_s, *spos_b;
00169 
00170        spos_s = strrchr(x_progname, '/');
00171        if (spos_s) {
00172            spos_s++;
00173        } else {
00174            spos_s = x_progname;
00175        }
00176 
00177        spos_b = strrchr(x_progname, '\\');
00178        if (spos_b) {
00179            spos_b++;
00180        } else {
00181            spos_b = x_progname;
00182        }
00183        if (spos_s < spos_b) spos_s = spos_b;
00184 
00185        len = strlen(spos_s);
00186        progname = new char[len + 1];
00187        strcpy(progname, spos_s);
00188     }
00189 
00190     lv = NORMAL;
00191     dest = IMLOG_DEFAULT;
00192 }
00193 
00194 IMLog::~IMLog()
00195 {
00196     if (progname) delete [] progname;
00197 }
00198 
00199 /* Local Variables: */
00200 /* c-file-style: "iiim-project" */
00201 /* End: */