blob: ad775b9bc2ad3452f58c7219a85b64665533a66f [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>
Harald Welteaa00f992016-12-02 15:30:02 +010012#include <stdbool.h>
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010013#include <osmocom/core/linuxlist.h>
Harald Welte3ae27582010-03-26 21:24:24 +080014
Harald Weltebd598e32011-08-16 23:26:52 +020015/*! \brief Maximum number of logging contexts */
Harald Welte3ae27582010-03-26 21:24:24 +080016#define LOG_MAX_CTX 8
Harald Weltebd598e32011-08-16 23:26:52 +020017/*! \brief Maximum number of logging filters */
Harald Welte3ae27582010-03-26 21:24:24 +080018#define LOG_MAX_FILTERS 8
19
20#define DEBUG
21
22#ifdef DEBUG
Harald Welte2d2e2cc2016-04-25 12:11:20 +020023/*! \brief Log a debug message through the Osmocom logging framework
24 * \param[in] ss logging subsystem (e.g. \ref DLGLOBAL)
25 * \param[in] fmt format string
26 * \param[in] args variable argument list
27 */
Jacob Erlbecka89d22c2015-11-17 11:52:25 +010028#define DEBUGP(ss, fmt, args...) \
29 do { \
30 if (log_check_level(ss, LOGL_DEBUG)) \
Harald Welte3d792402016-05-10 15:23:06 +020031 logp(ss, __BASE_FILE__, __LINE__, 0, fmt, ## args); \
Jacob Erlbecka89d22c2015-11-17 11:52:25 +010032 } while(0)
33
34#define DEBUGPC(ss, fmt, args...) \
35 do { \
36 if (log_check_level(ss, LOGL_DEBUG)) \
Harald Welte3d792402016-05-10 15:23:06 +020037 logp(ss, __BASE_FILE__, __LINE__, 1, fmt, ## args); \
Jacob Erlbecka89d22c2015-11-17 11:52:25 +010038 } while(0)
39
Harald Welte3ae27582010-03-26 21:24:24 +080040#else
41#define DEBUGP(xss, fmt, args...)
42#define DEBUGPC(ss, fmt, args...)
43#endif
44
Harald Welte3ae27582010-03-26 21:24:24 +080045
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +020046void osmo_vlogp(int subsys, int level, const char *file, int line,
Harald Welte36c5a3e2011-08-27 14:33:19 +020047 int cont, const char *format, va_list ap);
48
Andreas Eversberg2d6563b2012-07-10 13:10:15 +020049void 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 +080050
Harald Weltebd598e32011-08-16 23:26:52 +020051/*! \brief Log a new message through the Osmocom logging framework
52 * \param[in] ss logging subsystem (e.g. \ref DLGLOBAL)
53 * \param[in] level logging level (e.g. \ref LOGL_NOTICE)
54 * \param[in] fmt format string
55 * \param[in] args variable argument list
56 */
Harald Welte3ae27582010-03-26 21:24:24 +080057#define LOGP(ss, level, fmt, args...) \
Jacob Erlbecka89d22c2015-11-17 11:52:25 +010058 do { \
59 if (log_check_level(ss, level)) \
Harald Welte3d792402016-05-10 15:23:06 +020060 logp2(ss, level, __BASE_FILE__, __LINE__, 0, fmt, ##args); \
Jacob Erlbecka89d22c2015-11-17 11:52:25 +010061 } while(0)
Harald Weltebd598e32011-08-16 23:26:52 +020062
63/*! \brief Continue a log message through the Osmocom logging framework
64 * \param[in] ss logging subsystem (e.g. \ref DLGLOBAL)
65 * \param[in] level logging level (e.g. \ref LOGL_NOTICE)
66 * \param[in] fmt format string
67 * \param[in] args variable argument list
68 */
Harald Welte3ae27582010-03-26 21:24:24 +080069#define LOGPC(ss, level, fmt, args...) \
Jacob Erlbecka89d22c2015-11-17 11:52:25 +010070 do { \
71 if (log_check_level(ss, level)) \
Harald Welte3d792402016-05-10 15:23:06 +020072 logp2(ss, level, __BASE_FILE__, __LINE__, 1, fmt, ##args); \
Jacob Erlbecka89d22c2015-11-17 11:52:25 +010073 } while(0)
Harald Welte3ae27582010-03-26 21:24:24 +080074
Harald Welte18fc4652011-08-17 14:14:17 +020075/*! \brief different log levels */
76#define LOGL_DEBUG 1 /*!< \brief debugging information */
Harald Welte2d2e2cc2016-04-25 12:11:20 +020077#define LOGL_INFO 3 /*!< \brief general information */
Harald Welte18fc4652011-08-17 14:14:17 +020078#define LOGL_NOTICE 5 /*!< \brief abnormal/unexpected condition */
79#define LOGL_ERROR 7 /*!< \brief error condition, requires user action */
80#define LOGL_FATAL 8 /*!< \brief fatal, program aborted */
Harald Welte3ae27582010-03-26 21:24:24 +080081
82#define LOG_FILTER_ALL 0x0001
83
Harald Welteb43bc042011-06-27 10:29:17 +020084/* logging levels defined by the library itself */
Harald Welte2d2e2cc2016-04-25 12:11:20 +020085#define DLGLOBAL -1 /*!< global logging */
86#define DLLAPD -2 /*!< LAPD implementation */
87#define DLINP -3 /*!< (A-bis) Input sub-system */
88#define DLMUX -4 /*!< Osmocom Multiplex (Osmux) */
89#define DLMI -5 /*!< ISDN-layer below input sub-system */
90#define DLMIB -6 /*!< ISDN layer B-channel */
91#define DLSMS -7 /*!< SMS sub-system */
92#define DLCTRL -8 /*!< Control Interface */
93#define DLGTP -9 /*!< GTP (GPRS Tunneling Protocol */
94#define DLSTATS -10 /*!< Statistics */
Harald Welte3b6fb082016-04-25 18:46:22 +020095#define DLGSUP 11 /*!< Generic Subscriber Update Protocol */
96#define OSMO_NUM_DLIB 11 /*!< Number of logging sub-systems in libraries */
Harald Welteb43bc042011-06-27 10:29:17 +020097
Harald Welte2d2e2cc2016-04-25 12:11:20 +020098/*! Configuration of singgle log category / sub-system */
Harald Welte3ae27582010-03-26 21:24:24 +080099struct log_category {
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200100 uint8_t loglevel; /*!< configured log-level */
101 uint8_t enabled; /*!< is logging enabled? */
Harald Welte3ae27582010-03-26 21:24:24 +0800102};
103
Harald Welte18fc4652011-08-17 14:14:17 +0200104/*! \brief Information regarding one logging category */
Harald Welte3ae27582010-03-26 21:24:24 +0800105struct log_info_cat {
Harald Welte18fc4652011-08-17 14:14:17 +0200106 const char *name; /*!< name of category */
107 const char *color; /*!< color string for cateyory */
108 const char *description; /*!< description text */
109 uint8_t loglevel; /*!< currently selected log-level */
110 uint8_t enabled; /*!< is this category enabled or not */
Harald Welte3ae27582010-03-26 21:24:24 +0800111};
112
Harald Welte18fc4652011-08-17 14:14:17 +0200113/*! \brief Log context information, passed to filter */
Harald Welte3ae27582010-03-26 21:24:24 +0800114struct log_context {
115 void *ctx[LOG_MAX_CTX+1];
116};
117
118struct log_target;
119
Harald Welte18fc4652011-08-17 14:14:17 +0200120/*! \brief Log filter function */
Harald Welte3ae27582010-03-26 21:24:24 +0800121typedef int log_filter(const struct log_context *ctx,
122 struct log_target *target);
123
Harald Weltefb84f322013-06-06 07:33:54 +0200124struct log_info;
125struct vty;
Harald Welteaa00f992016-12-02 15:30:02 +0100126struct gsmtap_inst;
Harald Weltefb84f322013-06-06 07:33:54 +0200127
128typedef void log_print_filters(struct vty *vty,
129 const struct log_info *info,
130 const struct log_target *tgt);
131
132typedef void log_save_filters(struct vty *vty,
133 const struct log_info *info,
134 const struct log_target *tgt);
135
Harald Welte18fc4652011-08-17 14:14:17 +0200136/*! \brief Logging configuration, passed to \ref log_init */
Harald Welte3ae27582010-03-26 21:24:24 +0800137struct log_info {
Harald Welte18fc4652011-08-17 14:14:17 +0200138 /* \brief filter callback function */
Harald Welte3ae27582010-03-26 21:24:24 +0800139 log_filter *filter_fn;
140
Harald Welte18fc4652011-08-17 14:14:17 +0200141 /*! \brief per-category information */
Holger Hans Peter Freyther06f64552012-09-11 10:31:29 +0200142 const struct log_info_cat *cat;
Harald Welte18fc4652011-08-17 14:14:17 +0200143 /*! \brief total number of categories */
Harald Welte3ae27582010-03-26 21:24:24 +0800144 unsigned int num_cat;
Harald Welte18fc4652011-08-17 14:14:17 +0200145 /*! \brief total number of user categories (not library) */
Harald Welteb43bc042011-06-27 10:29:17 +0200146 unsigned int num_cat_user;
Harald Weltefb84f322013-06-06 07:33:54 +0200147
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200148 /*! \brief filter saving function */
Harald Weltefb84f322013-06-06 07:33:54 +0200149 log_save_filters *save_fn;
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200150 /*! \brief filter saving function */
Harald Weltefb84f322013-06-06 07:33:54 +0200151 log_print_filters *print_fn;
Harald Welte3ae27582010-03-26 21:24:24 +0800152};
153
Harald Welte18fc4652011-08-17 14:14:17 +0200154/*! \brief Type of logging target */
Harald Welte28222962011-02-18 20:37:04 +0100155enum log_target_type {
Harald Welte18fc4652011-08-17 14:14:17 +0200156 LOG_TGT_TYPE_VTY, /*!< \brief VTY logging */
157 LOG_TGT_TYPE_SYSLOG, /*!< \brief syslog based logging */
158 LOG_TGT_TYPE_FILE, /*!< \brief text file logging */
159 LOG_TGT_TYPE_STDERR, /*!< \brief stderr logging */
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000160 LOG_TGT_TYPE_STRRB, /*!< \brief osmo_strrb-backed logging */
Harald Welteaa00f992016-12-02 15:30:02 +0100161 LOG_TGT_TYPE_GSMTAP, /*!< \brief GSMTAP network logging */
Harald Welte28222962011-02-18 20:37:04 +0100162};
163
Harald Welte18fc4652011-08-17 14:14:17 +0200164/*! \brief structure representing a logging target */
Harald Welte3ae27582010-03-26 21:24:24 +0800165struct log_target {
Harald Welte18fc4652011-08-17 14:14:17 +0200166 struct llist_head entry; /*!< \brief linked list */
Harald Welte3ae27582010-03-26 21:24:24 +0800167
Harald Welte18fc4652011-08-17 14:14:17 +0200168 /*! \brief Internal data for filtering */
Harald Welte3ae27582010-03-26 21:24:24 +0800169 int filter_map;
Harald Welte18fc4652011-08-17 14:14:17 +0200170 /*! \brief Internal data for filtering */
Harald Welte3ae27582010-03-26 21:24:24 +0800171 void *filter_data[LOG_MAX_FILTERS+1];
172
Harald Welte18fc4652011-08-17 14:14:17 +0200173 /*! \brief logging categories */
Harald Welteb43bc042011-06-27 10:29:17 +0200174 struct log_category *categories;
175
Harald Welte18fc4652011-08-17 14:14:17 +0200176 /*! \brief global log level */
Harald Welte3ae27582010-03-26 21:24:24 +0800177 uint8_t loglevel;
Harald Welte18fc4652011-08-17 14:14:17 +0200178 /*! \brief should color be used when printing log messages? */
Harald Welte87dbca12011-07-16 11:57:53 +0200179 unsigned int use_color:1;
Harald Welte18fc4652011-08-17 14:14:17 +0200180 /*! \brief should log messages be prefixed with a timestamp? */
Harald Welte87dbca12011-07-16 11:57:53 +0200181 unsigned int print_timestamp:1;
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200182 /*! \brief should log messages be prefixed with a filename? */
183 unsigned int print_filename:1;
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100184 /*! \brief should log messages be prefixed with a category name? */
185 unsigned int print_category:1;
186 /*! \brief should log messages be prefixed with an extended timestamp? */
187 unsigned int print_ext_timestamp:1;
Harald Welte3ae27582010-03-26 21:24:24 +0800188
Harald Welte18fc4652011-08-17 14:14:17 +0200189 /*! \brief the type of this log taget */
Harald Welte28222962011-02-18 20:37:04 +0100190 enum log_target_type type;
191
Harald Welte3ae27582010-03-26 21:24:24 +0800192 union {
193 struct {
194 FILE *out;
Harald Welte72d0c532010-08-25 19:24:00 +0200195 const char *fname;
Harald Welte0083cd32010-08-25 14:55:44 +0200196 } tgt_file;
Harald Welte3ae27582010-03-26 21:24:24 +0800197
198 struct {
199 int priority;
Harald Welte28222962011-02-18 20:37:04 +0100200 int facility;
Harald Welte3ae27582010-03-26 21:24:24 +0800201 } tgt_syslog;
202
203 struct {
204 void *vty;
205 } tgt_vty;
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000206
207 struct {
208 void *rb;
209 } tgt_rb;
Harald Welteaa00f992016-12-02 15:30:02 +0100210
211 struct {
212 struct gsmtap_inst *gsmtap_inst;
213 const char *ident;
214 const char *hostname;
215 } tgt_gsmtap;
Harald Welte3ae27582010-03-26 21:24:24 +0800216 };
217
Harald Welte18fc4652011-08-17 14:14:17 +0200218 /*! \brief call-back function to be called when the logging framework
Harald Welted7c0a372016-12-02 13:52:59 +0100219 * wants to log a fully formatted string
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +0100220 * \param[in] target logging target
Harald Welte18fc4652011-08-17 14:14:17 +0200221 * \param[in] level log level of currnet message
222 * \param[in] string the string that is to be written to the log
223 */
Harald Welte76e72ab2011-02-17 15:52:39 +0100224 void (*output) (struct log_target *target, unsigned int level,
225 const char *string);
Harald Welted7c0a372016-12-02 13:52:59 +0100226
227 /*! \brief alternative call-back function to which the logging
228 * framework passes the unfortmatted input arguments,
229 * i.e. bypassing the internal string formatter
230 * \param[in] target logging target
231 * \param[in] subsys logging sub-system
232 * \param[in] level logging level
233 * \param[in] file soure code file name
234 * \param[in] line source code file line number
235 * \param[in] cont continuation of previous statement?
236 * \param[in] format format string
237 * \param[in] ap vararg list of printf arguments
238 */
239 void (*raw_output)(struct log_target *target, int subsys,
240 unsigned int level, const char *file, int line,
241 int cont, const char *format, va_list ap);
Harald Welte3ae27582010-03-26 21:24:24 +0800242};
243
244/* use the above macros */
Andreas Eversberg2d6563b2012-07-10 13:10:15 +0200245void logp2(int subsys, unsigned int level, const char *file,
Harald Welte3ae27582010-03-26 21:24:24 +0800246 int line, int cont, const char *format, ...)
247 __attribute__ ((format (printf, 6, 7)));
Harald Welteb43bc042011-06-27 10:29:17 +0200248int log_init(const struct log_info *inf, void *talloc_ctx);
Harald Welte69e6c3c2016-04-20 10:41:27 +0200249void log_fini(void);
Jacob Erlbeckde6dd722015-11-17 11:52:24 +0100250int log_check_level(int subsys, unsigned int level);
Harald Welte3ae27582010-03-26 21:24:24 +0800251
252/* context management */
253void log_reset_context(void);
254int log_set_context(uint8_t ctx, void *value);
255
256/* filter on the targets */
257void log_set_all_filter(struct log_target *target, int);
258
259void log_set_use_color(struct log_target *target, int);
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100260void log_set_print_extended_timestamp(struct log_target *target, int);
Harald Welte3ae27582010-03-26 21:24:24 +0800261void log_set_print_timestamp(struct log_target *target, int);
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200262void log_set_print_filename(struct log_target *target, int);
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100263void log_set_print_category(struct log_target *target, int);
Harald Welte3ae27582010-03-26 21:24:24 +0800264void log_set_log_level(struct log_target *target, int log_level);
265void log_parse_category_mask(struct log_target *target, const char* mask);
Harald Welteaa00f992016-12-02 15:30:02 +0100266const char* log_category_name(int subsys);
Harald Welte3ae27582010-03-26 21:24:24 +0800267int log_parse_level(const char *lvl);
Harald Welte9ac22252010-05-11 11:19:40 +0200268const char *log_level_str(unsigned int lvl);
Harald Welte3ae27582010-03-26 21:24:24 +0800269int log_parse_category(const char *category);
270void log_set_category_filter(struct log_target *target, int category,
271 int enable, int level);
272
273/* management of the targets */
274struct log_target *log_target_create(void);
Harald Welte72d0c532010-08-25 19:24:00 +0200275void log_target_destroy(struct log_target *target);
Harald Welte3ae27582010-03-26 21:24:24 +0800276struct log_target *log_target_create_stderr(void);
Harald Welte72d0c532010-08-25 19:24:00 +0200277struct log_target *log_target_create_file(const char *fname);
Harald Welte46cfd772011-02-17 15:56:56 +0100278struct log_target *log_target_create_syslog(const char *ident, int option,
279 int facility);
Harald Welteaa00f992016-12-02 15:30:02 +0100280struct log_target *log_target_create_gsmtap(const char *host, uint16_t port,
281 const char *ident,
282 bool ofd_wq_mode,
283 bool add_sink);
Harald Welte72d0c532010-08-25 19:24:00 +0200284int log_target_file_reopen(struct log_target *tgt);
Harald Welte4de854d2013-03-18 19:01:40 +0100285int log_targets_reopen(void);
Harald Welte72d0c532010-08-25 19:24:00 +0200286
Harald Welte3ae27582010-03-26 21:24:24 +0800287void log_add_target(struct log_target *target);
288void log_del_target(struct log_target *target);
289
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100290/* Generate command string for VTY use */
291const char *log_vty_command_string(const struct log_info *info);
292const char *log_vty_command_description(const struct log_info *info);
Harald Welte7638af92010-05-11 16:39:22 +0200293
Harald Welte28222962011-02-18 20:37:04 +0100294struct log_target *log_target_find(int type, const char *fname);
295extern struct llist_head osmo_log_target_list;
296
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200297/*! @} */