log.cpp

00001 /*   
00002  *   (c) Copyright 2008 Philipp Skadorov (philipp_s@users.sourceforge.net)
00003  *
00004  *   This file is part of FREESECS.
00005  *
00006  *   FREESECS is free software: you can redistribute it and/or modify
00007  *   it under the terms of the GNU General Public License as published by
00008  *   the Free Software Foundation, either version 3 of the License, or
00009  *   (at your option) any later version.
00010  *
00011  *   FREESECS is distributed in the hope that it will be useful,
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *   GNU General Public License for more details.
00015  *
00016  *   You should have received a copy of the GNU General Public License
00017  *   along with FREESECS, see COPYING.
00018  *   If not, see <http://www.gnu.org/licenses/>.
00019  */
00020 #include <stdio.h>
00021 #include <stdarg.h>
00022 #include <string.h>
00023 #include <ctype.h>
00024 #include <syslog.h>
00025 #include "log.h"
00026 
00027 char* debugstr_hex_dump (const void *ptr, int len)
00028 {
00029      /* Locals */
00030      char          dumpbuf[59];
00031      char          charbuf[20];
00032      char          tempbuf[8];
00033      const char    *p;
00034      int           i;
00035      unsigned int  nosign;
00036      char          *dst;
00037      char          *outptr;
00038  
00039  /* Begin function dbg_hex_dump */
00040  
00041      /*-----------------------------------------------------------------------
00042      **  Allocate an output buffer
00043      **      A reasonable value is one line overhand (80 chars), and
00044      **      then one line (80) for every 16 bytes.
00045      **---------------------------------------------------------------------*/
00046      outptr = dst = new char[len * (80 / 16) + 80];
00047  
00048      /*-----------------------------------------------------------------------
00049      **  Loop throught the input buffer, one character at a time
00050      **---------------------------------------------------------------------*/
00051      for (i = 0, p = (const char*)ptr; (i < len); i++, p++)
00052      {
00053  
00054          /*-------------------------------------------------------------------
00055          **  If we're just starting a line, 
00056          **      we need to possibly flush the old line, and then
00057          **      intialize the line buffer.
00058          **-----------------------------------------------------------------*/
00059          if ((i % 16) == 0)
00060          {
00061              if (i)
00062              {
00063                  sprintf(outptr, "  %-43.43s   %-16.16s\n", dumpbuf, charbuf);
00064                  outptr += strlen(outptr);
00065              }
00066              sprintf (dumpbuf, "%04x: ", i);
00067              strcpy (charbuf, "");
00068          }
00069  
00070          /*-------------------------------------------------------------------
00071          **  Add the current data byte to the dump section.
00072          **-----------------------------------------------------------------*/
00073          nosign = (unsigned char) *p;
00074          sprintf (tempbuf, "%02X", nosign);
00075  
00076          /*-------------------------------------------------------------------
00077          ** Separate bytes with a space
00078          **------------------------------------------------------------------*/
00079          /*if((i % 2) == 1)*/
00080             strcat(tempbuf, " ");
00081          strcat (dumpbuf, tempbuf);
00082  
00083          /*-------------------------------------------------------------------
00084          **  Add the current byte to the character display part of the
00085          **      hex dump
00086          **-----------------------------------------------------------------*/
00087          sprintf (tempbuf, "%c", isprint(*p) ? *p : '.');
00088          strcat (charbuf, tempbuf);
00089      }
00090  
00091      /*-----------------------------------------------------------------------
00092      **  Flush the last line, if any
00093      **---------------------------------------------------------------------*/
00094      if (i > 0)
00095      {
00096          sprintf(outptr, "  %-43.43s   %-16.16s\n", dumpbuf, charbuf);
00097          outptr += strlen(outptr);
00098      }
00099  
00100      return(dst);
00101 } /* End function dbg_hex_dump */
00102 
00103 
00104 #if 0
00105 
00106 #ifdef TRACE_TIME_ON
00107 #define ONE_MILLION 1000000L
00108 char *hsms_gettime()
00109 {
00110     const size_t len = 16;
00111     static char buf[len];
00112     struct timeval tv;
00113     gettimeofday(&tv, NULL);
00114     snprintf(buf, len, "%lu", tv.tv_sec*ONE_MILLION + tv.tv_usec);
00115     return buf;
00116 }
00117 #endif
00118 
00119 #endif //if 0
00120 
00121 
00122 #ifndef _MODULE_ID
00123 #define _MODULE_ID "HSMS"
00124 #endif 
00125 
00126 
00127 static class _log_init
00128 {
00129 public:
00130     _log_init()
00131     {
00132         ::openlog(_MODULE_ID, LOG_NDELAY, 0);
00133     }
00134     ~_log_init()
00135     {
00136         ::closelog();
00137     }
00138 
00139 } log_init;
00140 
00141 static struct _hsms_log
00142 {
00143 public:
00144     void
00145     operator()(int level, const char *prefix, const char *fmt, va_list ap)
00146     {
00147         const int bufsize = 128;
00148         char format[bufsize];
00149     
00150         if(LOG_LEVEL_UPPER_BOUND < level || level < LOG_LEVEL_LOWER_BOUND)
00151         {
00152             return;
00153         }
00154 #ifndef LOG_TO_CONSOLE
00155         snprintf(format, bufsize, "%s - %s", prefix, fmt);
00156         vsyslog(LOG_FACILITY | LOG_NOTICE, format, ap);
00157 #else
00158         snprintf(format, bufsize, "%s - %s\n", prefix, fmt);
00159         vprintf(format, ap);
00160 #endif
00161     }
00162     void
00163     operator()(int level, const char *prefix, const char *fmt...)
00164     {
00165         va_list ap;
00166         va_start(ap, fmt);
00167         this->operator()(level, prefix, fmt, ap);
00168         va_end(ap);
00169     }
00170 }hsms_log;
00171 
00172 #define HSMS_LOG(lvl, pfx, f) \
00173         { \
00174             va_list ap; \
00175             va_start(ap, f); \
00176             hsms_log(lvl, pfx, f, ap); \
00177             va_end(ap); \
00178         }
00179 
00180 #define HSMS_LOG_EXTRA_FMT(lvl, pfx, f1, f2) \
00181         { \
00182             va_list ap; \
00183             va_start(ap, f1); \
00184             hsms_log(lvl, pfx, f2, ap); \
00185             va_end(ap); \
00186         }
00187 
00188 void 
00189 freesecs::TRACE_CRITICAL      (const char *fmt, ...)
00190 {
00191     HSMS_LOG(0, "[CRTIICAL]", fmt);
00192 }
00193 void 
00194 freesecs::TRACE_ERROR         (const char *fmt, ...)
00195 {
00196     HSMS_LOG(1, "[ERROR]", fmt);
00197 }
00198 void 
00199 freesecs::TRACE_INFO          (int lvl, const char *fmt, ...)
00200 {
00201     HSMS_LOG(3, "[INFO]", fmt);
00202 }
00203 void 
00204 freesecs::TRACE_DEBUG         (int lvl, const char *fmt, ...)
00205 {
00206     HSMS_LOG(lvl, "[DEBUG]", fmt);
00207 }
00208 void 
00209 freesecs::TRACE_FSM_BEGIN     (int lvl, const char *fsm_name, int state, int event)
00210 {
00211     hsms_log(lvl, "[SM]", "{ %s: state %d, event e%d -->", fsm_name, state, event);
00212 }
00213 void 
00214 freesecs::TRACE_FSM_END       (int lvl, const char *fsm_name, int state, int event)
00215 {
00216     hsms_log(lvl, "[SM]", "} %s: state %d event e%d <--", fsm_name, state, event);
00217 }
00218 void 
00219 freesecs::TRACE_FSM_TRANS     (int lvl, const char *fsm_name, int prev_state, int new_state)
00220 {
00221     hsms_log(lvl, "[SM]", "T %s: state %d --> state %d", fsm_name, prev_state, new_state);
00222 }
00223 void 
00224 freesecs::TRACE_FSM_ERROR     (int lvl, const char *fsm_name, const char *fmt, ...)
00225 {
00226     char format[128];
00227     snprintf(format, 128, "! %s: %s", fsm_name, fmt);
00228     HSMS_LOG_EXTRA_FMT(lvl, "[SM]", fmt, format);
00229 }
00230 void 
00231 freesecs::TRACE_FSM_ACTION    (int lvl, const char *fsm_name, const char *act, const char *fmt, ...)
00232 {
00233     char format[128];
00234     snprintf(format, 128, "* %s: %s %s", fsm_name, act, fmt);
00235     HSMS_LOG_EXTRA_FMT(lvl, "[SM]", fmt, format);
00236 }
00237 void 
00238 freesecs::TRACE_HEX_DUMP      (int lvl, const char *msg, size_t len)
00239 {
00240     hsms_log(lvl, "[HEX]", debugstr_hex_dump(msg, len));
00241 }

Generated on Fri Oct 3 15:30:01 2008 for FREESECS hsms by  doxygen 1.5.1