blob: 290b33df82ddf74c24c3aee256ddb0ed19a8f3ad [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
22#define DEBUGP(ss, fmt, args...) logp(ss, __FILE__, __LINE__, 0, fmt, ## args)
23#define DEBUGPC(ss, fmt, args...) logp(ss, __FILE__, __LINE__, 1, fmt, ## args)
24#else
25#define DEBUGP(xss, fmt, args...)
26#define DEBUGPC(ss, fmt, args...)
27#endif
28
Harald Welte3ae27582010-03-26 21:24:24 +080029
Holger Hans Peter Freytherfb4bfc22012-07-12 09:26:25 +020030void osmo_vlogp(int subsys, int level, const char *file, int line,
Harald Welte36c5a3e2011-08-27 14:33:19 +020031 int cont, const char *format, va_list ap);
32
Andreas Eversberg2d6563b2012-07-10 13:10:15 +020033void 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 +080034
Harald Weltebd598e32011-08-16 23:26:52 +020035/*! \brief Log a new message through the Osmocom logging framework
36 * \param[in] ss logging subsystem (e.g. \ref DLGLOBAL)
37 * \param[in] level logging level (e.g. \ref LOGL_NOTICE)
38 * \param[in] fmt format string
39 * \param[in] args variable argument list
40 */
Harald Welte3ae27582010-03-26 21:24:24 +080041#define LOGP(ss, level, fmt, args...) \
42 logp2(ss, level, __FILE__, __LINE__, 0, fmt, ##args)
Harald Weltebd598e32011-08-16 23:26:52 +020043
44/*! \brief Continue a log message through the Osmocom logging framework
45 * \param[in] ss logging subsystem (e.g. \ref DLGLOBAL)
46 * \param[in] level logging level (e.g. \ref LOGL_NOTICE)
47 * \param[in] fmt format string
48 * \param[in] args variable argument list
49 */
Harald Welte3ae27582010-03-26 21:24:24 +080050#define LOGPC(ss, level, fmt, args...) \
51 logp2(ss, level, __FILE__, __LINE__, 1, fmt, ##args)
52
Harald Welte18fc4652011-08-17 14:14:17 +020053/*! \brief different log levels */
54#define LOGL_DEBUG 1 /*!< \brief debugging information */
Harald Welte3ae27582010-03-26 21:24:24 +080055#define LOGL_INFO 3
Harald Welte18fc4652011-08-17 14:14:17 +020056#define LOGL_NOTICE 5 /*!< \brief abnormal/unexpected condition */
57#define LOGL_ERROR 7 /*!< \brief error condition, requires user action */
58#define LOGL_FATAL 8 /*!< \brief fatal, program aborted */
Harald Welte3ae27582010-03-26 21:24:24 +080059
60#define LOG_FILTER_ALL 0x0001
61
Harald Welteb43bc042011-06-27 10:29:17 +020062/* logging levels defined by the library itself */
63#define DLGLOBAL -1
root8a996b42011-09-26 11:22:21 +020064#define DLLAPD -2
Harald Welte892e6212011-07-19 14:31:44 +020065#define DLINP -3
66#define DLMUX -4
67#define DLMI -5
68#define DLMIB -6
Andreas Eversbergc626da92011-10-28 03:53:50 +020069#define DLSMS -7
Harald Welte7fd0c832014-08-20 19:58:13 +020070#define DLCTRL -8
Holger Hans Peter Freythera5dc19d2014-12-04 14:35:21 +010071#define DLGTP -9
Jacob Erlbeck79125ec2015-11-02 15:17:50 +010072#define DLSTATS -10
73#define OSMO_NUM_DLIB 10
Harald Welteb43bc042011-06-27 10:29:17 +020074
Harald Welte3ae27582010-03-26 21:24:24 +080075struct log_category {
76 uint8_t loglevel;
77 uint8_t enabled;
78};
79
Harald Welte18fc4652011-08-17 14:14:17 +020080/*! \brief Information regarding one logging category */
Harald Welte3ae27582010-03-26 21:24:24 +080081struct log_info_cat {
Harald Welte18fc4652011-08-17 14:14:17 +020082 const char *name; /*!< name of category */
83 const char *color; /*!< color string for cateyory */
84 const char *description; /*!< description text */
85 uint8_t loglevel; /*!< currently selected log-level */
86 uint8_t enabled; /*!< is this category enabled or not */
Harald Welte3ae27582010-03-26 21:24:24 +080087};
88
Harald Welte18fc4652011-08-17 14:14:17 +020089/*! \brief Log context information, passed to filter */
Harald Welte3ae27582010-03-26 21:24:24 +080090struct log_context {
91 void *ctx[LOG_MAX_CTX+1];
92};
93
94struct log_target;
95
Harald Welte18fc4652011-08-17 14:14:17 +020096/*! \brief Log filter function */
Harald Welte3ae27582010-03-26 21:24:24 +080097typedef int log_filter(const struct log_context *ctx,
98 struct log_target *target);
99
Harald Weltefb84f322013-06-06 07:33:54 +0200100struct log_info;
101struct vty;
102
103typedef void log_print_filters(struct vty *vty,
104 const struct log_info *info,
105 const struct log_target *tgt);
106
107typedef void log_save_filters(struct vty *vty,
108 const struct log_info *info,
109 const struct log_target *tgt);
110
Harald Welte18fc4652011-08-17 14:14:17 +0200111/*! \brief Logging configuration, passed to \ref log_init */
Harald Welte3ae27582010-03-26 21:24:24 +0800112struct log_info {
Harald Welte18fc4652011-08-17 14:14:17 +0200113 /* \brief filter callback function */
Harald Welte3ae27582010-03-26 21:24:24 +0800114 log_filter *filter_fn;
115
Harald Welte18fc4652011-08-17 14:14:17 +0200116 /*! \brief per-category information */
Holger Hans Peter Freyther06f64552012-09-11 10:31:29 +0200117 const struct log_info_cat *cat;
Harald Welte18fc4652011-08-17 14:14:17 +0200118 /*! \brief total number of categories */
Harald Welte3ae27582010-03-26 21:24:24 +0800119 unsigned int num_cat;
Harald Welte18fc4652011-08-17 14:14:17 +0200120 /*! \brief total number of user categories (not library) */
Harald Welteb43bc042011-06-27 10:29:17 +0200121 unsigned int num_cat_user;
Harald Weltefb84f322013-06-06 07:33:54 +0200122
123 /* \brief filter saving function */
124 log_save_filters *save_fn;
125 /* \brief filter saving function */
126 log_print_filters *print_fn;
Harald Welte3ae27582010-03-26 21:24:24 +0800127};
128
Harald Welte18fc4652011-08-17 14:14:17 +0200129/*! \brief Type of logging target */
Harald Welte28222962011-02-18 20:37:04 +0100130enum log_target_type {
Harald Welte18fc4652011-08-17 14:14:17 +0200131 LOG_TGT_TYPE_VTY, /*!< \brief VTY logging */
132 LOG_TGT_TYPE_SYSLOG, /*!< \brief syslog based logging */
133 LOG_TGT_TYPE_FILE, /*!< \brief text file logging */
134 LOG_TGT_TYPE_STDERR, /*!< \brief stderr logging */
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000135 LOG_TGT_TYPE_STRRB, /*!< \brief osmo_strrb-backed logging */
Harald Welte28222962011-02-18 20:37:04 +0100136};
137
Harald Welte18fc4652011-08-17 14:14:17 +0200138/*! \brief structure representing a logging target */
Harald Welte3ae27582010-03-26 21:24:24 +0800139struct log_target {
Harald Welte18fc4652011-08-17 14:14:17 +0200140 struct llist_head entry; /*!< \brief linked list */
Harald Welte3ae27582010-03-26 21:24:24 +0800141
Harald Welte18fc4652011-08-17 14:14:17 +0200142 /*! \brief Internal data for filtering */
Harald Welte3ae27582010-03-26 21:24:24 +0800143 int filter_map;
Harald Welte18fc4652011-08-17 14:14:17 +0200144 /*! \brief Internal data for filtering */
Harald Welte3ae27582010-03-26 21:24:24 +0800145 void *filter_data[LOG_MAX_FILTERS+1];
146
Harald Welte18fc4652011-08-17 14:14:17 +0200147 /*! \brief logging categories */
Harald Welteb43bc042011-06-27 10:29:17 +0200148 struct log_category *categories;
149
Harald Welte18fc4652011-08-17 14:14:17 +0200150 /*! \brief global log level */
Harald Welte3ae27582010-03-26 21:24:24 +0800151 uint8_t loglevel;
Harald Welte18fc4652011-08-17 14:14:17 +0200152 /*! \brief should color be used when printing log messages? */
Harald Welte87dbca12011-07-16 11:57:53 +0200153 unsigned int use_color:1;
Harald Welte18fc4652011-08-17 14:14:17 +0200154 /*! \brief should log messages be prefixed with a timestamp? */
Harald Welte87dbca12011-07-16 11:57:53 +0200155 unsigned int print_timestamp:1;
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200156 /*! \brief should log messages be prefixed with a filename? */
157 unsigned int print_filename:1;
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100158 /*! \brief should log messages be prefixed with a category name? */
159 unsigned int print_category:1;
160 /*! \brief should log messages be prefixed with an extended timestamp? */
161 unsigned int print_ext_timestamp:1;
Harald Welte3ae27582010-03-26 21:24:24 +0800162
Harald Welte18fc4652011-08-17 14:14:17 +0200163 /*! \brief the type of this log taget */
Harald Welte28222962011-02-18 20:37:04 +0100164 enum log_target_type type;
165
Harald Welte3ae27582010-03-26 21:24:24 +0800166 union {
167 struct {
168 FILE *out;
Harald Welte72d0c532010-08-25 19:24:00 +0200169 const char *fname;
Harald Welte0083cd32010-08-25 14:55:44 +0200170 } tgt_file;
Harald Welte3ae27582010-03-26 21:24:24 +0800171
172 struct {
173 int priority;
Harald Welte28222962011-02-18 20:37:04 +0100174 int facility;
Harald Welte3ae27582010-03-26 21:24:24 +0800175 } tgt_syslog;
176
177 struct {
178 void *vty;
179 } tgt_vty;
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000180
181 struct {
182 void *rb;
183 } tgt_rb;
Harald Welte3ae27582010-03-26 21:24:24 +0800184 };
185
Harald Welte18fc4652011-08-17 14:14:17 +0200186 /*! \brief call-back function to be called when the logging framework
187 * wants to log somethnig.
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +0100188 * \param[in] target logging target
Harald Welte18fc4652011-08-17 14:14:17 +0200189 * \param[in] level log level of currnet message
190 * \param[in] string the string that is to be written to the log
191 */
Harald Welte76e72ab2011-02-17 15:52:39 +0100192 void (*output) (struct log_target *target, unsigned int level,
193 const char *string);
Harald Welte3ae27582010-03-26 21:24:24 +0800194};
195
196/* use the above macros */
Andreas Eversberg2d6563b2012-07-10 13:10:15 +0200197void logp2(int subsys, unsigned int level, const char *file,
Harald Welte3ae27582010-03-26 21:24:24 +0800198 int line, int cont, const char *format, ...)
199 __attribute__ ((format (printf, 6, 7)));
Harald Welteb43bc042011-06-27 10:29:17 +0200200int log_init(const struct log_info *inf, void *talloc_ctx);
Jacob Erlbeckde6dd722015-11-17 11:52:24 +0100201int log_check_level(int subsys, unsigned int level);
Harald Welte3ae27582010-03-26 21:24:24 +0800202
203/* context management */
204void log_reset_context(void);
205int log_set_context(uint8_t ctx, void *value);
206
207/* filter on the targets */
208void log_set_all_filter(struct log_target *target, int);
209
210void log_set_use_color(struct log_target *target, int);
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100211void log_set_print_extended_timestamp(struct log_target *target, int);
Harald Welte3ae27582010-03-26 21:24:24 +0800212void log_set_print_timestamp(struct log_target *target, int);
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200213void log_set_print_filename(struct log_target *target, int);
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100214void log_set_print_category(struct log_target *target, int);
Harald Welte3ae27582010-03-26 21:24:24 +0800215void log_set_log_level(struct log_target *target, int log_level);
216void log_parse_category_mask(struct log_target *target, const char* mask);
217int log_parse_level(const char *lvl);
Harald Welte9ac22252010-05-11 11:19:40 +0200218const char *log_level_str(unsigned int lvl);
Harald Welte3ae27582010-03-26 21:24:24 +0800219int log_parse_category(const char *category);
220void log_set_category_filter(struct log_target *target, int category,
221 int enable, int level);
222
223/* management of the targets */
224struct log_target *log_target_create(void);
Harald Welte72d0c532010-08-25 19:24:00 +0200225void log_target_destroy(struct log_target *target);
Harald Welte3ae27582010-03-26 21:24:24 +0800226struct log_target *log_target_create_stderr(void);
Harald Welte72d0c532010-08-25 19:24:00 +0200227struct log_target *log_target_create_file(const char *fname);
Harald Welte46cfd772011-02-17 15:56:56 +0100228struct log_target *log_target_create_syslog(const char *ident, int option,
229 int facility);
Harald Welte72d0c532010-08-25 19:24:00 +0200230int log_target_file_reopen(struct log_target *tgt);
Harald Welte4de854d2013-03-18 19:01:40 +0100231int log_targets_reopen(void);
Harald Welte72d0c532010-08-25 19:24:00 +0200232
Harald Welte3ae27582010-03-26 21:24:24 +0800233void log_add_target(struct log_target *target);
234void log_del_target(struct log_target *target);
235
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100236/* Generate command string for VTY use */
237const char *log_vty_command_string(const struct log_info *info);
238const char *log_vty_command_description(const struct log_info *info);
Harald Welte7638af92010-05-11 16:39:22 +0200239
Harald Welte28222962011-02-18 20:37:04 +0100240struct log_target *log_target_find(int type, const char *fname);
241extern struct llist_head osmo_log_target_list;
242
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200243/*! @} */