blob: 86a6977faf83d0b5ba84a46f8ca9bae65d4383a5 [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 */
Neels Hofmeyra7ccf612017-07-11 18:43:09 +0200107#define DLMGCP -17 /*!< Osmocom MGCP */
108#define OSMO_NUM_DLIB 17 /*!< Number of logging sub-systems in libraries */
Harald Welteb43bc042011-06-27 10:29:17 +0200109
Neels Hofmeyrfc47b032017-06-20 04:29:38 +0200110/*! Configuration of single log category / sub-system */
Harald Welte3ae27582010-03-26 21:24:24 +0800111struct log_category {
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200112 uint8_t loglevel; /*!< configured log-level */
113 uint8_t enabled; /*!< is logging enabled? */
Harald Welte3ae27582010-03-26 21:24:24 +0800114};
115
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200116/*! Information regarding one logging category */
Harald Welte3ae27582010-03-26 21:24:24 +0800117struct log_info_cat {
Harald Welte18fc4652011-08-17 14:14:17 +0200118 const char *name; /*!< name of category */
119 const char *color; /*!< color string for cateyory */
120 const char *description; /*!< description text */
121 uint8_t loglevel; /*!< currently selected log-level */
122 uint8_t enabled; /*!< is this category enabled or not */
Harald Welte3ae27582010-03-26 21:24:24 +0800123};
124
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200125/*! Log context information, passed to filter */
Harald Welte3ae27582010-03-26 21:24:24 +0800126struct log_context {
127 void *ctx[LOG_MAX_CTX+1];
128};
129
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200130/*! Indexes to indicate the object currently acted upon.
Neels Hofmeyr492e1802017-02-23 17:45:26 +0100131 * Array indexes for the global \a log_context array. */
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100132enum log_ctx_index {
133 LOG_CTX_GB_NSVC,
134 LOG_CTX_GB_BVC,
135 LOG_CTX_BSC_SUBSCR,
136 LOG_CTX_VLR_SUBSCR,
137 _LOG_CTX_COUNT
Neels Hofmeyr812ba6d2017-02-17 16:35:27 +0100138};
139
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200140/*! Indexes to indicate objects that should be logged.
Neels Hofmeyr492e1802017-02-23 17:45:26 +0100141 * Array indexes to log_target->filter_data and bit indexes for
142 * log_target->filter_map. */
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100143enum log_filter_index {
144 LOG_FLT_ALL,
145 LOG_FLT_GB_NSVC,
146 LOG_FLT_GB_BVC,
147 LOG_FLT_BSC_SUBSCR,
148 LOG_FLT_VLR_SUBSCR,
149 _LOG_FLT_COUNT
Neels Hofmeyr812ba6d2017-02-17 16:35:27 +0100150};
151
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200152/*! Compatibility with older libosmocore versions */
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100153#define LOG_FILTER_ALL (1<<LOG_FLT_ALL)
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200154/*! Compatibility with older libosmocore versions */
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100155#define GPRS_CTX_NSVC LOG_CTX_GB_NSVC
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200156/*! Compatibility with older libosmocore versions */
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100157#define GPRS_CTX_BVC LOG_CTX_GB_BVC
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200158/*! Indexes to indicate the object currently acted upon.
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100159 * Array indexes for the global \a log_context array. */
Neels Hofmeyr0d6420b2017-02-23 17:34:35 +0100160
Harald Welte3ae27582010-03-26 21:24:24 +0800161struct log_target;
162
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200163/*! Log filter function */
Harald Welte3ae27582010-03-26 21:24:24 +0800164typedef int log_filter(const struct log_context *ctx,
165 struct log_target *target);
166
Harald Weltefb84f322013-06-06 07:33:54 +0200167struct log_info;
168struct vty;
Harald Welteaa00f992016-12-02 15:30:02 +0100169struct gsmtap_inst;
Harald Weltefb84f322013-06-06 07:33:54 +0200170
171typedef void log_print_filters(struct vty *vty,
172 const struct log_info *info,
173 const struct log_target *tgt);
174
175typedef void log_save_filters(struct vty *vty,
176 const struct log_info *info,
177 const struct log_target *tgt);
178
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200179/*! Logging configuration, passed to \ref log_init */
Harald Welte3ae27582010-03-26 21:24:24 +0800180struct log_info {
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200181 /* filter callback function */
Harald Welte3ae27582010-03-26 21:24:24 +0800182 log_filter *filter_fn;
183
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200184 /*! per-category information */
Holger Hans Peter Freyther06f64552012-09-11 10:31:29 +0200185 const struct log_info_cat *cat;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200186 /*! total number of categories */
Harald Welte3ae27582010-03-26 21:24:24 +0800187 unsigned int num_cat;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200188 /*! total number of user categories (not library) */
Harald Welteb43bc042011-06-27 10:29:17 +0200189 unsigned int num_cat_user;
Harald Weltefb84f322013-06-06 07:33:54 +0200190
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200191 /*! filter saving function */
Harald Weltefb84f322013-06-06 07:33:54 +0200192 log_save_filters *save_fn;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200193 /*! filter saving function */
Harald Weltefb84f322013-06-06 07:33:54 +0200194 log_print_filters *print_fn;
Harald Welte3ae27582010-03-26 21:24:24 +0800195};
196
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200197/*! Type of logging target */
Harald Welte28222962011-02-18 20:37:04 +0100198enum log_target_type {
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200199 LOG_TGT_TYPE_VTY, /*!< VTY logging */
200 LOG_TGT_TYPE_SYSLOG, /*!< syslog based logging */
201 LOG_TGT_TYPE_FILE, /*!< text file logging */
202 LOG_TGT_TYPE_STDERR, /*!< stderr logging */
203 LOG_TGT_TYPE_STRRB, /*!< osmo_strrb-backed logging */
204 LOG_TGT_TYPE_GSMTAP, /*!< GSMTAP network logging */
Harald Welte28222962011-02-18 20:37:04 +0100205};
206
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200207/*! structure representing a logging target */
Harald Welte3ae27582010-03-26 21:24:24 +0800208struct log_target {
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200209 struct llist_head entry; /*!< linked list */
Harald Welte3ae27582010-03-26 21:24:24 +0800210
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200211 /*! Internal data for filtering */
Harald Welte3ae27582010-03-26 21:24:24 +0800212 int filter_map;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200213 /*! Internal data for filtering */
Harald Welte3ae27582010-03-26 21:24:24 +0800214 void *filter_data[LOG_MAX_FILTERS+1];
215
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200216 /*! logging categories */
Harald Welteb43bc042011-06-27 10:29:17 +0200217 struct log_category *categories;
218
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200219 /*! global log level */
Harald Welte3ae27582010-03-26 21:24:24 +0800220 uint8_t loglevel;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200221 /*! should color be used when printing log messages? */
Harald Welte87dbca12011-07-16 11:57:53 +0200222 unsigned int use_color:1;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200223 /*! should log messages be prefixed with a timestamp? */
Harald Welte87dbca12011-07-16 11:57:53 +0200224 unsigned int print_timestamp:1;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200225 /*! should log messages be prefixed with a filename? */
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200226 unsigned int print_filename:1;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200227 /*! should log messages be prefixed with a category name? */
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100228 unsigned int print_category:1;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200229 /*! should log messages be prefixed with an extended timestamp? */
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100230 unsigned int print_ext_timestamp:1;
Harald Welte3ae27582010-03-26 21:24:24 +0800231
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200232 /*! the type of this log taget */
Harald Welte28222962011-02-18 20:37:04 +0100233 enum log_target_type type;
234
Harald Welte3ae27582010-03-26 21:24:24 +0800235 union {
236 struct {
237 FILE *out;
Harald Welte72d0c532010-08-25 19:24:00 +0200238 const char *fname;
Harald Welte0083cd32010-08-25 14:55:44 +0200239 } tgt_file;
Harald Welte3ae27582010-03-26 21:24:24 +0800240
241 struct {
242 int priority;
Harald Welte28222962011-02-18 20:37:04 +0100243 int facility;
Harald Welte3ae27582010-03-26 21:24:24 +0800244 } tgt_syslog;
245
246 struct {
247 void *vty;
248 } tgt_vty;
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000249
250 struct {
251 void *rb;
252 } tgt_rb;
Harald Welteaa00f992016-12-02 15:30:02 +0100253
254 struct {
255 struct gsmtap_inst *gsmtap_inst;
256 const char *ident;
257 const char *hostname;
258 } tgt_gsmtap;
Harald Welte3ae27582010-03-26 21:24:24 +0800259 };
260
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200261 /*! call-back function to be called when the logging framework
Harald Welted7c0a372016-12-02 13:52:59 +0100262 * wants to log a fully formatted string
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +0100263 * \param[in] target logging target
Harald Welte18fc4652011-08-17 14:14:17 +0200264 * \param[in] level log level of currnet message
265 * \param[in] string the string that is to be written to the log
266 */
Harald Welte76e72ab2011-02-17 15:52:39 +0100267 void (*output) (struct log_target *target, unsigned int level,
268 const char *string);
Harald Welted7c0a372016-12-02 13:52:59 +0100269
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200270 /*! alternative call-back function to which the logging
Harald Welted7c0a372016-12-02 13:52:59 +0100271 * framework passes the unfortmatted input arguments,
272 * i.e. bypassing the internal string formatter
273 * \param[in] target logging target
274 * \param[in] subsys logging sub-system
275 * \param[in] level logging level
276 * \param[in] file soure code file name
277 * \param[in] line source code file line number
278 * \param[in] cont continuation of previous statement?
279 * \param[in] format format string
280 * \param[in] ap vararg list of printf arguments
281 */
282 void (*raw_output)(struct log_target *target, int subsys,
283 unsigned int level, const char *file, int line,
284 int cont, const char *format, va_list ap);
Harald Welte3ae27582010-03-26 21:24:24 +0800285};
286
287/* use the above macros */
Andreas Eversberg2d6563b2012-07-10 13:10:15 +0200288void logp2(int subsys, unsigned int level, const char *file,
Harald Welte3ae27582010-03-26 21:24:24 +0800289 int line, int cont, const char *format, ...)
290 __attribute__ ((format (printf, 6, 7)));
Harald Welteb43bc042011-06-27 10:29:17 +0200291int log_init(const struct log_info *inf, void *talloc_ctx);
Harald Welte69e6c3c2016-04-20 10:41:27 +0200292void log_fini(void);
Jacob Erlbeckde6dd722015-11-17 11:52:24 +0100293int log_check_level(int subsys, unsigned int level);
Harald Welte3ae27582010-03-26 21:24:24 +0800294
295/* context management */
296void log_reset_context(void);
297int log_set_context(uint8_t ctx, void *value);
298
299/* filter on the targets */
300void log_set_all_filter(struct log_target *target, int);
301
302void log_set_use_color(struct log_target *target, int);
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100303void log_set_print_extended_timestamp(struct log_target *target, int);
Harald Welte3ae27582010-03-26 21:24:24 +0800304void log_set_print_timestamp(struct log_target *target, int);
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200305void log_set_print_filename(struct log_target *target, int);
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100306void log_set_print_category(struct log_target *target, int);
Harald Welte3ae27582010-03-26 21:24:24 +0800307void log_set_log_level(struct log_target *target, int log_level);
308void log_parse_category_mask(struct log_target *target, const char* mask);
Harald Welteaa00f992016-12-02 15:30:02 +0100309const char* log_category_name(int subsys);
Max15b6d412017-07-06 16:57:15 +0200310int log_parse_level(const char *lvl) OSMO_DEPRECATED_OUTSIDE_LIBOSMOCORE;
311const char *log_level_str(unsigned int lvl) OSMO_DEPRECATED_OUTSIDE_LIBOSMOCORE;
Harald Welte3ae27582010-03-26 21:24:24 +0800312int log_parse_category(const char *category);
313void log_set_category_filter(struct log_target *target, int category,
314 int enable, int level);
315
316/* management of the targets */
317struct log_target *log_target_create(void);
Harald Welte72d0c532010-08-25 19:24:00 +0200318void log_target_destroy(struct log_target *target);
Harald Welte3ae27582010-03-26 21:24:24 +0800319struct log_target *log_target_create_stderr(void);
Harald Welte72d0c532010-08-25 19:24:00 +0200320struct log_target *log_target_create_file(const char *fname);
Harald Welte46cfd772011-02-17 15:56:56 +0100321struct log_target *log_target_create_syslog(const char *ident, int option,
322 int facility);
Harald Welteaa00f992016-12-02 15:30:02 +0100323struct log_target *log_target_create_gsmtap(const char *host, uint16_t port,
324 const char *ident,
325 bool ofd_wq_mode,
326 bool add_sink);
Harald Welte72d0c532010-08-25 19:24:00 +0200327int log_target_file_reopen(struct log_target *tgt);
Harald Welte4de854d2013-03-18 19:01:40 +0100328int log_targets_reopen(void);
Harald Welte72d0c532010-08-25 19:24:00 +0200329
Harald Welte3ae27582010-03-26 21:24:24 +0800330void log_add_target(struct log_target *target);
331void log_del_target(struct log_target *target);
332
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100333/* Generate command string for VTY use */
Pau Espin Pedrol69dfe5a2017-06-17 23:18:11 +0200334const char *log_vty_command_string() OSMO_DEPRECATED_OUTSIDE_LIBOSMOCORE;
335const char *log_vty_command_description() OSMO_DEPRECATED_OUTSIDE_LIBOSMOCORE;
Harald Welte7638af92010-05-11 16:39:22 +0200336
Harald Welte28222962011-02-18 20:37:04 +0100337struct log_target *log_target_find(int type, const char *fname);
338extern struct llist_head osmo_log_target_list;
339
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200340/*! @} */