libosmocore  0.9.3
Osmocom core library
include/osmocom/core/logging.h
Go to the documentation of this file.
00001 #pragma once
00002 
00009 #include <stdio.h>
00010 #include <stdint.h>
00011 #include <stdarg.h>
00012 #include <osmocom/core/linuxlist.h>
00013 
00015 #define LOG_MAX_CTX             8
00016 
00017 #define LOG_MAX_FILTERS 8
00018 
00019 #define DEBUG
00020 
00021 #ifdef DEBUG
00022 #define DEBUGP(ss, fmt, args...) logp(ss, __FILE__, __LINE__, 0, fmt, ## args)
00023 #define DEBUGPC(ss, fmt, args...) logp(ss, __FILE__, __LINE__, 1, fmt, ## args)
00024 #else
00025 #define DEBUGP(xss, fmt, args...)
00026 #define DEBUGPC(ss, fmt, args...)
00027 #endif
00028 
00029 
00030 void osmo_vlogp(int subsys, int level, const char *file, int line,
00031                 int cont, const char *format, va_list ap);
00032 
00033 void logp(int subsys, const char *file, int line, int cont, const char *format, ...) __attribute__ ((format (printf, 5, 6)));
00034 
00041 #define LOGP(ss, level, fmt, args...) \
00042         logp2(ss, level, __FILE__, __LINE__, 0, fmt, ##args)
00043 
00050 #define LOGPC(ss, level, fmt, args...) \
00051         logp2(ss, level, __FILE__, __LINE__, 1, fmt, ##args)
00052 
00054 #define LOGL_DEBUG      1       
00055 #define LOGL_INFO       3
00056 #define LOGL_NOTICE     5       
00057 #define LOGL_ERROR      7       
00058 #define LOGL_FATAL      8       
00060 #define LOG_FILTER_ALL  0x0001
00061 
00062 /* logging levels defined by the library itself */
00063 #define DLGLOBAL        -1
00064 #define DLLAPD          -2
00065 #define DLINP           -3
00066 #define DLMUX           -4
00067 #define DLMI            -5
00068 #define DLMIB           -6
00069 #define DLSMS           -7
00070 #define DLCTRL          -8
00071 #define DLGTP           -9
00072 #define DLSTATS         -10
00073 #define OSMO_NUM_DLIB   10
00074 
00075 struct log_category {
00076         uint8_t loglevel;
00077         uint8_t enabled;
00078 };
00079 
00081 struct log_info_cat {
00082         const char *name;               
00083         const char *color;              
00084         const char *description;        
00085         uint8_t loglevel;               
00086         uint8_t enabled;                
00087 };
00088 
00090 struct log_context {
00091         void *ctx[LOG_MAX_CTX+1];
00092 };
00093 
00094 struct log_target;
00095 
00097 typedef int log_filter(const struct log_context *ctx,
00098                        struct log_target *target);
00099 
00100 struct log_info;
00101 struct vty;
00102 
00103 typedef void log_print_filters(struct vty *vty,
00104                                const struct log_info *info,
00105                                const struct log_target *tgt);
00106 
00107 typedef void log_save_filters(struct vty *vty,
00108                               const struct log_info *info,
00109                               const struct log_target *tgt);
00110 
00112 struct log_info {
00113         /* \brief filter callback function */
00114         log_filter *filter_fn;
00115 
00117         const struct log_info_cat *cat;
00119         unsigned int num_cat;
00121         unsigned int num_cat_user;
00122 
00123         /* \brief filter saving function */
00124         log_save_filters *save_fn;
00125         /* \brief filter saving function */
00126         log_print_filters *print_fn;
00127 };
00128 
00130 enum log_target_type {
00131         LOG_TGT_TYPE_VTY,       
00132         LOG_TGT_TYPE_SYSLOG,    
00133         LOG_TGT_TYPE_FILE,      
00134         LOG_TGT_TYPE_STDERR,    
00135         LOG_TGT_TYPE_STRRB,     
00136 };
00137 
00139 struct log_target {
00140         struct llist_head entry;                
00143         int filter_map;
00145         void *filter_data[LOG_MAX_FILTERS+1];
00146 
00148         struct log_category *categories;
00149 
00151         uint8_t loglevel;
00153         unsigned int use_color:1;
00155         unsigned int print_timestamp:1;
00157         unsigned int print_filename:1;
00159         unsigned int print_category:1;
00161         unsigned int print_ext_timestamp:1;
00162 
00164         enum log_target_type type;
00165 
00166         union {
00167                 struct {
00168                         FILE *out;
00169                         const char *fname;
00170                 } tgt_file;
00171 
00172                 struct {
00173                         int priority;
00174                         int facility;
00175                 } tgt_syslog;
00176 
00177                 struct {
00178                         void *vty;
00179                 } tgt_vty;
00180 
00181                 struct {
00182                         void *rb;
00183                 } tgt_rb;
00184         };
00185 
00192         void (*output) (struct log_target *target, unsigned int level,
00193                         const char *string);
00194 };
00195 
00196 /* use the above macros */
00197 void logp2(int subsys, unsigned int level, const char *file,
00198            int line, int cont, const char *format, ...)
00199                                 __attribute__ ((format (printf, 6, 7)));
00200 int log_init(const struct log_info *inf, void *talloc_ctx);
00201 
00202 /* context management */
00203 void log_reset_context(void);
00204 int log_set_context(uint8_t ctx, void *value);
00205 
00206 /* filter on the targets */
00207 void log_set_all_filter(struct log_target *target, int);
00208 
00209 void log_set_use_color(struct log_target *target, int);
00210 void log_set_print_extended_timestamp(struct log_target *target, int);
00211 void log_set_print_timestamp(struct log_target *target, int);
00212 void log_set_print_filename(struct log_target *target, int);
00213 void log_set_print_category(struct log_target *target, int);
00214 void log_set_log_level(struct log_target *target, int log_level);
00215 void log_parse_category_mask(struct log_target *target, const char* mask);
00216 int log_parse_level(const char *lvl);
00217 const char *log_level_str(unsigned int lvl);
00218 int log_parse_category(const char *category);
00219 void log_set_category_filter(struct log_target *target, int category,
00220                                int enable, int level);
00221 
00222 /* management of the targets */
00223 struct log_target *log_target_create(void);
00224 void log_target_destroy(struct log_target *target);
00225 struct log_target *log_target_create_stderr(void);
00226 struct log_target *log_target_create_file(const char *fname);
00227 struct log_target *log_target_create_syslog(const char *ident, int option,
00228                                             int facility);
00229 int log_target_file_reopen(struct log_target *tgt);
00230 int log_targets_reopen(void);
00231 
00232 void log_add_target(struct log_target *target);
00233 void log_del_target(struct log_target *target);
00234 
00235 /* Generate command string for VTY use */
00236 const char *log_vty_command_string(const struct log_info *info);
00237 const char *log_vty_command_description(const struct log_info *info);
00238 
00239 struct log_target *log_target_find(int type, const char *fname);
00240 extern struct llist_head osmo_log_target_list;
00241