Leosac  0.8.0
Open Source Access Control
log.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014-2016 Leosac
3 
4  This file is part of Leosac.
5 
6  Leosac is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Affero General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Leosac is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Affero General Public License for more details.
15 
16  You should have received a copy of the GNU Affero General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #pragma once
21 
22 #include <cassert>
23 #include <csignal>
24 #include <iostream>
25 #include <spdlog/spdlog.h>
26 #include <sstream>
27 #include <string>
28 
30 {
31  CRIT = spdlog::level::critical,
32  ERROR = spdlog::level::err,
33  WARN = spdlog::level::warn,
34  INFO = spdlog::level::info,
35  DEBUG = spdlog::level::debug,
36 };
37 
38 namespace LogHelper
39 {
40 LogLevel log_level_from_string(const std::string &level);
41 
42 void log(const std::string &log_msg, int /*line*/, const char * /*funcName*/,
43  const char * /*fileName*/, LogLevel level);
44 };
45 
52 #ifdef __GNUC__
53 #define FUNCTION_NAME_MACRO __PRETTY_FUNCTION__
54 #else
55 #define FUNCTION_NAME_MACRO __FUNCTION__
56 #endif
57 
63 #define BUILD_STR(param) \
64  [&](void) { \
65  std::stringstream logger_macro_ss__; \
66  logger_macro_ss__ << param; \
67  return logger_macro_ss__.str(); \
68  }()
69 
70 
74 #define DEBUG_0(msg) \
75  LogHelper::log(BUILD_STR(msg), __LINE__, FUNCTION_NAME_MACRO, __FILE__, \
76  LogLevel::DEBUG)
77 
81 #define DEBUG_1(msg, loggers) \
82  LogHelper::log(BUILD_STR(msg), __LINE__, FUNCTION_NAME_MACRO, __FILE__, \
83  LogLevel::DEBUG, loggers)
84 
88 #define DEBUG_X(trash, msg, loggers, targetMacro, ...) targetMacro
89 
94 #define DEBUG(...) \
95  DEBUG_X(, ##__VA_ARGS__, DEBUG_1(__VA_ARGS__), DEBUG_0(__VA_ARGS__), \
96  DEBUG_NO_PARAM(__VA_ARGS__), )
97 
98 
102 #define INFO_0(msg) \
103  LogHelper::log(BUILD_STR(msg), __LINE__, FUNCTION_NAME_MACRO, __FILE__, \
104  LogLevel::INFO)
105 
109 #define INFO_1(msg, loggers) \
110  LogHelper::log(BUILD_STR(msg), __LINE__, FUNCTION_NAME_MACRO, __FILE__, \
111  LogLevel::INFO, loggers)
112 
116 #define INFO_X(trash, msg, loggers, targetMacro, ...) targetMacro
117 
122 #define INFO(...) \
123  INFO_X(, ##__VA_ARGS__, INFO_1(__VA_ARGS__), INFO_0(__VA_ARGS__), \
124  INFO_NO_PARAM(__VA_ARGS__), )
125 
126 
130 #define WARN_0(msg) \
131  LogHelper::log(BUILD_STR(msg), __LINE__, FUNCTION_NAME_MACRO, __FILE__, \
132  LogLevel::WARN)
133 
137 #define WARN_1(msg, loggers) \
138  LogHelper::log(BUILD_STR(msg), __LINE__, FUNCTION_NAME_MACRO, __FILE__, \
139  LogLevel::WARN)
140 
144 #define WARN_X(trash, msg, loggers, targetMacro, ...) targetMacro
145 
150 #define WARN(...) \
151  WARN_X(, ##__VA_ARGS__, WARN_1(__VA_ARGS__), WARN_0(__VA_ARGS__), \
152  WARN_NO_PARAM(__VA_ARGS__), )
153 
154 
158 #define ERROR_0(msg) \
159  LogHelper::log(BUILD_STR(msg), __LINE__, FUNCTION_NAME_MACRO, __FILE__, \
160  LogLevel::ERROR)
161 
165 #define ERROR_1(msg, loggers) \
166  LogHelper::log(BUILD_STR(msg), __LINE__, FUNCTION_NAME_MACRO, __FILE__, \
167  LogLevel::ERROR)
168 
172 #define ERROR_X(trash, msg, loggers, targetMacro, ...) targetMacro
173 
178 #define ERROR(...) \
179  ERROR_X(, ##__VA_ARGS__, ERROR_1(__VA_ARGS__), ERROR_0(__VA_ARGS__), \
180  ERROR_NO_PARAM(__VA_ARGS__), )
181 
182 
183 #ifdef NDEBUG
184 #define ASSERT_LOG(cond, msg) \
185  do \
186  { \
187  (void)sizeof(cond); \
188  } while (0)
189 #else
190 #define ASSERT_LOG(cond, msg) \
191  do \
192  { \
193  if (!(cond)) \
194  { \
195  ERROR(msg); \
196  ERROR("Assertion failed in " << __FILE__ << " --> " \
197  << FUNCTION_NAME_MACRO << ":" << __LINE__ \
198  << ". Aborting."); \
199  raise(SIGABRT); \
200  } \
201  } while (false)
202 #endif
INFO
#define INFO(...)
Information macro.
Definition: log.hpp:122
LogHelper
Definition: log.cpp:44
CRIT
@ CRIT
Definition: log.hpp:31
ERROR
#define ERROR(...)
Error macro.
Definition: log.hpp:178
WARN
#define WARN(...)
Warning macro.
Definition: log.hpp:150
DEBUG
#define DEBUG(...)
Debug macro.
Definition: log.hpp:94
LogHelper::log_level_from_string
LogLevel log_level_from_string(const std::string &level)
Definition: log.cpp:100
LogHelper::log
void log(const std::string &log_msg, int, const char *, const char *, LogLevel level)
Definition: log.cpp:47
LogLevel
LogLevel
Definition: log.hpp:29