3 @brief Flog - The F logging library
4 @author Nabeel Sowan (nabeel.sowan@vibes.se)
6 Useful as the main logger of a program
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
{
21 FLOG_NOTHING
= FLOG_NONE
,
24 FLOG_CRITICAL
= FLOG_CRIT
,
27 FLOG_ERROR
= FLOG_ERR
,
30 FLOG_WARNING
= FLOG_WARN
,
31 FLOG_ALERT
= FLOG_WARN
,
34 FLOG_NOTIFY
= FLOG_NOTE
,
36 FLOG_IMPORTANT
= FLOG_NOTE
,
39 FLOG_INFORMATION
= FLOG_INFO
,
41 FLOG_MESSAGE
= FLOG_INFO
,
44 FLOG_VERBOSE
= FLOG_VINFO
,
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
60 #else /* FLOG_MSG_TYPE_ENUM_API */
62 typedef unsigned char FLOG_MSG_TYPE_T
;
66 #define FLOG_NONE 0x00
67 #define FLOG_NOTHING FLOG_NONE
70 #define FLOG_CRIT 0x01
71 #define FLOG_CRITICAL FLOG_CRIT
75 #define FLOG_ERROR FLOG_ERR
78 #define FLOG_WARN 0x04
79 #define FLOG_WARNING FLOG_WARN
80 #define FLOG_ALERT FLOG_WARN
83 #define FLOG_NOTE 0x08
84 #define FLOG_NOTIFY FLOG_NOTE
85 #define FLOG_IMP FLOG_NOTE
86 #define FLOG_IMPORTANT FLOG_NOTE
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
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
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__)
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__)
126 //! Macros to allow removal of messages from release builds
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__)
131 #define flog_dprint(p, type, subsystem, text)
132 #define flog_dprintf(p, type, subsystem, ...)
135 #ifdef FLOG_TIMESTAMP
136 typedef int FLOG_TIMESTAMP_T
;
140 #ifdef FLOG_TIMESTAMP
141 FLOG_TIMESTAMP_T time
; //!< timestamp
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
148 FLOG_MSG_TYPE_T type
; //!< type of message
149 char *subsystem
; //!< subsystem which is outputting the msg
150 char *text
; //!< message contents
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
168 void init_flog_msg_t(FLOG_MSG_T
*p
);
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
);
172 FLOG_MSG_T
* create_flog_msg_t(FLOG_MSG_TYPE_T msg_type
,const char *subsystem
,const char *text
);
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
);
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
, ...);
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
, ...);
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);