Sylvain Munaut | 12ba778 | 2014-06-16 10:13:40 +0200 | [diff] [blame] | 1 | #pragma once |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 2 | |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 3 | /*! \defgroup logging Osmocom logging framework |
| 4 | * @{ |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 5 | */ |
| 6 | |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 7 | /*! \file logging.h */ |
| 8 | |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 9 | #include <stdio.h> |
| 10 | #include <stdint.h> |
Christoph Fritz | ab7c9c7 | 2011-09-01 16:22:17 +0200 | [diff] [blame] | 11 | #include <stdarg.h> |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 12 | #include <osmocom/core/linuxlist.h> |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 13 | |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 14 | /*! \brief Maximum number of logging contexts */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 15 | #define LOG_MAX_CTX 8 |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 16 | /*! \brief Maximum number of logging filters */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 17 | #define LOG_MAX_FILTERS 8 |
| 18 | |
| 19 | #define DEBUG |
| 20 | |
| 21 | #ifdef DEBUG |
Jacob Erlbeck | a89d22c | 2015-11-17 11:52:25 +0100 | [diff] [blame] | 22 | #define DEBUGP(ss, fmt, args...) \ |
| 23 | do { \ |
| 24 | if (log_check_level(ss, LOGL_DEBUG)) \ |
| 25 | logp(ss, __FILE__, __LINE__, 0, fmt, ## args); \ |
| 26 | } while(0) |
| 27 | |
| 28 | #define DEBUGPC(ss, fmt, args...) \ |
| 29 | do { \ |
| 30 | if (log_check_level(ss, LOGL_DEBUG)) \ |
| 31 | logp(ss, __FILE__, __LINE__, 1, fmt, ## args); \ |
| 32 | } while(0) |
| 33 | |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 34 | #else |
| 35 | #define DEBUGP(xss, fmt, args...) |
| 36 | #define DEBUGPC(ss, fmt, args...) |
| 37 | #endif |
| 38 | |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 39 | |
Holger Hans Peter Freyther | fb4bfc2 | 2012-07-12 09:26:25 +0200 | [diff] [blame] | 40 | void osmo_vlogp(int subsys, int level, const char *file, int line, |
Harald Welte | 36c5a3e | 2011-08-27 14:33:19 +0200 | [diff] [blame] | 41 | int cont, const char *format, va_list ap); |
| 42 | |
Andreas Eversberg | 2d6563b | 2012-07-10 13:10:15 +0200 | [diff] [blame] | 43 | void logp(int subsys, const char *file, int line, int cont, const char *format, ...) __attribute__ ((format (printf, 5, 6))); |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 44 | |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 45 | /*! \brief Log a new message through the Osmocom logging framework |
| 46 | * \param[in] ss logging subsystem (e.g. \ref DLGLOBAL) |
| 47 | * \param[in] level logging level (e.g. \ref LOGL_NOTICE) |
| 48 | * \param[in] fmt format string |
| 49 | * \param[in] args variable argument list |
| 50 | */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 51 | #define LOGP(ss, level, fmt, args...) \ |
Jacob Erlbeck | a89d22c | 2015-11-17 11:52:25 +0100 | [diff] [blame] | 52 | do { \ |
| 53 | if (log_check_level(ss, level)) \ |
| 54 | logp2(ss, level, __FILE__, __LINE__, 0, fmt, ##args); \ |
| 55 | } while(0) |
Harald Welte | bd598e3 | 2011-08-16 23:26:52 +0200 | [diff] [blame] | 56 | |
| 57 | /*! \brief Continue a log message through the Osmocom logging framework |
| 58 | * \param[in] ss logging subsystem (e.g. \ref DLGLOBAL) |
| 59 | * \param[in] level logging level (e.g. \ref LOGL_NOTICE) |
| 60 | * \param[in] fmt format string |
| 61 | * \param[in] args variable argument list |
| 62 | */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 63 | #define LOGPC(ss, level, fmt, args...) \ |
Jacob Erlbeck | a89d22c | 2015-11-17 11:52:25 +0100 | [diff] [blame] | 64 | do { \ |
| 65 | if (log_check_level(ss, level)) \ |
| 66 | logp2(ss, level, __FILE__, __LINE__, 1, fmt, ##args); \ |
| 67 | } while(0) |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 68 | |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 69 | /*! \brief different log levels */ |
| 70 | #define LOGL_DEBUG 1 /*!< \brief debugging information */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 71 | #define LOGL_INFO 3 |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 72 | #define LOGL_NOTICE 5 /*!< \brief abnormal/unexpected condition */ |
| 73 | #define LOGL_ERROR 7 /*!< \brief error condition, requires user action */ |
| 74 | #define LOGL_FATAL 8 /*!< \brief fatal, program aborted */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 75 | |
| 76 | #define LOG_FILTER_ALL 0x0001 |
| 77 | |
Harald Welte | b43bc04 | 2011-06-27 10:29:17 +0200 | [diff] [blame] | 78 | /* logging levels defined by the library itself */ |
| 79 | #define DLGLOBAL -1 |
root | 8a996b4 | 2011-09-26 11:22:21 +0200 | [diff] [blame] | 80 | #define DLLAPD -2 |
Harald Welte | 892e621 | 2011-07-19 14:31:44 +0200 | [diff] [blame] | 81 | #define DLINP -3 |
| 82 | #define DLMUX -4 |
| 83 | #define DLMI -5 |
| 84 | #define DLMIB -6 |
Andreas Eversberg | c626da9 | 2011-10-28 03:53:50 +0200 | [diff] [blame] | 85 | #define DLSMS -7 |
Harald Welte | 7fd0c83 | 2014-08-20 19:58:13 +0200 | [diff] [blame] | 86 | #define DLCTRL -8 |
Holger Hans Peter Freyther | a5dc19d | 2014-12-04 14:35:21 +0100 | [diff] [blame] | 87 | #define DLGTP -9 |
Jacob Erlbeck | 79125ec | 2015-11-02 15:17:50 +0100 | [diff] [blame] | 88 | #define DLSTATS -10 |
| 89 | #define OSMO_NUM_DLIB 10 |
Harald Welte | b43bc04 | 2011-06-27 10:29:17 +0200 | [diff] [blame] | 90 | |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 91 | struct log_category { |
| 92 | uint8_t loglevel; |
| 93 | uint8_t enabled; |
| 94 | }; |
| 95 | |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 96 | /*! \brief Information regarding one logging category */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 97 | struct log_info_cat { |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 98 | const char *name; /*!< name of category */ |
| 99 | const char *color; /*!< color string for cateyory */ |
| 100 | const char *description; /*!< description text */ |
| 101 | uint8_t loglevel; /*!< currently selected log-level */ |
| 102 | uint8_t enabled; /*!< is this category enabled or not */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 103 | }; |
| 104 | |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 105 | /*! \brief Log context information, passed to filter */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 106 | struct log_context { |
| 107 | void *ctx[LOG_MAX_CTX+1]; |
| 108 | }; |
| 109 | |
| 110 | struct log_target; |
| 111 | |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 112 | /*! \brief Log filter function */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 113 | typedef int log_filter(const struct log_context *ctx, |
| 114 | struct log_target *target); |
| 115 | |
Harald Welte | fb84f32 | 2013-06-06 07:33:54 +0200 | [diff] [blame] | 116 | struct log_info; |
| 117 | struct vty; |
| 118 | |
| 119 | typedef void log_print_filters(struct vty *vty, |
| 120 | const struct log_info *info, |
| 121 | const struct log_target *tgt); |
| 122 | |
| 123 | typedef void log_save_filters(struct vty *vty, |
| 124 | const struct log_info *info, |
| 125 | const struct log_target *tgt); |
| 126 | |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 127 | /*! \brief Logging configuration, passed to \ref log_init */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 128 | struct log_info { |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 129 | /* \brief filter callback function */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 130 | log_filter *filter_fn; |
| 131 | |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 132 | /*! \brief per-category information */ |
Holger Hans Peter Freyther | 06f6455 | 2012-09-11 10:31:29 +0200 | [diff] [blame] | 133 | const struct log_info_cat *cat; |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 134 | /*! \brief total number of categories */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 135 | unsigned int num_cat; |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 136 | /*! \brief total number of user categories (not library) */ |
Harald Welte | b43bc04 | 2011-06-27 10:29:17 +0200 | [diff] [blame] | 137 | unsigned int num_cat_user; |
Harald Welte | fb84f32 | 2013-06-06 07:33:54 +0200 | [diff] [blame] | 138 | |
| 139 | /* \brief filter saving function */ |
| 140 | log_save_filters *save_fn; |
| 141 | /* \brief filter saving function */ |
| 142 | log_print_filters *print_fn; |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 143 | }; |
| 144 | |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 145 | /*! \brief Type of logging target */ |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 146 | enum log_target_type { |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 147 | LOG_TGT_TYPE_VTY, /*!< \brief VTY logging */ |
| 148 | LOG_TGT_TYPE_SYSLOG, /*!< \brief syslog based logging */ |
| 149 | LOG_TGT_TYPE_FILE, /*!< \brief text file logging */ |
| 150 | LOG_TGT_TYPE_STDERR, /*!< \brief stderr logging */ |
Katerina Barone-Adesi | 3309a43 | 2013-02-21 05:16:29 +0000 | [diff] [blame] | 151 | LOG_TGT_TYPE_STRRB, /*!< \brief osmo_strrb-backed logging */ |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 152 | }; |
| 153 | |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 154 | /*! \brief structure representing a logging target */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 155 | struct log_target { |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 156 | struct llist_head entry; /*!< \brief linked list */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 157 | |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 158 | /*! \brief Internal data for filtering */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 159 | int filter_map; |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 160 | /*! \brief Internal data for filtering */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 161 | void *filter_data[LOG_MAX_FILTERS+1]; |
| 162 | |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 163 | /*! \brief logging categories */ |
Harald Welte | b43bc04 | 2011-06-27 10:29:17 +0200 | [diff] [blame] | 164 | struct log_category *categories; |
| 165 | |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 166 | /*! \brief global log level */ |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 167 | uint8_t loglevel; |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 168 | /*! \brief should color be used when printing log messages? */ |
Harald Welte | 87dbca1 | 2011-07-16 11:57:53 +0200 | [diff] [blame] | 169 | unsigned int use_color:1; |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 170 | /*! \brief should log messages be prefixed with a timestamp? */ |
Harald Welte | 87dbca1 | 2011-07-16 11:57:53 +0200 | [diff] [blame] | 171 | unsigned int print_timestamp:1; |
Holger Hans Peter Freyther | db15336 | 2012-09-11 11:24:51 +0200 | [diff] [blame] | 172 | /*! \brief should log messages be prefixed with a filename? */ |
| 173 | unsigned int print_filename:1; |
Holger Hans Peter Freyther | 2d6ad13 | 2014-12-05 09:35:30 +0100 | [diff] [blame] | 174 | /*! \brief should log messages be prefixed with a category name? */ |
| 175 | unsigned int print_category:1; |
| 176 | /*! \brief should log messages be prefixed with an extended timestamp? */ |
| 177 | unsigned int print_ext_timestamp:1; |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 178 | |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 179 | /*! \brief the type of this log taget */ |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 180 | enum log_target_type type; |
| 181 | |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 182 | union { |
| 183 | struct { |
| 184 | FILE *out; |
Harald Welte | 72d0c53 | 2010-08-25 19:24:00 +0200 | [diff] [blame] | 185 | const char *fname; |
Harald Welte | 0083cd3 | 2010-08-25 14:55:44 +0200 | [diff] [blame] | 186 | } tgt_file; |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 187 | |
| 188 | struct { |
| 189 | int priority; |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 190 | int facility; |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 191 | } tgt_syslog; |
| 192 | |
| 193 | struct { |
| 194 | void *vty; |
| 195 | } tgt_vty; |
Katerina Barone-Adesi | 3309a43 | 2013-02-21 05:16:29 +0000 | [diff] [blame] | 196 | |
| 197 | struct { |
| 198 | void *rb; |
| 199 | } tgt_rb; |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 200 | }; |
| 201 | |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 202 | /*! \brief call-back function to be called when the logging framework |
| 203 | * wants to log somethnig. |
Katerina Barone-Adesi | c28c6a0 | 2013-02-15 13:27:59 +0100 | [diff] [blame] | 204 | * \param[in] target logging target |
Harald Welte | 18fc465 | 2011-08-17 14:14:17 +0200 | [diff] [blame] | 205 | * \param[in] level log level of currnet message |
| 206 | * \param[in] string the string that is to be written to the log |
| 207 | */ |
Harald Welte | 76e72ab | 2011-02-17 15:52:39 +0100 | [diff] [blame] | 208 | void (*output) (struct log_target *target, unsigned int level, |
| 209 | const char *string); |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 210 | }; |
| 211 | |
| 212 | /* use the above macros */ |
Andreas Eversberg | 2d6563b | 2012-07-10 13:10:15 +0200 | [diff] [blame] | 213 | void logp2(int subsys, unsigned int level, const char *file, |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 214 | int line, int cont, const char *format, ...) |
| 215 | __attribute__ ((format (printf, 6, 7))); |
Harald Welte | b43bc04 | 2011-06-27 10:29:17 +0200 | [diff] [blame] | 216 | int log_init(const struct log_info *inf, void *talloc_ctx); |
Jacob Erlbeck | de6dd72 | 2015-11-17 11:52:24 +0100 | [diff] [blame] | 217 | int log_check_level(int subsys, unsigned int level); |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 218 | |
| 219 | /* context management */ |
| 220 | void log_reset_context(void); |
| 221 | int log_set_context(uint8_t ctx, void *value); |
| 222 | |
| 223 | /* filter on the targets */ |
| 224 | void log_set_all_filter(struct log_target *target, int); |
| 225 | |
| 226 | void log_set_use_color(struct log_target *target, int); |
Holger Hans Peter Freyther | 2d6ad13 | 2014-12-05 09:35:30 +0100 | [diff] [blame] | 227 | void log_set_print_extended_timestamp(struct log_target *target, int); |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 228 | void log_set_print_timestamp(struct log_target *target, int); |
Holger Hans Peter Freyther | db15336 | 2012-09-11 11:24:51 +0200 | [diff] [blame] | 229 | void log_set_print_filename(struct log_target *target, int); |
Holger Hans Peter Freyther | 2d6ad13 | 2014-12-05 09:35:30 +0100 | [diff] [blame] | 230 | void log_set_print_category(struct log_target *target, int); |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 231 | void log_set_log_level(struct log_target *target, int log_level); |
| 232 | void log_parse_category_mask(struct log_target *target, const char* mask); |
| 233 | int log_parse_level(const char *lvl); |
Harald Welte | 9ac2225 | 2010-05-11 11:19:40 +0200 | [diff] [blame] | 234 | const char *log_level_str(unsigned int lvl); |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 235 | int log_parse_category(const char *category); |
| 236 | void log_set_category_filter(struct log_target *target, int category, |
| 237 | int enable, int level); |
| 238 | |
| 239 | /* management of the targets */ |
| 240 | struct log_target *log_target_create(void); |
Harald Welte | 72d0c53 | 2010-08-25 19:24:00 +0200 | [diff] [blame] | 241 | void log_target_destroy(struct log_target *target); |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 242 | struct log_target *log_target_create_stderr(void); |
Harald Welte | 72d0c53 | 2010-08-25 19:24:00 +0200 | [diff] [blame] | 243 | struct log_target *log_target_create_file(const char *fname); |
Harald Welte | 46cfd77 | 2011-02-17 15:56:56 +0100 | [diff] [blame] | 244 | struct log_target *log_target_create_syslog(const char *ident, int option, |
| 245 | int facility); |
Harald Welte | 72d0c53 | 2010-08-25 19:24:00 +0200 | [diff] [blame] | 246 | int log_target_file_reopen(struct log_target *tgt); |
Harald Welte | 4de854d | 2013-03-18 19:01:40 +0100 | [diff] [blame] | 247 | int log_targets_reopen(void); |
Harald Welte | 72d0c53 | 2010-08-25 19:24:00 +0200 | [diff] [blame] | 248 | |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 249 | void log_add_target(struct log_target *target); |
| 250 | void log_del_target(struct log_target *target); |
| 251 | |
Pablo Neira Ayuso | 04139f1 | 2011-03-09 13:05:08 +0100 | [diff] [blame] | 252 | /* Generate command string for VTY use */ |
| 253 | const char *log_vty_command_string(const struct log_info *info); |
| 254 | const char *log_vty_command_description(const struct log_info *info); |
Harald Welte | 7638af9 | 2010-05-11 16:39:22 +0200 | [diff] [blame] | 255 | |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 256 | struct log_target *log_target_find(int type, const char *fname); |
| 257 | extern struct llist_head osmo_log_target_list; |
| 258 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 259 | /*! @} */ |