Simple utilities sink - stuff that doesn't fit anywhere else / objects in plain C Snapshot
logg.h
Go to the documentation of this file.
00001 #ifndef __LOGG_H__
00002 #define __LOGG_H__
00003 
00004 #include <stdlib.h>
00005 
00006 /**
00007  * @defgroup logging library.
00008  *
00009  * Not so simple logging subsystem - one logging category is supported (one global log level).
00010  * Otherwise this is a very versatile logging subsystem.
00011  *
00012  * @{
00013  */
00014 
00015 /**
00016  * @brief defined logging levels 
00017  */
00018 typedef enum {
00019   MLOG_LEVEL_TURN_OFF_LOGGING,  /** setting this level as currrent level will turn off logging */
00020   MLOG_LEVEL_ERROR, /** logging level for errors */
00021   MLOG_LEVEL_WARN,  /** logging level for warnings */
00022   MLOG_LEVEL_INFO, /** logging level for info messages */
00023   MLOG_LEVEL_DEBUG, /** logging level for debug messages */
00024   MLOG_LEVEL_TRACE, /** logging level for trace messages */
00025 
00026 } MLOG_LEVEL;
00027 
00028 /**
00029  * @brief actions for logging stuff
00030  */
00031 typedef enum {
00032   MLOG_ACTION_TO_FILE  = 0x1, /** log entries are dumped to file */
00033   MLOG_ACTION_SYSLOG_TRACE = 0x2, /** log entries are dumped to one syslog level */
00034   MLOG_ACTION_CONSOLE = 0x4, /** log entries are dumped to console */
00035 
00036 } MLOG_ACTION;
00037 
00038 
00039 /** 
00040  * @brief features to include in logging prefix
00041  */
00042 typedef enum {
00043   MLOG_PREFIX_LOG_LEVEL = 0x1,    /** add log level to entry */
00044   MLOG_PREFIX_SOURCE_FILE = 0x2,  /** add source file / line number of log statement to entry */
00045   MLOG_PREFIX_TIME = 0x4,         /** add HOUR:MINUTE:SECOND:MICROSECOND to log entry */
00046 
00047 } MLOG_PREFIX;
00048 
00049 
00050 /**
00051  * @brief how to allocation the scrach pad buffer for logging
00052  */
00053 typedef enum {
00054   MLOG_ALLOC_STACK,     /** buffer for logging allocated on stack (alloca) */
00055   MLOG_ALLOC_HEAP,      /** buffer for logging allocated on heap (pre log request) */
00056   MLOG_ALLOC_TLS_HEAP,  /** buffer for logging allocated on heap once for each operating system thread, stored in thread local storage */
00057 
00058 } MLOG_ALLOC;
00059 
00060 
00061 /**
00062  * @brief explicitly initialises logging. 
00063  */
00064 int  MLOG_init( MLOG_LEVEL current, MLOG_ACTION action, void *arg);
00065 
00066 
00067 /**
00068  * @brief sets level of detail added to prefix of log entry (A bitmask of MLOG_PREFIX values)
00069  *
00070  */
00071 void MLOG_set_prefix_features( int prefix );
00072 
00073 /**
00074  * @brief determines memory allocation stragegy used here
00075  * @param option - how to allocate buffer for logging. options are: use stack (MLOG_ALLOC_STACK), heap (MLOG_ALLOC_HEAP), heap and store in tls (MLOG_ALLOC_TLS_HEAP)
00076  * @param size - maximum length of a log entry allowed here.
00077  *  
00078  */
00079 int  MLOG_alloc_option( MLOG_ALLOC option, size_t size);
00080 
00081 /**
00082  * @brief set log level for which the current stack is dumped. By default this would happen for LOG_LEVEL_ERROR
00083  */
00084 void MLOG_dump_stack_level( MLOG_LEVEL stack_dump_level );
00085 
00086 
00087 #ifndef NO_LOG
00088 
00089 
00090 extern MLOG_LEVEL m_current_log_level;
00091 
00092 /**
00093  * @brief create a log entry, do not use this function directly, use MLOG_<LOG_LEVEL_NAME> macros instead
00094  */
00095 int MLOG_printf( MLOG_LEVEL current, const char *file, int line,  const char *format , ... );
00096 
00097 
00098 
00099 #define MLOG_ERROR(...) do { if (m_current_log_level >= MLOG_LEVEL_ERROR) {  MLOG_printf( MLOG_LEVEL_ERROR, __FILE__, __LINE__,  __VA_ARGS__ ); } } while(0);
00100 
00101 #define MLOG_WARN(...)  do { if (m_current_log_level >= MLOG_LEVEL_WARN) {  MLOG_printf( MLOG_LEVEL_WARN, __FILE__, __LINE__, __VA_ARGS__ ); } } while(0);
00102  
00103 #define MLOG_INFO(...)  do { if (m_current_log_level >= MLOG_LEVEL_INFO) {  MLOG_printf( MLOG_LEVEL_INFO,  __FILE__, __LINE__, __VA_ARGS__ ); } } while(0);
00104  
00105 #define MLOG_DEBUG(...) do { if (m_current_log_level >= MLOG_LEVEL_DEBUG) {  MLOG_printf( MLOG_LEVEL_DEBUG,  __FILE__, __LINE__, __VA_ARGS__ ); } } while(0);
00106  
00107 #define MLOG_TRACE(...) do { if (m_current_log_level >= MLOG_LEVEL_TRACE) {  MLOG_printf( MLOG_LEVEL_TRACE, __FILE__, __LINE__, __VA_ARGS__ ); } } while(0);
00108 
00109 #else
00110 
00111 #define MLOG_ERROR(...) 
00112 
00113 #define MLOG_WARN(...) 
00114  
00115 #define MLOG_INFO(...)  
00116  
00117 #define MLOG_DEBUG(...) 
00118  
00119 #define MLOG_TRACE(...) 
00120 
00121 #endif
00122 
00123 /**
00124  * @}
00125  */
00126 
00127 #endif
00128 
00129