Haka
log.h
Go to the documentation of this file.
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 
10 #ifndef HAKA_LOG_H
11 #define HAKA_LOG_H
12 
13 #include <haka/types.h>
14 #include <haka/compiler.h>
15 #include <haka/container/list.h>
16 
17 
21 typedef enum {
31 } log_level;
32 
33 #define HAKA_LOG_LEVEL_MAX 6
34 
41 const char *level_to_str(log_level level);
42 
47 log_level str_to_level(const char *str);
48 
52 typedef int section_id;
53 
57 #define INVALID_SECTION_ID -1
58 
62 section_id register_log_section(const char *name);
63 
64 #define DECLARE_LOG_SECTION(name) \
65  int _##name##_section;
66 
67 #define REGISTER_LOG_SECTION(name) \
68  int _##name##_section = LOG_SECTION(external); \
69  INIT static void _init_log_section_##name() { _##name##_section = register_log_section(#name); }
70 
71 #define LOG_SECTION(name) _##name##_section
72 
76 enum {
77  LOG_SECTION(core),
78  LOG_SECTION(packet),
79  LOG_SECTION(time),
80  LOG_SECTION(states),
81  LOG_SECTION(remote),
82  LOG_SECTION(external),
83  LOG_SECTION(lua)
84 };
85 
90 section_id search_log_section(const char *name);
91 
95 bool check_section_log_level(section_id section, log_level level);
96 
97 void _messagef(log_level level, section_id section, const char *fmt, ...) FORMAT_PRINTF(3, 4);
98 
99 #define SHOULD_LOG(level, section) \
100  (check_section_log_level(LOG_SECTION(section), level))
101 
102 #define LOG(level, section, fmt, ...) \
103  do { if (check_section_log_level(LOG_SECTION(section), level)) { \
104  _messagef(level, LOG_SECTION(section), fmt, ##__VA_ARGS__); \
105  } } while(0)
106 
110 #define LOG_FATAL(section, fmt, ...) LOG(HAKA_LOG_FATAL, section, fmt, ##__VA_ARGS__)
111 #define LOG_ERROR(section, fmt, ...) LOG(HAKA_LOG_ERROR, section, fmt, ##__VA_ARGS__)
112 #define LOG_WARNING(section, fmt, ...) LOG(HAKA_LOG_WARNING, section, fmt, ##__VA_ARGS__)
113 #define LOG_INFO(section, fmt, ...) LOG(HAKA_LOG_INFO, section, fmt, ##__VA_ARGS__)
114 #define LOG_DEBUG(section, fmt, ...) LOG(HAKA_LOG_DEBUG, section, fmt, ##__VA_ARGS__)
115 
116 #define SHOULD_LOG_FATAL(section) SHOULD_LOG(HAKA_LOG_FATAL, section)
117 #define SHOULD_LOG_ERROR(section) SHOULD_LOG(HAKA_LOG_ERROR, section)
118 #define SHOULD_LOG_WARNING(section) SHOULD_LOG(HAKA_LOG_WARNING, section)
119 #define SHOULD_LOG_INFO(section) SHOULD_LOG(HAKA_LOG_INFO, section)
120 #define SHOULD_LOG_DEBUG(section) SHOULD_LOG(HAKA_LOG_DEBUG, section)
121 
122 #ifdef HAKA_DEBUG
123  #define LOG_TRACE(section, fmt, ...) LOG(HAKA_LOG_TRACE, section, fmt, ##__VA_ARGS__)
124  #define SHOULD_LOG_TRACE(section) SHOULD_LOG(HAKA_LOG_TRACE, section)
125 #else
126  #define LOG_TRACE(section, fmt, ...)
127  #define SHOULD_LOG_TRACE(section) 0
128 #endif
129 
134 bool setlevel(log_level level, const char *name);
135 
139 log_level getlevel(const char *name);
140 
144 void enable_stdout_logging(bool enable);
145 
149 bool stdout_message(log_level lvl, const char *module, const char *message);
150 
154 struct logger {
155  struct list list;
156  void (*destroy)(struct logger *state);
157  int (*message)(struct logger *state, log_level level, const char *module, const char *message);
158  bool mark_for_remove;
159 };
160 
166 bool add_logger(struct logger *logger);
167 
171 bool remove_logger(struct logger *logger);
172 
176 void remove_all_logger();
177 
178 #endif /* HAKA_LOG_H */
void remove_all_logger()
Definition: log.c:188
log_level str_to_level(const char *str)
Definition: log.c:224
Definition: log.h:24
bool setlevel(log_level level, const char *name)
Definition: log.c:416
Definition: log.h:27
Definition: module.h:34
log_level getlevel(const char *name)
Definition: log.c:468
bool remove_logger(struct logger *logger)
Definition: log.c:156
Definition: log.h:22
int section_id
Definition: log.h:52
bool stdout_message(log_level lvl, const char *module, const char *message)
Definition: log.c:243
Definition: log.h:25
bool check_section_log_level(section_id section, log_level level)
Definition: log.c:403
Definition: packet.h:24
void enable_stdout_logging(bool enable)
Definition: log.c:238
Definition: log.h:23
Definition: log.h:30
Definition: log.h:154
bool add_logger(struct logger *logger)
Definition: log.c:136
Definition: log.h:29
Definition: log.h:26
log_level
Definition: log.h:21
const char * level_to_str(log_level level)
Definition: log.c:218
Definition: time.h:21
section_id search_log_section(const char *name)
Definition: log.c:313
section_id register_log_section(const char *name)
Definition: log.c:296