blob: 20c1254afec5a3ffd29c6e2978986b23e1214b53 [file] [log] [blame]
Harald Welte3ae27582010-03-26 21:24:24 +08001#ifndef _OSMOCORE_LOGGING_H
2#define _OSMOCORE_LOGGING_H
3
4#include <stdio.h>
5#include <stdint.h>
Pablo Neira Ayuso83419342011-03-22 16:36:13 +01006#include <osmocom/core/linuxlist.h>
Harald Welte3ae27582010-03-26 21:24:24 +08007
Harald Welte3ae27582010-03-26 21:24:24 +08008#define LOG_MAX_CTX 8
9#define LOG_MAX_FILTERS 8
10
11#define DEBUG
12
13#ifdef DEBUG
14#define DEBUGP(ss, fmt, args...) logp(ss, __FILE__, __LINE__, 0, fmt, ## args)
15#define DEBUGPC(ss, fmt, args...) logp(ss, __FILE__, __LINE__, 1, fmt, ## args)
16#else
17#define DEBUGP(xss, fmt, args...)
18#define DEBUGPC(ss, fmt, args...)
19#endif
20
Harald Welte3ae27582010-03-26 21:24:24 +080021
Harald Welteb43bc042011-06-27 10:29:17 +020022void logp(int subsys, char *file, int line, int cont, const char *format, ...) __attribute__ ((format (printf, 5, 6)));
Harald Welte3ae27582010-03-26 21:24:24 +080023
24/* new logging interface */
25#define LOGP(ss, level, fmt, args...) \
26 logp2(ss, level, __FILE__, __LINE__, 0, fmt, ##args)
27#define LOGPC(ss, level, fmt, args...) \
28 logp2(ss, level, __FILE__, __LINE__, 1, fmt, ##args)
29
30/* different levels */
31#define LOGL_DEBUG 1 /* debugging information */
32#define LOGL_INFO 3
33#define LOGL_NOTICE 5 /* abnormal/unexpected condition */
34#define LOGL_ERROR 7 /* error condition, requires user action */
35#define LOGL_FATAL 8 /* fatal, program aborted */
36
37#define LOG_FILTER_ALL 0x0001
38
Harald Welteb43bc042011-06-27 10:29:17 +020039/* logging levels defined by the library itself */
40#define DLGLOBAL -1
Harald Welte1f0b8c22011-06-27 10:51:37 +020041#define DLLAPDM -2
Harald Welte892e6212011-07-19 14:31:44 +020042#define DLINP -3
43#define DLMUX -4
44#define DLMI -5
45#define DLMIB -6
46#define DLRSL -7
47#define DLNM -8
Pablo Neira Ayuso199f3772011-07-07 19:46:38 +020048#define OSMO_NUM_DLIB 9
Harald Welteb43bc042011-06-27 10:29:17 +020049
Harald Welte3ae27582010-03-26 21:24:24 +080050struct log_category {
51 uint8_t loglevel;
52 uint8_t enabled;
53};
54
55struct log_info_cat {
56 const char *name;
57 const char *color;
58 const char *description;
59 uint8_t loglevel;
60 uint8_t enabled;
61};
62
63/* log context information, passed to filter */
64struct log_context {
65 void *ctx[LOG_MAX_CTX+1];
66};
67
68struct log_target;
69
70typedef int log_filter(const struct log_context *ctx,
71 struct log_target *target);
72
73struct log_info {
74 /* filter callback function */
75 log_filter *filter_fn;
76
77 /* per-category information */
Harald Welteb43bc042011-06-27 10:29:17 +020078 struct log_info_cat *cat;
Harald Welte3ae27582010-03-26 21:24:24 +080079 unsigned int num_cat;
Harald Welteb43bc042011-06-27 10:29:17 +020080 unsigned int num_cat_user;
Harald Welte3ae27582010-03-26 21:24:24 +080081};
82
Harald Welte28222962011-02-18 20:37:04 +010083enum log_target_type {
84 LOG_TGT_TYPE_VTY,
85 LOG_TGT_TYPE_SYSLOG,
86 LOG_TGT_TYPE_FILE,
87 LOG_TGT_TYPE_STDERR,
88};
89
Harald Welte3ae27582010-03-26 21:24:24 +080090struct log_target {
91 struct llist_head entry;
92
93 int filter_map;
94 void *filter_data[LOG_MAX_FILTERS+1];
95
Harald Welteb43bc042011-06-27 10:29:17 +020096 struct log_category *categories;
97
Harald Welte3ae27582010-03-26 21:24:24 +080098 uint8_t loglevel;
Harald Welte87dbca12011-07-16 11:57:53 +020099 unsigned int use_color:1;
100 unsigned int print_timestamp:1;
Harald Welte3ae27582010-03-26 21:24:24 +0800101
Harald Welte28222962011-02-18 20:37:04 +0100102 enum log_target_type type;
103
Harald Welte3ae27582010-03-26 21:24:24 +0800104 union {
105 struct {
106 FILE *out;
Harald Welte72d0c532010-08-25 19:24:00 +0200107 const char *fname;
Harald Welte0083cd32010-08-25 14:55:44 +0200108 } tgt_file;
Harald Welte3ae27582010-03-26 21:24:24 +0800109
110 struct {
111 int priority;
Harald Welte28222962011-02-18 20:37:04 +0100112 int facility;
Harald Welte3ae27582010-03-26 21:24:24 +0800113 } tgt_syslog;
114
115 struct {
116 void *vty;
117 } tgt_vty;
118 };
119
Harald Welte76e72ab2011-02-17 15:52:39 +0100120 void (*output) (struct log_target *target, unsigned int level,
121 const char *string);
Harald Welte3ae27582010-03-26 21:24:24 +0800122};
123
124/* use the above macros */
Harald Welteb43bc042011-06-27 10:29:17 +0200125void logp2(int subsys, unsigned int level, char *file,
Harald Welte3ae27582010-03-26 21:24:24 +0800126 int line, int cont, const char *format, ...)
127 __attribute__ ((format (printf, 6, 7)));
Harald Welteb43bc042011-06-27 10:29:17 +0200128int log_init(const struct log_info *inf, void *talloc_ctx);
Harald Welte3ae27582010-03-26 21:24:24 +0800129
130/* context management */
131void log_reset_context(void);
132int log_set_context(uint8_t ctx, void *value);
133
134/* filter on the targets */
135void log_set_all_filter(struct log_target *target, int);
136
137void log_set_use_color(struct log_target *target, int);
138void log_set_print_timestamp(struct log_target *target, int);
139void log_set_log_level(struct log_target *target, int log_level);
140void log_parse_category_mask(struct log_target *target, const char* mask);
141int log_parse_level(const char *lvl);
Harald Welte9ac22252010-05-11 11:19:40 +0200142const char *log_level_str(unsigned int lvl);
Harald Welte3ae27582010-03-26 21:24:24 +0800143int log_parse_category(const char *category);
144void log_set_category_filter(struct log_target *target, int category,
145 int enable, int level);
146
147/* management of the targets */
148struct log_target *log_target_create(void);
Harald Welte72d0c532010-08-25 19:24:00 +0200149void log_target_destroy(struct log_target *target);
Harald Welte3ae27582010-03-26 21:24:24 +0800150struct log_target *log_target_create_stderr(void);
Harald Welte72d0c532010-08-25 19:24:00 +0200151struct log_target *log_target_create_file(const char *fname);
Harald Welte46cfd772011-02-17 15:56:56 +0100152struct log_target *log_target_create_syslog(const char *ident, int option,
153 int facility);
Harald Welte72d0c532010-08-25 19:24:00 +0200154int log_target_file_reopen(struct log_target *tgt);
155
Harald Welte3ae27582010-03-26 21:24:24 +0800156void log_add_target(struct log_target *target);
157void log_del_target(struct log_target *target);
158
Pablo Neira Ayuso04139f12011-03-09 13:05:08 +0100159/* Generate command string for VTY use */
160const char *log_vty_command_string(const struct log_info *info);
161const char *log_vty_command_description(const struct log_info *info);
Harald Welte7638af92010-05-11 16:39:22 +0200162
Harald Welte28222962011-02-18 20:37:04 +0100163struct log_target *log_target_find(int type, const char *fname);
164extern struct llist_head osmo_log_target_list;
165
Harald Welte3ae27582010-03-26 21:24:24 +0800166#endif /* _OSMOCORE_LOGGING_H */