libosmocore  0.9.6-16.20170220git32ee5af8.fc35
Osmocom core library
logging.h
Go to the documentation of this file.
1 #pragma once
2 
9 #include <stdio.h>
10 #include <stdint.h>
11 #include <stdarg.h>
12 #include <stdbool.h>
13 #include <osmocom/core/defs.h>
14 #include <osmocom/core/linuxlist.h>
15 
17 #define LOG_MAX_CTX 8
19 #define LOG_MAX_FILTERS 8
20 
21 #define DEBUG
22 
23 #ifdef DEBUG
29 #define DEBUGP(ss, fmt, args...) LOGP(ss, LOGL_DEBUG, fmt, ##args)
30 #define DEBUGPC(ss, fmt, args...) LOGPC(ss, LOGL_DEBUG, fmt, ##args)
31 #else
32 #define DEBUGP(xss, fmt, args...)
33 #define DEBUGPC(ss, fmt, args...)
34 #endif
35 
36 
37 void osmo_vlogp(int subsys, int level, const char *file, int line,
38  int cont, const char *format, va_list ap);
39 
40 void logp(int subsys, const char *file, int line, int cont, const char *format, ...) OSMO_DEPRECATED("Use DEBUGP* macros instead");
41 
48 #define LOGP(ss, level, fmt, args...) \
49  LOGPSRC(ss, level, NULL, 0, fmt, ## args)
50 
57 #define LOGPC(ss, level, fmt, args...) \
58  do { \
59  if (log_check_level(ss, level)) \
60  logp2(ss, level, __BASE_FILE__, __LINE__, 1, fmt, ##args); \
61  } while(0)
62 
75 #define LOGPSRC(ss, level, caller_file, caller_line, fmt, args...) \
76  do { \
77  if (log_check_level(ss, level)) {\
78  if (caller_file) \
79  logp2(ss, level, caller_file, caller_line, 0, fmt, ##args); \
80  else \
81  logp2(ss, level, __BASE_FILE__, __LINE__, 0, fmt, ##args); \
82  }\
83  } while(0)
84 
86 #define LOGL_DEBUG 1
87 #define LOGL_INFO 3
88 #define LOGL_NOTICE 5
89 #define LOGL_ERROR 7
90 #define LOGL_FATAL 8
92 #define LOG_FILTER_ALL 0x0001
93 
94 /* logging levels defined by the library itself */
95 #define DLGLOBAL -1
96 #define DLLAPD -2
97 #define DLINP -3
98 #define DLMUX -4
99 #define DLMI -5
100 #define DLMIB -6
101 #define DLSMS -7
102 #define DLCTRL -8
103 #define DLGTP -9
104 #define DLSTATS -10
105 #define DLGSUP -11
106 #define DLOAP -12
107 #define OSMO_NUM_DLIB 12
110 struct log_category {
111  uint8_t loglevel;
112  uint8_t enabled;
113 };
114 
116 struct log_info_cat {
117  const char *name;
118  const char *color;
119  const char *description;
120  uint8_t loglevel;
121  uint8_t enabled;
122 };
123 
125 struct log_context {
126  void *ctx[LOG_MAX_CTX+1];
127 };
128 
129 struct log_target;
130 
132 typedef int log_filter(const struct log_context *ctx,
133  struct log_target *target);
134 
135 struct log_info;
136 struct vty;
137 struct gsmtap_inst;
138 
139 typedef void log_print_filters(struct vty *vty,
140  const struct log_info *info,
141  const struct log_target *tgt);
142 
143 typedef void log_save_filters(struct vty *vty,
144  const struct log_info *info,
145  const struct log_target *tgt);
146 
148 struct log_info {
149  /* \brief filter callback function */
150  log_filter *filter_fn;
151 
153  const struct log_info_cat *cat;
155  unsigned int num_cat;
157  unsigned int num_cat_user;
158 
160  log_save_filters *save_fn;
162  log_print_filters *print_fn;
163 };
164 
173 };
174 
176 struct log_target {
177  struct llist_head entry;
183 
186 
188  uint8_t loglevel;
190  unsigned int use_color:1;
192  unsigned int print_timestamp:1;
194  unsigned int print_filename:1;
196  unsigned int print_category:1;
198  unsigned int print_ext_timestamp:1;
199 
201  enum log_target_type type;
202 
203  union {
204  struct {
205  FILE *out;
206  const char *fname;
207  } tgt_file;
208 
209  struct {
210  int priority;
211  int facility;
212  } tgt_syslog;
213 
214  struct {
215  void *vty;
216  } tgt_vty;
217 
218  struct {
219  void *rb;
220  } tgt_rb;
221 
222  struct {
223  struct gsmtap_inst *gsmtap_inst;
224  const char *ident;
225  const char *hostname;
226  } tgt_gsmtap;
227  };
228 
235  void (*output) (struct log_target *target, unsigned int level,
236  const char *string);
237 
250  void (*raw_output)(struct log_target *target, int subsys,
251  unsigned int level, const char *file, int line,
252  int cont, const char *format, va_list ap);
253 };
254 
255 /* use the above macros */
256 void logp2(int subsys, unsigned int level, const char *file,
257  int line, int cont, const char *format, ...)
258  __attribute__ ((format (printf, 6, 7)));
259 int log_init(const struct log_info *inf, void *talloc_ctx);
260 void log_fini(void);
261 int log_check_level(int subsys, unsigned int level);
262 
263 /* context management */
264 void log_reset_context(void);
265 int log_set_context(uint8_t ctx, void *value);
266 
267 /* filter on the targets */
268 void log_set_all_filter(struct log_target *target, int);
269 
270 void log_set_use_color(struct log_target *target, int);
271 void log_set_print_extended_timestamp(struct log_target *target, int);
272 void log_set_print_timestamp(struct log_target *target, int);
273 void log_set_print_filename(struct log_target *target, int);
274 void log_set_print_category(struct log_target *target, int);
275 void log_set_log_level(struct log_target *target, int log_level);
276 void log_parse_category_mask(struct log_target *target, const char* mask);
277 const char* log_category_name(int subsys);
278 int log_parse_level(const char *lvl);
279 const char *log_level_str(unsigned int lvl);
280 int log_parse_category(const char *category);
281 void log_set_category_filter(struct log_target *target, int category,
282  int enable, int level);
283 
284 /* management of the targets */
285 struct log_target *log_target_create(void);
286 void log_target_destroy(struct log_target *target);
288 struct log_target *log_target_create_file(const char *fname);
289 struct log_target *log_target_create_syslog(const char *ident, int option,
290  int facility);
291 struct log_target *log_target_create_gsmtap(const char *host, uint16_t port,
292  const char *ident,
293  bool ofd_wq_mode,
294  bool add_sink);
295 int log_target_file_reopen(struct log_target *tgt);
296 int log_targets_reopen(void);
297 
298 void log_add_target(struct log_target *target);
299 void log_del_target(struct log_target *target);
300 
301 /* Generate command string for VTY use */
302 const char *log_vty_command_string(const struct log_info *info);
303 const char *log_vty_command_description(const struct log_info *info);
304 
305 struct log_target *log_target_find(int type, const char *fname);
306 extern struct llist_head osmo_log_target_list;
307 
General definitions that are meant to be included from header files.
struct log_target * log_target_find(int type, const char *fname)
Find a registered log target.
Definition: logging.c:690
void log_set_all_filter(struct log_target *target, int)
Enable the LOG_FILTER_ALL log filter.
Definition: logging.c:503
void log_set_print_timestamp(struct log_target *target, int)
Enable or disable printing of timestamps while logging.
Definition: logging.c:524
log_target_type
Type of logging target.
Definition: logging.h:166
int log_target_file_reopen(struct log_target *tgt)
close and re-open a log file (for log file rotation)
Definition: logging.c:734
void log_set_print_category(struct log_target *target, int)
Enable or disable printing of the category name.
Definition: logging.c:557
int log_parse_level(const char *lvl)
Parse a human-readable log level into a numeric value.
Definition: logging.c:160
const char * log_vty_command_description(const struct log_info *info)
Generates the logging command description for VTY.
Definition: logging.c:850
struct log_target * log_target_create(void)
Create a new log target skeleton.
Definition: logging.c:602
struct log_target * log_target_create_gsmtap(const char *host, uint16_t port, const char *ident, bool ofd_wq_mode, bool add_sink)
Create a new logging target for GSMTAP logging.
Definition: logging_gsmtap.c:103
void osmo_vlogp(int subsys, int level, const char *file, int line, int cont, const char *format, va_list ap)
vararg version of logging function
Definition: logging.c:395
void log_set_log_level(struct log_target *target, int log_level)
Set the global log level for a given log target.
Definition: logging.c:566
#define LOG_MAX_FILTERS
Maximum number of logging filters.
Definition: logging.h:19
struct log_target * log_target_create_stderr(void)
Create the STDERR log target.
Definition: logging.c:640
void log_target_destroy(struct log_target *target)
Unregister, close and delete a log target.
Definition: logging.c:708
int log_check_level(int subsys, unsigned int level)
Check whether a log entry will be generated.
Definition: logging.c:972
void log_set_use_color(struct log_target *target, int)
Enable or disable the use of colored output.
Definition: logging.c:515
const char * log_vty_command_string(const struct log_info *info)
Generates the logging command string for VTY.
Definition: logging.c:772
void log_set_print_extended_timestamp(struct log_target *target, int)
Enable or disable printing of extended timestamps while logging.
Definition: logging.c:537
void logp(int subsys, const char *file, int line, int cont, const char *format,...) OSMO_DEPRECATED("Use DEBUGP* macros instead")
logging function used by DEBUGP() macro
Definition: logging.c:426
int log_filter(const struct log_context *ctx, struct log_target *target)
Log filter function.
Definition: logging.h:132
const char * log_level_str(unsigned int lvl)
convert a numeric log level into human-readable string
Definition: logging.c:169
void log_set_category_filter(struct log_target *target, int category, int enable, int level)
Set a category filter on a given log target.
Definition: logging.c:577
void int log_init(const struct log_info *inf, void *talloc_ctx)
Initialize the Osmocom logging core.
Definition: logging.c:912
#define DEBUGP(ss, fmt, args...)
Log a debug message through the Osmocom logging framework.
Definition: logging.h:29
#define LOG_MAX_CTX
Maximum number of logging contexts.
Definition: logging.h:17
struct log_target * log_target_create_file(const char *fname)
Create a new file-based log target.
Definition: logging.c:664
int log_parse_category(const char *category)
parse a human-readable log category into numeric form
Definition: logging.c:178
void log_set_print_filename(struct log_target *target, int)
Enable or disable printing of the filename while logging.
Definition: logging.c:546
void log_parse_category_mask(struct log_target *target, const char *mask)
parse the log category mask
Definition: logging.c:199
int log_targets_reopen(void)
close and re-open all log files (for log file rotation)
Definition: logging.c:749
int log_set_context(uint8_t ctx, void *value)
Set the logging context.
Definition: logging.c:485
struct log_target * log_target_create_syslog(const char *ident, int option, int facility)
Create a new logging target for syslog logging.
Definition: logging_syslog.c:72
void log_add_target(struct log_target *target)
Register a new log target with the logging core.
Definition: logging.c:455
void log_del_target(struct log_target *target)
Unregister a log target from the logging core.
Definition: logging.c:463
void log_reset_context(void)
Reset (clear) the logging context.
Definition: logging.c:469
@ LOG_TGT_TYPE_VTY
VTY logging.
Definition: logging.h:167
@ LOG_TGT_TYPE_FILE
text file logging
Definition: logging.h:169
@ LOG_TGT_TYPE_SYSLOG
syslog based logging
Definition: logging.h:168
@ LOG_TGT_TYPE_STRRB
osmo_strrb-backed logging
Definition: logging.h:171
@ LOG_TGT_TYPE_STDERR
stderr logging
Definition: logging.h:170
@ LOG_TGT_TYPE_GSMTAP
GSMTAP network logging.
Definition: logging.h:172
#define OSMO_DEPRECATED(text)
Set the deprecated attribute with a message.
Definition: defs.h:41
Simple doubly linked list implementation.
one gsmtap instance
Definition: gsmtap_util.h:23
(double) linked list header structure
Definition: linuxlist.h:47
Definition: logging.h:110
uint8_t enabled
Definition: logging.h:112
uint8_t loglevel
Definition: logging.h:111
Log context information, passed to filter.
Definition: logging.h:125
Information regarding one logging category.
Definition: logging.h:116
uint8_t enabled
Definition: logging.h:121
const char * name
Definition: logging.h:117
const char * description
Definition: logging.h:119
const char * color
Definition: logging.h:118
uint8_t loglevel
Definition: logging.h:120
Logging configuration, passed to log_init.
Definition: logging.h:148
unsigned int num_cat
total number of categories
Definition: logging.h:155
log_print_filters * print_fn
filter saving function
Definition: logging.h:162
log_save_filters * save_fn
filter saving function
Definition: logging.h:160
const struct log_info_cat * cat
per-category information
Definition: logging.h:153
unsigned int num_cat_user
total number of user categories (not library)
Definition: logging.h:157
structure representing a logging target
Definition: logging.h:176
unsigned int print_timestamp
should log messages be prefixed with a timestamp?
Definition: logging.h:192
struct log_category * categories
logging categories
Definition: logging.h:185
struct llist_head entry
linked list
Definition: logging.h:177
int filter_map
Internal data for filtering.
Definition: logging.h:180
void * filter_data[LOG_MAX_FILTERS+1]
Internal data for filtering.
Definition: logging.h:182
void(* raw_output)(struct log_target *target, int subsys, unsigned int level, const char *file, int line, int cont, const char *format, va_list ap)
alternative call-back function to which the logging framework passes the unfortmatted input arguments...
Definition: logging.h:250
unsigned int print_filename
should log messages be prefixed with a filename?
Definition: logging.h:194
unsigned int use_color
should color be used when printing log messages?
Definition: logging.h:190
unsigned int print_ext_timestamp
should log messages be prefixed with an extended timestamp?
Definition: logging.h:198
unsigned int print_category
should log messages be prefixed with a category name?
Definition: logging.h:196
enum log_target_type type
the type of this log taget
Definition: logging.h:201
void(* output)(struct log_target *target, unsigned int level, const char *string)
call-back function to be called when the logging framework wants to log a fully formatted string
Definition: logging.h:235
uint8_t loglevel
global log level
Definition: logging.h:188