blob: 9c498761e610afde21ce05e34ed15aeda451ac7d [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
Kévin Redond1e220f2019-06-13 13:41:00 +020014#ifndef DEBUG
Harald Welte3ae27582010-03-26 21:24:24 +080015#define DEBUG
Kévin Redond1e220f2019-06-13 13:41:00 +020016#endif
Harald Welte3ae27582010-03-26 21:24:24 +080017
Ericd02090b2021-11-24 20:24:46 +010018#ifdef LIBOSMOCORE_NO_LOGGING
19#undef DEBUG
20#endif
21
Harald Welte3ae27582010-03-26 21:24:24 +080022#ifdef DEBUG
Neels Hofmeyr87e45502017-06-20 00:17:59 +020023/*! Log a debug message through the Osmocom logging framework
Harald Welte2d2e2cc2016-04-25 12:11:20 +020024 * \param[in] ss logging subsystem (e.g. \ref DLGLOBAL)
25 * \param[in] fmt format string
26 * \param[in] args variable argument list
27 */
Maxaa1bc012017-01-13 11:01:12 +010028#define DEBUGP(ss, fmt, args...) LOGP(ss, LOGL_DEBUG, fmt, ##args)
29#define DEBUGPC(ss, fmt, args...) LOGPC(ss, LOGL_DEBUG, fmt, ##args)
Harald Welte3ae27582010-03-26 21:24:24 +080030#else
31#define DEBUGP(xss, fmt, args...)
32#define DEBUGPC(ss, fmt, args...)
33#endif
34
Harald Welte3ae27582010-03-26 21:24:24 +080035
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +020036void osmo_vlogp(int subsys, int level, const char *file, int line,
Harald Welte36c5a3e2011-08-27 14:33:19 +020037 int cont, const char *format, va_list ap);
38
Maxaa1bc012017-01-13 11:01:12 +010039void 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 +080040
Neels Hofmeyr87e45502017-06-20 00:17:59 +020041/*! Log a new message through the Osmocom logging framework
Harald Weltebd598e32011-08-16 23:26:52 +020042 * \param[in] ss logging subsystem (e.g. \ref DLGLOBAL)
43 * \param[in] level logging level (e.g. \ref LOGL_NOTICE)
44 * \param[in] fmt format string
45 * \param[in] args variable argument list
46 */
Harald Welte3ae27582010-03-26 21:24:24 +080047#define LOGP(ss, level, fmt, args...) \
Neels Hofmeyr725698a2016-12-14 17:24:54 +010048 LOGPSRC(ss, level, NULL, 0, fmt, ## args)
Harald Weltebd598e32011-08-16 23:26:52 +020049
Neels Hofmeyr87e45502017-06-20 00:17:59 +020050/*! Continue a log message through the Osmocom logging framework
Harald Weltebd598e32011-08-16 23:26:52 +020051 * \param[in] ss logging subsystem (e.g. \ref DLGLOBAL)
52 * \param[in] level logging level (e.g. \ref LOGL_NOTICE)
53 * \param[in] fmt format string
54 * \param[in] args variable argument list
55 */
Eric Wild63e1b2b2021-11-26 21:09:01 +010056#ifndef LIBOSMOCORE_NO_LOGGING
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)) \
Neels Hofmeyr983dcb92018-08-20 12:33:22 +020060 logp2(ss, level, __FILE__, __LINE__, 1, fmt, ##args); \
Jacob Erlbecka89d22c2015-11-17 11:52:25 +010061 } while(0)
Eric Wild63e1b2b2021-11-26 21:09:01 +010062#else
63#define LOGPC(ss, level, fmt, args...)
64#endif
Harald Welte3ae27582010-03-26 21:24:24 +080065
Neels Hofmeyr87e45502017-06-20 00:17:59 +020066/*! Log through the Osmocom logging framework with explicit source.
Neels Hofmeyr983dcb92018-08-20 12:33:22 +020067 * If caller_file is passed as NULL, __FILE__ and __LINE__ are used
Neels Hofmeyr725698a2016-12-14 17:24:54 +010068 * instead of caller_file and caller_line (so that this macro here defines
69 * both cases in the same place, and to catch cases where callers fail to pass
70 * a non-null filename string).
71 * \param[in] ss logging subsystem (e.g. \ref DLGLOBAL)
72 * \param[in] level logging level (e.g. \ref LOGL_NOTICE)
Neels Hofmeyr983dcb92018-08-20 12:33:22 +020073 * \param[in] caller_file caller's source file string (e.g. __FILE__)
Neels Hofmeyr725698a2016-12-14 17:24:54 +010074 * \param[in] caller_line caller's source line nr (e.g. __LINE__)
75 * \param[in] fmt format string
76 * \param[in] args variable argument list
77 */
78#define LOGPSRC(ss, level, caller_file, caller_line, fmt, args...) \
Holger Hans Peter Freyther37a83402017-11-29 11:46:39 +080079 LOGPSRCC(ss, level, caller_file, caller_line, 0, fmt, ##args)
80
81/*! Log through the Osmocom logging framework with explicit source.
Neels Hofmeyr983dcb92018-08-20 12:33:22 +020082 * If caller_file is passed as NULL, __FILE__ and __LINE__ are used
Holger Hans Peter Freyther37a83402017-11-29 11:46:39 +080083 * instead of caller_file and caller_line (so that this macro here defines
84 * both cases in the same place, and to catch cases where callers fail to pass
85 * a non-null filename string).
86 * \param[in] ss logging subsystem (e.g. \ref DLGLOBAL)
87 * \param[in] level logging level (e.g. \ref LOGL_NOTICE)
Neels Hofmeyr983dcb92018-08-20 12:33:22 +020088 * \param[in] caller_file caller's source file string (e.g. __FILE__)
Holger Hans Peter Freyther37a83402017-11-29 11:46:39 +080089 * \param[in] caller_line caller's source line nr (e.g. __LINE__)
90 * \param[in] cont continuation (1) or new line (0)
91 * \param[in] fmt format string
92 * \param[in] args variable argument list
93 */
Eric Wild63e1b2b2021-11-26 21:09:01 +010094#ifndef LIBOSMOCORE_NO_LOGGING
Holger Hans Peter Freyther37a83402017-11-29 11:46:39 +080095#define LOGPSRCC(ss, level, caller_file, caller_line, cont, fmt, args...) \
Neels Hofmeyr725698a2016-12-14 17:24:54 +010096 do { \
97 if (log_check_level(ss, level)) {\
98 if (caller_file) \
Holger Hans Peter Freyther37a83402017-11-29 11:46:39 +080099 logp2(ss, level, caller_file, caller_line, cont, fmt, ##args); \
Neels Hofmeyr725698a2016-12-14 17:24:54 +0100100 else \
Neels Hofmeyr983dcb92018-08-20 12:33:22 +0200101 logp2(ss, level, __FILE__, __LINE__, cont, fmt, ##args); \
Neels Hofmeyr725698a2016-12-14 17:24:54 +0100102 }\
103 } while(0)
Eric Wild63e1b2b2021-11-26 21:09:01 +0100104#else
105#define LOGPSRCC(ss, level, caller_file, caller_line, cont, fmt, args...)
106#endif
Neels Hofmeyr725698a2016-12-14 17:24:54 +0100107
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200108/*! different log levels */
109#define LOGL_DEBUG 1 /*!< debugging information */
110#define LOGL_INFO 3 /*!< general information */
111#define LOGL_NOTICE 5 /*!< abnormal/unexpected condition */
112#define LOGL_ERROR 7 /*!< error condition, requires user action */
113#define LOGL_FATAL 8 /*!< fatal, program aborted */
Harald Welte3ae27582010-03-26 21:24:24 +0800114
Neels Hofmeyre883de52019-11-20 04:28:52 +0100115/* logging subsystems defined by the library itself */
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200116#define DLGLOBAL -1 /*!< global logging */
117#define DLLAPD -2 /*!< LAPD implementation */
118#define DLINP -3 /*!< (A-bis) Input sub-system */
119#define DLMUX -4 /*!< Osmocom Multiplex (Osmux) */
120#define DLMI -5 /*!< ISDN-layer below input sub-system */
121#define DLMIB -6 /*!< ISDN layer B-channel */
122#define DLSMS -7 /*!< SMS sub-system */
123#define DLCTRL -8 /*!< Control Interface */
124#define DLGTP -9 /*!< GTP (GPRS Tunneling Protocol */
125#define DLSTATS -10 /*!< Statistics */
Neels Hofmeyr9795cf12016-12-10 17:01:06 +0100126#define DLGSUP -11 /*!< Generic Subscriber Update Protocol */
Harald Weltec0f00072016-04-27 18:32:35 +0200127#define DLOAP -12 /*!< Osmocom Authentication Protocol */
Harald Welte62d32962017-04-03 19:37:11 +0200128#define DLSS7 -13 /*!< Osmocom SS7 */
129#define DLSCCP -14 /*!< Osmocom SCCP */
130#define DLSUA -15 /*!< Osmocom SUA */
131#define DLM3UA -16 /*!< Osmocom M3UA */
Neels Hofmeyra7ccf612017-07-11 18:43:09 +0200132#define DLMGCP -17 /*!< Osmocom MGCP */
Pau Espin Pedrol8fd85572018-02-27 19:43:10 +0100133#define DLJIBUF -18 /*!< Osmocom Jitter Buffer */
Harald Welteb99ed7f2018-08-13 20:54:44 +0200134#define DLRSPRO -19 /*!< Osmocom Remote SIM Protocol */
Alexander Couzens6a161492020-07-12 13:45:50 +0200135#define DLNS -20 /*!< Osmocom NS layer */
Harald Weltefde19ed2020-12-07 21:43:51 +0100136#define DLBSSGP -21 /*!< Osmocom BSSGP layer */
Alexander Couzens6cf65d92021-01-18 17:55:35 +0100137#define DLNSDATA -22 /*!< Osmocom NS layer data pdus */
138#define DLNSSIGNAL -23 /*!< Osmocom NS layer signal pdus */
Harald Welte9fe1f9f2018-11-29 13:47:39 +0100139#define DLIUUP -24 /*!< Osmocom IuUP layer */
Neels Hofmeyr13fa00e2021-11-26 11:02:47 +0100140#define DLPFCP -25 /*!< Osmocom Packet Forwarding Control Protocol */
141#define OSMO_NUM_DLIB 25 /*!< Number of logging sub-systems in libraries */
Harald Welteb43bc042011-06-27 10:29:17 +0200142
Neels Hofmeyrf2644ae2019-11-20 04:00:29 +0100143/* Colors that can be used in log_info_cat.color */
144#define OSMO_LOGCOLOR_NORMAL NULL
145#define OSMO_LOGCOLOR_RED "\033[1;31m"
146#define OSMO_LOGCOLOR_GREEN "\033[1;32m"
147#define OSMO_LOGCOLOR_YELLOW "\033[1;33m"
148#define OSMO_LOGCOLOR_BLUE "\033[1;34m"
149#define OSMO_LOGCOLOR_PURPLE "\033[1;35m"
150#define OSMO_LOGCOLOR_CYAN "\033[1;36m"
151#define OSMO_LOGCOLOR_DARKRED "\033[31m"
152#define OSMO_LOGCOLOR_DARKGREEN "\033[32m"
153#define OSMO_LOGCOLOR_DARKYELLOW "\033[33m"
154#define OSMO_LOGCOLOR_DARKBLUE "\033[34m"
155#define OSMO_LOGCOLOR_DARKPURPLE "\033[35m"
156#define OSMO_LOGCOLOR_DARKCYAN "\033[36m"
157#define OSMO_LOGCOLOR_DARKGREY "\033[1;30m"
158#define OSMO_LOGCOLOR_GREY "\033[37m"
159#define OSMO_LOGCOLOR_BRIGHTWHITE "\033[1;37m"
160#define OSMO_LOGCOLOR_END "\033[0;m"
161
Neels Hofmeyrfc47b032017-06-20 04:29:38 +0200162/*! Configuration of single log category / sub-system */
Harald Welte3ae27582010-03-26 21:24:24 +0800163struct log_category {
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200164 uint8_t loglevel; /*!< configured log-level */
165 uint8_t enabled; /*!< is logging enabled? */
Harald Welte3ae27582010-03-26 21:24:24 +0800166};
167
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200168/*! Information regarding one logging category */
Harald Welte3ae27582010-03-26 21:24:24 +0800169struct log_info_cat {
Harald Welte18fc4652011-08-17 14:14:17 +0200170 const char *name; /*!< name of category */
171 const char *color; /*!< color string for cateyory */
172 const char *description; /*!< description text */
173 uint8_t loglevel; /*!< currently selected log-level */
174 uint8_t enabled; /*!< is this category enabled or not */
Harald Welte3ae27582010-03-26 21:24:24 +0800175};
176
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200177/*! Indexes to indicate the object currently acted upon.
Neels Hofmeyr492e1802017-02-23 17:45:26 +0100178 * Array indexes for the global \a log_context array. */
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100179enum log_ctx_index {
180 LOG_CTX_GB_NSVC,
181 LOG_CTX_GB_BVC,
182 LOG_CTX_BSC_SUBSCR,
183 LOG_CTX_VLR_SUBSCR,
Oliver Smith210acc62019-09-12 17:13:34 +0200184 LOG_CTX_L1_SAPI,
Daniel Willmann751977b2020-12-02 18:59:44 +0100185 LOG_CTX_GB_NSE,
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100186 _LOG_CTX_COUNT
Neels Hofmeyr812ba6d2017-02-17 16:35:27 +0100187};
188
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200189/*! Indexes to indicate objects that should be logged.
Neels Hofmeyr492e1802017-02-23 17:45:26 +0100190 * Array indexes to log_target->filter_data and bit indexes for
191 * log_target->filter_map. */
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100192enum log_filter_index {
193 LOG_FLT_ALL,
194 LOG_FLT_GB_NSVC,
195 LOG_FLT_GB_BVC,
196 LOG_FLT_BSC_SUBSCR,
197 LOG_FLT_VLR_SUBSCR,
Oliver Smith210acc62019-09-12 17:13:34 +0200198 LOG_FLT_L1_SAPI,
Daniel Willmann751977b2020-12-02 18:59:44 +0100199 LOG_FLT_GB_NSE,
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100200 _LOG_FLT_COUNT
Neels Hofmeyr812ba6d2017-02-17 16:35:27 +0100201};
202
Daniel Willmannfd3478c2020-12-02 18:14:17 +0100203/*! Maximum number of logging contexts */
204#define LOG_MAX_CTX _LOG_CTX_COUNT
205/*! Maximum number of logging filters */
206#define LOG_MAX_FILTERS _LOG_FLT_COUNT
207
208/*! Log context information, passed to filter */
209struct log_context {
210 void *ctx[LOG_MAX_CTX+1];
211};
212
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200213/*! Compatibility with older libosmocore versions */
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100214#define LOG_FILTER_ALL (1<<LOG_FLT_ALL)
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200215/*! Compatibility with older libosmocore versions */
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100216#define GPRS_CTX_NSVC LOG_CTX_GB_NSVC
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200217/*! Compatibility with older libosmocore versions */
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100218#define GPRS_CTX_BVC LOG_CTX_GB_BVC
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200219/*! Indexes to indicate the object currently acted upon.
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100220 * Array indexes for the global \a log_context array. */
Neels Hofmeyr0d6420b2017-02-23 17:34:35 +0100221
Harald Welte3ae27582010-03-26 21:24:24 +0800222struct log_target;
223
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200224/*! Log filter function */
Harald Welte3ae27582010-03-26 21:24:24 +0800225typedef int log_filter(const struct log_context *ctx,
226 struct log_target *target);
227
Harald Weltefb84f322013-06-06 07:33:54 +0200228struct log_info;
229struct vty;
Harald Welteaa00f992016-12-02 15:30:02 +0100230struct gsmtap_inst;
Harald Weltefb84f322013-06-06 07:33:54 +0200231
232typedef void log_print_filters(struct vty *vty,
233 const struct log_info *info,
234 const struct log_target *tgt);
235
236typedef void log_save_filters(struct vty *vty,
237 const struct log_info *info,
238 const struct log_target *tgt);
239
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200240/*! Logging configuration, passed to \ref log_init */
Harald Welte3ae27582010-03-26 21:24:24 +0800241struct log_info {
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200242 /* filter callback function */
Harald Welte3ae27582010-03-26 21:24:24 +0800243 log_filter *filter_fn;
244
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200245 /*! per-category information */
Holger Hans Peter Freyther06f64552012-09-11 10:31:29 +0200246 const struct log_info_cat *cat;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200247 /*! total number of categories */
Harald Welte3ae27582010-03-26 21:24:24 +0800248 unsigned int num_cat;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200249 /*! total number of user categories (not library) */
Harald Welteb43bc042011-06-27 10:29:17 +0200250 unsigned int num_cat_user;
Harald Weltefb84f322013-06-06 07:33:54 +0200251
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200252 /*! filter saving function */
Harald Weltefb84f322013-06-06 07:33:54 +0200253 log_save_filters *save_fn;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200254 /*! filter saving function */
Harald Weltefb84f322013-06-06 07:33:54 +0200255 log_print_filters *print_fn;
Harald Welte3ae27582010-03-26 21:24:24 +0800256};
257
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200258/*! Type of logging target */
Harald Welte28222962011-02-18 20:37:04 +0100259enum log_target_type {
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200260 LOG_TGT_TYPE_VTY, /*!< VTY logging */
261 LOG_TGT_TYPE_SYSLOG, /*!< syslog based logging */
262 LOG_TGT_TYPE_FILE, /*!< text file logging */
263 LOG_TGT_TYPE_STDERR, /*!< stderr logging */
264 LOG_TGT_TYPE_STRRB, /*!< osmo_strrb-backed logging */
265 LOG_TGT_TYPE_GSMTAP, /*!< GSMTAP network logging */
Vadim Yanitskiye7bf4352020-09-09 03:36:48 +0700266 LOG_TGT_TYPE_SYSTEMD, /*!< systemd journal logging */
Harald Welte28222962011-02-18 20:37:04 +0100267};
268
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100269/*! Whether/how to log the source filename (and line number). */
270enum log_filename_type {
271 LOG_FILENAME_NONE,
272 LOG_FILENAME_PATH,
Neels Hofmeyr0e2a9432018-01-16 02:49:48 +0100273 LOG_FILENAME_BASENAME,
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100274};
275
Neels Hofmeyr77ae45d2018-08-27 20:32:36 +0200276/*! Where on a log line source file and line should be logged. */
277enum log_filename_pos {
278 LOG_FILENAME_POS_HEADER_END,
279 LOG_FILENAME_POS_LINE_END,
280};
281
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200282/*! structure representing a logging target */
Harald Welte3ae27582010-03-26 21:24:24 +0800283struct log_target {
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200284 struct llist_head entry; /*!< linked list */
Harald Welte3ae27582010-03-26 21:24:24 +0800285
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200286 /*! Internal data for filtering */
Harald Welte3ae27582010-03-26 21:24:24 +0800287 int filter_map;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200288 /*! Internal data for filtering */
Harald Welte3ae27582010-03-26 21:24:24 +0800289 void *filter_data[LOG_MAX_FILTERS+1];
290
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200291 /*! logging categories */
Harald Welteb43bc042011-06-27 10:29:17 +0200292 struct log_category *categories;
293
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200294 /*! global log level */
Harald Welte3ae27582010-03-26 21:24:24 +0800295 uint8_t loglevel;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200296 /*! should color be used when printing log messages? */
Harald Welte87dbca12011-07-16 11:57:53 +0200297 unsigned int use_color:1;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200298 /*! should log messages be prefixed with a timestamp? */
Harald Welte87dbca12011-07-16 11:57:53 +0200299 unsigned int print_timestamp:1;
Pau Espin Pedrol662d10d2021-02-18 18:19:23 +0100300 /*! should log messages be prefixed with the logger Thread ID? */
301 unsigned int print_tid:1;
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100302 /*! DEPRECATED: use print_filename2 instead. */
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200303 unsigned int print_filename:1;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200304 /*! should log messages be prefixed with a category name? */
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100305 unsigned int print_category:1;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200306 /*! should log messages be prefixed with an extended timestamp? */
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100307 unsigned int print_ext_timestamp:1;
Harald Welte3ae27582010-03-26 21:24:24 +0800308
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200309 /*! the type of this log taget */
Harald Welte28222962011-02-18 20:37:04 +0100310 enum log_target_type type;
311
Harald Welte3ae27582010-03-26 21:24:24 +0800312 union {
313 struct {
Harald Welteb72867f2020-09-26 21:45:16 +0200314 /* direct, blocking output via stdio */
Harald Welte3ae27582010-03-26 21:24:24 +0800315 FILE *out;
Harald Welte72d0c532010-08-25 19:24:00 +0200316 const char *fname;
Harald Welteb72867f2020-09-26 21:45:16 +0200317 /* indirect output via write_queue and osmo_select_main() */
318 struct osmo_wqueue *wqueue;
Harald Welte0083cd32010-08-25 14:55:44 +0200319 } tgt_file;
Harald Welte3ae27582010-03-26 21:24:24 +0800320
321 struct {
322 int priority;
Harald Welte28222962011-02-18 20:37:04 +0100323 int facility;
Harald Welte3ae27582010-03-26 21:24:24 +0800324 } tgt_syslog;
325
326 struct {
327 void *vty;
328 } tgt_vty;
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000329
330 struct {
331 void *rb;
332 } tgt_rb;
Harald Welteaa00f992016-12-02 15:30:02 +0100333
334 struct {
335 struct gsmtap_inst *gsmtap_inst;
336 const char *ident;
337 const char *hostname;
338 } tgt_gsmtap;
Vadim Yanitskiye7bf4352020-09-09 03:36:48 +0700339
340 struct {
341 bool raw;
342 } sd_journal;
Harald Welte3ae27582010-03-26 21:24:24 +0800343 };
344
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200345 /*! call-back function to be called when the logging framework
Harald Welted7c0a372016-12-02 13:52:59 +0100346 * wants to log a fully formatted string
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +0100347 * \param[in] target logging target
Harald Welte18fc4652011-08-17 14:14:17 +0200348 * \param[in] level log level of currnet message
349 * \param[in] string the string that is to be written to the log
350 */
Harald Welte76e72ab2011-02-17 15:52:39 +0100351 void (*output) (struct log_target *target, unsigned int level,
352 const char *string);
Harald Welted7c0a372016-12-02 13:52:59 +0100353
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200354 /*! alternative call-back function to which the logging
Harald Welted7c0a372016-12-02 13:52:59 +0100355 * framework passes the unfortmatted input arguments,
356 * i.e. bypassing the internal string formatter
357 * \param[in] target logging target
358 * \param[in] subsys logging sub-system
359 * \param[in] level logging level
360 * \param[in] file soure code file name
361 * \param[in] line source code file line number
362 * \param[in] cont continuation of previous statement?
363 * \param[in] format format string
364 * \param[in] ap vararg list of printf arguments
365 */
366 void (*raw_output)(struct log_target *target, int subsys,
367 unsigned int level, const char *file, int line,
368 int cont, const char *format, va_list ap);
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100369
370 /* Should the log level be printed? */
371 bool print_level;
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100372 /* Should we print the subsys in hex like '<000b>'? */
373 bool print_category_hex;
374 /* Should we print the source file and line, and in which way? */
375 enum log_filename_type print_filename2;
Neels Hofmeyr77ae45d2018-08-27 20:32:36 +0200376 /* Where on a log line to put the source file info. */
377 enum log_filename_pos print_filename_pos;
Harald Welte3ae27582010-03-26 21:24:24 +0800378};
379
380/* use the above macros */
Andreas Eversberg2d6563b2012-07-10 13:10:15 +0200381void logp2(int subsys, unsigned int level, const char *file,
Harald Welte3ae27582010-03-26 21:24:24 +0800382 int line, int cont, const char *format, ...)
383 __attribute__ ((format (printf, 6, 7)));
Harald Welteb43bc042011-06-27 10:29:17 +0200384int log_init(const struct log_info *inf, void *talloc_ctx);
Harald Welte69e6c3c2016-04-20 10:41:27 +0200385void log_fini(void);
Jacob Erlbeckde6dd722015-11-17 11:52:24 +0100386int log_check_level(int subsys, unsigned int level);
Harald Welte3ae27582010-03-26 21:24:24 +0800387
388/* context management */
389void log_reset_context(void);
390int log_set_context(uint8_t ctx, void *value);
391
392/* filter on the targets */
393void log_set_all_filter(struct log_target *target, int);
394
395void log_set_use_color(struct log_target *target, int);
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100396void log_set_print_extended_timestamp(struct log_target *target, int);
Harald Welte3ae27582010-03-26 21:24:24 +0800397void log_set_print_timestamp(struct log_target *target, int);
Pau Espin Pedrol662d10d2021-02-18 18:19:23 +0100398void log_set_print_tid(struct log_target *target, int);
Pau Espin Pedrol6e9dd022021-02-18 19:27:38 +0100399void log_set_print_filename(struct log_target *target, int) OSMO_DEPRECATED("Use log_set_print_filename2() instead");
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100400void log_set_print_filename2(struct log_target *target, enum log_filename_type lft);
Neels Hofmeyr77ae45d2018-08-27 20:32:36 +0200401void log_set_print_filename_pos(struct log_target *target, enum log_filename_pos pos);
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100402void log_set_print_category(struct log_target *target, int);
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100403void log_set_print_category_hex(struct log_target *target, int);
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100404void log_set_print_level(struct log_target *target, int);
Harald Welte3ae27582010-03-26 21:24:24 +0800405void log_set_log_level(struct log_target *target, int log_level);
406void log_parse_category_mask(struct log_target *target, const char* mask);
Harald Welteaa00f992016-12-02 15:30:02 +0100407const char* log_category_name(int subsys);
Max15b6d412017-07-06 16:57:15 +0200408int log_parse_level(const char *lvl) OSMO_DEPRECATED_OUTSIDE_LIBOSMOCORE;
409const char *log_level_str(unsigned int lvl) OSMO_DEPRECATED_OUTSIDE_LIBOSMOCORE;
Harald Welte3ae27582010-03-26 21:24:24 +0800410int log_parse_category(const char *category);
411void log_set_category_filter(struct log_target *target, int category,
412 int enable, int level);
413
414/* management of the targets */
415struct log_target *log_target_create(void);
Harald Welte72d0c532010-08-25 19:24:00 +0200416void log_target_destroy(struct log_target *target);
Harald Welte3ae27582010-03-26 21:24:24 +0800417struct log_target *log_target_create_stderr(void);
Harald Welte72d0c532010-08-25 19:24:00 +0200418struct log_target *log_target_create_file(const char *fname);
Harald Welte46cfd772011-02-17 15:56:56 +0100419struct log_target *log_target_create_syslog(const char *ident, int option,
420 int facility);
Harald Welteaa00f992016-12-02 15:30:02 +0100421struct log_target *log_target_create_gsmtap(const char *host, uint16_t port,
422 const char *ident,
423 bool ofd_wq_mode,
424 bool add_sink);
Vadim Yanitskiye7bf4352020-09-09 03:36:48 +0700425struct log_target *log_target_create_systemd(bool raw);
426void log_target_systemd_set_raw(struct log_target *target, bool raw);
Harald Welte72d0c532010-08-25 19:24:00 +0200427int log_target_file_reopen(struct log_target *tgt);
Harald Welteb72867f2020-09-26 21:45:16 +0200428int log_target_file_switch_to_stream(struct log_target *tgt);
429int log_target_file_switch_to_wqueue(struct log_target *tgt);
Harald Welte4de854d2013-03-18 19:01:40 +0100430int log_targets_reopen(void);
Harald Welte72d0c532010-08-25 19:24:00 +0200431
Harald Welte3ae27582010-03-26 21:24:24 +0800432void log_add_target(struct log_target *target);
433void log_del_target(struct log_target *target);
434
Harald Weltee6fb8902022-01-09 12:00:03 +0100435struct log_target *log_target_find(enum log_target_type type, const char *fname);
Harald Welte28222962011-02-18 20:37:04 +0100436
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200437void log_enable_multithread(void);
438
439void log_tgt_mutex_lock_impl(void);
440void log_tgt_mutex_unlock_impl(void);
441#define LOG_MTX_DEBUG 0
442#if LOG_MTX_DEBUG
443 #include <pthread.h>
444 #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)
445 #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)
446#else
447 #define log_tgt_mutex_lock() log_tgt_mutex_lock_impl()
448 #define log_tgt_mutex_unlock() log_tgt_mutex_unlock_impl()
449#endif
450
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200451/*! @} */