blob: 7af0ad20f5d78ada6afd499bd7ffb76e91e3ac16 [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 * @{
Harald Weltebd598e32011-08-16 23:26:52 +02005 */
6
Harald Welte18fc4652011-08-17 14:14:17 +02007/*! \file logging.h */
8
Harald Welte3ae27582010-03-26 21:24:24 +08009#include <stdio.h>
10#include <stdint.h>
Christoph Fritzab7c9c72011-09-01 16:22:17 +020011#include <stdarg.h>
Harald Welteaa00f992016-12-02 15:30:02 +010012#include <stdbool.h>
Maxaa1bc012017-01-13 11:01:12 +010013#include <osmocom/core/defs.h>
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010014#include <osmocom/core/linuxlist.h>
Harald Welte3ae27582010-03-26 21:24:24 +080015
Harald Weltebd598e32011-08-16 23:26:52 +020016/*! \brief Maximum number of logging contexts */
Harald Welte3ae27582010-03-26 21:24:24 +080017#define LOG_MAX_CTX 8
Harald Weltebd598e32011-08-16 23:26:52 +020018/*! \brief Maximum number of logging filters */
Harald Welte3ae27582010-03-26 21:24:24 +080019#define LOG_MAX_FILTERS 8
20
21#define DEBUG
22
23#ifdef DEBUG
Harald Welte2d2e2cc2016-04-25 12:11:20 +020024/*! \brief Log a debug message through the Osmocom logging framework
25 * \param[in] ss logging subsystem (e.g. \ref DLGLOBAL)
26 * \param[in] fmt format string
27 * \param[in] args variable argument list
28 */
Maxaa1bc012017-01-13 11:01:12 +010029#define DEBUGP(ss, fmt, args...) LOGP(ss, LOGL_DEBUG, fmt, ##args)
30#define DEBUGPC(ss, fmt, args...) LOGPC(ss, LOGL_DEBUG, fmt, ##args)
Harald Welte3ae27582010-03-26 21:24:24 +080031#else
32#define DEBUGP(xss, fmt, args...)
33#define DEBUGPC(ss, fmt, args...)
34#endif
35
Harald Welte3ae27582010-03-26 21:24:24 +080036
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +020037void osmo_vlogp(int subsys, int level, const char *file, int line,
Harald Welte36c5a3e2011-08-27 14:33:19 +020038 int cont, const char *format, va_list ap);
39
Maxaa1bc012017-01-13 11:01:12 +010040void 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 +080041
Harald Weltebd598e32011-08-16 23:26:52 +020042/*! \brief Log a new message through the Osmocom logging framework
43 * \param[in] ss logging subsystem (e.g. \ref DLGLOBAL)
44 * \param[in] level logging level (e.g. \ref LOGL_NOTICE)
45 * \param[in] fmt format string
46 * \param[in] args variable argument list
47 */
Harald Welte3ae27582010-03-26 21:24:24 +080048#define LOGP(ss, level, fmt, args...) \
Neels Hofmeyr725698a2016-12-14 17:24:54 +010049 LOGPSRC(ss, level, NULL, 0, fmt, ## args)
Harald Weltebd598e32011-08-16 23:26:52 +020050
51/*! \brief Continue a log message through the Osmocom logging framework
52 * \param[in] ss logging subsystem (e.g. \ref DLGLOBAL)
53 * \param[in] level logging level (e.g. \ref LOGL_NOTICE)
54 * \param[in] fmt format string
55 * \param[in] args variable argument list
56 */
Harald Welte3ae27582010-03-26 21:24:24 +080057#define LOGPC(ss, level, fmt, args...) \
Jacob Erlbecka89d22c2015-11-17 11:52:25 +010058 do { \
59 if (log_check_level(ss, level)) \
Harald Welte3d792402016-05-10 15:23:06 +020060 logp2(ss, level, __BASE_FILE__, __LINE__, 1, fmt, ##args); \
Jacob Erlbecka89d22c2015-11-17 11:52:25 +010061 } while(0)
Harald Welte3ae27582010-03-26 21:24:24 +080062
Neels Hofmeyr725698a2016-12-14 17:24:54 +010063/*! \brief Log through the Osmocom logging framework with explicit source.
64 * If caller_file is passed as NULL, __BASE_FILE__ and __LINE__ are used
65 * instead of caller_file and caller_line (so that this macro here defines
66 * both cases in the same place, and to catch cases where callers fail to pass
67 * a non-null filename string).
68 * \param[in] ss logging subsystem (e.g. \ref DLGLOBAL)
69 * \param[in] level logging level (e.g. \ref LOGL_NOTICE)
70 * \param[in] caller_file caller's source file string (e.g. __BASE_FILE__)
71 * \param[in] caller_line caller's source line nr (e.g. __LINE__)
72 * \param[in] fmt format string
73 * \param[in] args variable argument list
74 */
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
Harald Welte18fc4652011-08-17 14:14:17 +020085/*! \brief different log levels */
86#define LOGL_DEBUG 1 /*!< \brief debugging information */
Harald Welte2d2e2cc2016-04-25 12:11:20 +020087#define LOGL_INFO 3 /*!< \brief general information */
Harald Welte18fc4652011-08-17 14:14:17 +020088#define LOGL_NOTICE 5 /*!< \brief abnormal/unexpected condition */
89#define LOGL_ERROR 7 /*!< \brief error condition, requires user action */
90#define LOGL_FATAL 8 /*!< \brief fatal, program aborted */
Harald Welte3ae27582010-03-26 21:24:24 +080091
Harald Welteb43bc042011-06-27 10:29:17 +020092/* logging levels defined by the library itself */
Harald Welte2d2e2cc2016-04-25 12:11:20 +020093#define DLGLOBAL -1 /*!< global logging */
94#define DLLAPD -2 /*!< LAPD implementation */
95#define DLINP -3 /*!< (A-bis) Input sub-system */
96#define DLMUX -4 /*!< Osmocom Multiplex (Osmux) */
97#define DLMI -5 /*!< ISDN-layer below input sub-system */
98#define DLMIB -6 /*!< ISDN layer B-channel */
99#define DLSMS -7 /*!< SMS sub-system */
100#define DLCTRL -8 /*!< Control Interface */
101#define DLGTP -9 /*!< GTP (GPRS Tunneling Protocol */
102#define DLSTATS -10 /*!< Statistics */
Neels Hofmeyr9795cf12016-12-10 17:01:06 +0100103#define DLGSUP -11 /*!< Generic Subscriber Update Protocol */
Harald Weltec0f00072016-04-27 18:32:35 +0200104#define DLOAP -12 /*!< Osmocom Authentication Protocol */
Harald Welte62d32962017-04-03 19:37:11 +0200105#define DLSS7 -13 /*!< Osmocom SS7 */
106#define DLSCCP -14 /*!< Osmocom SCCP */
107#define DLSUA -15 /*!< Osmocom SUA */
108#define DLM3UA -16 /*!< Osmocom M3UA */
109#define OSMO_NUM_DLIB 16 /*!< Number of logging sub-systems in libraries */
Harald Welteb43bc042011-06-27 10:29:17 +0200110
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200111/*! Configuration of singgle log category / sub-system */
Harald Welte3ae27582010-03-26 21:24:24 +0800112struct log_category {
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200113 uint8_t loglevel; /*!< configured log-level */
114 uint8_t enabled; /*!< is logging enabled? */
Harald Welte3ae27582010-03-26 21:24:24 +0800115};
116
Harald Welte18fc4652011-08-17 14:14:17 +0200117/*! \brief Information regarding one logging category */
Harald Welte3ae27582010-03-26 21:24:24 +0800118struct log_info_cat {
Harald Welte18fc4652011-08-17 14:14:17 +0200119 const char *name; /*!< name of category */
120 const char *color; /*!< color string for cateyory */
121 const char *description; /*!< description text */
122 uint8_t loglevel; /*!< currently selected log-level */
123 uint8_t enabled; /*!< is this category enabled or not */
Harald Welte3ae27582010-03-26 21:24:24 +0800124};
125
Harald Welte18fc4652011-08-17 14:14:17 +0200126/*! \brief Log context information, passed to filter */
Harald Welte3ae27582010-03-26 21:24:24 +0800127struct log_context {
128 void *ctx[LOG_MAX_CTX+1];
129};
130
Neels Hofmeyr492e1802017-02-23 17:45:26 +0100131/*! \brief Indexes to indicate the object currently acted upon.
132 * Array indexes for the global \a log_context array. */
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100133enum log_ctx_index {
134 LOG_CTX_GB_NSVC,
135 LOG_CTX_GB_BVC,
136 LOG_CTX_BSC_SUBSCR,
137 LOG_CTX_VLR_SUBSCR,
138 _LOG_CTX_COUNT
Neels Hofmeyr812ba6d2017-02-17 16:35:27 +0100139};
140
Neels Hofmeyr492e1802017-02-23 17:45:26 +0100141/*! \brief Indexes to indicate objects that should be logged.
142 * Array indexes to log_target->filter_data and bit indexes for
143 * log_target->filter_map. */
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100144enum log_filter_index {
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
Neels Hofmeyr812ba6d2017-02-17 16:35:27 +0100151};
152
Neels Hofmeyr0d6420b2017-02-23 17:34:35 +0100153/*! \brief Compatibility with older libosmocore versions */
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100154#define LOG_FILTER_ALL (1<<LOG_FLT_ALL)
Neels Hofmeyr0d6420b2017-02-23 17:34:35 +0100155/*! \brief Compatibility with older libosmocore versions */
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100156#define GPRS_CTX_NSVC LOG_CTX_GB_NSVC
Neels Hofmeyr0d6420b2017-02-23 17:34:35 +0100157/*! \brief Compatibility with older libosmocore versions */
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100158#define GPRS_CTX_BVC LOG_CTX_GB_BVC
159/*! \brief Indexes to indicate the object currently acted upon.
160 * Array indexes for the global \a log_context array. */
Neels Hofmeyr0d6420b2017-02-23 17:34:35 +0100161
Harald Welte3ae27582010-03-26 21:24:24 +0800162struct log_target;
163
Harald Welte18fc4652011-08-17 14:14:17 +0200164/*! \brief Log filter function */
Harald Welte3ae27582010-03-26 21:24:24 +0800165typedef int log_filter(const struct log_context *ctx,
166 struct log_target *target);
167
Harald Weltefb84f322013-06-06 07:33:54 +0200168struct log_info;
169struct vty;
Harald Welteaa00f992016-12-02 15:30:02 +0100170struct gsmtap_inst;
Harald Weltefb84f322013-06-06 07:33:54 +0200171
172typedef void log_print_filters(struct vty *vty,
173 const struct log_info *info,
174 const struct log_target *tgt);
175
176typedef void log_save_filters(struct vty *vty,
177 const struct log_info *info,
178 const struct log_target *tgt);
179
Harald Welte18fc4652011-08-17 14:14:17 +0200180/*! \brief Logging configuration, passed to \ref log_init */
Harald Welte3ae27582010-03-26 21:24:24 +0800181struct log_info {
Harald Welte18fc4652011-08-17 14:14:17 +0200182 /* \brief filter callback function */
Harald Welte3ae27582010-03-26 21:24:24 +0800183 log_filter *filter_fn;
184
Harald Welte18fc4652011-08-17 14:14:17 +0200185 /*! \brief per-category information */
Holger Hans Peter Freyther06f64552012-09-11 10:31:29 +0200186 const struct log_info_cat *cat;
Harald Welte18fc4652011-08-17 14:14:17 +0200187 /*! \brief total number of categories */
Harald Welte3ae27582010-03-26 21:24:24 +0800188 unsigned int num_cat;
Harald Welte18fc4652011-08-17 14:14:17 +0200189 /*! \brief total number of user categories (not library) */
Harald Welteb43bc042011-06-27 10:29:17 +0200190 unsigned int num_cat_user;
Harald Weltefb84f322013-06-06 07:33:54 +0200191
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200192 /*! \brief filter saving function */
Harald Weltefb84f322013-06-06 07:33:54 +0200193 log_save_filters *save_fn;
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200194 /*! \brief filter saving function */
Harald Weltefb84f322013-06-06 07:33:54 +0200195 log_print_filters *print_fn;
Harald Welte3ae27582010-03-26 21:24:24 +0800196};
197
Harald Welte18fc4652011-08-17 14:14:17 +0200198/*! \brief Type of logging target */
Harald Welte28222962011-02-18 20:37:04 +0100199enum log_target_type {
Harald Welte18fc4652011-08-17 14:14:17 +0200200 LOG_TGT_TYPE_VTY, /*!< \brief VTY logging */
201 LOG_TGT_TYPE_SYSLOG, /*!< \brief syslog based logging */
202 LOG_TGT_TYPE_FILE, /*!< \brief text file logging */
203 LOG_TGT_TYPE_STDERR, /*!< \brief stderr logging */
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000204 LOG_TGT_TYPE_STRRB, /*!< \brief osmo_strrb-backed logging */
Harald Welteaa00f992016-12-02 15:30:02 +0100205 LOG_TGT_TYPE_GSMTAP, /*!< \brief GSMTAP network logging */
Harald Welte28222962011-02-18 20:37:04 +0100206};
207
Harald Welte18fc4652011-08-17 14:14:17 +0200208/*! \brief structure representing a logging target */
Harald Welte3ae27582010-03-26 21:24:24 +0800209struct log_target {
Harald Welte18fc4652011-08-17 14:14:17 +0200210 struct llist_head entry; /*!< \brief linked list */
Harald Welte3ae27582010-03-26 21:24:24 +0800211
Harald Welte18fc4652011-08-17 14:14:17 +0200212 /*! \brief Internal data for filtering */
Harald Welte3ae27582010-03-26 21:24:24 +0800213 int filter_map;
Harald Welte18fc4652011-08-17 14:14:17 +0200214 /*! \brief Internal data for filtering */
Harald Welte3ae27582010-03-26 21:24:24 +0800215 void *filter_data[LOG_MAX_FILTERS+1];
216
Harald Welte18fc4652011-08-17 14:14:17 +0200217 /*! \brief logging categories */
Harald Welteb43bc042011-06-27 10:29:17 +0200218 struct log_category *categories;
219
Harald Welte18fc4652011-08-17 14:14:17 +0200220 /*! \brief global log level */
Harald Welte3ae27582010-03-26 21:24:24 +0800221 uint8_t loglevel;
Harald Welte18fc4652011-08-17 14:14:17 +0200222 /*! \brief should color be used when printing log messages? */
Harald Welte87dbca12011-07-16 11:57:53 +0200223 unsigned int use_color:1;
Harald Welte18fc4652011-08-17 14:14:17 +0200224 /*! \brief should log messages be prefixed with a timestamp? */
Harald Welte87dbca12011-07-16 11:57:53 +0200225 unsigned int print_timestamp:1;
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200226 /*! \brief should log messages be prefixed with a filename? */
227 unsigned int print_filename:1;
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100228 /*! \brief should log messages be prefixed with a category name? */
229 unsigned int print_category:1;
230 /*! \brief should log messages be prefixed with an extended timestamp? */
231 unsigned int print_ext_timestamp:1;
Harald Welte3ae27582010-03-26 21:24:24 +0800232
Harald Welte18fc4652011-08-17 14:14:17 +0200233 /*! \brief the type of this log taget */
Harald Welte28222962011-02-18 20:37:04 +0100234 enum log_target_type type;
235
Harald Welte3ae27582010-03-26 21:24:24 +0800236 union {
237 struct {
238 FILE *out;
Harald Welte72d0c532010-08-25 19:24:00 +0200239 const char *fname;
Harald Welte0083cd32010-08-25 14:55:44 +0200240 } tgt_file;
Harald Welte3ae27582010-03-26 21:24:24 +0800241
242 struct {
243 int priority;
Harald Welte28222962011-02-18 20:37:04 +0100244 int facility;
Harald Welte3ae27582010-03-26 21:24:24 +0800245 } tgt_syslog;
246
247 struct {
248 void *vty;
249 } tgt_vty;
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000250
251 struct {
252 void *rb;
253 } tgt_rb;
Harald Welteaa00f992016-12-02 15:30:02 +0100254
255 struct {
256 struct gsmtap_inst *gsmtap_inst;
257 const char *ident;
258 const char *hostname;
259 } tgt_gsmtap;
Harald Welte3ae27582010-03-26 21:24:24 +0800260 };
261
Harald Welte18fc4652011-08-17 14:14:17 +0200262 /*! \brief call-back function to be called when the logging framework
Harald Welted7c0a372016-12-02 13:52:59 +0100263 * wants to log a fully formatted string
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +0100264 * \param[in] target logging target
Harald Welte18fc4652011-08-17 14:14:17 +0200265 * \param[in] level log level of currnet message
266 * \param[in] string the string that is to be written to the log
267 */
Harald Welte76e72ab2011-02-17 15:52:39 +0100268 void (*output) (struct log_target *target, unsigned int level,
269 const char *string);
Harald Welted7c0a372016-12-02 13:52:59 +0100270
271 /*! \brief alternative call-back function to which the logging
272 * framework passes the unfortmatted input arguments,
273 * i.e. bypassing the internal string formatter
274 * \param[in] target logging target
275 * \param[in] subsys logging sub-system
276 * \param[in] level logging level
277 * \param[in] file soure code file name
278 * \param[in] line source code file line number
279 * \param[in] cont continuation of previous statement?
280 * \param[in] format format string
281 * \param[in] ap vararg list of printf arguments
282 */
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);
Harald Welte3ae27582010-03-26 21:24:24 +0800286};
287
288/* use the above macros */
Andreas Eversberg2d6563b2012-07-10 13:10:15 +0200289void logp2(int subsys, unsigned int level, const char *file,
Harald Welte3ae27582010-03-26 21:24:24 +0800290 int line, int cont, const char *format, ...)
291 __attribute__ ((format (printf, 6, 7)));
Harald Welteb43bc042011-06-27 10:29:17 +0200292int log_init(const struct log_info *inf, void *talloc_ctx);
Harald Welte69e6c3c2016-04-20 10:41:27 +0200293void log_fini(void);
Jacob Erlbeckde6dd722015-11-17 11:52:24 +0100294int log_check_level(int subsys, unsigned int level);
Harald Welte3ae27582010-03-26 21:24:24 +0800295
296/* context management */
297void log_reset_context(void);
298int log_set_context(uint8_t ctx, void *value);
299
300/* filter on the targets */
301void log_set_all_filter(struct log_target *target, int);
302
303void log_set_use_color(struct log_target *target, int);
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100304void log_set_print_extended_timestamp(struct log_target *target, int);
Harald Welte3ae27582010-03-26 21:24:24 +0800305void log_set_print_timestamp(struct log_target *target, int);
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200306void log_set_print_filename(struct log_target *target, int);
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100307void log_set_print_category(struct log_target *target, int);
Harald Welte3ae27582010-03-26 21:24:24 +0800308void log_set_log_level(struct log_target *target, int log_level);
309void log_parse_category_mask(struct log_target *target, const char* mask);
Harald Welteaa00f992016-12-02 15:30:02 +0100310const char* log_category_name(int subsys);
Harald Welte3ae27582010-03-26 21:24:24 +0800311int log_parse_level(const char *lvl);
Harald Welte9ac22252010-05-11 11:19:40 +0200312const char *log_level_str(unsigned int lvl);
Harald Welte3ae27582010-03-26 21:24:24 +0800313int log_parse_category(const char *category);
314void log_set_category_filter(struct log_target *target, int category,
315 int enable, int level);
316
317/* management of the targets */
318struct log_target *log_target_create(void);
Harald Welte72d0c532010-08-25 19:24:00 +0200319void log_target_destroy(struct log_target *target);
Harald Welte3ae27582010-03-26 21:24:24 +0800320struct log_target *log_target_create_stderr(void);
Harald Welte72d0c532010-08-25 19:24:00 +0200321struct log_target *log_target_create_file(const char *fname);
Harald Welte46cfd772011-02-17 15:56:56 +0100322struct log_target *log_target_create_syslog(const char *ident, int option,
323 int facility);
Harald Welteaa00f992016-12-02 15:30:02 +0100324struct 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);
Harald Welte72d0c532010-08-25 19:24:00 +0200328int log_target_file_reopen(struct log_target *tgt);
Harald Welte4de854d2013-03-18 19:01:40 +0100329int log_targets_reopen(void);
Harald Welte72d0c532010-08-25 19:24:00 +0200330
Harald Welte3ae27582010-03-26 21:24:24 +0800331void log_add_target(struct log_target *target);
332void log_del_target(struct log_target *target);
333
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100334/* Generate command string for VTY use */
Pau Espin Pedrol69dfe5a2017-06-17 23:18:11 +0200335const char *log_vty_command_string() OSMO_DEPRECATED_OUTSIDE_LIBOSMOCORE;
336const char *log_vty_command_description() OSMO_DEPRECATED_OUTSIDE_LIBOSMOCORE;
Harald Welte7638af92010-05-11 16:39:22 +0200337
Harald Welte28222962011-02-18 20:37:04 +0100338struct log_target *log_target_find(int type, const char *fname);
339extern struct llist_head osmo_log_target_list;
340
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200341/*! @} */