libosmocore  0.9.6.260-2f98
Osmocom core library
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Modules Pages
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
18 
19 #define LOG_MAX_FILTERS 8
20 
21 #define DEBUG
22 
23 #ifdef DEBUG
24 
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 /* logging levels defined by the library itself */
93 #define DLGLOBAL -1
94 #define DLLAPD -2
95 #define DLINP -3
96 #define DLMUX -4
97 #define DLMI -5
98 #define DLMIB -6
99 #define DLSMS -7
100 #define DLCTRL -8
101 #define DLGTP -9
102 #define DLSTATS -10
103 #define DLGSUP -11
104 #define DLOAP -12
105 #define DLSS7 -13
106 #define DLSCCP -14
107 #define DLSUA -15
108 #define DLM3UA -16
109 #define OSMO_NUM_DLIB 16
112 struct log_category {
113  uint8_t loglevel;
114  uint8_t enabled;
115 };
116 
118 struct log_info_cat {
119  const char *name;
120  const char *color;
121  const char *description;
122  uint8_t loglevel;
123  uint8_t enabled;
124 };
125 
127 struct log_context {
128  void *ctx[LOG_MAX_CTX+1];
129 };
130 
134  LOG_CTX_GB_NSVC,
135  LOG_CTX_GB_BVC,
136  LOG_CTX_BSC_SUBSCR,
137  LOG_CTX_VLR_SUBSCR,
138  _LOG_CTX_COUNT
139 };
140 
145  LOG_FLT_ALL,
146  LOG_FLT_GB_NSVC,
147  LOG_FLT_GB_BVC,
148  LOG_FLT_BSC_SUBSCR,
149  LOG_FLT_VLR_SUBSCR,
150  _LOG_FLT_COUNT
151 };
152 
154 #define LOG_FILTER_ALL (1<<LOG_FLT_ALL)
155 
156 #define GPRS_CTX_NSVC LOG_CTX_GB_NSVC
157 
158 #define GPRS_CTX_BVC LOG_CTX_GB_BVC
159 
162 struct log_target;
163 
165 typedef int log_filter(const struct log_context *ctx,
166  struct log_target *target);
167 
168 struct log_info;
169 struct vty;
170 struct gsmtap_inst;
171 
172 typedef void log_print_filters(struct vty *vty,
173  const struct log_info *info,
174  const struct log_target *tgt);
175 
176 typedef void log_save_filters(struct vty *vty,
177  const struct log_info *info,
178  const struct log_target *tgt);
179 
181 struct log_info {
182  /* \brief filter callback function */
183  log_filter *filter_fn;
184 
186  const struct log_info_cat *cat;
188  unsigned int num_cat;
190  unsigned int num_cat_user;
191 
193  log_save_filters *save_fn;
195  log_print_filters *print_fn;
196 };
197 
206 };
207 
209 struct log_target {
210  struct llist_head entry;
216 
219 
221  uint8_t loglevel;
223  unsigned int use_color:1;
225  unsigned int print_timestamp:1;
227  unsigned int print_filename:1;
229  unsigned int print_category:1;
231  unsigned int print_ext_timestamp:1;
232 
235 
236  union {
237  struct {
238  FILE *out;
239  const char *fname;
240  } tgt_file;
241 
242  struct {
243  int priority;
244  int facility;
245  } tgt_syslog;
246 
247  struct {
248  void *vty;
249  } tgt_vty;
250 
251  struct {
252  void *rb;
253  } tgt_rb;
254 
255  struct {
256  struct gsmtap_inst *gsmtap_inst;
257  const char *ident;
258  const char *hostname;
259  } tgt_gsmtap;
260  };
261 
268  void (*output) (struct log_target *target, unsigned int level,
269  const char *string);
270 
283  void (*raw_output)(struct log_target *target, int subsys,
284  unsigned int level, const char *file, int line,
285  int cont, const char *format, va_list ap);
286 };
287 
288 /* use the above macros */
289 void logp2(int subsys, unsigned int level, const char *file,
290  int line, int cont, const char *format, ...)
291  __attribute__ ((format (printf, 6, 7)));
292 int log_init(const struct log_info *inf, void *talloc_ctx);
293 void log_fini(void);
294 int log_check_level(int subsys, unsigned int level);
295 
296 /* context management */
297 void log_reset_context(void);
298 int log_set_context(uint8_t ctx, void *value);
299 
300 /* filter on the targets */
301 void log_set_all_filter(struct log_target *target, int);
302 
303 void log_set_use_color(struct log_target *target, int);
304 void log_set_print_extended_timestamp(struct log_target *target, int);
305 void log_set_print_timestamp(struct log_target *target, int);
306 void log_set_print_filename(struct log_target *target, int);
307 void log_set_print_category(struct log_target *target, int);
308 void log_set_log_level(struct log_target *target, int log_level);
309 void log_parse_category_mask(struct log_target *target, const char* mask);
310 const char* log_category_name(int subsys);
311 int log_parse_level(const char *lvl);
312 const char *log_level_str(unsigned int lvl);
313 int log_parse_category(const char *category);
314 void log_set_category_filter(struct log_target *target, int category,
315  int enable, int level);
316 
317 /* management of the targets */
318 struct log_target *log_target_create(void);
319 void log_target_destroy(struct log_target *target);
321 struct log_target *log_target_create_file(const char *fname);
322 struct log_target *log_target_create_syslog(const char *ident, int option,
323  int facility);
324 struct log_target *log_target_create_gsmtap(const char *host, uint16_t port,
325  const char *ident,
326  bool ofd_wq_mode,
327  bool add_sink);
328 int log_target_file_reopen(struct log_target *tgt);
329 int log_targets_reopen(void);
330 
331 void log_add_target(struct log_target *target);
332 void log_del_target(struct log_target *target);
333 
334 /* Generate command string for VTY use */
335 const char *log_vty_command_string() OSMO_DEPRECATED("For internal use inside libosmocore only.");
336 const char *log_vty_command_description() OSMO_DEPRECATED("For internal use inside libosmocore only.");
337 
338 struct log_target *log_target_find(int type, const char *fname);
339 extern struct llist_head osmo_log_target_list;
340 
Information regarding one logging category.
Definition: logging.h:118
int filter_map
Internal data for filtering.
Definition: logging.h:213
void log_set_all_filter(struct log_target *target, int)
Enable the LOG_FLT_ALL log filter.
Definition: logging.c:544
int log_parse_level(const char *lvl)
Parse a human-readable log level into a numeric value.
Definition: logging.c:196
#define OSMO_DEPRECATED(text)
Set the deprecated attribute with a message.
Definition: defs.h:41
int log_parse_category(const char *category)
parse a human-readable log category into numeric form
Definition: logging.c:214
const char * name
Definition: logging.h:119
VTY logging.
Definition: logging.h:200
log_save_filters * save_fn
filter saving function
Definition: logging.h:193
void log_set_print_timestamp(struct log_target *target, int)
Enable or disable printing of timestamps while logging.
Definition: logging.c:565
enum log_target_type type
the type of this log taget
Definition: logging.h:234
const char * log_vty_command_description() OSMO_DEPRECATED("For internal use inside libosmocore only.")
Generates the logging command description for VTY.
Definition: logging.c:895
Definition: logging.h:112
log_ctx_index
Indexes to indicate the object currently acted upon. Array indexes for the global log_context array...
Definition: logging.h:133
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
uint8_t enabled
Definition: logging.h:123
log_filter_index
Indexes to indicate objects that should be logged. Array indexes to log_target->filter_data and bit i...
Definition: logging.h:144
syslog based logging
Definition: logging.h:201
const char * log_level_str(unsigned int lvl)
convert a numeric log level into human-readable string
Definition: logging.c:205
General definitions that are meant to be included from header files.
log_target_type
Type of logging target.
Definition: logging.h:199
void log_target_destroy(struct log_target *target)
Unregister, close and delete a log target.
Definition: logging.c:751
void log_set_use_color(struct log_target *target, int)
Enable or disable the use of colored output.
Definition: logging.c:556
#define DEBUGP(ss, fmt, args...)
Log a debug message through the Osmocom logging framework.
Definition: logging.h:29
#define LOG_MAX_FILTERS
Maximum number of logging filters.
Definition: logging.h:19
one gsmtap instance
Definition: gsmtap_util.h:23
uint8_t enabled
Definition: logging.h:114
unsigned int print_category
should log messages be prefixed with a category name?
Definition: logging.h:229
void log_add_target(struct log_target *target)
Register a new log target with the logging core.
Definition: logging.c:496
int log_filter(const struct log_context *ctx, struct log_target *target)
Log filter function.
Definition: logging.h:165
osmo_strrb-backed logging
Definition: logging.h:204
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:283
text file logging
Definition: logging.h:202
void log_del_target(struct log_target *target)
Unregister a log target from the logging core.
Definition: logging.c:504
struct log_target * log_target_create_file(const char *fname)
Create a new file-based log target.
Definition: logging.c:707
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:467
const char * log_vty_command_string() OSMO_DEPRECATED("For internal use inside libosmocore only.")
Generates the logging command string for VTY.
Definition: logging.c:815
stderr logging
Definition: logging.h:203
Simple doubly linked list implementation.
unsigned int print_filename
should log messages be prefixed with a filename?
Definition: logging.h:227
(double) linked list header structure
Definition: linuxlist.h:47
const char * color
Definition: logging.h:120
void log_set_print_filename(struct log_target *target, int)
Enable or disable printing of the filename while logging.
Definition: logging.c:587
int log_set_context(uint8_t ctx, void *value)
Set the logging context.
Definition: logging.c:526
void log_set_print_category(struct log_target *target, int)
Enable or disable printing of the category name.
Definition: logging.c:598
struct log_category * categories
logging categories
Definition: logging.h:218
uint8_t loglevel
global log level
Definition: logging.h:221
struct log_target * log_target_create_stderr(void)
Create the STDERR log target.
Definition: logging.c:683
void log_parse_category_mask(struct log_target *target, const char *mask)
parse the log category mask
Definition: logging.c:237
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:618
GSMTAP network logging.
Definition: logging.h:205
int log_check_level(int subsys, unsigned int level)
Check whether a log entry will be generated.
Definition: logging.c:1019
uint8_t loglevel
Definition: logging.h:113
void int log_init(const struct log_info *inf, void *talloc_ctx)
Initialize the Osmocom logging core.
Definition: logging.c:959
Logging configuration, passed to log_init.
Definition: logging.h:181
unsigned int print_timestamp
should log messages be prefixed with a timestamp?
Definition: logging.h:225
log_print_filters * print_fn
filter saving function
Definition: logging.h:195
unsigned int num_cat_user
total number of user categories (not library)
Definition: logging.h:190
Log context information, passed to filter.
Definition: logging.h:127
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:268
struct log_target * log_target_create(void)
Create a new log target skeleton.
Definition: logging.c:643
const struct log_info_cat * cat
per-category information
Definition: logging.h:186
unsigned int use_color
should color be used when printing log messages?
Definition: logging.h:223
struct log_target * log_target_find(int type, const char *fname)
Find a registered log target.
Definition: logging.c:733
unsigned int print_ext_timestamp
should log messages be prefixed with an extended timestamp?
Definition: logging.h:231
int log_target_file_reopen(struct log_target *tgt)
close and re-open a log file (for log file rotation)
Definition: logging.c:777
structure representing a logging target
Definition: logging.h:209
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:436
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:607
void log_reset_context(void)
Reset (clear) the logging context.
Definition: logging.c:510
#define LOG_MAX_CTX
Maximum number of logging contexts.
Definition: logging.h:17
uint8_t loglevel
Definition: logging.h:122
unsigned int num_cat
total number of categories
Definition: logging.h:188
void * filter_data[LOG_MAX_FILTERS+1]
Internal data for filtering.
Definition: logging.h:215
int log_targets_reopen(void)
close and re-open all log files (for log file rotation)
Definition: logging.c:792
void log_set_print_extended_timestamp(struct log_target *target, int)
Enable or disable printing of extended timestamps while logging.
Definition: logging.c:578
const char * description
Definition: logging.h:121
struct llist_head entry
linked list
Definition: logging.h:210