blob: ba41762facd66e0f92ceb44f1a89b9b934ebfec1 [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
72#define OSMO_NUM_DLIB 9
Harald Welteb43bc042011-06-27 10:29:17 +020073
Harald Welte3ae27582010-03-26 21:24:24 +080074struct log_category {
75 uint8_t loglevel;
76 uint8_t enabled;
77};
78
Harald Welte18fc4652011-08-17 14:14:17 +020079/*! \brief Information regarding one logging category */
Harald Welte3ae27582010-03-26 21:24:24 +080080struct log_info_cat {
Harald Welte18fc4652011-08-17 14:14:17 +020081 const char *name; /*!< name of category */
82 const char *color; /*!< color string for cateyory */
83 const char *description; /*!< description text */
84 uint8_t loglevel; /*!< currently selected log-level */
85 uint8_t enabled; /*!< is this category enabled or not */
Harald Welte3ae27582010-03-26 21:24:24 +080086};
87
Harald Welte18fc4652011-08-17 14:14:17 +020088/*! \brief Log context information, passed to filter */
Harald Welte3ae27582010-03-26 21:24:24 +080089struct log_context {
90 void *ctx[LOG_MAX_CTX+1];
91};
92
93struct log_target;
94
Harald Welte18fc4652011-08-17 14:14:17 +020095/*! \brief Log filter function */
Harald Welte3ae27582010-03-26 21:24:24 +080096typedef int log_filter(const struct log_context *ctx,
97 struct log_target *target);
98
Harald Weltefb84f322013-06-06 07:33:54 +020099struct log_info;
100struct vty;
101
102typedef void log_print_filters(struct vty *vty,
103 const struct log_info *info,
104 const struct log_target *tgt);
105
106typedef void log_save_filters(struct vty *vty,
107 const struct log_info *info,
108 const struct log_target *tgt);
109
Harald Welte18fc4652011-08-17 14:14:17 +0200110/*! \brief Logging configuration, passed to \ref log_init */
Harald Welte3ae27582010-03-26 21:24:24 +0800111struct log_info {
Harald Welte18fc4652011-08-17 14:14:17 +0200112 /* \brief filter callback function */
Harald Welte3ae27582010-03-26 21:24:24 +0800113 log_filter *filter_fn;
114
Harald Welte18fc4652011-08-17 14:14:17 +0200115 /*! \brief per-category information */
Holger Hans Peter Freyther06f64552012-09-11 10:31:29 +0200116 const struct log_info_cat *cat;
Harald Welte18fc4652011-08-17 14:14:17 +0200117 /*! \brief total number of categories */
Harald Welte3ae27582010-03-26 21:24:24 +0800118 unsigned int num_cat;
Harald Welte18fc4652011-08-17 14:14:17 +0200119 /*! \brief total number of user categories (not library) */
Harald Welteb43bc042011-06-27 10:29:17 +0200120 unsigned int num_cat_user;
Harald Weltefb84f322013-06-06 07:33:54 +0200121
122 /* \brief filter saving function */
123 log_save_filters *save_fn;
124 /* \brief filter saving function */
125 log_print_filters *print_fn;
Harald Welte3ae27582010-03-26 21:24:24 +0800126};
127
Harald Welte18fc4652011-08-17 14:14:17 +0200128/*! \brief Type of logging target */
Harald Welte28222962011-02-18 20:37:04 +0100129enum log_target_type {
Harald Welte18fc4652011-08-17 14:14:17 +0200130 LOG_TGT_TYPE_VTY, /*!< \brief VTY logging */
131 LOG_TGT_TYPE_SYSLOG, /*!< \brief syslog based logging */
132 LOG_TGT_TYPE_FILE, /*!< \brief text file logging */
133 LOG_TGT_TYPE_STDERR, /*!< \brief stderr logging */
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000134 LOG_TGT_TYPE_STRRB, /*!< \brief osmo_strrb-backed logging */
Harald Welte28222962011-02-18 20:37:04 +0100135};
136
Harald Welte18fc4652011-08-17 14:14:17 +0200137/*! \brief structure representing a logging target */
Harald Welte3ae27582010-03-26 21:24:24 +0800138struct log_target {
Harald Welte18fc4652011-08-17 14:14:17 +0200139 struct llist_head entry; /*!< \brief linked list */
Harald Welte3ae27582010-03-26 21:24:24 +0800140
Harald Welte18fc4652011-08-17 14:14:17 +0200141 /*! \brief Internal data for filtering */
Harald Welte3ae27582010-03-26 21:24:24 +0800142 int filter_map;
Harald Welte18fc4652011-08-17 14:14:17 +0200143 /*! \brief Internal data for filtering */
Harald Welte3ae27582010-03-26 21:24:24 +0800144 void *filter_data[LOG_MAX_FILTERS+1];
145
Harald Welte18fc4652011-08-17 14:14:17 +0200146 /*! \brief logging categories */
Harald Welteb43bc042011-06-27 10:29:17 +0200147 struct log_category *categories;
148
Harald Welte18fc4652011-08-17 14:14:17 +0200149 /*! \brief global log level */
Harald Welte3ae27582010-03-26 21:24:24 +0800150 uint8_t loglevel;
Harald Welte18fc4652011-08-17 14:14:17 +0200151 /*! \brief should color be used when printing log messages? */
Harald Welte87dbca12011-07-16 11:57:53 +0200152 unsigned int use_color:1;
Harald Welte18fc4652011-08-17 14:14:17 +0200153 /*! \brief should log messages be prefixed with a timestamp? */
Harald Welte87dbca12011-07-16 11:57:53 +0200154 unsigned int print_timestamp:1;
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200155 /*! \brief should log messages be prefixed with a filename? */
156 unsigned int print_filename:1;
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100157 /*! \brief should log messages be prefixed with a category name? */
158 unsigned int print_category:1;
159 /*! \brief should log messages be prefixed with an extended timestamp? */
160 unsigned int print_ext_timestamp:1;
Harald Welte3ae27582010-03-26 21:24:24 +0800161
Harald Welte18fc4652011-08-17 14:14:17 +0200162 /*! \brief the type of this log taget */
Harald Welte28222962011-02-18 20:37:04 +0100163 enum log_target_type type;
164
Harald Welte3ae27582010-03-26 21:24:24 +0800165 union {
166 struct {
167 FILE *out;
Harald Welte72d0c532010-08-25 19:24:00 +0200168 const char *fname;
Harald Welte0083cd32010-08-25 14:55:44 +0200169 } tgt_file;
Harald Welte3ae27582010-03-26 21:24:24 +0800170
171 struct {
172 int priority;
Harald Welte28222962011-02-18 20:37:04 +0100173 int facility;
Harald Welte3ae27582010-03-26 21:24:24 +0800174 } tgt_syslog;
175
176 struct {
177 void *vty;
178 } tgt_vty;
Katerina Barone-Adesi3309a432013-02-21 05:16:29 +0000179
180 struct {
181 void *rb;
182 } tgt_rb;
Harald Welte3ae27582010-03-26 21:24:24 +0800183 };
184
Harald Welte18fc4652011-08-17 14:14:17 +0200185 /*! \brief call-back function to be called when the logging framework
186 * wants to log somethnig.
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +0100187 * \param[in] target logging target
Harald Welte18fc4652011-08-17 14:14:17 +0200188 * \param[in] level log level of currnet message
189 * \param[in] string the string that is to be written to the log
190 */
Harald Welte76e72ab2011-02-17 15:52:39 +0100191 void (*output) (struct log_target *target, unsigned int level,
192 const char *string);
Harald Welte3ae27582010-03-26 21:24:24 +0800193};
194
195/* use the above macros */
Andreas Eversberg2d6563b2012-07-10 13:10:15 +0200196void logp2(int subsys, unsigned int level, const char *file,
Harald Welte3ae27582010-03-26 21:24:24 +0800197 int line, int cont, const char *format, ...)
198 __attribute__ ((format (printf, 6, 7)));
Harald Welteb43bc042011-06-27 10:29:17 +0200199int log_init(const struct log_info *inf, void *talloc_ctx);
Harald Welte3ae27582010-03-26 21:24:24 +0800200
201/* context management */
202void log_reset_context(void);
203int log_set_context(uint8_t ctx, void *value);
204
205/* filter on the targets */
206void log_set_all_filter(struct log_target *target, int);
207
208void log_set_use_color(struct log_target *target, int);
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100209void log_set_print_extended_timestamp(struct log_target *target, int);
Harald Welte3ae27582010-03-26 21:24:24 +0800210void log_set_print_timestamp(struct log_target *target, int);
Holger Hans Peter Freytherdb153362012-09-11 11:24:51 +0200211void log_set_print_filename(struct log_target *target, int);
Holger Hans Peter Freyther2d6ad132014-12-05 09:35:30 +0100212void log_set_print_category(struct log_target *target, int);
Harald Welte3ae27582010-03-26 21:24:24 +0800213void log_set_log_level(struct log_target *target, int log_level);
214void log_parse_category_mask(struct log_target *target, const char* mask);
215int log_parse_level(const char *lvl);
Harald Welte9ac22252010-05-11 11:19:40 +0200216const char *log_level_str(unsigned int lvl);
Harald Welte3ae27582010-03-26 21:24:24 +0800217int log_parse_category(const char *category);
218void log_set_category_filter(struct log_target *target, int category,
219 int enable, int level);
220
221/* management of the targets */
222struct log_target *log_target_create(void);
Harald Welte72d0c532010-08-25 19:24:00 +0200223void log_target_destroy(struct log_target *target);
Harald Welte3ae27582010-03-26 21:24:24 +0800224struct log_target *log_target_create_stderr(void);
Harald Welte72d0c532010-08-25 19:24:00 +0200225struct log_target *log_target_create_file(const char *fname);
Harald Welte46cfd772011-02-17 15:56:56 +0100226struct log_target *log_target_create_syslog(const char *ident, int option,
227 int facility);
Harald Welte72d0c532010-08-25 19:24:00 +0200228int log_target_file_reopen(struct log_target *tgt);
Harald Welte4de854d2013-03-18 19:01:40 +0100229int log_targets_reopen(void);
Harald Welte72d0c532010-08-25 19:24:00 +0200230
Harald Welte3ae27582010-03-26 21:24:24 +0800231void log_add_target(struct log_target *target);
232void log_del_target(struct log_target *target);
233
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100234/* Generate command string for VTY use */
235const char *log_vty_command_string(const struct log_info *info);
236const char *log_vty_command_description(const struct log_info *info);
Harald Welte7638af92010-05-11 16:39:22 +0200237
Harald Welte28222962011-02-18 20:37:04 +0100238struct log_target *log_target_find(int type, const char *fname);
239extern struct llist_head osmo_log_target_list;
240
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200241/*! @} */