blob: 4b79c054e8879a72649a0fbad9473d0365a8d4ab [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)) \
30 logp(ss, __FILE__, __LINE__, 0, fmt, ## args); \
31 } while(0)
32
33#define DEBUGPC(ss, fmt, args...) \
34 do { \
35 if (log_check_level(ss, LOGL_DEBUG)) \
36 logp(ss, __FILE__, __LINE__, 1, fmt, ## args); \
37 } 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)) \
59 logp2(ss, level, __FILE__, __LINE__, 0, fmt, ##args); \
60 } 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)) \
71 logp2(ss, level, __FILE__, __LINE__, 1, fmt, ##args); \
72 } 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 */
94#define OSMO_NUM_DLIB 10 /*!< Number of logging sub-systems in libraries */
Harald Welteb43bc042011-06-27 10:29:17 +020095
Harald Welte2d2e2cc2016-04-25 12:11:20 +020096/*! Configuration of singgle log category / sub-system */
Harald Welte3ae27582010-03-26 21:24:24 +080097struct log_category {
Harald Welte2d2e2cc2016-04-25 12:11:20 +020098 uint8_t loglevel; /*!< configured log-level */
99 uint8_t enabled; /*!< is logging enabled? */
Harald Welte3ae27582010-03-26 21:24:24 +0800100};
101
Harald Welte18fc4652011-08-17 14:14:17 +0200102/*! \brief Information regarding one logging category */
Harald Welte3ae27582010-03-26 21:24:24 +0800103struct log_info_cat {
Harald Welte18fc4652011-08-17 14:14:17 +0200104 const char *name; /*!< name of category */
105 const char *color; /*!< color string for cateyory */
106 const char *description; /*!< description text */
107 uint8_t loglevel; /*!< currently selected log-level */
108 uint8_t enabled; /*!< is this category enabled or not */
Harald Welte3ae27582010-03-26 21:24:24 +0800109};
110
Harald Welte18fc4652011-08-17 14:14:17 +0200111/*! \brief Log context information, passed to filter */
Harald Welte3ae27582010-03-26 21:24:24 +0800112struct log_context {
113 void *ctx[LOG_MAX_CTX+1];
114};
115
116struct log_target;
117
Harald Welte18fc4652011-08-17 14:14:17 +0200118/*! \brief Log filter function */
Harald Welte3ae27582010-03-26 21:24:24 +0800119typedef int log_filter(const struct log_context *ctx,
120 struct log_target *target);
121
Harald Weltefb84f322013-06-06 07:33:54 +0200122struct log_info;
123struct vty;
124
125typedef void log_print_filters(struct vty *vty,
126 const struct log_info *info,
127 const struct log_target *tgt);
128
129typedef void log_save_filters(struct vty *vty,
130 const struct log_info *info,
131 const struct log_target *tgt);
132
Harald Welte18fc4652011-08-17 14:14:17 +0200133/*! \brief Logging configuration, passed to \ref log_init */
Harald Welte3ae27582010-03-26 21:24:24 +0800134struct log_info {
Harald Welte18fc4652011-08-17 14:14:17 +0200135 /* \brief filter callback function */
Harald Welte3ae27582010-03-26 21:24:24 +0800136 log_filter *filter_fn;
137
Harald Welte18fc4652011-08-17 14:14:17 +0200138 /*! \brief per-category information */
Holger Hans Peter Freyther06f64552012-09-11 10:31:29 +0200139 const struct log_info_cat *cat;
Harald Welte18fc4652011-08-17 14:14:17 +0200140 /*! \brief total number of categories */
Harald Welte3ae27582010-03-26 21:24:24 +0800141 unsigned int num_cat;
Harald Welte18fc4652011-08-17 14:14:17 +0200142 /*! \brief total number of user categories (not library) */
Harald Welteb43bc042011-06-27 10:29:17 +0200143 unsigned int num_cat_user;
Harald Weltefb84f322013-06-06 07:33:54 +0200144
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200145 /*! \brief filter saving function */
Harald Weltefb84f322013-06-06 07:33:54 +0200146 log_save_filters *save_fn;
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200147 /*! \brief filter saving function */
Harald Weltefb84f322013-06-06 07:33:54 +0200148 log_print_filters *print_fn;
Harald Welte3ae27582010-03-26 21:24:24 +0800149};
150
Harald Welte18fc4652011-08-17 14:14:17 +0200151/*! \brief Type of logging target */
Harald Welte28222962011-02-18 20:37:04 +0100152enum log_target_type {
Harald Welte18fc4652011-08-17 14:14:17 +0200153 LOG_TGT_TYPE_VTY, /*!< \brief VTY logging */
154 LOG_TGT_TYPE_SYSLOG, /*!< \brief syslog based logging */
155 LOG_TGT_TYPE_FILE, /*!< \brief text file logging */
156 LOG_TGT_TYPE_STDERR, /*!< \brief stderr logging */
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000157 LOG_TGT_TYPE_STRRB, /*!< \brief osmo_strrb-backed logging */
Harald Welte28222962011-02-18 20:37:04 +0100158};
159
Harald Welte18fc4652011-08-17 14:14:17 +0200160/*! \brief structure representing a logging target */
Harald Welte3ae27582010-03-26 21:24:24 +0800161struct log_target {
Harald Welte18fc4652011-08-17 14:14:17 +0200162 struct llist_head entry; /*!< \brief linked list */
Harald Welte3ae27582010-03-26 21:24:24 +0800163
Harald Welte18fc4652011-08-17 14:14:17 +0200164 /*! \brief Internal data for filtering */
Harald Welte3ae27582010-03-26 21:24:24 +0800165 int filter_map;
Harald Welte18fc4652011-08-17 14:14:17 +0200166 /*! \brief Internal data for filtering */
Harald Welte3ae27582010-03-26 21:24:24 +0800167 void *filter_data[LOG_MAX_FILTERS+1];
168
Harald Welte18fc4652011-08-17 14:14:17 +0200169 /*! \brief logging categories */
Harald Welteb43bc042011-06-27 10:29:17 +0200170 struct log_category *categories;
171
Harald Welte18fc4652011-08-17 14:14:17 +0200172 /*! \brief global log level */
Harald Welte3ae27582010-03-26 21:24:24 +0800173 uint8_t loglevel;
Harald Welte18fc4652011-08-17 14:14:17 +0200174 /*! \brief should color be used when printing log messages? */
Harald Welte87dbca12011-07-16 11:57:53 +0200175 unsigned int use_color:1;
Harald Welte18fc4652011-08-17 14:14:17 +0200176 /*! \brief should log messages be prefixed with a timestamp? */
Harald Welte87dbca12011-07-16 11:57:53 +0200177 unsigned int print_timestamp:1;
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200178 /*! \brief should log messages be prefixed with a filename? */
179 unsigned int print_filename:1;
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100180 /*! \brief should log messages be prefixed with a category name? */
181 unsigned int print_category:1;
182 /*! \brief should log messages be prefixed with an extended timestamp? */
183 unsigned int print_ext_timestamp:1;
Harald Welte3ae27582010-03-26 21:24:24 +0800184
Harald Welte18fc4652011-08-17 14:14:17 +0200185 /*! \brief the type of this log taget */
Harald Welte28222962011-02-18 20:37:04 +0100186 enum log_target_type type;
187
Harald Welte3ae27582010-03-26 21:24:24 +0800188 union {
189 struct {
190 FILE *out;
Harald Welte72d0c532010-08-25 19:24:00 +0200191 const char *fname;
Harald Welte0083cd32010-08-25 14:55:44 +0200192 } tgt_file;
Harald Welte3ae27582010-03-26 21:24:24 +0800193
194 struct {
195 int priority;
Harald Welte28222962011-02-18 20:37:04 +0100196 int facility;
Harald Welte3ae27582010-03-26 21:24:24 +0800197 } tgt_syslog;
198
199 struct {
200 void *vty;
201 } tgt_vty;
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000202
203 struct {
204 void *rb;
205 } tgt_rb;
Harald Welte3ae27582010-03-26 21:24:24 +0800206 };
207
Harald Welte18fc4652011-08-17 14:14:17 +0200208 /*! \brief call-back function to be called when the logging framework
209 * wants to log somethnig.
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +0100210 * \param[in] target logging target
Harald Welte18fc4652011-08-17 14:14:17 +0200211 * \param[in] level log level of currnet message
212 * \param[in] string the string that is to be written to the log
213 */
Harald Welte76e72ab2011-02-17 15:52:39 +0100214 void (*output) (struct log_target *target, unsigned int level,
215 const char *string);
Harald Welte3ae27582010-03-26 21:24:24 +0800216};
217
218/* use the above macros */
Andreas Eversberg2d6563b2012-07-10 13:10:15 +0200219void logp2(int subsys, unsigned int level, const char *file,
Harald Welte3ae27582010-03-26 21:24:24 +0800220 int line, int cont, const char *format, ...)
221 __attribute__ ((format (printf, 6, 7)));
Harald Welteb43bc042011-06-27 10:29:17 +0200222int log_init(const struct log_info *inf, void *talloc_ctx);
Harald Welte69e6c3c2016-04-20 10:41:27 +0200223void log_fini(void);
Jacob Erlbeckde6dd722015-11-17 11:52:24 +0100224int log_check_level(int subsys, unsigned int level);
Harald Welte3ae27582010-03-26 21:24:24 +0800225
226/* context management */
227void log_reset_context(void);
228int log_set_context(uint8_t ctx, void *value);
229
230/* filter on the targets */
231void log_set_all_filter(struct log_target *target, int);
232
233void log_set_use_color(struct log_target *target, int);
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100234void log_set_print_extended_timestamp(struct log_target *target, int);
Harald Welte3ae27582010-03-26 21:24:24 +0800235void log_set_print_timestamp(struct log_target *target, int);
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200236void log_set_print_filename(struct log_target *target, int);
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100237void log_set_print_category(struct log_target *target, int);
Harald Welte3ae27582010-03-26 21:24:24 +0800238void log_set_log_level(struct log_target *target, int log_level);
239void log_parse_category_mask(struct log_target *target, const char* mask);
240int log_parse_level(const char *lvl);
Harald Welte9ac22252010-05-11 11:19:40 +0200241const char *log_level_str(unsigned int lvl);
Harald Welte3ae27582010-03-26 21:24:24 +0800242int log_parse_category(const char *category);
243void log_set_category_filter(struct log_target *target, int category,
244 int enable, int level);
245
246/* management of the targets */
247struct log_target *log_target_create(void);
Harald Welte72d0c532010-08-25 19:24:00 +0200248void log_target_destroy(struct log_target *target);
Harald Welte3ae27582010-03-26 21:24:24 +0800249struct log_target *log_target_create_stderr(void);
Harald Welte72d0c532010-08-25 19:24:00 +0200250struct log_target *log_target_create_file(const char *fname);
Harald Welte46cfd772011-02-17 15:56:56 +0100251struct log_target *log_target_create_syslog(const char *ident, int option,
252 int facility);
Harald Welte72d0c532010-08-25 19:24:00 +0200253int log_target_file_reopen(struct log_target *tgt);
Harald Welte4de854d2013-03-18 19:01:40 +0100254int log_targets_reopen(void);
Harald Welte72d0c532010-08-25 19:24:00 +0200255
Harald Welte3ae27582010-03-26 21:24:24 +0800256void log_add_target(struct log_target *target);
257void log_del_target(struct log_target *target);
258
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100259/* Generate command string for VTY use */
260const char *log_vty_command_string(const struct log_info *info);
261const char *log_vty_command_description(const struct log_info *info);
Harald Welte7638af92010-05-11 16:39:22 +0200262
Harald Welte28222962011-02-18 20:37:04 +0100263struct log_target *log_target_find(int type, const char *fname);
264extern struct llist_head osmo_log_target_list;
265
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200266/*! @} */