blob: e058a576614eb2c47c9287d7d8e16d8a13a93ed6 [file] [log] [blame]
Sylvain Munaut12ba7782014-06-16 10:13:40 +02001#pragma once
Harald Welte3ae27582010-03-26 21:24:24 +08002
Harald Welte18fc4652011-08-17 14:14:17 +02003/*! \defgroup logging Osmocom logging framework
4 * @{
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02005 * \file logging.h */
Harald Welte18fc4652011-08-17 14:14:17 +02006
Harald Welte3ae27582010-03-26 21:24:24 +08007#include <stdio.h>
8#include <stdint.h>
Christoph Fritzab7c9c72011-09-01 16:22:17 +02009#include <stdarg.h>
Harald Welteaa00f992016-12-02 15:30:02 +010010#include <stdbool.h>
Maxaa1bc012017-01-13 11:01:12 +010011#include <osmocom/core/defs.h>
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010012#include <osmocom/core/linuxlist.h>
Harald Welte3ae27582010-03-26 21:24:24 +080013
Neels Hofmeyr87e45502017-06-20 00:17:59 +020014/*! Maximum number of logging contexts */
Harald Welte3ae27582010-03-26 21:24:24 +080015#define LOG_MAX_CTX 8
Neels Hofmeyr87e45502017-06-20 00:17:59 +020016/*! Maximum number of logging filters */
Harald Welte3ae27582010-03-26 21:24:24 +080017#define LOG_MAX_FILTERS 8
18
19#define DEBUG
20
21#ifdef DEBUG
Neels Hofmeyr87e45502017-06-20 00:17:59 +020022/*! Log a debug message through the Osmocom logging framework
Harald Welte2d2e2cc2016-04-25 12:11:20 +020023 * \param[in] ss logging subsystem (e.g. \ref DLGLOBAL)
24 * \param[in] fmt format string
25 * \param[in] args variable argument list
26 */
Maxaa1bc012017-01-13 11:01:12 +010027#define DEBUGP(ss, fmt, args...) LOGP(ss, LOGL_DEBUG, fmt, ##args)
28#define DEBUGPC(ss, fmt, args...) LOGPC(ss, LOGL_DEBUG, fmt, ##args)
Harald Welte3ae27582010-03-26 21:24:24 +080029#else
30#define DEBUGP(xss, fmt, args...)
31#define DEBUGPC(ss, fmt, args...)
32#endif
33
Harald Welte3ae27582010-03-26 21:24:24 +080034
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +020035void osmo_vlogp(int subsys, int level, const char *file, int line,
Harald Welte36c5a3e2011-08-27 14:33:19 +020036 int cont, const char *format, va_list ap);
37
Maxaa1bc012017-01-13 11:01:12 +010038void logp(int subsys, const char *file, int line, int cont, const char *format, ...) OSMO_DEPRECATED("Use DEBUGP* macros instead");
Harald Welte3ae27582010-03-26 21:24:24 +080039
Neels Hofmeyr87e45502017-06-20 00:17:59 +020040/*! Log a new message through the Osmocom logging framework
Harald Weltebd598e32011-08-16 23:26:52 +020041 * \param[in] ss logging subsystem (e.g. \ref DLGLOBAL)
42 * \param[in] level logging level (e.g. \ref LOGL_NOTICE)
43 * \param[in] fmt format string
44 * \param[in] args variable argument list
45 */
Harald Welte3ae27582010-03-26 21:24:24 +080046#define LOGP(ss, level, fmt, args...) \
Neels Hofmeyr725698a2016-12-14 17:24:54 +010047 LOGPSRC(ss, level, NULL, 0, fmt, ## args)
Harald Weltebd598e32011-08-16 23:26:52 +020048
Neels Hofmeyr87e45502017-06-20 00:17:59 +020049/*! Continue a log message through the Osmocom logging framework
Harald Weltebd598e32011-08-16 23:26:52 +020050 * \param[in] ss logging subsystem (e.g. \ref DLGLOBAL)
51 * \param[in] level logging level (e.g. \ref LOGL_NOTICE)
52 * \param[in] fmt format string
53 * \param[in] args variable argument list
54 */
Harald Welte3ae27582010-03-26 21:24:24 +080055#define LOGPC(ss, level, fmt, args...) \
Jacob Erlbecka89d22c2015-11-17 11:52:25 +010056 do { \
57 if (log_check_level(ss, level)) \
Harald Welte3d792402016-05-10 15:23:06 +020058 logp2(ss, level, __BASE_FILE__, __LINE__, 1, fmt, ##args); \
Jacob Erlbecka89d22c2015-11-17 11:52:25 +010059 } while(0)
Harald Welte3ae27582010-03-26 21:24:24 +080060
Neels Hofmeyr87e45502017-06-20 00:17:59 +020061/*! Log through the Osmocom logging framework with explicit source.
Neels Hofmeyr725698a2016-12-14 17:24:54 +010062 * If caller_file is passed as NULL, __BASE_FILE__ and __LINE__ are used
63 * instead of caller_file and caller_line (so that this macro here defines
64 * both cases in the same place, and to catch cases where callers fail to pass
65 * a non-null filename string).
66 * \param[in] ss logging subsystem (e.g. \ref DLGLOBAL)
67 * \param[in] level logging level (e.g. \ref LOGL_NOTICE)
68 * \param[in] caller_file caller's source file string (e.g. __BASE_FILE__)
69 * \param[in] caller_line caller's source line nr (e.g. __LINE__)
70 * \param[in] fmt format string
71 * \param[in] args variable argument list
72 */
73#define LOGPSRC(ss, level, caller_file, caller_line, fmt, args...) \
74 do { \
75 if (log_check_level(ss, level)) {\
76 if (caller_file) \
77 logp2(ss, level, caller_file, caller_line, 0, fmt, ##args); \
78 else \
79 logp2(ss, level, __BASE_FILE__, __LINE__, 0, fmt, ##args); \
80 }\
81 } while(0)
82
Neels Hofmeyr87e45502017-06-20 00:17:59 +020083/*! different log levels */
84#define LOGL_DEBUG 1 /*!< debugging information */
85#define LOGL_INFO 3 /*!< general information */
86#define LOGL_NOTICE 5 /*!< abnormal/unexpected condition */
87#define LOGL_ERROR 7 /*!< error condition, requires user action */
88#define LOGL_FATAL 8 /*!< fatal, program aborted */
Harald Welte3ae27582010-03-26 21:24:24 +080089
Harald Welteb43bc042011-06-27 10:29:17 +020090/* logging levels defined by the library itself */
Harald Welte2d2e2cc2016-04-25 12:11:20 +020091#define DLGLOBAL -1 /*!< global logging */
92#define DLLAPD -2 /*!< LAPD implementation */
93#define DLINP -3 /*!< (A-bis) Input sub-system */
94#define DLMUX -4 /*!< Osmocom Multiplex (Osmux) */
95#define DLMI -5 /*!< ISDN-layer below input sub-system */
96#define DLMIB -6 /*!< ISDN layer B-channel */
97#define DLSMS -7 /*!< SMS sub-system */
98#define DLCTRL -8 /*!< Control Interface */
99#define DLGTP -9 /*!< GTP (GPRS Tunneling Protocol */
100#define DLSTATS -10 /*!< Statistics */
Neels Hofmeyr9795cf12016-12-10 17:01:06 +0100101#define DLGSUP -11 /*!< Generic Subscriber Update Protocol */
Harald Weltec0f00072016-04-27 18:32:35 +0200102#define DLOAP -12 /*!< Osmocom Authentication Protocol */
Harald Welte62d32962017-04-03 19:37:11 +0200103#define DLSS7 -13 /*!< Osmocom SS7 */
104#define DLSCCP -14 /*!< Osmocom SCCP */
105#define DLSUA -15 /*!< Osmocom SUA */
106#define DLM3UA -16 /*!< Osmocom M3UA */
107#define OSMO_NUM_DLIB 16 /*!< Number of logging sub-systems in libraries */
Harald Welteb43bc042011-06-27 10:29:17 +0200108
Neels Hofmeyrfc47b032017-06-20 04:29:38 +0200109/*! Configuration of single log category / sub-system */
Harald Welte3ae27582010-03-26 21:24:24 +0800110struct log_category {
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200111 uint8_t loglevel; /*!< configured log-level */
112 uint8_t enabled; /*!< is logging enabled? */
Harald Welte3ae27582010-03-26 21:24:24 +0800113};
114
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200115/*! Information regarding one logging category */
Harald Welte3ae27582010-03-26 21:24:24 +0800116struct log_info_cat {
Harald Welte18fc4652011-08-17 14:14:17 +0200117 const char *name; /*!< name of category */
118 const char *color; /*!< color string for cateyory */
119 const char *description; /*!< description text */
120 uint8_t loglevel; /*!< currently selected log-level */
121 uint8_t enabled; /*!< is this category enabled or not */
Harald Welte3ae27582010-03-26 21:24:24 +0800122};
123
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200124/*! Log context information, passed to filter */
Harald Welte3ae27582010-03-26 21:24:24 +0800125struct log_context {
126 void *ctx[LOG_MAX_CTX+1];
127};
128
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200129/*! Indexes to indicate the object currently acted upon.
Neels Hofmeyr492e1802017-02-23 17:45:26 +0100130 * Array indexes for the global \a log_context array. */
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100131enum log_ctx_index {
132 LOG_CTX_GB_NSVC,
133 LOG_CTX_GB_BVC,
134 LOG_CTX_BSC_SUBSCR,
135 LOG_CTX_VLR_SUBSCR,
136 _LOG_CTX_COUNT
Neels Hofmeyr812ba6d2017-02-17 16:35:27 +0100137};
138
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200139/*! Indexes to indicate objects that should be logged.
Neels Hofmeyr492e1802017-02-23 17:45:26 +0100140 * Array indexes to log_target->filter_data and bit indexes for
141 * log_target->filter_map. */
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100142enum log_filter_index {
143 LOG_FLT_ALL,
144 LOG_FLT_GB_NSVC,
145 LOG_FLT_GB_BVC,
146 LOG_FLT_BSC_SUBSCR,
147 LOG_FLT_VLR_SUBSCR,
148 _LOG_FLT_COUNT
Neels Hofmeyr812ba6d2017-02-17 16:35:27 +0100149};
150
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200151/*! Compatibility with older libosmocore versions */
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100152#define LOG_FILTER_ALL (1<<LOG_FLT_ALL)
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200153/*! Compatibility with older libosmocore versions */
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100154#define GPRS_CTX_NSVC LOG_CTX_GB_NSVC
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200155/*! Compatibility with older libosmocore versions */
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100156#define GPRS_CTX_BVC LOG_CTX_GB_BVC
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200157/*! Indexes to indicate the object currently acted upon.
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100158 * Array indexes for the global \a log_context array. */
Neels Hofmeyr0d6420b2017-02-23 17:34:35 +0100159
Harald Welte3ae27582010-03-26 21:24:24 +0800160struct log_target;
161
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200162/*! Log filter function */
Harald Welte3ae27582010-03-26 21:24:24 +0800163typedef int log_filter(const struct log_context *ctx,
164 struct log_target *target);
165
Harald Weltefb84f322013-06-06 07:33:54 +0200166struct log_info;
167struct vty;
Harald Welteaa00f992016-12-02 15:30:02 +0100168struct gsmtap_inst;
Harald Weltefb84f322013-06-06 07:33:54 +0200169
170typedef void log_print_filters(struct vty *vty,
171 const struct log_info *info,
172 const struct log_target *tgt);
173
174typedef void log_save_filters(struct vty *vty,
175 const struct log_info *info,
176 const struct log_target *tgt);
177
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200178/*! Logging configuration, passed to \ref log_init */
Harald Welte3ae27582010-03-26 21:24:24 +0800179struct log_info {
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200180 /* filter callback function */
Harald Welte3ae27582010-03-26 21:24:24 +0800181 log_filter *filter_fn;
182
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200183 /*! per-category information */
Holger Hans Peter Freyther06f64552012-09-11 10:31:29 +0200184 const struct log_info_cat *cat;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200185 /*! total number of categories */
Harald Welte3ae27582010-03-26 21:24:24 +0800186 unsigned int num_cat;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200187 /*! total number of user categories (not library) */
Harald Welteb43bc042011-06-27 10:29:17 +0200188 unsigned int num_cat_user;
Harald Weltefb84f322013-06-06 07:33:54 +0200189
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200190 /*! filter saving function */
Harald Weltefb84f322013-06-06 07:33:54 +0200191 log_save_filters *save_fn;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200192 /*! filter saving function */
Harald Weltefb84f322013-06-06 07:33:54 +0200193 log_print_filters *print_fn;
Harald Welte3ae27582010-03-26 21:24:24 +0800194};
195
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200196/*! Type of logging target */
Harald Welte28222962011-02-18 20:37:04 +0100197enum log_target_type {
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200198 LOG_TGT_TYPE_VTY, /*!< VTY logging */
199 LOG_TGT_TYPE_SYSLOG, /*!< syslog based logging */
200 LOG_TGT_TYPE_FILE, /*!< text file logging */
201 LOG_TGT_TYPE_STDERR, /*!< stderr logging */
202 LOG_TGT_TYPE_STRRB, /*!< osmo_strrb-backed logging */
203 LOG_TGT_TYPE_GSMTAP, /*!< GSMTAP network logging */
Harald Welte28222962011-02-18 20:37:04 +0100204};
205
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200206/*! structure representing a logging target */
Harald Welte3ae27582010-03-26 21:24:24 +0800207struct log_target {
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200208 struct llist_head entry; /*!< linked list */
Harald Welte3ae27582010-03-26 21:24:24 +0800209
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200210 /*! Internal data for filtering */
Harald Welte3ae27582010-03-26 21:24:24 +0800211 int filter_map;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200212 /*! Internal data for filtering */
Harald Welte3ae27582010-03-26 21:24:24 +0800213 void *filter_data[LOG_MAX_FILTERS+1];
214
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200215 /*! logging categories */
Harald Welteb43bc042011-06-27 10:29:17 +0200216 struct log_category *categories;
217
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200218 /*! global log level */
Harald Welte3ae27582010-03-26 21:24:24 +0800219 uint8_t loglevel;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200220 /*! should color be used when printing log messages? */
Harald Welte87dbca12011-07-16 11:57:53 +0200221 unsigned int use_color:1;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200222 /*! should log messages be prefixed with a timestamp? */
Harald Welte87dbca12011-07-16 11:57:53 +0200223 unsigned int print_timestamp:1;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200224 /*! should log messages be prefixed with a filename? */
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200225 unsigned int print_filename:1;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200226 /*! should log messages be prefixed with a category name? */
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100227 unsigned int print_category:1;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200228 /*! should log messages be prefixed with an extended timestamp? */
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100229 unsigned int print_ext_timestamp:1;
Harald Welte3ae27582010-03-26 21:24:24 +0800230
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200231 /*! the type of this log taget */
Harald Welte28222962011-02-18 20:37:04 +0100232 enum log_target_type type;
233
Harald Welte3ae27582010-03-26 21:24:24 +0800234 union {
235 struct {
236 FILE *out;
Harald Welte72d0c532010-08-25 19:24:00 +0200237 const char *fname;
Harald Welte0083cd32010-08-25 14:55:44 +0200238 } tgt_file;
Harald Welte3ae27582010-03-26 21:24:24 +0800239
240 struct {
241 int priority;
Harald Welte28222962011-02-18 20:37:04 +0100242 int facility;
Harald Welte3ae27582010-03-26 21:24:24 +0800243 } tgt_syslog;
244
245 struct {
246 void *vty;
247 } tgt_vty;
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000248
249 struct {
250 void *rb;
251 } tgt_rb;
Harald Welteaa00f992016-12-02 15:30:02 +0100252
253 struct {
254 struct gsmtap_inst *gsmtap_inst;
255 const char *ident;
256 const char *hostname;
257 } tgt_gsmtap;
Harald Welte3ae27582010-03-26 21:24:24 +0800258 };
259
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200260 /*! call-back function to be called when the logging framework
Harald Welted7c0a372016-12-02 13:52:59 +0100261 * wants to log a fully formatted string
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +0100262 * \param[in] target logging target
Harald Welte18fc4652011-08-17 14:14:17 +0200263 * \param[in] level log level of currnet message
264 * \param[in] string the string that is to be written to the log
265 */
Harald Welte76e72ab2011-02-17 15:52:39 +0100266 void (*output) (struct log_target *target, unsigned int level,
267 const char *string);
Harald Welted7c0a372016-12-02 13:52:59 +0100268
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200269 /*! alternative call-back function to which the logging
Harald Welted7c0a372016-12-02 13:52:59 +0100270 * framework passes the unfortmatted input arguments,
271 * i.e. bypassing the internal string formatter
272 * \param[in] target logging target
273 * \param[in] subsys logging sub-system
274 * \param[in] level logging level
275 * \param[in] file soure code file name
276 * \param[in] line source code file line number
277 * \param[in] cont continuation of previous statement?
278 * \param[in] format format string
279 * \param[in] ap vararg list of printf arguments
280 */
281 void (*raw_output)(struct log_target *target, int subsys,
282 unsigned int level, const char *file, int line,
283 int cont, const char *format, va_list ap);
Harald Welte3ae27582010-03-26 21:24:24 +0800284};
285
286/* use the above macros */
Andreas Eversberg2d6563b2012-07-10 13:10:15 +0200287void logp2(int subsys, unsigned int level, const char *file,
Harald Welte3ae27582010-03-26 21:24:24 +0800288 int line, int cont, const char *format, ...)
289 __attribute__ ((format (printf, 6, 7)));
Harald Welteb43bc042011-06-27 10:29:17 +0200290int log_init(const struct log_info *inf, void *talloc_ctx);
Harald Welte69e6c3c2016-04-20 10:41:27 +0200291void log_fini(void);
Jacob Erlbeckde6dd722015-11-17 11:52:24 +0100292int log_check_level(int subsys, unsigned int level);
Harald Welte3ae27582010-03-26 21:24:24 +0800293
294/* context management */
295void log_reset_context(void);
296int log_set_context(uint8_t ctx, void *value);
297
298/* filter on the targets */
299void log_set_all_filter(struct log_target *target, int);
300
301void log_set_use_color(struct log_target *target, int);
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100302void log_set_print_extended_timestamp(struct log_target *target, int);
Harald Welte3ae27582010-03-26 21:24:24 +0800303void log_set_print_timestamp(struct log_target *target, int);
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200304void log_set_print_filename(struct log_target *target, int);
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100305void log_set_print_category(struct log_target *target, int);
Harald Welte3ae27582010-03-26 21:24:24 +0800306void log_set_log_level(struct log_target *target, int log_level);
307void log_parse_category_mask(struct log_target *target, const char* mask);
Harald Welteaa00f992016-12-02 15:30:02 +0100308const char* log_category_name(int subsys);
Max15b6d412017-07-06 16:57:15 +0200309int log_parse_level(const char *lvl) OSMO_DEPRECATED_OUTSIDE_LIBOSMOCORE;
310const char *log_level_str(unsigned int lvl) OSMO_DEPRECATED_OUTSIDE_LIBOSMOCORE;
Harald Welte3ae27582010-03-26 21:24:24 +0800311int log_parse_category(const char *category);
312void log_set_category_filter(struct log_target *target, int category,
313 int enable, int level);
314
315/* management of the targets */
316struct log_target *log_target_create(void);
Harald Welte72d0c532010-08-25 19:24:00 +0200317void log_target_destroy(struct log_target *target);
Harald Welte3ae27582010-03-26 21:24:24 +0800318struct log_target *log_target_create_stderr(void);
Harald Welte72d0c532010-08-25 19:24:00 +0200319struct log_target *log_target_create_file(const char *fname);
Harald Welte46cfd772011-02-17 15:56:56 +0100320struct log_target *log_target_create_syslog(const char *ident, int option,
321 int facility);
Harald Welteaa00f992016-12-02 15:30:02 +0100322struct log_target *log_target_create_gsmtap(const char *host, uint16_t port,
323 const char *ident,
324 bool ofd_wq_mode,
325 bool add_sink);
Harald Welte72d0c532010-08-25 19:24:00 +0200326int log_target_file_reopen(struct log_target *tgt);
Harald Welte4de854d2013-03-18 19:01:40 +0100327int log_targets_reopen(void);
Harald Welte72d0c532010-08-25 19:24:00 +0200328
Harald Welte3ae27582010-03-26 21:24:24 +0800329void log_add_target(struct log_target *target);
330void log_del_target(struct log_target *target);
331
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100332/* Generate command string for VTY use */
Pau Espin Pedrol69dfe5a2017-06-17 23:18:11 +0200333const char *log_vty_command_string() OSMO_DEPRECATED_OUTSIDE_LIBOSMOCORE;
334const char *log_vty_command_description() OSMO_DEPRECATED_OUTSIDE_LIBOSMOCORE;
Harald Welte7638af92010-05-11 16:39:22 +0200335
Harald Welte28222962011-02-18 20:37:04 +0100336struct log_target *log_target_find(int type, const char *fname);
337extern struct llist_head osmo_log_target_list;
338
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200339/*! @} */