|
9 | 9 | #include <string>
|
10 | 10 |
|
11 | 11 | #include "ark/error.hpp"
|
| 12 | +#include "ark/log.hpp" |
12 | 13 |
|
13 | 14 | namespace ark {
|
14 | 15 |
|
15 |
| -typedef enum { DEBUG, INFO, WARN, ERROR } LogLevel; |
16 |
| - |
17 | 16 | class Logging {
|
18 | 17 | public:
|
19 | 18 | Logging(const std::string &lv);
|
@@ -46,43 +45,44 @@ void _log_helper(std::stringstream &ss, T value, Args... args) {
|
46 | 45 | _log_helper(ss, args...);
|
47 | 46 | }
|
48 | 47 |
|
49 |
| -template <LogLevel Level, bool AppendNewLine, typename T, typename... Args> |
50 |
| -inline std::string _log_msg(const std::string &file, int line, T value, |
| 48 | +template <typename T, typename... Args> |
| 49 | +inline std::string _log_msg(LogLevel level, bool append_newline, |
| 50 | + const std::string &file, int line, T value, |
51 | 51 | Args... args) {
|
52 | 52 | std::stringstream ss;
|
53 |
| - _log_header(ss, Level, file, line); |
| 53 | + _log_header(ss, level, file, line); |
54 | 54 | _log_helper(ss, value, args...);
|
55 |
| - if constexpr (AppendNewLine) ss << std::endl; |
| 55 | + if (append_newline) ss << std::endl; |
56 | 56 | return ss.str();
|
57 | 57 | }
|
58 | 58 |
|
59 |
| -template <LogLevel Level, typename T, typename... Args> |
60 |
| -inline void _log(const std::string &file, int line, T value, Args... args) { |
61 |
| - if (Level >= get_logging().get_level()) { |
62 |
| - std::clog << _log_msg<Level, true>(file, line, value, args...); |
| 59 | +template <typename T, typename... Args> |
| 60 | +inline void _log(LogLevel level, const std::string &file, int line, T value, |
| 61 | + Args... args) { |
| 62 | + if (level >= get_logging().get_level()) { |
| 63 | + std::clog << _log_msg(level, true, file, line, value, args...); |
63 | 64 | }
|
64 |
| - if constexpr (Level == ERROR) { |
| 65 | + if (level == ERROR) { |
65 | 66 | throw std::runtime_error("ARK runtime error");
|
66 | 67 | }
|
67 | 68 | }
|
68 | 69 |
|
69 | 70 | template <typename Exception, typename T, typename... Args>
|
70 | 71 | inline void _err(const std::string &file, int line, T value, Args... args) {
|
71 |
| - throw Exception(_log_msg<ERROR, false>(file, line, value, args...)); |
| 72 | + throw Exception(_log_msg(ERROR, false, file, line, value, args...)); |
72 | 73 | }
|
73 | 74 |
|
74 | 75 | // Logging.
|
75 | 76 | #define LOG(level, ...) \
|
76 | 77 | do { \
|
77 |
| - ark::_log<level>(__FILE__, __LINE__, __VA_ARGS__); \ |
| 78 | + ark::_log(level, __FILE__, __LINE__, __VA_ARGS__); \ |
78 | 79 | break; \
|
79 | 80 | } while (0)
|
80 | 81 |
|
81 |
| -#define ERR(exception, ...) \ |
82 |
| - do { \ |
83 |
| - std::string exc_str = " (" #exception ")"; \ |
84 |
| - ark::_err<exception>(__FILE__, __LINE__, __VA_ARGS__, exc_str); \ |
85 |
| - break; \ |
| 82 | +#define ERR(exception, ...) \ |
| 83 | + do { \ |
| 84 | + ark::_err<exception>(__FILE__, __LINE__, __VA_ARGS__); \ |
| 85 | + break; \ |
86 | 86 | } while (0)
|
87 | 87 |
|
88 | 88 | #define CHECK(cond) \
|
|
0 commit comments