libosmocore  1.4.0.171-b3b83
Osmocom core library
logging.h
Go to the documentation of this file.
1 #pragma once
2 
7 #include <stdio.h>
8 #include <stdint.h>
9 #include <stdarg.h>
10 #include <stdbool.h>
11 #include <osmocom/core/defs.h>
12 #include <osmocom/core/linuxlist.h>
13 
15 #define LOG_MAX_CTX 8
16 
17 #define LOG_MAX_FILTERS 8
18 
19 #ifndef DEBUG
20 #define DEBUG
21 #endif
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, __FILE__, __LINE__, 1, fmt, ##args); \
61  } while(0)
62 
75 #define LOGPSRC(ss, level, caller_file, caller_line, fmt, args...) \
76  LOGPSRCC(ss, level, caller_file, caller_line, 0, fmt, ##args)
77 
91 #define LOGPSRCC(ss, level, caller_file, caller_line, cont, fmt, args...) \
92  do { \
93  if (log_check_level(ss, level)) {\
94  if (caller_file) \
95  logp2(ss, level, caller_file, caller_line, cont, fmt, ##args); \
96  else \
97  logp2(ss, level, __FILE__, __LINE__, cont, fmt, ##args); \
98  }\
99  } while(0)
100 
102 #define LOGL_DEBUG 1
103 #define LOGL_INFO 3
104 #define LOGL_NOTICE 5
105 #define LOGL_ERROR 7
106 #define LOGL_FATAL 8
108 /* logging subsystems defined by the library itself */
109 #define DLGLOBAL -1
110 #define DLLAPD -2
111 #define DLINP -3
112 #define DLMUX -4
113 #define DLMI -5
114 #define DLMIB -6
115 #define DLSMS -7
116 #define DLCTRL -8
117 #define DLGTP -9
118 #define DLSTATS -10
119 #define DLGSUP -11
120 #define DLOAP -12
121 #define DLSS7 -13
122 #define DLSCCP -14
123 #define DLSUA -15
124 #define DLM3UA -16
125 #define DLMGCP -17
126 #define DLJIBUF -18
127 #define DLRSPRO -19
128 #define DLNS -20
129 #define OSMO_NUM_DLIB 20
131 /* Colors that can be used in log_info_cat.color */
132 #define OSMO_LOGCOLOR_NORMAL NULL
133 #define OSMO_LOGCOLOR_RED "\033[1;31m"
134 #define OSMO_LOGCOLOR_GREEN "\033[1;32m"
135 #define OSMO_LOGCOLOR_YELLOW "\033[1;33m"
136 #define OSMO_LOGCOLOR_BLUE "\033[1;34m"
137 #define OSMO_LOGCOLOR_PURPLE "\033[1;35m"
138 #define OSMO_LOGCOLOR_CYAN "\033[1;36m"
139 #define OSMO_LOGCOLOR_DARKRED "\033[31m"
140 #define OSMO_LOGCOLOR_DARKGREEN "\033[32m"
141 #define OSMO_LOGCOLOR_DARKYELLOW "\033[33m"
142 #define OSMO_LOGCOLOR_DARKBLUE "\033[34m"
143 #define OSMO_LOGCOLOR_DARKPURPLE "\033[35m"
144 #define OSMO_LOGCOLOR_DARKCYAN "\033[36m"
145 #define OSMO_LOGCOLOR_DARKGREY "\033[1;30m"
146 #define OSMO_LOGCOLOR_GREY "\033[37m"
147 #define OSMO_LOGCOLOR_BRIGHTWHITE "\033[1;37m"
148 #define OSMO_LOGCOLOR_END "\033[0;m"
149 
151 struct log_category {
152  uint8_t loglevel;
153  uint8_t enabled;
154 };
155 
157 struct log_info_cat {
158  const char *name;
159  const char *color;
160  const char *description;
161  uint8_t loglevel;
162  uint8_t enabled;
163 };
164 
166 struct log_context {
167  void *ctx[LOG_MAX_CTX+1];
168 };
169 
179 };
180 
192 };
193 
195 #define LOG_FILTER_ALL (1<<LOG_FLT_ALL)
196 
197 #define GPRS_CTX_NSVC LOG_CTX_GB_NSVC
198 
199 #define GPRS_CTX_BVC LOG_CTX_GB_BVC
200 
203 struct log_target;
204 
206 typedef int log_filter(const struct log_context *ctx,
207  struct log_target *target);
208 
209 struct log_info;
210 struct vty;
211 struct gsmtap_inst;
212 
213 typedef void log_print_filters(struct vty *vty,
214  const struct log_info *info,
215  const struct log_target *tgt);
216 
217 typedef void log_save_filters(struct vty *vty,
218  const struct log_info *info,
219  const struct log_target *tgt);
220 
222 struct log_info {
223  /* filter callback function */
225 
227  const struct log_info_cat *cat;
229  unsigned int num_cat;
231  unsigned int num_cat_user;
232 
237 };
238 
248 };
249 
255 };
256 
261 };
262 
264 struct log_target {
265  struct llist_head entry;
270  void *filter_data[LOG_MAX_FILTERS+1];
271 
274 
276  uint8_t loglevel;
278  unsigned int use_color:1;
280  unsigned int print_timestamp:1;
282  unsigned int print_filename:1;
284  unsigned int print_category:1;
286  unsigned int print_ext_timestamp:1;
287 
290 
291  union {
292  struct {
293  FILE *out;
294  const char *fname;
295  } tgt_file;
296 
297  struct {
298  int priority;
299  int facility;
300  } tgt_syslog;
301 
302  struct {
303  void *vty;
304  } tgt_vty;
305 
306  struct {
307  void *rb;
308  } tgt_rb;
309 
310  struct {
312  const char *ident;
313  const char *hostname;
314  } tgt_gsmtap;
315 
316  struct {
317  bool raw;
318  } sd_journal;
319  };
320 
327  void (*output) (struct log_target *target, unsigned int level,
328  const char *string);
329 
342  void (*raw_output)(struct log_target *target, int subsys,
343  unsigned int level, const char *file, int line,
344  int cont, const char *format, va_list ap);
345 
346  /* Should the log level be printed? */
348  /* Should we print the subsys in hex like '<000b>'? */
350  /* Should we print the source file and line, and in which way? */
352  /* Where on a log line to put the source file info. */
354 };
355 
356 /* use the above macros */
357 void logp2(int subsys, unsigned int level, const char *file,
358  int line, int cont, const char *format, ...)
359  __attribute__ ((format (printf, 6, 7)));
360 int log_init(const struct log_info *inf, void *talloc_ctx);
361 void log_fini(void);
362 int log_check_level(int subsys, unsigned int level);
363 
364 /* context management */
365 void log_reset_context(void);
366 int log_set_context(uint8_t ctx, void *value);
367 
368 /* filter on the targets */
369 void log_set_all_filter(struct log_target *target, int);
370 
371 void log_set_use_color(struct log_target *target, int);
372 void log_set_print_extended_timestamp(struct log_target *target, int);
373 void log_set_print_timestamp(struct log_target *target, int);
374 void log_set_print_filename(struct log_target *target, int);
375 void log_set_print_filename2(struct log_target *target, enum log_filename_type lft);
376 void log_set_print_filename_pos(struct log_target *target, enum log_filename_pos pos);
377 void log_set_print_category(struct log_target *target, int);
378 void log_set_print_category_hex(struct log_target *target, int);
379 void log_set_print_level(struct log_target *target, int);
380 void log_set_log_level(struct log_target *target, int log_level);
381 void log_parse_category_mask(struct log_target *target, const char* mask);
382 const char* log_category_name(int subsys);
384 const char *log_level_str(unsigned int lvl) OSMO_DEPRECATED_OUTSIDE_LIBOSMOCORE;
385 int log_parse_category(const char *category);
386 void log_set_category_filter(struct log_target *target, int category,
387  int enable, int level);
388 
389 /* management of the targets */
390 struct log_target *log_target_create(void);
391 void log_target_destroy(struct log_target *target);
393 struct log_target *log_target_create_file(const char *fname);
394 struct log_target *log_target_create_syslog(const char *ident, int option,
395  int facility);
396 struct log_target *log_target_create_gsmtap(const char *host, uint16_t port,
397  const char *ident,
398  bool ofd_wq_mode,
399  bool add_sink);
401 void log_target_systemd_set_raw(struct log_target *target, bool raw);
402 int log_target_file_reopen(struct log_target *tgt);
403 int log_targets_reopen(void);
404 
405 void log_add_target(struct log_target *target);
406 void log_del_target(struct log_target *target);
407 
408 struct log_target *log_target_find(int type, const char *fname);
409 
410 void log_enable_multithread(void);
411 
412 void log_tgt_mutex_lock_impl(void);
413 void log_tgt_mutex_unlock_impl(void);
414 #define LOG_MTX_DEBUG 0
415 #if LOG_MTX_DEBUG
416  #include <pthread.h>
417  #define log_tgt_mutex_lock() do { fprintf(stderr, "[%lu] %s:%d [%s] lock\n", pthread_self(), __FILE__, __LINE__, __func__); log_tgt_mutex_lock_impl(); } while (0)
418  #define log_tgt_mutex_unlock() do { fprintf(stderr, "[%lu] %s:%d [%s] unlock\n", pthread_self(), __FILE__, __LINE__, __func__); log_tgt_mutex_unlock_impl(); } while (0)
419 #else
420  #define log_tgt_mutex_lock() log_tgt_mutex_lock_impl()
421  #define log_tgt_mutex_unlock() log_tgt_mutex_unlock_impl()
422 #endif
423 
void log_set_print_category_hex(struct log_target *target, int)
Enable or disable printing of the category number in hex (&#39;<000b>&#39;).
Definition: logging.c:795
Definition: logging.h:259
Information regarding one logging category.
Definition: logging.h:157
int filter_map
Internal data for filtering.
Definition: logging.h:268
int log_parse_level(const char *lvl) OSMO_DEPRECATED_OUTSIDE_LIBOSMOCORE
Parse a human-readable log level into a numeric value.
Definition: logging.c:262
#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:280
const char * name
name of category
Definition: logging.h:158
VTY logging.
Definition: logging.h:241
struct osmo_gsm48_classmark __attribute__
Definition: conv_acc_generic.c:140
log_save_filters * save_fn
filter saving function
Definition: logging.h:234
Definition: logging.h:191
log_filename_type
Whether/how to log the source filename (and line number).
Definition: logging.h:251
bool print_level
Definition: logging.h:347
void * rb
Definition: logging.h:307
char subsys[16]
logging sub-system
Definition: gsmtap.h:129
void log_set_print_timestamp(struct log_target *target, int)
Enable or disable printing of timestamps while logging.
Definition: logging.c:725
const char * fname
Definition: logging.h:294
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:627
void log_set_print_filename(struct log_target *target, int)
Use log_set_print_filename2() instead.
Definition: logging.c:752
Configuration of single log category / sub-system.
Definition: logging.h:151
void log_set_print_extended_timestamp(struct log_target *target, int)
Enable or disable printing of extended timestamps while logging.
Definition: logging.c:738
log_ctx_index
Indexes to indicate the object currently acted upon.
Definition: logging.h:172
Definition: logging.h:174
#define OSMO_DEPRECATED_OUTSIDE_LIBOSMOCORE
Definition: defs.h:48
Definition: logging.h:253
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:127
Definition: logging.h:187
uint8_t enabled
is this category enabled or not
Definition: logging.h:162
void log_set_print_filename2(struct log_target *target, enum log_filename_type lft)
Enable or disable printing of the filename while logging.
Definition: logging.c:764
log_filter_index
Indexes to indicate objects that should be logged.
Definition: logging.h:184
Definition: logging.h:185
int log_set_context(uint8_t ctx, void *value)
Set the logging context.
Definition: logging.c:686
syslog based logging
Definition: logging.h:242
const char * ident
Definition: logging.h:312
Definition: logging.h:177
Definition: logging.h:188
General definitions that are meant to be included from header files.
bool raw
Definition: logging.h:317
log_target_type
Type of logging target.
Definition: logging.h:240
void log_target_destroy(struct log_target *target)
Unregister, close and delete a log target.
Definition: logging.c:970
#define DEBUGP(ss, fmt, args...)
Log a debug message through the Osmocom logging framework.
Definition: logging.h:29
void * vty
Definition: logging.h:303
#define LOG_MAX_FILTERS
Maximum number of logging filters.
Definition: logging.h:17
write Write running configuration to or terminal n Write configuration to the file(same as write file)\n") ALIAS(config_write_file
struct log_target * log_target_find(int type, const char *fname)
Find a registered log target.
Definition: logging.c:945
one gsmtap instance
Definition: gsmtap_util.h:27
uint8_t enabled
is logging enabled?
Definition: logging.h:153
void log_target_systemd_set_raw(struct log_target *target, bool raw)
Change meta information handling of an existing logging target.
Definition: logging_systemd.c:109
enum log_filename_type print_filename2
Definition: logging.h:351
int facility
Definition: logging.h:299
uint8_t level
logging level
Definition: gsmtap.h:126
void log_add_target(struct log_target *target)
Register a new log target with the logging core.
Definition: logging.c:656
int log_filter(const struct log_context *ctx, struct log_target *target)
Log filter function.
Definition: logging.h:206
osmo_strrb-backed logging
Definition: logging.h:245
Definition: logging.h:189
int priority
Definition: logging.h:298
void log_enable_multithread(void)
Enable multithread support (mutex) in libosmocore logging system.
Definition: logging.c:87
text file logging
Definition: logging.h:243
Definition: logging.h:252
bool print_category_hex
Definition: logging.h:349
void log_del_target(struct log_target *target)
Unregister a log target from the logging core.
Definition: logging.c:664
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:342
FILE * out
Definition: logging.h:293
enum log_filename_pos print_filename_pos
Definition: logging.h:353
void log_set_all_filter(struct log_target *target, int)
Enable the LOG_FLT_ALL log filter.
Definition: logging.c:704
stderr logging
Definition: logging.h:244
void log_save_filters(struct vty *vty, const struct log_info *info, const struct log_target *tgt)
Definition: logging.h:217
struct gsmtap_inst * gsmtap_inst
Definition: logging.h:311
Simple doubly linked list implementation.
void log_print_filters(struct vty *vty, const struct log_info *info, const struct log_target *tgt)
Definition: logging.h:213
(double) linked list header structure
Definition: linuxlist.h:45
void log_set_use_color(struct log_target *target, int)
Enable or disable the use of colored output.
Definition: logging.c:716
const char * color
color string for cateyory
Definition: logging.h:159
struct log_target * log_target_create_systemd(bool raw)
Create a new logging target for systemd journal logging.
Definition: logging_systemd.c:90
Definition: logging.h:186
void log_set_print_category(struct log_target *target, int)
Enable or disable printing of the category name.
Definition: logging.c:786
log_filter * filter_fn
Definition: logging.h:224
Definition: logging.h:175
int log_target_file_reopen(struct log_target *tgt)
close and re-open a log file (for log file rotation)
Definition: logging.c:1001
Definition: logging.h:176
void int log_init(const struct log_info *inf, void *talloc_ctx)
Initialize the Osmocom logging core.
Definition: logging.c:1046
struct log_target * log_target_create_stderr(void)
Create the STDERR log target.
Definition: logging.c:892
const char * log_level_str(unsigned int lvl) OSMO_DEPRECATED_OUTSIDE_LIBOSMOCORE
convert a numeric log level into human-readable string
Definition: logging.c:271
struct log_category * categories
logging categories
Definition: logging.h:273
uint8_t loglevel
global log level
Definition: logging.h:276
void log_tgt_mutex_lock_impl(void)
Acquire the osmo_log_tgt_mutex.
Definition: logging.c:97
Definition: logging.h:254
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:826
void log_tgt_mutex_unlock_impl(void)
Release the osmo_log_tgt_mutex.
Definition: logging.c:113
GSMTAP network logging.
Definition: logging.h:246
void log_fini(void)
Definition: logging.c:1105
int log_check_level(int subsys, unsigned int level)
Check whether a log entry will be generated.
Definition: logging.c:1124
uint8_t loglevel
configured log-level
Definition: logging.h:152
void log_set_print_filename_pos(struct log_target *target, enum log_filename_pos pos)
Set the position where on a log line the source file info should be logged.
Definition: logging.c:775
Definition: logging.h:190
Definition: logging.h:178
struct log_target * log_target_create_file(const char *fname)
Create a new file-based log target.
Definition: logging.c:916
log_filename_pos
Where on a log line source file and line should be logged.
Definition: logging.h:258
Logging configuration, passed to log_init.
Definition: logging.h:222
uint8_t type
see GSMTAP_TYPE_*
Definition: gsmtap.h:122
void log_parse_category_mask(struct log_target *target, const char *mask)
parse the log category mask
Definition: logging.c:303
Definition: logging.h:173
log_print_filters * print_fn
filter saving function
Definition: logging.h:236
unsigned int num_cat_user
total number of user categories (not library)
Definition: logging.h:231
const char * log_category_name(int subsys)
Definition: logging.c:376
Log context information, passed to filter.
Definition: logging.h:166
const struct log_info_cat * cat
per-category information
Definition: logging.h:227
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:73
struct log_target * log_target_create(void)
Create a new log target skeleton.
Definition: logging.c:851
void log_set_print_level(struct log_target *target, int)
Enable or disable printing of the log level name.
Definition: logging.c:806
const char * hostname
Definition: logging.h:313
structure representing a logging target
Definition: logging.h:264
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:592
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:815
void log_reset_context(void)
Reset (clear) the logging context.
Definition: logging.c:670
#define LOG_MAX_CTX
Maximum number of logging contexts.
Definition: logging.h:15
systemd journal logging
Definition: logging.h:247
uint8_t loglevel
currently selected log-level
Definition: logging.h:161
unsigned int num_cat
total number of categories
Definition: logging.h:229
void logp2(int subsys, unsigned int level, const char *file, int line, int cont, const char *format,...) __attribute__((format(printf
logging function used by LOGP() macro
Definition: logging.c:644
int log_targets_reopen(void)
close and re-open all log files (for log file rotation)
Definition: logging.c:1016
Definition: logging.h:260
const char * description
description text
Definition: logging.h:160