Main Page | Class Hierarchy | Alphabetical List | Compound List | File List | Compound Members | File Members | Related Pages

LOW_helper_msglog.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002                           LOW_helper_msglog.cpp  -  description
00003                              -------------------
00004     begin                : Sun Jul 21 2002
00005     copyright            : (C) 2002 by Harald Roelle
00006     email                : roelle@informatik.uni-muenchen.de
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00018 
00019 #include <memory>
00020 
00021 #include <stdlib.h>
00022 #include <stdio.h>
00023 #include <string.h>
00024 #include <errno.h>
00025 #include <stdarg.h>
00026 
00027 
00028 #include "LOW_helper_msglog.h"
00029 #include "LOW_platformMisc.h"
00030 #include "LOW_thread_Factory.h"
00031 
00032 
00033 //=====================================================================================
00034 //
00035 // static attributes
00036 //
00037 
00038 bool LOW_helper_msglog::errorOccured = false;
00039 bool LOW_helper_msglog::debugLevels[all_dl];
00040 
00041 bool LOW_helper_msglog::useStdMsgStream     = true;
00042 bool LOW_helper_msglog::useStdWarnStream    = true;
00043 bool LOW_helper_msglog::useStdErrStream     = true;
00044 bool LOW_helper_msglog::useStdDebugStream   = true;
00045 bool LOW_helper_msglog::useExtraMsgStream   = false;
00046 bool LOW_helper_msglog::useExtraWarnStream  = false;
00047 bool LOW_helper_msglog::useExtraErrStream   = false;
00048 bool LOW_helper_msglog::useExtraDebugStream = false;
00049   
00050 FILE* LOW_helper_msglog::stdOutStream     = stdout;
00051 FILE* LOW_helper_msglog::stdWarnStream    = stderr;
00052 FILE* LOW_helper_msglog::stdErrStream     = stderr;
00053 FILE* LOW_helper_msglog::stdDebugStream   = stderr;
00054 FILE* LOW_helper_msglog::extraOutStream   = NULL;
00055 FILE* LOW_helper_msglog::extraWarnStream  = NULL;
00056 FILE* LOW_helper_msglog::extraErrStream   = NULL;
00057 FILE* LOW_helper_msglog::extraDebugStream = NULL;
00058 
00059 
00060 
00061 //=====================================================================================
00062 //
00063 // constructors
00064 //
00065 
00066 LOW_helper_msglog::LOW_helper_msglog()
00067 {
00068 };
00069 
00070 
00071 LOW_helper_msglog::~LOW_helper_msglog()
00072 {
00073 };
00074 
00075 
00076 
00077 //=====================================================================================
00078 //
00079 // static methods
00080 //
00081 
00082 void LOW_helper_msglog::setDebugLevelEnabled( const debugLevel_t inLevel, const bool isEnabled)
00083 {
00084   msgLock lock = msgLock();
00085   debugLevels[inLevel] = isEnabled;
00086 }
00087 
00088 
00089 bool LOW_helper_msglog::getDebugLevelEnabled( const debugLevel_t inLevel)
00090 {
00091   return debugLevels[inLevel];
00092 }
00093 
00094 
00095 //=====================================================================================
00096 //
00097 // static log methods
00098 //
00099 
00100 /**
00101  * Print error message
00102  */
00103 void LOW_helper_msglog::printPerror( const int inErrno, const char *inFormat, ...)
00104 {
00105   va_list inParamList;
00106   int     length = 0;
00107   char    dummy;
00108   char    *buffer;
00109 
00110   // calculate length
00111   va_start( inParamList, inFormat);
00112   length += vsnprintf( &dummy, 0, inFormat, inParamList);
00113   va_end( inParamList);
00114   if ( inErrno > 0 && inErrno < sys_nerr-1 ) {
00115     length += strlen( ": ");
00116     length += strlen( sys_errlist[inErrno]);
00117   }
00118   length ++; // the null char
00119   
00120   buffer = new char[length];
00121   
00122   va_start( inParamList, inFormat);
00123   vsprintf( buffer, inFormat, inParamList);
00124   va_end( inParamList);
00125   if ( inErrno > 0 && inErrno < sys_nerr-1 ) {
00126     strcat( buffer, ": ");
00127     strcat( buffer, sys_errlist[inErrno]);
00128   }
00129 
00130   printError( "%s (Error# %d)", buffer, inErrno);
00131   
00132   delete[] buffer;
00133 }
00134 
00135 
00136 /**
00137  * Print error message
00138  */
00139 void LOW_helper_msglog::printError( const char *inFormat, ...)
00140 {
00141   va_list inParamList;
00142 
00143   va_start( inParamList, inFormat);
00144   va_printToLog( err_log, inFormat, inParamList);
00145   va_end( inParamList);
00146 }
00147 
00148 
00149 /**
00150  * Print warning
00151  */
00152 void LOW_helper_msglog::printWarning( const char *inFormat, ...)
00153 {
00154   va_list inParamList;
00155 
00156   va_start( inParamList, inFormat);
00157   va_printToLog( warn_log, inFormat, inParamList);
00158   va_end( inParamList);
00159 }
00160 
00161 
00162 /**
00163  * Print message
00164  */
00165 void LOW_helper_msglog::printMessage( const char *inFormat, ...)
00166 {
00167   va_list inParamList;
00168 
00169   va_start( inParamList, inFormat);
00170   va_printToLog( msg_log, inFormat, inParamList);
00171   va_end( inParamList);
00172 }
00173 
00174 
00175 /**
00176  * Print debug message
00177  */
00178 void LOW_helper_msglog::printDebug( const debugLevel_t inLevel, const char *inFormat, ...)
00179 {
00180   va_list inParamList;
00181 
00182   if ( debugLevels[inLevel] || debugLevels[all_dl] ) {
00183     va_start( inParamList, inFormat);
00184     va_printToLog( debug_log, inFormat, inParamList);
00185     va_end( inParamList);
00186   }
00187 }
00188 
00189 
00190 
00191 //=====================================================================================
00192 //
00193 // locks
00194 //
00195 
00196 LOW_thread_mutex*  LOW_helper_msglog::msgLock::msgMutex = LOW_thread_Factory::new_mutex( LOW_thread_mutex::mutexKind_recursive);
00197 
00198 
00199 
00200 //=====================================================================================
00201 //
00202 // private methods
00203 //
00204 
00205 void LOW_helper_msglog::va_printToLog( const logType_t inLogType, const char *inFormat, va_list inParamList)
00206 {
00207   FILE    *extraStream = 0, *stdStream = 0;
00208   char    dummy, *buffer, *indentStr, *tmpPtr;
00209   int     headLen, length;
00210 
00211   msgLock lock = msgLock();
00212   
00213   switch ( inLogType ) {
00214     case msg_log:
00215       stdStream   = useStdMsgStream   ? stdOutStream   : NULL;
00216       extraStream = useExtraMsgStream ? extraOutStream : NULL;
00217       break;
00218     case warn_log:
00219       stdStream   = useStdWarnStream   ? stdWarnStream   : NULL;
00220       extraStream = useExtraWarnStream ? extraWarnStream : NULL;
00221       break;
00222     case err_log:
00223       stdStream   = useStdErrStream   ? stdErrStream   : NULL;
00224       extraStream = useExtraErrStream ? extraErrStream : NULL;
00225       break;
00226     case debug_log:
00227       stdStream   = useStdDebugStream   ? stdDebugStream   : NULL;
00228       extraStream = useExtraDebugStream ? extraDebugStream : NULL;
00229       break;
00230   }
00231   
00232   headLen = fprintLogHeader( extraStream, stdStream);
00233 
00234   // build string for indention
00235   indentStr = new char[headLen+1];
00236   memset( indentStr, ' ', headLen);
00237   indentStr[headLen] = '\0';
00238 
00239   if ( inLogType==err_log )
00240     fprintfMulti( extraStream, stdStream, "ERROR -- ");
00241     
00242   if ( inLogType==debug_log )
00243     fprintfMulti( extraStream, stdStream, "DEBUG -- ");
00244 
00245   // detect length and reserve buffer
00246   length = vsnprintf( &dummy, 0, inFormat, inParamList);  // detect length
00247   buffer = new char[length+1];  // get buffer (remember ther \0!)
00248 
00249   vsprintf( buffer, inFormat, inParamList);  // print into buffer
00250   tmpPtr = strtok( buffer, "\n"); 
00251   fprintfMulti( extraStream, stdStream, "%s\n", tmpPtr); // first line without indent
00252   while( (tmpPtr=strtok( NULL, "\n")) != NULL ) {  // subsequent lines with indent
00253     fprintfMulti( extraStream, stdStream, "%s%s\n", indentStr, tmpPtr);
00254   }
00255 
00256   delete[] buffer;
00257   delete[] indentStr;
00258 }
00259 
00260 
00261 unsigned int LOW_helper_msglog::fprintLogHeader( FILE *inExtraStream, FILE *inStdStream)
00262 {
00263   char      tmpStr[200];
00264   time_t    curtime;
00265   struct tm *loctime;
00266 
00267   curtime = time( NULL);
00268   loctime = localtime( &curtime);
00269   strftime( tmpStr, 200, "%a %Y/%m/%d %H:%M:%S", loctime);
00270   fprintfMulti( inExtraStream, inStdStream, "%s [%d]: ", tmpStr, LOW_platformMisc::getThreadID());
00271   return( strlen( tmpStr));
00272 }
00273 
00274 
00275 void LOW_helper_msglog::fprintfMulti( FILE *inExtraStream, FILE *inStdStream, const char *inFormat, ...)
00276 {
00277   va_list inParamList;
00278   va_start( inParamList, inFormat);
00279   vfprintfMulti( inExtraStream, inStdStream, inFormat, inParamList);
00280   va_end( inParamList);
00281 }
00282 
00283 
00284 void LOW_helper_msglog::vfprintfMulti( FILE *inExtraStream, FILE *inStdStream, const char *inFormat, va_list inAp)
00285 {
00286   if ( inExtraStream )  vfprintf( inExtraStream, inFormat, inAp);
00287   if ( inStdStream )    vfprintf( inStdStream, inFormat, inAp);
00288 }
00289 
00290 
00291 void* LOW_helper_msglog::callocCheck( const size_t inSize)
00292 {
00293   void* retPtr;
00294 
00295   if ( (retPtr=calloc( 1, inSize)) == NULL ) {
00296     LOW_helper_msglog::printPerror( errno, "Error in LOW_helper_msglog::callocCheck()");
00297     std::terminate();
00298   }
00299 
00300   return retPtr;
00301 }

Generated on Tue Feb 3 11:30:25 2004 for OneWireLibrary++ by doxygen 1.3.2