Sylvain Munaut | 12ba778 | 2014-06-16 10:13:40 +0200 | [diff] [blame] | 1 | #pragma once |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 2 | |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 3 | /*! \defgroup logging Osmocom logging framework |
| 4 | * @{ |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 5 | * \file logging.h */ |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 6 | |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 7 | #include <stdio.h> |
| 8 | #include <stdint.h> |
Christoph Fritz | ab7c9c7 | 2011-09-01 16:22:17 +0200 | [diff] [blame] | 9 | #include <stdarg.h> |
Harald Welte | aa00f99 | 2016-12-02 15:30:02 +0100 | [diff] [blame] | 10 | #include <stdbool.h> |
Max | aa1bc01 | 2017-01-13 11:01:12 +0100 | [diff] [blame] | 11 | #include <osmocom/core/defs.h> |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 12 | #include <osmocom/core/linuxlist.h> |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 13 | |
Kévin Redon | d1e220f | 2019-06-13 13:41:00 +0200 | [diff] [blame] | 14 | #ifndef DEBUG |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 15 | #define DEBUG |
Kévin Redon | d1e220f | 2019-06-13 13:41:00 +0200 | [diff] [blame] | 16 | #endif |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 17 | |
| 18 | #ifdef DEBUG |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 19 | /*! Log a debug message through the Osmocom logging framework |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 20 | * \param[in] ss logging subsystem (e.g. \ref DLGLOBAL) |
| 21 | * \param[in] fmt format string |
| 22 | * \param[in] args variable argument list |
| 23 | */ |
Max | aa1bc01 | 2017-01-13 11:01:12 +0100 | [diff] [blame] | 24 | #define DEBUGP(ss, fmt, args...) LOGP(ss, LOGL_DEBUG, fmt, ##args) |
| 25 | #define DEBUGPC(ss, fmt, args...) LOGPC(ss, LOGL_DEBUG, fmt, ##args) |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 26 | #else |
| 27 | #define DEBUGP(xss, fmt, args...) |
| 28 | #define DEBUGPC(ss, fmt, args...) |
| 29 | #endif |
| 30 | |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 31 | |
Holger Hans Peter Freyther | fb4bfc2 | 2012-07-12 09:26:25 +0200 | [diff] [blame] | 32 | void osmo_vlogp(int subsys, int level, const char *file, int line, |
Harald Welte | 36c5a3e | 2011-08-27 14:33:19 +0200 | [diff] [blame] | 33 | int cont, const char *format, va_list ap); |
| 34 | |
Max | aa1bc01 | 2017-01-13 11:01:12 +0100 | [diff] [blame] | 35 | void logp(int subsys, const char *file, int line, int cont, const char *format, ...) OSMO_DEPRECATED("Use DEBUGP* macros instead"); |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 36 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 37 | /*! Log a new message through the Osmocom logging framework |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 38 | * \param[in] ss logging subsystem (e.g. \ref DLGLOBAL) |
| 39 | * \param[in] level logging level (e.g. \ref LOGL_NOTICE) |
| 40 | * \param[in] fmt format string |
| 41 | * \param[in] args variable argument list |
| 42 | */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 43 | #define LOGP(ss, level, fmt, args...) \ |
Neels Hofmeyr | 725698a | 2016-12-14 17:24:54 +0100 | [diff] [blame] | 44 | LOGPSRC(ss, level, NULL, 0, fmt, ## args) |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 45 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 46 | /*! Continue a log message through the Osmocom logging framework |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 47 | * \param[in] ss logging subsystem (e.g. \ref DLGLOBAL) |
| 48 | * \param[in] level logging level (e.g. \ref LOGL_NOTICE) |
| 49 | * \param[in] fmt format string |
| 50 | * \param[in] args variable argument list |
| 51 | */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 52 | #define LOGPC(ss, level, fmt, args...) \ |
Jacob Erlbeck | a89d22c | 2015-11-17 11:52:25 +0100 | [diff] [blame] | 53 | do { \ |
| 54 | if (log_check_level(ss, level)) \ |
Neels Hofmeyr | 983dcb9 | 2018-08-20 12:33:22 +0200 | [diff] [blame] | 55 | logp2(ss, level, __FILE__, __LINE__, 1, fmt, ##args); \ |
Jacob Erlbeck | a89d22c | 2015-11-17 11:52:25 +0100 | [diff] [blame] | 56 | } while(0) |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 57 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 58 | /*! Log through the Osmocom logging framework with explicit source. |
Neels Hofmeyr | 983dcb9 | 2018-08-20 12:33:22 +0200 | [diff] [blame] | 59 | * If caller_file is passed as NULL, __FILE__ and __LINE__ are used |
Neels Hofmeyr | 725698a | 2016-12-14 17:24:54 +0100 | [diff] [blame] | 60 | * instead of caller_file and caller_line (so that this macro here defines |
| 61 | * both cases in the same place, and to catch cases where callers fail to pass |
| 62 | * a non-null filename string). |
| 63 | * \param[in] ss logging subsystem (e.g. \ref DLGLOBAL) |
| 64 | * \param[in] level logging level (e.g. \ref LOGL_NOTICE) |
Neels Hofmeyr | 983dcb9 | 2018-08-20 12:33:22 +0200 | [diff] [blame] | 65 | * \param[in] caller_file caller's source file string (e.g. __FILE__) |
Neels Hofmeyr | 725698a | 2016-12-14 17:24:54 +0100 | [diff] [blame] | 66 | * \param[in] caller_line caller's source line nr (e.g. __LINE__) |
| 67 | * \param[in] fmt format string |
| 68 | * \param[in] args variable argument list |
| 69 | */ |
| 70 | #define LOGPSRC(ss, level, caller_file, caller_line, fmt, args...) \ |
Holger Hans Peter Freyther | 37a8340 | 2017-11-29 11:46:39 +0800 | [diff] [blame] | 71 | LOGPSRCC(ss, level, caller_file, caller_line, 0, fmt, ##args) |
| 72 | |
| 73 | /*! Log through the Osmocom logging framework with explicit source. |
Neels Hofmeyr | 983dcb9 | 2018-08-20 12:33:22 +0200 | [diff] [blame] | 74 | * If caller_file is passed as NULL, __FILE__ and __LINE__ are used |
Holger Hans Peter Freyther | 37a8340 | 2017-11-29 11:46:39 +0800 | [diff] [blame] | 75 | * instead of caller_file and caller_line (so that this macro here defines |
| 76 | * both cases in the same place, and to catch cases where callers fail to pass |
| 77 | * a non-null filename string). |
| 78 | * \param[in] ss logging subsystem (e.g. \ref DLGLOBAL) |
| 79 | * \param[in] level logging level (e.g. \ref LOGL_NOTICE) |
Neels Hofmeyr | 983dcb9 | 2018-08-20 12:33:22 +0200 | [diff] [blame] | 80 | * \param[in] caller_file caller's source file string (e.g. __FILE__) |
Holger Hans Peter Freyther | 37a8340 | 2017-11-29 11:46:39 +0800 | [diff] [blame] | 81 | * \param[in] caller_line caller's source line nr (e.g. __LINE__) |
| 82 | * \param[in] cont continuation (1) or new line (0) |
| 83 | * \param[in] fmt format string |
| 84 | * \param[in] args variable argument list |
| 85 | */ |
| 86 | #define LOGPSRCC(ss, level, caller_file, caller_line, cont, fmt, args...) \ |
Neels Hofmeyr | 725698a | 2016-12-14 17:24:54 +0100 | [diff] [blame] | 87 | do { \ |
| 88 | if (log_check_level(ss, level)) {\ |
| 89 | if (caller_file) \ |
Holger Hans Peter Freyther | 37a8340 | 2017-11-29 11:46:39 +0800 | [diff] [blame] | 90 | logp2(ss, level, caller_file, caller_line, cont, fmt, ##args); \ |
Neels Hofmeyr | 725698a | 2016-12-14 17:24:54 +0100 | [diff] [blame] | 91 | else \ |
Neels Hofmeyr | 983dcb9 | 2018-08-20 12:33:22 +0200 | [diff] [blame] | 92 | logp2(ss, level, __FILE__, __LINE__, cont, fmt, ##args); \ |
Neels Hofmeyr | 725698a | 2016-12-14 17:24:54 +0100 | [diff] [blame] | 93 | }\ |
| 94 | } while(0) |
| 95 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 96 | /*! different log levels */ |
| 97 | #define LOGL_DEBUG 1 /*!< debugging information */ |
| 98 | #define LOGL_INFO 3 /*!< general information */ |
| 99 | #define LOGL_NOTICE 5 /*!< abnormal/unexpected condition */ |
| 100 | #define LOGL_ERROR 7 /*!< error condition, requires user action */ |
| 101 | #define LOGL_FATAL 8 /*!< fatal, program aborted */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 102 | |
Neels Hofmeyr | e883de5 | 2019-11-20 04:28:52 +0100 | [diff] [blame] | 103 | /* logging subsystems defined by the library itself */ |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 104 | #define DLGLOBAL -1 /*!< global logging */ |
| 105 | #define DLLAPD -2 /*!< LAPD implementation */ |
| 106 | #define DLINP -3 /*!< (A-bis) Input sub-system */ |
| 107 | #define DLMUX -4 /*!< Osmocom Multiplex (Osmux) */ |
| 108 | #define DLMI -5 /*!< ISDN-layer below input sub-system */ |
| 109 | #define DLMIB -6 /*!< ISDN layer B-channel */ |
| 110 | #define DLSMS -7 /*!< SMS sub-system */ |
| 111 | #define DLCTRL -8 /*!< Control Interface */ |
| 112 | #define DLGTP -9 /*!< GTP (GPRS Tunneling Protocol */ |
| 113 | #define DLSTATS -10 /*!< Statistics */ |
Neels Hofmeyr | 9795cf1 | 2016-12-10 17:01:06 +0100 | [diff] [blame] | 114 | #define DLGSUP -11 /*!< Generic Subscriber Update Protocol */ |
Harald Welte | c0f0007 | 2016-04-27 18:32:35 +0200 | [diff] [blame] | 115 | #define DLOAP -12 /*!< Osmocom Authentication Protocol */ |
Harald Welte | 62d3296 | 2017-04-03 19:37:11 +0200 | [diff] [blame] | 116 | #define DLSS7 -13 /*!< Osmocom SS7 */ |
| 117 | #define DLSCCP -14 /*!< Osmocom SCCP */ |
| 118 | #define DLSUA -15 /*!< Osmocom SUA */ |
| 119 | #define DLM3UA -16 /*!< Osmocom M3UA */ |
Neels Hofmeyr | a7ccf61 | 2017-07-11 18:43:09 +0200 | [diff] [blame] | 120 | #define DLMGCP -17 /*!< Osmocom MGCP */ |
Pau Espin Pedrol | 8fd8557 | 2018-02-27 19:43:10 +0100 | [diff] [blame] | 121 | #define DLJIBUF -18 /*!< Osmocom Jitter Buffer */ |
Harald Welte | b99ed7f | 2018-08-13 20:54:44 +0200 | [diff] [blame] | 122 | #define DLRSPRO -19 /*!< Osmocom Remote SIM Protocol */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 123 | #define DLNS -20 /*!< Osmocom NS layer */ |
Harald Welte | fde19ed | 2020-12-07 21:43:51 +0100 | [diff] [blame] | 124 | #define DLBSSGP -21 /*!< Osmocom BSSGP layer */ |
Alexander Couzens | 6cf65d9 | 2021-01-18 17:55:35 +0100 | [diff] [blame] | 125 | #define DLNSDATA -22 /*!< Osmocom NS layer data pdus */ |
| 126 | #define DLNSSIGNAL -23 /*!< Osmocom NS layer signal pdus */ |
| 127 | #define OSMO_NUM_DLIB 23 /*!< Number of logging sub-systems in libraries */ |
Harald Welte | b43bc04 | 2011-06-27 10:29:17 +0200 | [diff] [blame] | 128 | |
Neels Hofmeyr | f2644ae | 2019-11-20 04:00:29 +0100 | [diff] [blame] | 129 | /* Colors that can be used in log_info_cat.color */ |
| 130 | #define OSMO_LOGCOLOR_NORMAL NULL |
| 131 | #define OSMO_LOGCOLOR_RED "\033[1;31m" |
| 132 | #define OSMO_LOGCOLOR_GREEN "\033[1;32m" |
| 133 | #define OSMO_LOGCOLOR_YELLOW "\033[1;33m" |
| 134 | #define OSMO_LOGCOLOR_BLUE "\033[1;34m" |
| 135 | #define OSMO_LOGCOLOR_PURPLE "\033[1;35m" |
| 136 | #define OSMO_LOGCOLOR_CYAN "\033[1;36m" |
| 137 | #define OSMO_LOGCOLOR_DARKRED "\033[31m" |
| 138 | #define OSMO_LOGCOLOR_DARKGREEN "\033[32m" |
| 139 | #define OSMO_LOGCOLOR_DARKYELLOW "\033[33m" |
| 140 | #define OSMO_LOGCOLOR_DARKBLUE "\033[34m" |
| 141 | #define OSMO_LOGCOLOR_DARKPURPLE "\033[35m" |
| 142 | #define OSMO_LOGCOLOR_DARKCYAN "\033[36m" |
| 143 | #define OSMO_LOGCOLOR_DARKGREY "\033[1;30m" |
| 144 | #define OSMO_LOGCOLOR_GREY "\033[37m" |
| 145 | #define OSMO_LOGCOLOR_BRIGHTWHITE "\033[1;37m" |
| 146 | #define OSMO_LOGCOLOR_END "\033[0;m" |
| 147 | |
Neels Hofmeyr | fc47b03 | 2017-06-20 04:29:38 +0200 | [diff] [blame] | 148 | /*! Configuration of single log category / sub-system */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 149 | struct log_category { |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 150 | uint8_t loglevel; /*!< configured log-level */ |
| 151 | uint8_t enabled; /*!< is logging enabled? */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 152 | }; |
| 153 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 154 | /*! Information regarding one logging category */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 155 | struct log_info_cat { |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 156 | const char *name; /*!< name of category */ |
| 157 | const char *color; /*!< color string for cateyory */ |
| 158 | const char *description; /*!< description text */ |
| 159 | uint8_t loglevel; /*!< currently selected log-level */ |
| 160 | uint8_t enabled; /*!< is this category enabled or not */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 161 | }; |
| 162 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 163 | /*! Indexes to indicate the object currently acted upon. |
Neels Hofmeyr | 492e180 | 2017-02-23 17:45:26 +0100 | [diff] [blame] | 164 | * Array indexes for the global \a log_context array. */ |
Neels Hofmeyr | 8b86cd7 | 2017-02-23 18:03:28 +0100 | [diff] [blame] | 165 | enum log_ctx_index { |
| 166 | LOG_CTX_GB_NSVC, |
| 167 | LOG_CTX_GB_BVC, |
| 168 | LOG_CTX_BSC_SUBSCR, |
| 169 | LOG_CTX_VLR_SUBSCR, |
Oliver Smith | 210acc6 | 2019-09-12 17:13:34 +0200 | [diff] [blame] | 170 | LOG_CTX_L1_SAPI, |
Daniel Willmann | 751977b | 2020-12-02 18:59:44 +0100 | [diff] [blame] | 171 | LOG_CTX_GB_NSE, |
Neels Hofmeyr | 8b86cd7 | 2017-02-23 18:03:28 +0100 | [diff] [blame] | 172 | _LOG_CTX_COUNT |
Neels Hofmeyr | 812ba6d | 2017-02-17 16:35:27 +0100 | [diff] [blame] | 173 | }; |
| 174 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 175 | /*! Indexes to indicate objects that should be logged. |
Neels Hofmeyr | 492e180 | 2017-02-23 17:45:26 +0100 | [diff] [blame] | 176 | * Array indexes to log_target->filter_data and bit indexes for |
| 177 | * log_target->filter_map. */ |
Neels Hofmeyr | 8b86cd7 | 2017-02-23 18:03:28 +0100 | [diff] [blame] | 178 | enum log_filter_index { |
| 179 | LOG_FLT_ALL, |
| 180 | LOG_FLT_GB_NSVC, |
| 181 | LOG_FLT_GB_BVC, |
| 182 | LOG_FLT_BSC_SUBSCR, |
| 183 | LOG_FLT_VLR_SUBSCR, |
Oliver Smith | 210acc6 | 2019-09-12 17:13:34 +0200 | [diff] [blame] | 184 | LOG_FLT_L1_SAPI, |
Daniel Willmann | 751977b | 2020-12-02 18:59:44 +0100 | [diff] [blame] | 185 | LOG_FLT_GB_NSE, |
Neels Hofmeyr | 8b86cd7 | 2017-02-23 18:03:28 +0100 | [diff] [blame] | 186 | _LOG_FLT_COUNT |
Neels Hofmeyr | 812ba6d | 2017-02-17 16:35:27 +0100 | [diff] [blame] | 187 | }; |
| 188 | |
Daniel Willmann | fd3478c | 2020-12-02 18:14:17 +0100 | [diff] [blame] | 189 | /*! Maximum number of logging contexts */ |
| 190 | #define LOG_MAX_CTX _LOG_CTX_COUNT |
| 191 | /*! Maximum number of logging filters */ |
| 192 | #define LOG_MAX_FILTERS _LOG_FLT_COUNT |
| 193 | |
| 194 | /*! Log context information, passed to filter */ |
| 195 | struct log_context { |
| 196 | void *ctx[LOG_MAX_CTX+1]; |
| 197 | }; |
| 198 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 199 | /*! Compatibility with older libosmocore versions */ |
Neels Hofmeyr | 8b86cd7 | 2017-02-23 18:03:28 +0100 | [diff] [blame] | 200 | #define LOG_FILTER_ALL (1<<LOG_FLT_ALL) |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 201 | /*! Compatibility with older libosmocore versions */ |
Neels Hofmeyr | 8b86cd7 | 2017-02-23 18:03:28 +0100 | [diff] [blame] | 202 | #define GPRS_CTX_NSVC LOG_CTX_GB_NSVC |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 203 | /*! Compatibility with older libosmocore versions */ |
Neels Hofmeyr | 8b86cd7 | 2017-02-23 18:03:28 +0100 | [diff] [blame] | 204 | #define GPRS_CTX_BVC LOG_CTX_GB_BVC |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 205 | /*! Indexes to indicate the object currently acted upon. |
Neels Hofmeyr | 8b86cd7 | 2017-02-23 18:03:28 +0100 | [diff] [blame] | 206 | * Array indexes for the global \a log_context array. */ |
Neels Hofmeyr | 0d6420b | 2017-02-23 17:34:35 +0100 | [diff] [blame] | 207 | |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 208 | struct log_target; |
| 209 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 210 | /*! Log filter function */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 211 | typedef int log_filter(const struct log_context *ctx, |
| 212 | struct log_target *target); |
| 213 | |
Harald Welte | fb84f32 | 2013-06-06 07:33:54 +0200 | [diff] [blame] | 214 | struct log_info; |
| 215 | struct vty; |
Harald Welte | aa00f99 | 2016-12-02 15:30:02 +0100 | [diff] [blame] | 216 | struct gsmtap_inst; |
Harald Welte | fb84f32 | 2013-06-06 07:33:54 +0200 | [diff] [blame] | 217 | |
| 218 | typedef void log_print_filters(struct vty *vty, |
| 219 | const struct log_info *info, |
| 220 | const struct log_target *tgt); |
| 221 | |
| 222 | typedef void log_save_filters(struct vty *vty, |
| 223 | const struct log_info *info, |
| 224 | const struct log_target *tgt); |
| 225 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 226 | /*! Logging configuration, passed to \ref log_init */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 227 | struct log_info { |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 228 | /* filter callback function */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 229 | log_filter *filter_fn; |
| 230 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 231 | /*! per-category information */ |
Holger Hans Peter Freyther | 06f6455 | 2012-09-11 10:31:29 +0200 | [diff] [blame] | 232 | const struct log_info_cat *cat; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 233 | /*! total number of categories */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 234 | unsigned int num_cat; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 235 | /*! total number of user categories (not library) */ |
Harald Welte | b43bc04 | 2011-06-27 10:29:17 +0200 | [diff] [blame] | 236 | unsigned int num_cat_user; |
Harald Welte | fb84f32 | 2013-06-06 07:33:54 +0200 | [diff] [blame] | 237 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 238 | /*! filter saving function */ |
Harald Welte | fb84f32 | 2013-06-06 07:33:54 +0200 | [diff] [blame] | 239 | log_save_filters *save_fn; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 240 | /*! filter saving function */ |
Harald Welte | fb84f32 | 2013-06-06 07:33:54 +0200 | [diff] [blame] | 241 | log_print_filters *print_fn; |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 242 | }; |
| 243 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 244 | /*! Type of logging target */ |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 245 | enum log_target_type { |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 246 | LOG_TGT_TYPE_VTY, /*!< VTY logging */ |
| 247 | LOG_TGT_TYPE_SYSLOG, /*!< syslog based logging */ |
| 248 | LOG_TGT_TYPE_FILE, /*!< text file logging */ |
| 249 | LOG_TGT_TYPE_STDERR, /*!< stderr logging */ |
| 250 | LOG_TGT_TYPE_STRRB, /*!< osmo_strrb-backed logging */ |
| 251 | LOG_TGT_TYPE_GSMTAP, /*!< GSMTAP network logging */ |
Vadim Yanitskiy | e7bf435 | 2020-09-09 03:36:48 +0700 | [diff] [blame] | 252 | LOG_TGT_TYPE_SYSTEMD, /*!< systemd journal logging */ |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 253 | }; |
| 254 | |
Neels Hofmeyr | bd7bd39 | 2018-01-16 01:52:29 +0100 | [diff] [blame] | 255 | /*! Whether/how to log the source filename (and line number). */ |
| 256 | enum log_filename_type { |
| 257 | LOG_FILENAME_NONE, |
| 258 | LOG_FILENAME_PATH, |
Neels Hofmeyr | 0e2a943 | 2018-01-16 02:49:48 +0100 | [diff] [blame] | 259 | LOG_FILENAME_BASENAME, |
Neels Hofmeyr | bd7bd39 | 2018-01-16 01:52:29 +0100 | [diff] [blame] | 260 | }; |
| 261 | |
Neels Hofmeyr | 77ae45d | 2018-08-27 20:32:36 +0200 | [diff] [blame] | 262 | /*! Where on a log line source file and line should be logged. */ |
| 263 | enum log_filename_pos { |
| 264 | LOG_FILENAME_POS_HEADER_END, |
| 265 | LOG_FILENAME_POS_LINE_END, |
| 266 | }; |
| 267 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 268 | /*! structure representing a logging target */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 269 | struct log_target { |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 270 | struct llist_head entry; /*!< linked list */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 271 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 272 | /*! Internal data for filtering */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 273 | int filter_map; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 274 | /*! Internal data for filtering */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 275 | void *filter_data[LOG_MAX_FILTERS+1]; |
| 276 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 277 | /*! logging categories */ |
Harald Welte | b43bc04 | 2011-06-27 10:29:17 +0200 | [diff] [blame] | 278 | struct log_category *categories; |
| 279 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 280 | /*! global log level */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 281 | uint8_t loglevel; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 282 | /*! should color be used when printing log messages? */ |
Harald Welte | 87dbca1 | 2011-07-16 11:57:53 +0200 | [diff] [blame] | 283 | unsigned int use_color:1; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 284 | /*! should log messages be prefixed with a timestamp? */ |
Harald Welte | 87dbca1 | 2011-07-16 11:57:53 +0200 | [diff] [blame] | 285 | unsigned int print_timestamp:1; |
Pau Espin Pedrol | 662d10d | 2021-02-18 18:19:23 +0100 | [diff] [blame] | 286 | /*! should log messages be prefixed with the logger Thread ID? */ |
| 287 | unsigned int print_tid:1; |
Neels Hofmeyr | bd7bd39 | 2018-01-16 01:52:29 +0100 | [diff] [blame] | 288 | /*! DEPRECATED: use print_filename2 instead. */ |
Holger Hans Peter Freyther | db15336 | 2012-09-11 11:24:51 +0200 | [diff] [blame] | 289 | unsigned int print_filename:1; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 290 | /*! should log messages be prefixed with a category name? */ |
Holger Hans Peter Freyther | 2d6ad13 | 2014-12-05 09:35:30 +0100 | [diff] [blame] | 291 | unsigned int print_category:1; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 292 | /*! should log messages be prefixed with an extended timestamp? */ |
Holger Hans Peter Freyther | 2d6ad13 | 2014-12-05 09:35:30 +0100 | [diff] [blame] | 293 | unsigned int print_ext_timestamp:1; |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 294 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 295 | /*! the type of this log taget */ |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 296 | enum log_target_type type; |
| 297 | |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 298 | union { |
| 299 | struct { |
| 300 | FILE *out; |
Harald Welte | 72d0c53 | 2010-08-25 19:24:00 +0200 | [diff] [blame] | 301 | const char *fname; |
Harald Welte | 0083cd3 | 2010-08-25 14:55:44 +0200 | [diff] [blame] | 302 | } tgt_file; |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 303 | |
| 304 | struct { |
| 305 | int priority; |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 306 | int facility; |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 307 | } tgt_syslog; |
| 308 | |
| 309 | struct { |
| 310 | void *vty; |
| 311 | } tgt_vty; |
Katerina Barone-Adesi | 3309a43 | 2013-02-21 05:16:29 +0000 | [diff] [blame] | 312 | |
| 313 | struct { |
| 314 | void *rb; |
| 315 | } tgt_rb; |
Harald Welte | aa00f99 | 2016-12-02 15:30:02 +0100 | [diff] [blame] | 316 | |
| 317 | struct { |
| 318 | struct gsmtap_inst *gsmtap_inst; |
| 319 | const char *ident; |
| 320 | const char *hostname; |
| 321 | } tgt_gsmtap; |
Vadim Yanitskiy | e7bf435 | 2020-09-09 03:36:48 +0700 | [diff] [blame] | 322 | |
| 323 | struct { |
| 324 | bool raw; |
| 325 | } sd_journal; |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 326 | }; |
| 327 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 328 | /*! call-back function to be called when the logging framework |
Harald Welte | d7c0a37 | 2016-12-02 13:52:59 +0100 | [diff] [blame] | 329 | * wants to log a fully formatted string |
Katerina Barone-Adesi | c28c6a0 | 2013-02-15 13:27:59 +0100 | [diff] [blame] | 330 | * \param[in] target logging target |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 331 | * \param[in] level log level of currnet message |
| 332 | * \param[in] string the string that is to be written to the log |
| 333 | */ |
Harald Welte | 76e72ab | 2011-02-17 15:52:39 +0100 | [diff] [blame] | 334 | void (*output) (struct log_target *target, unsigned int level, |
| 335 | const char *string); |
Harald Welte | d7c0a37 | 2016-12-02 13:52:59 +0100 | [diff] [blame] | 336 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 337 | /*! alternative call-back function to which the logging |
Harald Welte | d7c0a37 | 2016-12-02 13:52:59 +0100 | [diff] [blame] | 338 | * framework passes the unfortmatted input arguments, |
| 339 | * i.e. bypassing the internal string formatter |
| 340 | * \param[in] target logging target |
| 341 | * \param[in] subsys logging sub-system |
| 342 | * \param[in] level logging level |
| 343 | * \param[in] file soure code file name |
| 344 | * \param[in] line source code file line number |
| 345 | * \param[in] cont continuation of previous statement? |
| 346 | * \param[in] format format string |
| 347 | * \param[in] ap vararg list of printf arguments |
| 348 | */ |
| 349 | void (*raw_output)(struct log_target *target, int subsys, |
| 350 | unsigned int level, const char *file, int line, |
| 351 | int cont, const char *format, va_list ap); |
Neels Hofmeyr | 886e548 | 2018-01-16 01:49:37 +0100 | [diff] [blame] | 352 | |
| 353 | /* Should the log level be printed? */ |
| 354 | bool print_level; |
Neels Hofmeyr | bd7bd39 | 2018-01-16 01:52:29 +0100 | [diff] [blame] | 355 | /* Should we print the subsys in hex like '<000b>'? */ |
| 356 | bool print_category_hex; |
| 357 | /* Should we print the source file and line, and in which way? */ |
| 358 | enum log_filename_type print_filename2; |
Neels Hofmeyr | 77ae45d | 2018-08-27 20:32:36 +0200 | [diff] [blame] | 359 | /* Where on a log line to put the source file info. */ |
| 360 | enum log_filename_pos print_filename_pos; |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 361 | }; |
| 362 | |
| 363 | /* use the above macros */ |
Andreas Eversberg | 2d6563b | 2012-07-10 13:10:15 +0200 | [diff] [blame] | 364 | void logp2(int subsys, unsigned int level, const char *file, |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 365 | int line, int cont, const char *format, ...) |
| 366 | __attribute__ ((format (printf, 6, 7))); |
Harald Welte | b43bc04 | 2011-06-27 10:29:17 +0200 | [diff] [blame] | 367 | int log_init(const struct log_info *inf, void *talloc_ctx); |
Harald Welte | 69e6c3c | 2016-04-20 10:41:27 +0200 | [diff] [blame] | 368 | void log_fini(void); |
Jacob Erlbeck | de6dd72 | 2015-11-17 11:52:24 +0100 | [diff] [blame] | 369 | int log_check_level(int subsys, unsigned int level); |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 370 | |
| 371 | /* context management */ |
| 372 | void log_reset_context(void); |
| 373 | int log_set_context(uint8_t ctx, void *value); |
| 374 | |
| 375 | /* filter on the targets */ |
| 376 | void log_set_all_filter(struct log_target *target, int); |
| 377 | |
| 378 | void log_set_use_color(struct log_target *target, int); |
Holger Hans Peter Freyther | 2d6ad13 | 2014-12-05 09:35:30 +0100 | [diff] [blame] | 379 | void log_set_print_extended_timestamp(struct log_target *target, int); |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 380 | void log_set_print_timestamp(struct log_target *target, int); |
Pau Espin Pedrol | 662d10d | 2021-02-18 18:19:23 +0100 | [diff] [blame] | 381 | void log_set_print_tid(struct log_target *target, int); |
Pau Espin Pedrol | 6e9dd02 | 2021-02-18 19:27:38 +0100 | [diff] [blame] | 382 | void log_set_print_filename(struct log_target *target, int) OSMO_DEPRECATED("Use log_set_print_filename2() instead"); |
Neels Hofmeyr | bd7bd39 | 2018-01-16 01:52:29 +0100 | [diff] [blame] | 383 | void log_set_print_filename2(struct log_target *target, enum log_filename_type lft); |
Neels Hofmeyr | 77ae45d | 2018-08-27 20:32:36 +0200 | [diff] [blame] | 384 | void log_set_print_filename_pos(struct log_target *target, enum log_filename_pos pos); |
Holger Hans Peter Freyther | 2d6ad13 | 2014-12-05 09:35:30 +0100 | [diff] [blame] | 385 | void log_set_print_category(struct log_target *target, int); |
Neels Hofmeyr | bd7bd39 | 2018-01-16 01:52:29 +0100 | [diff] [blame] | 386 | void log_set_print_category_hex(struct log_target *target, int); |
Neels Hofmeyr | 886e548 | 2018-01-16 01:49:37 +0100 | [diff] [blame] | 387 | void log_set_print_level(struct log_target *target, int); |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 388 | void log_set_log_level(struct log_target *target, int log_level); |
| 389 | void log_parse_category_mask(struct log_target *target, const char* mask); |
Harald Welte | aa00f99 | 2016-12-02 15:30:02 +0100 | [diff] [blame] | 390 | const char* log_category_name(int subsys); |
Max | 15b6d41 | 2017-07-06 16:57:15 +0200 | [diff] [blame] | 391 | int log_parse_level(const char *lvl) OSMO_DEPRECATED_OUTSIDE_LIBOSMOCORE; |
| 392 | const char *log_level_str(unsigned int lvl) OSMO_DEPRECATED_OUTSIDE_LIBOSMOCORE; |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 393 | int log_parse_category(const char *category); |
| 394 | void log_set_category_filter(struct log_target *target, int category, |
| 395 | int enable, int level); |
| 396 | |
| 397 | /* management of the targets */ |
| 398 | struct log_target *log_target_create(void); |
Harald Welte | 72d0c53 | 2010-08-25 19:24:00 +0200 | [diff] [blame] | 399 | void log_target_destroy(struct log_target *target); |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 400 | struct log_target *log_target_create_stderr(void); |
Harald Welte | 72d0c53 | 2010-08-25 19:24:00 +0200 | [diff] [blame] | 401 | struct log_target *log_target_create_file(const char *fname); |
Harald Welte | 46cfd77 | 2011-02-17 15:56:56 +0100 | [diff] [blame] | 402 | struct log_target *log_target_create_syslog(const char *ident, int option, |
| 403 | int facility); |
Harald Welte | aa00f99 | 2016-12-02 15:30:02 +0100 | [diff] [blame] | 404 | struct log_target *log_target_create_gsmtap(const char *host, uint16_t port, |
| 405 | const char *ident, |
| 406 | bool ofd_wq_mode, |
| 407 | bool add_sink); |
Vadim Yanitskiy | e7bf435 | 2020-09-09 03:36:48 +0700 | [diff] [blame] | 408 | struct log_target *log_target_create_systemd(bool raw); |
| 409 | void log_target_systemd_set_raw(struct log_target *target, bool raw); |
Harald Welte | 72d0c53 | 2010-08-25 19:24:00 +0200 | [diff] [blame] | 410 | int log_target_file_reopen(struct log_target *tgt); |
Harald Welte | 4de854d | 2013-03-18 19:01:40 +0100 | [diff] [blame] | 411 | int log_targets_reopen(void); |
Harald Welte | 72d0c53 | 2010-08-25 19:24:00 +0200 | [diff] [blame] | 412 | |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 413 | void log_add_target(struct log_target *target); |
| 414 | void log_del_target(struct log_target *target); |
| 415 | |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 416 | struct log_target *log_target_find(int type, const char *fname); |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 417 | |
Pau Espin Pedrol | d12f698 | 2019-09-17 18:38:58 +0200 | [diff] [blame] | 418 | void log_enable_multithread(void); |
| 419 | |
| 420 | void log_tgt_mutex_lock_impl(void); |
| 421 | void log_tgt_mutex_unlock_impl(void); |
| 422 | #define LOG_MTX_DEBUG 0 |
| 423 | #if LOG_MTX_DEBUG |
| 424 | #include <pthread.h> |
| 425 | #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) |
| 426 | #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) |
| 427 | #else |
| 428 | #define log_tgt_mutex_lock() log_tgt_mutex_lock_impl() |
| 429 | #define log_tgt_mutex_unlock() log_tgt_mutex_unlock_impl() |
| 430 | #endif |
| 431 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 432 | /*! @} */ |