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

ErrorReporting.h

00001 /*****************************************************************************
00002  * Author:   Valient Gough <vgough@pobox.com>
00003  *
00004  *****************************************************************************
00005  * Copyright (c) 2001, Valient Gough
00006  *
00007  * This library is free software; you can distribute it and/or modify it under
00008  * the terms of the GNU Lesser General Public License (LGPL), as published by
00009  * the Free Software Foundation; either version 2.1 of the License, or (at your
00010  * option) any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful, but WITHOUT
00013  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014  * FITNESS FOR A PARTICULAR PURPOSE.  See the LGPL in the file COPYING for more
00015  * details.
00016  *
00017  */
00018 
00019 #ifndef _ErrorReporting_incl
00020 #define _ErrorReporting_incl
00021 
00022 #include <rel/SmartPtr.h>
00023 
00024 #include <stdio.h>
00025 #include <errno.h>
00026 
00027 namespace rel
00028 {
00029 
00030     class DebugChannel
00031     {
00032     public:
00033         enum MsgType
00034         {
00035             Debug,
00036             Info,
00037             Warning,
00038             Error,
00039             Fatal
00040         };
00041 
00042         DebugChannel(MsgType type);
00043         virtual ~DebugChannel();
00044 
00045         // emit message on channel..
00046         virtual void emit(const char *file, int line, const char *message);
00047 
00048     private:
00049         MsgType type;
00050 
00051     };
00052 
00053 
00054 
00055     // formats message and calls channel->emit() to emit message..
00056     void rLogMessage(DebugChannel *channel,
00057                      const char *file,
00058                      int lineNum,
00059                      const char *format, ...)
00060 #ifdef __GNUC__
00061                      __attribute((format (printf, 4, 5)))
00062 #endif
00063                      ;
00064 
00065     // initialize default channels (debug/info/warning/error/fatal) - the
00066     // default is to send all output to stderr..
00067     void initializeDebugChannels();
00068 
00069 } // namespace rel
00070 
00071 
00072 // some predefined channels.  The defaults can be replaced by assigning he
00073 // pointers to point to new handlers..
00074 extern rel::SmartPtr<rel::DebugChannel> gDebugChannel;
00075 extern rel::SmartPtr<rel::DebugChannel> gInfoChannel;
00076 extern rel::SmartPtr<rel::DebugChannel> gWarningChannel;
00077 extern rel::SmartPtr<rel::DebugChannel> gErrorChannel;
00078 extern rel::SmartPtr<rel::DebugChannel> gFatalChannel;
00079 
00080 
00081 #define rMessage(Channel,Format,args...) \
00082     rel::rLogMessage(Channel, __FILE__,__LINE__,Format , ## args)
00083 
00084 // rDebug() macro: log to 'debug' channel
00085 #define rDebug(Format, args...) \
00086     rMessage(gDebugChannel.get(),Format , ## args)
00087 
00088 // rInfo() macro: log to 'info' channel
00089 #define rInfo(Format, args...) \
00090     rMessage(gInfoChannel.get(),Format , ## args)
00091 
00092 // rWarning() macro: log to 'warning' channel
00093 #define rWarning(Format, args...)  \
00094     rMessage(gWarningChannel.get(),Format , ## args)
00095 
00096 // rError() macro: log to 'error' channel
00097 #define rError(Format, args...) \
00098     rMessage(gErrorChannel.get(),Format , ## args)
00099 
00100 // rFatal() macro: log to 'fatal' channel
00101 #define rFatal(Format, args...) \
00102     rMessage(gFatalChannel.get(),Format , ## args)
00103 
00104 
00105 
00106 // for reporting system errors
00107 #define rPerror(Format,args...) \
00108 { \
00109     rError(Format, ## args); \
00110     if(errno < sys_nerr) \
00111         rError("errno = %i: %s", errno, sys_errlist[errno]); \
00112     else \
00113         rError("errno = %i", errno); \
00114 }
00115 
00116 // ASSERT checks
00117 #define assert_rError(CMD) \
00118 if(! (CMD) ) \
00119     rError("Assert failure: " #CMD);
00120 
00121 #define assert_rFatal(CMD) \
00122 if(! (CMD) ) \
00123     rFatal("Assert failure: " #CMD);
00124 
00125 
00126 #endif // _ErrorReporting_incl

Generated at Sat Sep 22 02:20:00 2001 for librel by doxygen1.2.10 written by Dimitri van Heesch, © 1997-2001