blob: 4a34c7d652c5a731f27c04378bd6f1f1efacd205 [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 */
140#define OSMO_NUM_DLIB 24 /*!< Number of logging sub-systems in libraries */
Harald Welteb43bc042011-06-27 10:29:17 +0200141
Neels Hofmeyrf2644ae2019-11-20 04:00:29 +0100142/* Colors that can be used in log_info_cat.color */
143#define OSMO_LOGCOLOR_NORMAL NULL
144#define OSMO_LOGCOLOR_RED "\033[1;31m"
145#define OSMO_LOGCOLOR_GREEN "\033[1;32m"
146#define OSMO_LOGCOLOR_YELLOW "\033[1;33m"
147#define OSMO_LOGCOLOR_BLUE "\033[1;34m"
148#define OSMO_LOGCOLOR_PURPLE "\033[1;35m"
149#define OSMO_LOGCOLOR_CYAN "\033[1;36m"
150#define OSMO_LOGCOLOR_DARKRED "\033[31m"
151#define OSMO_LOGCOLOR_DARKGREEN "\033[32m"
152#define OSMO_LOGCOLOR_DARKYELLOW "\033[33m"
153#define OSMO_LOGCOLOR_DARKBLUE "\033[34m"
154#define OSMO_LOGCOLOR_DARKPURPLE "\033[35m"
155#define OSMO_LOGCOLOR_DARKCYAN "\033[36m"
156#define OSMO_LOGCOLOR_DARKGREY "\033[1;30m"
157#define OSMO_LOGCOLOR_GREY "\033[37m"
158#define OSMO_LOGCOLOR_BRIGHTWHITE "\033[1;37m"
159#define OSMO_LOGCOLOR_END "\033[0;m"
160
Neels Hofmeyrfc47b032017-06-20 04:29:38 +0200161/*! Configuration of single log category / sub-system */
Harald Welte3ae27582010-03-26 21:24:24 +0800162struct log_category {
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200163 uint8_t loglevel; /*!< configured log-level */
164 uint8_t enabled; /*!< is logging enabled? */
Harald Welte3ae27582010-03-26 21:24:24 +0800165};
166
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200167/*! Information regarding one logging category */
Harald Welte3ae27582010-03-26 21:24:24 +0800168struct log_info_cat {
Harald Welte18fc4652011-08-17 14:14:17 +0200169 const char *name; /*!< name of category */
170 const char *color; /*!< color string for cateyory */
171 const char *description; /*!< description text */
172 uint8_t loglevel; /*!< currently selected log-level */
173 uint8_t enabled; /*!< is this category enabled or not */
Harald Welte3ae27582010-03-26 21:24:24 +0800174};
175
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200176/*! Indexes to indicate the object currently acted upon.
Neels Hofmeyr492e1802017-02-23 17:45:26 +0100177 * Array indexes for the global \a log_context array. */
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100178enum log_ctx_index {
179 LOG_CTX_GB_NSVC,
180 LOG_CTX_GB_BVC,
181 LOG_CTX_BSC_SUBSCR,
182 LOG_CTX_VLR_SUBSCR,
Oliver Smith210acc62019-09-12 17:13:34 +0200183 LOG_CTX_L1_SAPI,
Daniel Willmann751977b2020-12-02 18:59:44 +0100184 LOG_CTX_GB_NSE,
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100185 _LOG_CTX_COUNT
Neels Hofmeyr812ba6d2017-02-17 16:35:27 +0100186};
187
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200188/*! Indexes to indicate objects that should be logged.
Neels Hofmeyr492e1802017-02-23 17:45:26 +0100189 * Array indexes to log_target->filter_data and bit indexes for
190 * log_target->filter_map. */
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100191enum log_filter_index {
192 LOG_FLT_ALL,
193 LOG_FLT_GB_NSVC,
194 LOG_FLT_GB_BVC,
195 LOG_FLT_BSC_SUBSCR,
196 LOG_FLT_VLR_SUBSCR,
Oliver Smith210acc62019-09-12 17:13:34 +0200197 LOG_FLT_L1_SAPI,
Daniel Willmann751977b2020-12-02 18:59:44 +0100198 LOG_FLT_GB_NSE,
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100199 _LOG_FLT_COUNT
Neels Hofmeyr812ba6d2017-02-17 16:35:27 +0100200};
201
Daniel Willmannfd3478c2020-12-02 18:14:17 +0100202/*! Maximum number of logging contexts */
203#define LOG_MAX_CTX _LOG_CTX_COUNT
204/*! Maximum number of logging filters */
205#define LOG_MAX_FILTERS _LOG_FLT_COUNT
206
207/*! Log context information, passed to filter */
208struct log_context {
209 void *ctx[LOG_MAX_CTX+1];
210};
211
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200212/*! Compatibility with older libosmocore versions */
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100213#define LOG_FILTER_ALL (1<<LOG_FLT_ALL)
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200214/*! Compatibility with older libosmocore versions */
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100215#define GPRS_CTX_NSVC LOG_CTX_GB_NSVC
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200216/*! Compatibility with older libosmocore versions */
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100217#define GPRS_CTX_BVC LOG_CTX_GB_BVC
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200218/*! Indexes to indicate the object currently acted upon.
Neels Hofmeyr8b86cd72017-02-23 18:03:28 +0100219 * Array indexes for the global \a log_context array. */
Neels Hofmeyr0d6420b2017-02-23 17:34:35 +0100220
Harald Welte3ae27582010-03-26 21:24:24 +0800221struct log_target;
222
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200223/*! Log filter function */
Harald Welte3ae27582010-03-26 21:24:24 +0800224typedef int log_filter(const struct log_context *ctx,
225 struct log_target *target);
226
Harald Weltefb84f322013-06-06 07:33:54 +0200227struct log_info;
228struct vty;
Harald Welteaa00f992016-12-02 15:30:02 +0100229struct gsmtap_inst;
Harald Weltefb84f322013-06-06 07:33:54 +0200230
231typedef void log_print_filters(struct vty *vty,
232 const struct log_info *info,
233 const struct log_target *tgt);
234
235typedef void log_save_filters(struct vty *vty,
236 const struct log_info *info,
237 const struct log_target *tgt);
238
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200239/*! Logging configuration, passed to \ref log_init */
Harald Welte3ae27582010-03-26 21:24:24 +0800240struct log_info {
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200241 /* filter callback function */
Harald Welte3ae27582010-03-26 21:24:24 +0800242 log_filter *filter_fn;
243
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200244 /*! per-category information */
Holger Hans Peter Freyther06f64552012-09-11 10:31:29 +0200245 const struct log_info_cat *cat;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200246 /*! total number of categories */
Harald Welte3ae27582010-03-26 21:24:24 +0800247 unsigned int num_cat;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200248 /*! total number of user categories (not library) */
Harald Welteb43bc042011-06-27 10:29:17 +0200249 unsigned int num_cat_user;
Harald Weltefb84f322013-06-06 07:33:54 +0200250
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200251 /*! filter saving function */
Harald Weltefb84f322013-06-06 07:33:54 +0200252 log_save_filters *save_fn;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200253 /*! filter saving function */
Harald Weltefb84f322013-06-06 07:33:54 +0200254 log_print_filters *print_fn;
Harald Welte3ae27582010-03-26 21:24:24 +0800255};
256
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200257/*! Type of logging target */
Harald Welte28222962011-02-18 20:37:04 +0100258enum log_target_type {
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200259 LOG_TGT_TYPE_VTY, /*!< VTY logging */
260 LOG_TGT_TYPE_SYSLOG, /*!< syslog based logging */
261 LOG_TGT_TYPE_FILE, /*!< text file logging */
262 LOG_TGT_TYPE_STDERR, /*!< stderr logging */
263 LOG_TGT_TYPE_STRRB, /*!< osmo_strrb-backed logging */
264 LOG_TGT_TYPE_GSMTAP, /*!< GSMTAP network logging */
Vadim Yanitskiye7bf4352020-09-09 03:36:48 +0700265 LOG_TGT_TYPE_SYSTEMD, /*!< systemd journal logging */
Harald Welte28222962011-02-18 20:37:04 +0100266};
267
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100268/*! Whether/how to log the source filename (and line number). */
269enum log_filename_type {
270 LOG_FILENAME_NONE,
271 LOG_FILENAME_PATH,
Neels Hofmeyr0e2a9432018-01-16 02:49:48 +0100272 LOG_FILENAME_BASENAME,
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100273};
274
Neels Hofmeyr77ae45d2018-08-27 20:32:36 +0200275/*! Where on a log line source file and line should be logged. */
276enum log_filename_pos {
277 LOG_FILENAME_POS_HEADER_END,
278 LOG_FILENAME_POS_LINE_END,
279};
280
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200281/*! structure representing a logging target */
Harald Welte3ae27582010-03-26 21:24:24 +0800282struct log_target {
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200283 struct llist_head entry; /*!< linked list */
Harald Welte3ae27582010-03-26 21:24:24 +0800284
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200285 /*! Internal data for filtering */
Harald Welte3ae27582010-03-26 21:24:24 +0800286 int filter_map;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200287 /*! Internal data for filtering */
Harald Welte3ae27582010-03-26 21:24:24 +0800288 void *filter_data[LOG_MAX_FILTERS+1];
289
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200290 /*! logging categories */
Harald Welteb43bc042011-06-27 10:29:17 +0200291 struct log_category *categories;
292
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200293 /*! global log level */
Harald Welte3ae27582010-03-26 21:24:24 +0800294 uint8_t loglevel;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200295 /*! should color be used when printing log messages? */
Harald Welte87dbca12011-07-16 11:57:53 +0200296 unsigned int use_color:1;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200297 /*! should log messages be prefixed with a timestamp? */
Harald Welte87dbca12011-07-16 11:57:53 +0200298 unsigned int print_timestamp:1;
Pau Espin Pedrol662d10d2021-02-18 18:19:23 +0100299 /*! should log messages be prefixed with the logger Thread ID? */
300 unsigned int print_tid:1;
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100301 /*! DEPRECATED: use print_filename2 instead. */
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200302 unsigned int print_filename:1;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200303 /*! should log messages be prefixed with a category name? */
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100304 unsigned int print_category:1;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200305 /*! should log messages be prefixed with an extended timestamp? */
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100306 unsigned int print_ext_timestamp:1;
Harald Welte3ae27582010-03-26 21:24:24 +0800307
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200308 /*! the type of this log taget */
Harald Welte28222962011-02-18 20:37:04 +0100309 enum log_target_type type;
310
Harald Welte3ae27582010-03-26 21:24:24 +0800311 union {
312 struct {
Harald Welteb72867f2020-09-26 21:45:16 +0200313 /* direct, blocking output via stdio */
Harald Welte3ae27582010-03-26 21:24:24 +0800314 FILE *out;
Harald Welte72d0c532010-08-25 19:24:00 +0200315 const char *fname;
Harald Welteb72867f2020-09-26 21:45:16 +0200316 /* indirect output via write_queue and osmo_select_main() */
317 struct osmo_wqueue *wqueue;
Harald Welte0083cd32010-08-25 14:55:44 +0200318 } tgt_file;
Harald Welte3ae27582010-03-26 21:24:24 +0800319
320 struct {
321 int priority;
Harald Welte28222962011-02-18 20:37:04 +0100322 int facility;
Harald Welte3ae27582010-03-26 21:24:24 +0800323 } tgt_syslog;
324
325 struct {
326 void *vty;
327 } tgt_vty;
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000328
329 struct {
330 void *rb;
331 } tgt_rb;
Harald Welteaa00f992016-12-02 15:30:02 +0100332
333 struct {
334 struct gsmtap_inst *gsmtap_inst;
335 const char *ident;
336 const char *hostname;
337 } tgt_gsmtap;
Vadim Yanitskiye7bf4352020-09-09 03:36:48 +0700338
339 struct {
340 bool raw;
341 } sd_journal;
Harald Welte3ae27582010-03-26 21:24:24 +0800342 };
343
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200344 /*! call-back function to be called when the logging framework
Harald Welted7c0a372016-12-02 13:52:59 +0100345 * wants to log a fully formatted string
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +0100346 * \param[in] target logging target
Harald Welte18fc4652011-08-17 14:14:17 +0200347 * \param[in] level log level of currnet message
348 * \param[in] string the string that is to be written to the log
349 */
Harald Welte76e72ab2011-02-17 15:52:39 +0100350 void (*output) (struct log_target *target, unsigned int level,
351 const char *string);
Harald Welted7c0a372016-12-02 13:52:59 +0100352
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200353 /*! alternative call-back function to which the logging
Harald Welted7c0a372016-12-02 13:52:59 +0100354 * framework passes the unfortmatted input arguments,
355 * i.e. bypassing the internal string formatter
356 * \param[in] target logging target
357 * \param[in] subsys logging sub-system
358 * \param[in] level logging level
359 * \param[in] file soure code file name
360 * \param[in] line source code file line number
361 * \param[in] cont continuation of previous statement?
362 * \param[in] format format string
363 * \param[in] ap vararg list of printf arguments
364 */
365 void (*raw_output)(struct log_target *target, int subsys,
366 unsigned int level, const char *file, int line,
367 int cont, const char *format, va_list ap);
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100368
369 /* Should the log level be printed? */
370 bool print_level;
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100371 /* Should we print the subsys in hex like '<000b>'? */
372 bool print_category_hex;
373 /* Should we print the source file and line, and in which way? */
374 enum log_filename_type print_filename2;
Neels Hofmeyr77ae45d2018-08-27 20:32:36 +0200375 /* Where on a log line to put the source file info. */
376 enum log_filename_pos print_filename_pos;
Harald Welte3ae27582010-03-26 21:24:24 +0800377};
378
379/* use the above macros */
Andreas Eversberg2d6563b2012-07-10 13:10:15 +0200380void logp2(int subsys, unsigned int level, const char *file,
Harald Welte3ae27582010-03-26 21:24:24 +0800381 int line, int cont, const char *format, ...)
382 __attribute__ ((format (printf, 6, 7)));
Harald Welteb43bc042011-06-27 10:29:17 +0200383int log_init(const struct log_info *inf, void *talloc_ctx);
Harald Welte69e6c3c2016-04-20 10:41:27 +0200384void log_fini(void);
Jacob Erlbeckde6dd722015-11-17 11:52:24 +0100385int log_check_level(int subsys, unsigned int level);
Harald Welte3ae27582010-03-26 21:24:24 +0800386
387/* context management */
388void log_reset_context(void);
389int log_set_context(uint8_t ctx, void *value);
390
391/* filter on the targets */
392void log_set_all_filter(struct log_target *target, int);
393
394void log_set_use_color(struct log_target *target, int);
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100395void log_set_print_extended_timestamp(struct log_target *target, int);
Harald Welte3ae27582010-03-26 21:24:24 +0800396void log_set_print_timestamp(struct log_target *target, int);
Pau Espin Pedrol662d10d2021-02-18 18:19:23 +0100397void log_set_print_tid(struct log_target *target, int);
Pau Espin Pedrol6e9dd022021-02-18 19:27:38 +0100398void log_set_print_filename(struct log_target *target, int) OSMO_DEPRECATED("Use log_set_print_filename2() instead");
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100399void log_set_print_filename2(struct log_target *target, enum log_filename_type lft);
Neels Hofmeyr77ae45d2018-08-27 20:32:36 +0200400void log_set_print_filename_pos(struct log_target *target, enum log_filename_pos pos);
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100401void log_set_print_category(struct log_target *target, int);
Neels Hofmeyrbd7bd392018-01-16 01:52:29 +0100402void log_set_print_category_hex(struct log_target *target, int);
Neels Hofmeyr886e5482018-01-16 01:49:37 +0100403void log_set_print_level(struct log_target *target, int);
Harald Welte3ae27582010-03-26 21:24:24 +0800404void log_set_log_level(struct log_target *target, int log_level);
405void log_parse_category_mask(struct log_target *target, const char* mask);
Harald Welteaa00f992016-12-02 15:30:02 +0100406const char* log_category_name(int subsys);
Max15b6d412017-07-06 16:57:15 +0200407int log_parse_level(const char *lvl) OSMO_DEPRECATED_OUTSIDE_LIBOSMOCORE;
408const char *log_level_str(unsigned int lvl) OSMO_DEPRECATED_OUTSIDE_LIBOSMOCORE;
Harald Welte3ae27582010-03-26 21:24:24 +0800409int log_parse_category(const char *category);
410void log_set_category_filter(struct log_target *target, int category,
411 int enable, int level);
412
413/* management of the targets */
414struct log_target *log_target_create(void);
Harald Welte72d0c532010-08-25 19:24:00 +0200415void log_target_destroy(struct log_target *target);
Harald Welte3ae27582010-03-26 21:24:24 +0800416struct log_target *log_target_create_stderr(void);
Harald Welte72d0c532010-08-25 19:24:00 +0200417struct log_target *log_target_create_file(const char *fname);
Harald Welte46cfd772011-02-17 15:56:56 +0100418struct log_target *log_target_create_syslog(const char *ident, int option,
419 int facility);
Harald Welteaa00f992016-12-02 15:30:02 +0100420struct log_target *log_target_create_gsmtap(const char *host, uint16_t port,
421 const char *ident,
422 bool ofd_wq_mode,
423 bool add_sink);
Vadim Yanitskiye7bf4352020-09-09 03:36:48 +0700424struct log_target *log_target_create_systemd(bool raw);
425void log_target_systemd_set_raw(struct log_target *target, bool raw);
Harald Welte72d0c532010-08-25 19:24:00 +0200426int log_target_file_reopen(struct log_target *tgt);
Harald Welteb72867f2020-09-26 21:45:16 +0200427int log_target_file_switch_to_stream(struct log_target *tgt);
428int log_target_file_switch_to_wqueue(struct log_target *tgt);
Harald Welte4de854d2013-03-18 19:01:40 +0100429int log_targets_reopen(void);
Harald Welte72d0c532010-08-25 19:24:00 +0200430
Harald Welte3ae27582010-03-26 21:24:24 +0800431void log_add_target(struct log_target *target);
432void log_del_target(struct log_target *target);
433
Harald Weltee6fb8902022-01-09 12:00:03 +0100434struct log_target *log_target_find(enum log_target_type type, const char *fname);
Harald Welte28222962011-02-18 20:37:04 +0100435
Pau Espin Pedrold12f6982019-09-17 18:38:58 +0200436void log_enable_multithread(void);
437
438void log_tgt_mutex_lock_impl(void);
439void log_tgt_mutex_unlock_impl(void);
440#define LOG_MTX_DEBUG 0
441#if LOG_MTX_DEBUG
442 #include <pthread.h>
443 #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)
444 #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)
445#else
446 #define log_tgt_mutex_lock() log_tgt_mutex_lock_impl()
447 #define log_tgt_mutex_unlock() log_tgt_mutex_unlock_impl()
448#endif
449
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200450/*! @} */