|
8 | 8 | namespace odr |
9 | 9 | { |
10 | 10 |
|
11 | | -enum LogLevel |
| 11 | +namespace log |
| 12 | +{ |
| 13 | + |
| 14 | +enum Level |
12 | 15 | { |
13 | 16 | Info = 0, |
14 | 17 | Warn = 1, |
15 | 18 | Error = 2 |
16 | 19 | }; |
17 | 20 |
|
18 | | -inline const char* log_level_to_string(const LogLevel level) |
| 21 | +inline const char* level_to_string(const Level level) |
19 | 22 | { |
20 | 23 | switch (level) |
21 | 24 | { |
22 | | - case LogLevel::Info: |
| 25 | + case Level::Info: |
23 | 26 | return "INFO"; |
24 | | - case LogLevel::Warn: |
| 27 | + case Level::Warn: |
25 | 28 | return "WARN"; |
26 | | - case LogLevel::Error: |
| 29 | + case Level::Error: |
27 | 30 | return "ERROR"; |
28 | 31 | default: |
29 | 32 | return "UNKNOWN"; |
30 | 33 | } |
31 | 34 | } |
32 | 35 |
|
33 | | -using LogFunction = void (*)(LogLevel, const char*); |
| 36 | +using LogFunction = void (*)(Level, const char*); |
34 | 37 |
|
35 | | -inline void default_log_function(LogLevel lvl, const char* msg) |
| 38 | +inline void default_log_function(Level lvl, const char* msg) |
36 | 39 | { |
37 | | - std::fprintf(stderr, "[%s] %s\n", log_level_to_string(lvl), msg); |
| 40 | + std::fprintf(stderr, "[%s] %s\n", level_to_string(lvl), msg); |
38 | 41 | } |
39 | 42 |
|
40 | 43 | inline std::atomic<LogFunction> g_log_function{&default_log_function}; |
41 | | -inline std::atomic<LogLevel> g_log_level{LogLevel::Warn}; |
| 44 | +inline std::atomic<Level> g_log_level{Level::Warn}; |
42 | 45 |
|
43 | | -inline void set_log_callback(LogFunction log_function) |
| 46 | +inline void set_callback(LogFunction log_function) |
44 | 47 | { |
45 | 48 | g_log_function.store(log_function, std::memory_order_relaxed); |
46 | 49 | } |
47 | 50 |
|
48 | | -inline void set_log_level(LogLevel lvl) |
| 51 | +inline void set_level(Level lvl) |
49 | 52 | { |
50 | 53 | g_log_level.store(lvl, std::memory_order_relaxed); |
51 | 54 | } |
52 | 55 |
|
53 | | -inline void log(LogLevel lvl, const char* msg) |
| 56 | +template<class... Args> |
| 57 | +void log(Level lvl, const char* fmt, Args&&... args) |
54 | 58 | { |
55 | 59 | if (lvl < g_log_level.load(std::memory_order_relaxed)) |
56 | 60 | return; |
57 | | - g_log_function.load(std::memory_order_relaxed)(lvl, msg); |
| 61 | + std::string s = strfmt(fmt, std::forward<Args>(args)...); |
| 62 | + g_log_function.load(std::memory_order_relaxed)(lvl, s.c_str()); |
58 | 63 | } |
59 | 64 |
|
60 | 65 | template<class... Args> |
61 | | -void logf(LogLevel lvl, const char* fmt, Args&&... args) |
| 66 | +void info(const char* fmt, Args&&... args) |
62 | 67 | { |
63 | | - std::string s = strfmt(fmt, std::forward<Args>(args)...); |
64 | | - log(lvl, s.c_str()); |
| 68 | + log(Level::Info, fmt, std::forward<Args>(args)...); |
| 69 | +} |
| 70 | + |
| 71 | +template<class... Args> |
| 72 | +void warn(const char* fmt, Args&&... args) |
| 73 | +{ |
| 74 | + log(Level::Warn, fmt, std::forward<Args>(args)...); |
65 | 75 | } |
66 | 76 |
|
| 77 | +template<class... Args> |
| 78 | +void error(const char* fmt, Args&&... args) |
| 79 | +{ |
| 80 | + log(Level::Error, fmt, std::forward<Args>(args)...); |
| 81 | +} |
| 82 | + |
| 83 | +} // namespace log |
| 84 | + |
67 | 85 | } // namespace odr |
0 commit comments