Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 1 | #ifndef _OSMOCORE_LOGGING_H |
| 2 | #define _OSMOCORE_LOGGING_H |
| 3 | |
| 4 | #include <stdio.h> |
| 5 | #include <stdint.h> |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 6 | #include <osmocom/core/linuxlist.h> |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 7 | |
| 8 | #define LOG_MAX_CATEGORY 32 |
| 9 | #define LOG_MAX_CTX 8 |
| 10 | #define LOG_MAX_FILTERS 8 |
| 11 | |
| 12 | #define DEBUG |
| 13 | |
| 14 | #ifdef DEBUG |
| 15 | #define DEBUGP(ss, fmt, args...) logp(ss, __FILE__, __LINE__, 0, fmt, ## args) |
| 16 | #define DEBUGPC(ss, fmt, args...) logp(ss, __FILE__, __LINE__, 1, fmt, ## args) |
| 17 | #else |
| 18 | #define DEBUGP(xss, fmt, args...) |
| 19 | #define DEBUGPC(ss, fmt, args...) |
| 20 | #endif |
| 21 | |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 22 | |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 23 | void logp(unsigned int subsys, char *file, int line, int cont, const char *format, ...) __attribute__ ((format (printf, 5, 6))); |
| 24 | |
| 25 | /* new logging interface */ |
| 26 | #define LOGP(ss, level, fmt, args...) \ |
| 27 | logp2(ss, level, __FILE__, __LINE__, 0, fmt, ##args) |
| 28 | #define LOGPC(ss, level, fmt, args...) \ |
| 29 | logp2(ss, level, __FILE__, __LINE__, 1, fmt, ##args) |
| 30 | |
| 31 | /* different levels */ |
| 32 | #define LOGL_DEBUG 1 /* debugging information */ |
| 33 | #define LOGL_INFO 3 |
| 34 | #define LOGL_NOTICE 5 /* abnormal/unexpected condition */ |
| 35 | #define LOGL_ERROR 7 /* error condition, requires user action */ |
| 36 | #define LOGL_FATAL 8 /* fatal, program aborted */ |
| 37 | |
| 38 | #define LOG_FILTER_ALL 0x0001 |
| 39 | |
| 40 | struct log_category { |
| 41 | uint8_t loglevel; |
| 42 | uint8_t enabled; |
| 43 | }; |
| 44 | |
| 45 | struct log_info_cat { |
| 46 | const char *name; |
| 47 | const char *color; |
| 48 | const char *description; |
| 49 | uint8_t loglevel; |
| 50 | uint8_t enabled; |
| 51 | }; |
| 52 | |
| 53 | /* log context information, passed to filter */ |
| 54 | struct log_context { |
| 55 | void *ctx[LOG_MAX_CTX+1]; |
| 56 | }; |
| 57 | |
| 58 | struct log_target; |
| 59 | |
| 60 | typedef int log_filter(const struct log_context *ctx, |
| 61 | struct log_target *target); |
| 62 | |
| 63 | struct log_info { |
| 64 | /* filter callback function */ |
| 65 | log_filter *filter_fn; |
| 66 | |
| 67 | /* per-category information */ |
| 68 | const struct log_info_cat *cat; |
| 69 | unsigned int num_cat; |
| 70 | }; |
| 71 | |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 72 | enum log_target_type { |
| 73 | LOG_TGT_TYPE_VTY, |
| 74 | LOG_TGT_TYPE_SYSLOG, |
| 75 | LOG_TGT_TYPE_FILE, |
| 76 | LOG_TGT_TYPE_STDERR, |
| 77 | }; |
| 78 | |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 79 | struct log_target { |
| 80 | struct llist_head entry; |
| 81 | |
| 82 | int filter_map; |
| 83 | void *filter_data[LOG_MAX_FILTERS+1]; |
| 84 | |
| 85 | struct log_category categories[LOG_MAX_CATEGORY+1]; |
| 86 | uint8_t loglevel; |
| 87 | int use_color:1; |
| 88 | int print_timestamp:1; |
| 89 | |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 90 | enum log_target_type type; |
| 91 | |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 92 | union { |
| 93 | struct { |
| 94 | FILE *out; |
Harald Welte | 72d0c53 | 2010-08-25 19:24:00 +0200 | [diff] [blame] | 95 | const char *fname; |
Harald Welte | 0083cd3 | 2010-08-25 14:55:44 +0200 | [diff] [blame] | 96 | } tgt_file; |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 97 | |
| 98 | struct { |
| 99 | int priority; |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 100 | int facility; |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 101 | } tgt_syslog; |
| 102 | |
| 103 | struct { |
| 104 | void *vty; |
| 105 | } tgt_vty; |
| 106 | }; |
| 107 | |
Harald Welte | 76e72ab | 2011-02-17 15:52:39 +0100 | [diff] [blame] | 108 | void (*output) (struct log_target *target, unsigned int level, |
| 109 | const char *string); |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | /* use the above macros */ |
| 113 | void logp2(unsigned int subsys, unsigned int level, char *file, |
| 114 | int line, int cont, const char *format, ...) |
| 115 | __attribute__ ((format (printf, 6, 7))); |
| 116 | void log_init(const struct log_info *cat); |
| 117 | |
| 118 | /* context management */ |
| 119 | void log_reset_context(void); |
| 120 | int log_set_context(uint8_t ctx, void *value); |
| 121 | |
| 122 | /* filter on the targets */ |
| 123 | void log_set_all_filter(struct log_target *target, int); |
| 124 | |
| 125 | void log_set_use_color(struct log_target *target, int); |
| 126 | void log_set_print_timestamp(struct log_target *target, int); |
| 127 | void log_set_log_level(struct log_target *target, int log_level); |
| 128 | void log_parse_category_mask(struct log_target *target, const char* mask); |
| 129 | int log_parse_level(const char *lvl); |
Harald Welte | 9ac2225 | 2010-05-11 11:19:40 +0200 | [diff] [blame] | 130 | const char *log_level_str(unsigned int lvl); |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 131 | int log_parse_category(const char *category); |
| 132 | void log_set_category_filter(struct log_target *target, int category, |
| 133 | int enable, int level); |
| 134 | |
| 135 | /* management of the targets */ |
| 136 | struct log_target *log_target_create(void); |
Harald Welte | 72d0c53 | 2010-08-25 19:24:00 +0200 | [diff] [blame] | 137 | void log_target_destroy(struct log_target *target); |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 138 | struct log_target *log_target_create_stderr(void); |
Harald Welte | 72d0c53 | 2010-08-25 19:24:00 +0200 | [diff] [blame] | 139 | struct log_target *log_target_create_file(const char *fname); |
Harald Welte | 46cfd77 | 2011-02-17 15:56:56 +0100 | [diff] [blame] | 140 | struct log_target *log_target_create_syslog(const char *ident, int option, |
| 141 | int facility); |
Harald Welte | 72d0c53 | 2010-08-25 19:24:00 +0200 | [diff] [blame] | 142 | int log_target_file_reopen(struct log_target *tgt); |
| 143 | |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 144 | void log_add_target(struct log_target *target); |
| 145 | void log_del_target(struct log_target *target); |
| 146 | |
Pablo Neira Ayuso | 04139f1 | 2011-03-09 13:05:08 +0100 | [diff] [blame] | 147 | /* Generate command string for VTY use */ |
| 148 | const char *log_vty_command_string(const struct log_info *info); |
| 149 | const char *log_vty_command_description(const struct log_info *info); |
Harald Welte | 7638af9 | 2010-05-11 16:39:22 +0200 | [diff] [blame] | 150 | |
Harald Welte | 2822296 | 2011-02-18 20:37:04 +0100 | [diff] [blame] | 151 | struct log_target *log_target_find(int type, const char *fname); |
| 152 | extern struct llist_head osmo_log_target_list; |
| 153 | |
Harald Welte | 3ae2758 | 2010-03-26 21:24:24 +0800 | [diff] [blame] | 154 | #endif /* _OSMOCORE_LOGGING_H */ |