Add Makefile, Doxyfile and minor improvement of flog and flog doxygen comments
[flog.git] / flog.h
blob551e9787ba7512ac7443b29a6d907f7e4af77667
1 /*!
2 @file flog.h
3 @brief Flog - The F logging library
4 @author Nabeel Sowan (nabeel.sowan@vibes.se)
6 Useful as the main logger of a program
7 */
9 #ifndef FLOG_H
10 #define FLOG_H
12 /* FLOG_MSG_TYPE_ENUM_API allows switching to an enum API for flog.
13 What this means is that FLOG_MSG_TYPE_T will be defined as an int
14 (instead of unsigned char) In return, enums may yield stronger
15 type checking and therefore easier debugging. */
17 #ifdef FLOG_MSG_TYPE_ENUM_API
19 typedef enum flog_msg_type {
20 FLOG_NONE = 0x00,
21 FLOG_NOTHING = FLOG_NONE,
23 FLOG_CRIT = 0x01,
24 FLOG_CRITICAL = FLOG_CRIT,
26 FLOG_ERR = 0x02,
27 FLOG_ERROR = FLOG_ERR,
29 FLOG_WARN = 0x04,
30 FLOG_WARNING = FLOG_WARN,
31 FLOG_ALERT = FLOG_WARN,
33 FLOG_NOTE = 0x08,
34 FLOG_NOTIFY = FLOG_NOTE,
35 FLOG_IMP = FLOG_NOTE,
36 FLOG_IMPORTANT = FLOG_NOTE,
38 FLOG_INFO = 0x10,
39 FLOG_INFORMATION = FLOG_INFO,
40 FLOG_MSG = FLOG_INFO,
41 FLOG_MESSAGE = FLOG_INFO,
43 FLOG_VINFO = 0x20,
44 FLOG_VERBOSE = FLOG_VINFO,
46 FLOG_DEBUG = 0x40,
48 FLOG_FLOG_DEBUG = 0x80,
50 FLOG_ACCEPT_ONLY_CRITICAL = FLOG_CRIT,
51 FLOG_ACCEPT_ONLY_ERROR = FLOG_CRIT | FLOG_ERR,
52 FLOG_ACCEPT_ERROR_AND_WARNING = FLOG_CRIT | FLOG_ERR | FLOG_WARN,
53 FLOG_ACCEPT_IMPORTANT_NOTES = FLOG_CRIT | FLOG_ERR | FLOG_WARN | FLOG_NOTE,
54 FLOG_ACCEPT_INFO = FLOG_CRIT | FLOG_ERR | FLOG_WARN | FLOG_NOTE | FLOG_INFO,
55 FLOG_ACCEPT_VERBOSE_INFO = FLOG_CRIT | FLOG_ERR | FLOG_WARN | FLOG_NOTE | FLOG_INFO | FLOG_VINFO,
56 FLOG_ACCEPT_ALL = FLOG_CRIT | FLOG_ERR | FLOG_WARN | FLOG_NOTE | FLOG_INFO | FLOG_VINFO | FLOG_DEBUG,
57 FLOG_ACCEPT_FLOG_DEBUG = FLOG_CRIT | FLOG_ERR | FLOG_WARN | FLOG_NOTE | FLOG_INFO | FLOG_VINFO | FLOG_DEBUG | FLOG_FLOG_DEBUG
58 } FLOG_MSG_TYPE_T;
60 #else /* FLOG_MSG_TYPE_ENUM_API */
62 typedef unsigned char FLOG_MSG_TYPE_T;
64 //! Nothing
66 #define FLOG_NONE 0x00
67 #define FLOG_NOTHING FLOG_NONE
69 //! Critical error
70 #define FLOG_CRIT 0x01
71 #define FLOG_CRITICAL FLOG_CRIT
73 //! Error
74 #define FLOG_ERR 0x02
75 #define FLOG_ERROR FLOG_ERR
77 //! Warning
78 #define FLOG_WARN 0x04
79 #define FLOG_WARNING FLOG_WARN
80 #define FLOG_ALERT FLOG_WARN
82 //! Note
83 #define FLOG_NOTE 0x08
84 #define FLOG_NOTIFY FLOG_NOTE
85 #define FLOG_IMP FLOG_NOTE
86 #define FLOG_IMPORTANT FLOG_NOTE
88 //! Info
89 #define FLOG_INFO 0x10
90 #define FLOG_INFORMATION FLOG_INFO
91 #define FLOG_MSG FLOG_INFO
92 #define FLOG_MESSAGE FLOG_INFO
94 //! Info in verbose mode
95 #define FLOG_VINFO 0x20
96 #define FLOG_VERBOSE FLOG_VINFO
98 //! Debug info
99 #define FLOG_DEBUG 0x40
101 //! Debug info for flog itself
102 #define FLOG_FLOG_DEBUG 0x80
104 //! Bitmask to show only errors
105 #define FLOG_ACCEPT_ONLY_CRITICAL FLOG_CRIT
106 #define FLOG_ACCEPT_ONLY_ERROR FLOG_CRIT | FLOG_ERR
107 #define FLOG_ACCEPT_ERROR_AND_WARNING FLOG_CRIT | FLOG_ERR | FLOG_WARN
108 #define FLOG_ACCEPT_IMPORTANT_NOTE FLOG_CRIT | FLOG_ERR | FLOG_WARN | FLOG_NOTE
109 #define FLOG_ACCEPT_INFO FLOG_CRIT | FLOG_ERR | FLOG_WARN | FLOG_NOTE | FLOG_INFO
110 #define FLOG_ACCEPT_VERBOSE_INFO FLOG_CRIT | FLOG_ERR | FLOG_WARN | FLOG_NOTE | FLOG_INFO | FLOG_VINFO
111 #define FLOG_ACCEPT_ALL FLOG_CRIT | FLOG_ERR | FLOG_WARN | FLOG_NOTE | FLOG_INFO | FLOG_VINFO | FLOG_DEBUG
112 #define FLOG_ACCEPT_FLOG_DEBUG FLOG_CRIT | FLOG_ERR | FLOG_WARN | FLOG_NOTE | FLOG_INFO | FLOG_VINFO | FLOG_DEBUG | FLOG_FLOG_DEBUG
114 #endif /* FLOG_MSG_TYPE_ENUM_API */
116 //! Macros to insert source info into print strings
117 #ifdef FLOG_SRC_INFO
118 //Maybe it is better to use __func__ than __FUNCTION__ ?
119 #define flog_print(p, type, subsystem, text) _flog_print (p, __FILE__, __LINE__, __FUNCTION__, type, subsystem, text)
120 #define flog_printf(p, type, subsystem, ...) _flog_printf (p, __FILE__, __LINE__, __FUNCTION__, type, subsystem, __VA_ARGS__)
121 #else
122 #define flog_print(p, type, subsystem, text) _flog_print (p, type, subsystem, text)
123 #define flog_printf(p, type, subsystem, ...) _flog_printf (p, type, subsystem, __VA_ARGS__)
124 #endif
126 //! Macros to allow removal of messages from release builds
127 #ifdef DEBUG
128 #define flog_dprint(p, type, subsystem, text) flog_print (p, type, subsystem, text)
129 #define flog_dprintf(p, type, subsystem, ...) flog_printf (p, type, subsystem, __VA_ARGS__)
130 #else
131 #define flog_dprint(p, type, subsystem, text)
132 #define flog_dprintf(p, type, subsystem, ...)
133 #endif
135 #ifdef FLOG_TIMESTAMP
136 typedef int FLOG_TIMESTAMP_T;
137 #endif
139 typedef struct {
140 #ifdef FLOG_TIMESTAMP
141 FLOG_TIMESTAMP_T time; //!< timestamp
142 #endif
143 #ifdef FLOG_SRC_INFO
144 char *src_file; //!< source file emitting message
145 int src_line; //!< source line number emitting message
146 char *src_func; //!< source function emitting message
147 #endif
148 FLOG_MSG_TYPE_T type; //!< type of message
149 char *subsystem; //!< subsystem which is outputting the msg
150 char *text; //!< message contents
151 } FLOG_MSG_T;
153 typedef struct flog_t {
154 char *name; //!< name of log
155 FLOG_MSG_TYPE_T accepted_msg_type; //!< bitmask of which messages to accept
156 int (*output_func)(struct flog_t *,const FLOG_MSG_T *); //!< function to output messages to
157 void *output_func_data; //!< data passed to output func
158 int output_error; //!< errors occurred on output
159 int output_stop_on_error; //!< stop outputting messages on error
160 struct flog_t *error_log; //!< error log for flog errors
161 FLOG_MSG_T **msg; //!< array of messages
162 int msg_amount; //!< amount of messages in array
163 int msg_max; //!< maximum amount of buffered messages
164 struct flog_t **sublog; //!< array of sublogs
165 int sublog_amount; //!< amount of sublogs in array
166 } FLOG_T;
168 void init_flog_msg_t(FLOG_MSG_T *p);
169 #ifdef FLOG_SRC_INFO
170 FLOG_MSG_T * create_flog_msg_t(const char *src_file,int src_line,const char *src_func,FLOG_MSG_TYPE_T msg_type,const char *subsystem,const char *text);
171 #else
172 FLOG_MSG_T * create_flog_msg_t(FLOG_MSG_TYPE_T msg_type,const char *subsystem,const char *text);
173 #endif
174 void destroy_flog_msg_t(FLOG_MSG_T *p);
176 void init_flog_t(FLOG_T *p);
177 FLOG_T * create_flog_t(const char *name, FLOG_MSG_TYPE_T accepted_msg_type);
178 void destroy_flog_t(FLOG_T *p);
180 int flog_add_msg(FLOG_T *p,FLOG_MSG_T *msg);
181 void flog_clear_msg_buffer(FLOG_T *p);
182 int flog_append_sublog(FLOG_T *p,FLOG_T *sublog);
184 #ifdef FLOG_SRC_INFO
185 int _flog_print(FLOG_T *p,const char *src_file,int src_line,const char *src_func,FLOG_MSG_TYPE_T type,const char *subsystem,const char *text);
186 int _flog_printf(FLOG_T *p,const char *src_file,int src_line,const char *src_func,FLOG_MSG_TYPE_T type,const char *subsystem,const char *textf, ...);
187 #else
188 int _flog_print(FLOG_T *p,FLOG_MSG_TYPE_T type,const char *subsystem,const char *text);
189 int _flog_printf(FLOG_T *p,FLOG_MSG_TYPE_T type,const char *subsystem,const char *textf, ...);
190 #endif
192 char * flog_msg_t_to_str(const FLOG_MSG_T *p);
193 char * flog_get_msg_type_str(FLOG_MSG_TYPE_T type);
194 void flog_test(FLOG_T *p);
196 #ifdef FLOG_TIMESTAMP
197 const char * get_timestamp(void);
198 #endif
200 #endif