blob: 581ebcec66122de0fa4e3f8f554a0c63ffd54959 [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>
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010012#include <osmocom/core/linuxlist.h>
Harald Welte3ae27582010-03-26 21:24:24 +080013
Harald Weltebd598e32011-08-16 23:26:52 +020014/*! \brief Maximum number of logging contexts */
Harald Welte3ae27582010-03-26 21:24:24 +080015#define LOG_MAX_CTX 8
Harald Weltebd598e32011-08-16 23:26:52 +020016/*! \brief Maximum number of logging filters */
Harald Welte3ae27582010-03-26 21:24:24 +080017#define LOG_MAX_FILTERS 8
18
19#define DEBUG
20
21#ifdef DEBUG
Harald Welte2d2e2cc2016-04-25 12:11:20 +020022/*! \brief Log a debug message through the Osmocom logging framework
23 * \param[in] ss logging subsystem (e.g. \ref DLGLOBAL)
24 * \param[in] fmt format string
25 * \param[in] args variable argument list
26 */
Jacob Erlbecka89d22c2015-11-17 11:52:25 +010027#define DEBUGP(ss, fmt, args...) \
28 do { \
29 if (log_check_level(ss, LOGL_DEBUG)) \
Harald Welte3d792402016-05-10 15:23:06 +020030 logp(ss, __BASE_FILE__, __LINE__, 0, fmt, ## args); \
Jacob Erlbecka89d22c2015-11-17 11:52:25 +010031 } while(0)
32
33#define DEBUGPC(ss, fmt, args...) \
34 do { \
35 if (log_check_level(ss, LOGL_DEBUG)) \
Harald Welte3d792402016-05-10 15:23:06 +020036 logp(ss, __BASE_FILE__, __LINE__, 1, fmt, ## args); \
Jacob Erlbecka89d22c2015-11-17 11:52:25 +010037 } while(0)
38
Harald Welte3ae27582010-03-26 21:24:24 +080039#else
40#define DEBUGP(xss, fmt, args...)
41#define DEBUGPC(ss, fmt, args...)
42#endif
43
Harald Welte3ae27582010-03-26 21:24:24 +080044
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +020045void osmo_vlogp(int subsys, int level, const char *file, int line,
Harald Welte36c5a3e2011-08-27 14:33:19 +020046 int cont, const char *format, va_list ap);
47
Andreas Eversberg2d6563b2012-07-10 13:10:15 +020048void logp(int subsys, const char *file, int line, int cont, const char *format, ...) __attribute__ ((format (printf, 5, 6)));
Harald Welte3ae27582010-03-26 21:24:24 +080049
Harald Weltebd598e32011-08-16 23:26:52 +020050/*! \brief Log a new message through the Osmocom logging framework
51 * \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 */
Harald Welte3ae27582010-03-26 21:24:24 +080056#define LOGP(ss, level, fmt, args...) \
Jacob Erlbecka89d22c2015-11-17 11:52:25 +010057 do { \
58 if (log_check_level(ss, level)) \
Harald Welte3d792402016-05-10 15:23:06 +020059 logp2(ss, level, __BASE_FILE__, __LINE__, 0, fmt, ##args); \
Jacob Erlbecka89d22c2015-11-17 11:52:25 +010060 } while(0)
Harald Weltebd598e32011-08-16 23:26:52 +020061
62/*! \brief Continue a log message through the Osmocom logging framework
63 * \param[in] ss logging subsystem (e.g. \ref DLGLOBAL)
64 * \param[in] level logging level (e.g. \ref LOGL_NOTICE)
65 * \param[in] fmt format string
66 * \param[in] args variable argument list
67 */
Harald Welte3ae27582010-03-26 21:24:24 +080068#define LOGPC(ss, level, fmt, args...) \
Jacob Erlbecka89d22c2015-11-17 11:52:25 +010069 do { \
70 if (log_check_level(ss, level)) \
Harald Welte3d792402016-05-10 15:23:06 +020071 logp2(ss, level, __BASE_FILE__, __LINE__, 1, fmt, ##args); \
Jacob Erlbecka89d22c2015-11-17 11:52:25 +010072 } while(0)
Harald Welte3ae27582010-03-26 21:24:24 +080073
Harald Welte18fc4652011-08-17 14:14:17 +020074/*! \brief different log levels */
75#define LOGL_DEBUG 1 /*!< \brief debugging information */
Harald Welte2d2e2cc2016-04-25 12:11:20 +020076#define LOGL_INFO 3 /*!< \brief general information */
Harald Welte18fc4652011-08-17 14:14:17 +020077#define LOGL_NOTICE 5 /*!< \brief abnormal/unexpected condition */
78#define LOGL_ERROR 7 /*!< \brief error condition, requires user action */
79#define LOGL_FATAL 8 /*!< \brief fatal, program aborted */
Harald Welte3ae27582010-03-26 21:24:24 +080080
81#define LOG_FILTER_ALL 0x0001
82
Harald Welteb43bc042011-06-27 10:29:17 +020083/* logging levels defined by the library itself */
Harald Welte2d2e2cc2016-04-25 12:11:20 +020084#define DLGLOBAL -1 /*!< global logging */
85#define DLLAPD -2 /*!< LAPD implementation */
86#define DLINP -3 /*!< (A-bis) Input sub-system */
87#define DLMUX -4 /*!< Osmocom Multiplex (Osmux) */
88#define DLMI -5 /*!< ISDN-layer below input sub-system */
89#define DLMIB -6 /*!< ISDN layer B-channel */
90#define DLSMS -7 /*!< SMS sub-system */
91#define DLCTRL -8 /*!< Control Interface */
92#define DLGTP -9 /*!< GTP (GPRS Tunneling Protocol */
93#define DLSTATS -10 /*!< Statistics */
Harald Welte3b6fb082016-04-25 18:46:22 +020094#define DLGSUP 11 /*!< Generic Subscriber Update Protocol */
95#define OSMO_NUM_DLIB 11 /*!< Number of logging sub-systems in libraries */
Harald Welteb43bc042011-06-27 10:29:17 +020096
Harald Welte2d2e2cc2016-04-25 12:11:20 +020097/*! Configuration of singgle log category / sub-system */
Harald Welte3ae27582010-03-26 21:24:24 +080098struct log_category {
Harald Welte2d2e2cc2016-04-25 12:11:20 +020099 uint8_t loglevel; /*!< configured log-level */
100 uint8_t enabled; /*!< is logging enabled? */
Harald Welte3ae27582010-03-26 21:24:24 +0800101};
102
Harald Welte18fc4652011-08-17 14:14:17 +0200103/*! \brief Information regarding one logging category */
Harald Welte3ae27582010-03-26 21:24:24 +0800104struct log_info_cat {
Harald Welte18fc4652011-08-17 14:14:17 +0200105 const char *name; /*!< name of category */
106 const char *color; /*!< color string for cateyory */
107 const char *description; /*!< description text */
108 uint8_t loglevel; /*!< currently selected log-level */
109 uint8_t enabled; /*!< is this category enabled or not */
Harald Welte3ae27582010-03-26 21:24:24 +0800110};
111
Harald Welte18fc4652011-08-17 14:14:17 +0200112/*! \brief Log context information, passed to filter */
Harald Welte3ae27582010-03-26 21:24:24 +0800113struct log_context {
114 void *ctx[LOG_MAX_CTX+1];
115};
116
117struct log_target;
118
Harald Welte18fc4652011-08-17 14:14:17 +0200119/*! \brief Log filter function */
Harald Welte3ae27582010-03-26 21:24:24 +0800120typedef int log_filter(const struct log_context *ctx,
121 struct log_target *target);
122
Harald Weltefb84f322013-06-06 07:33:54 +0200123struct log_info;
124struct vty;
125
126typedef void log_print_filters(struct vty *vty,
127 const struct log_info *info,
128 const struct log_target *tgt);
129
130typedef void log_save_filters(struct vty *vty,
131 const struct log_info *info,
132 const struct log_target *tgt);
133
Harald Welte18fc4652011-08-17 14:14:17 +0200134/*! \brief Logging configuration, passed to \ref log_init */
Harald Welte3ae27582010-03-26 21:24:24 +0800135struct log_info {
Harald Welte18fc4652011-08-17 14:14:17 +0200136 /* \brief filter callback function */
Harald Welte3ae27582010-03-26 21:24:24 +0800137 log_filter *filter_fn;
138
Harald Welte18fc4652011-08-17 14:14:17 +0200139 /*! \brief per-category information */
Holger Hans Peter Freyther06f64552012-09-11 10:31:29 +0200140 const struct log_info_cat *cat;
Harald Welte18fc4652011-08-17 14:14:17 +0200141 /*! \brief total number of categories */
Harald Welte3ae27582010-03-26 21:24:24 +0800142 unsigned int num_cat;
Harald Welte18fc4652011-08-17 14:14:17 +0200143 /*! \brief total number of user categories (not library) */
Harald Welteb43bc042011-06-27 10:29:17 +0200144 unsigned int num_cat_user;
Harald Weltefb84f322013-06-06 07:33:54 +0200145
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200146 /*! \brief filter saving function */
Harald Weltefb84f322013-06-06 07:33:54 +0200147 log_save_filters *save_fn;
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200148 /*! \brief filter saving function */
Harald Weltefb84f322013-06-06 07:33:54 +0200149 log_print_filters *print_fn;
Harald Welte3ae27582010-03-26 21:24:24 +0800150};
151
Harald Welte18fc4652011-08-17 14:14:17 +0200152/*! \brief Type of logging target */
Harald Welte28222962011-02-18 20:37:04 +0100153enum log_target_type {
Harald Welte18fc4652011-08-17 14:14:17 +0200154 LOG_TGT_TYPE_VTY, /*!< \brief VTY logging */
155 LOG_TGT_TYPE_SYSLOG, /*!< \brief syslog based logging */
156 LOG_TGT_TYPE_FILE, /*!< \brief text file logging */
157 LOG_TGT_TYPE_STDERR, /*!< \brief stderr logging */
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000158 LOG_TGT_TYPE_STRRB, /*!< \brief osmo_strrb-backed logging */
Harald Welte28222962011-02-18 20:37:04 +0100159};
160
Harald Welte18fc4652011-08-17 14:14:17 +0200161/*! \brief structure representing a logging target */
Harald Welte3ae27582010-03-26 21:24:24 +0800162struct log_target {
Harald Welte18fc4652011-08-17 14:14:17 +0200163 struct llist_head entry; /*!< \brief linked list */
Harald Welte3ae27582010-03-26 21:24:24 +0800164
Harald Welte18fc4652011-08-17 14:14:17 +0200165 /*! \brief Internal data for filtering */
Harald Welte3ae27582010-03-26 21:24:24 +0800166 int filter_map;
Harald Welte18fc4652011-08-17 14:14:17 +0200167 /*! \brief Internal data for filtering */
Harald Welte3ae27582010-03-26 21:24:24 +0800168 void *filter_data[LOG_MAX_FILTERS+1];
169
Harald Welte18fc4652011-08-17 14:14:17 +0200170 /*! \brief logging categories */
Harald Welteb43bc042011-06-27 10:29:17 +0200171 struct log_category *categories;
172
Harald Welte18fc4652011-08-17 14:14:17 +0200173 /*! \brief global log level */
Harald Welte3ae27582010-03-26 21:24:24 +0800174 uint8_t loglevel;
Harald Welte18fc4652011-08-17 14:14:17 +0200175 /*! \brief should color be used when printing log messages? */
Harald Welte87dbca12011-07-16 11:57:53 +0200176 unsigned int use_color:1;
Harald Welte18fc4652011-08-17 14:14:17 +0200177 /*! \brief should log messages be prefixed with a timestamp? */
Harald Welte87dbca12011-07-16 11:57:53 +0200178 unsigned int print_timestamp:1;
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200179 /*! \brief should log messages be prefixed with a filename? */
180 unsigned int print_filename:1;
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100181 /*! \brief should log messages be prefixed with a category name? */
182 unsigned int print_category:1;
183 /*! \brief should log messages be prefixed with an extended timestamp? */
184 unsigned int print_ext_timestamp:1;
Harald Welte3ae27582010-03-26 21:24:24 +0800185
Harald Welte18fc4652011-08-17 14:14:17 +0200186 /*! \brief the type of this log taget */
Harald Welte28222962011-02-18 20:37:04 +0100187 enum log_target_type type;
188
Harald Welte3ae27582010-03-26 21:24:24 +0800189 union {
190 struct {
191 FILE *out;
Harald Welte72d0c532010-08-25 19:24:00 +0200192 const char *fname;
Harald Welte0083cd32010-08-25 14:55:44 +0200193 } tgt_file;
Harald Welte3ae27582010-03-26 21:24:24 +0800194
195 struct {
196 int priority;
Harald Welte28222962011-02-18 20:37:04 +0100197 int facility;
Harald Welte3ae27582010-03-26 21:24:24 +0800198 } tgt_syslog;
199
200 struct {
201 void *vty;
202 } tgt_vty;
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000203
204 struct {
205 void *rb;
206 } tgt_rb;
Harald Welte3ae27582010-03-26 21:24:24 +0800207 };
208
Harald Welte18fc4652011-08-17 14:14:17 +0200209 /*! \brief call-back function to be called when the logging framework
Harald Welted7c0a372016-12-02 13:52:59 +0100210 * wants to log a fully formatted string
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +0100211 * \param[in] target logging target
Harald Welte18fc4652011-08-17 14:14:17 +0200212 * \param[in] level log level of currnet message
213 * \param[in] string the string that is to be written to the log
214 */
Harald Welte76e72ab2011-02-17 15:52:39 +0100215 void (*output) (struct log_target *target, unsigned int level,
216 const char *string);
Harald Welted7c0a372016-12-02 13:52:59 +0100217
218 /*! \brief alternative call-back function to which the logging
219 * framework passes the unfortmatted input arguments,
220 * i.e. bypassing the internal string formatter
221 * \param[in] target logging target
222 * \param[in] subsys logging sub-system
223 * \param[in] level logging level
224 * \param[in] file soure code file name
225 * \param[in] line source code file line number
226 * \param[in] cont continuation of previous statement?
227 * \param[in] format format string
228 * \param[in] ap vararg list of printf arguments
229 */
230 void (*raw_output)(struct log_target *target, int subsys,
231 unsigned int level, const char *file, int line,
232 int cont, const char *format, va_list ap);
Harald Welte3ae27582010-03-26 21:24:24 +0800233};
234
235/* use the above macros */
Andreas Eversberg2d6563b2012-07-10 13:10:15 +0200236void logp2(int subsys, unsigned int level, const char *file,
Harald Welte3ae27582010-03-26 21:24:24 +0800237 int line, int cont, const char *format, ...)
238 __attribute__ ((format (printf, 6, 7)));
Harald Welteb43bc042011-06-27 10:29:17 +0200239int log_init(const struct log_info *inf, void *talloc_ctx);
Harald Welte69e6c3c2016-04-20 10:41:27 +0200240void log_fini(void);
Jacob Erlbeckde6dd722015-11-17 11:52:24 +0100241int log_check_level(int subsys, unsigned int level);
Harald Welte3ae27582010-03-26 21:24:24 +0800242
243/* context management */
244void log_reset_context(void);
245int log_set_context(uint8_t ctx, void *value);
246
247/* filter on the targets */
248void log_set_all_filter(struct log_target *target, int);
249
250void log_set_use_color(struct log_target *target, int);
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100251void log_set_print_extended_timestamp(struct log_target *target, int);
Harald Welte3ae27582010-03-26 21:24:24 +0800252void log_set_print_timestamp(struct log_target *target, int);
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200253void log_set_print_filename(struct log_target *target, int);
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100254void log_set_print_category(struct log_target *target, int);
Harald Welte3ae27582010-03-26 21:24:24 +0800255void log_set_log_level(struct log_target *target, int log_level);
256void log_parse_category_mask(struct log_target *target, const char* mask);
257int log_parse_level(const char *lvl);
Harald Welte9ac22252010-05-11 11:19:40 +0200258const char *log_level_str(unsigned int lvl);
Harald Welte3ae27582010-03-26 21:24:24 +0800259int log_parse_category(const char *category);
260void log_set_category_filter(struct log_target *target, int category,
261 int enable, int level);
262
263/* management of the targets */
264struct log_target *log_target_create(void);
Harald Welte72d0c532010-08-25 19:24:00 +0200265void log_target_destroy(struct log_target *target);
Harald Welte3ae27582010-03-26 21:24:24 +0800266struct log_target *log_target_create_stderr(void);
Harald Welte72d0c532010-08-25 19:24:00 +0200267struct log_target *log_target_create_file(const char *fname);
Harald Welte46cfd772011-02-17 15:56:56 +0100268struct log_target *log_target_create_syslog(const char *ident, int option,
269 int facility);
Harald Welte72d0c532010-08-25 19:24:00 +0200270int log_target_file_reopen(struct log_target *tgt);
Harald Welte4de854d2013-03-18 19:01:40 +0100271int log_targets_reopen(void);
Harald Welte72d0c532010-08-25 19:24:00 +0200272
Harald Welte3ae27582010-03-26 21:24:24 +0800273void log_add_target(struct log_target *target);
274void log_del_target(struct log_target *target);
275
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100276/* Generate command string for VTY use */
277const char *log_vty_command_string(const struct log_info *info);
278const char *log_vty_command_description(const struct log_info *info);
Harald Welte7638af92010-05-11 16:39:22 +0200279
Harald Welte28222962011-02-18 20:37:04 +0100280struct log_target *log_target_find(int type, const char *fname);
281extern struct llist_head osmo_log_target_list;
282
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200283/*! @} */