YACLib
C++ library for concurrent tasks execution
Loading...
Searching...
No Matches
log.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <string_view>
4
5namespace yaclib {
6
7// TODO(MBkkt) Maybe add callbacks overload for source_location, stacktrace arguments?
8
9using LogCallback = void (*)(std::string_view file, std::size_t line, std::string_view function,
10 std::string_view condition, std::string_view message) noexcept;
11
12namespace detail {
13
14enum class LogLevel : unsigned char {
15 Debug = 0,
16 Warn = 1,
17 Count = 3,
18};
19
21
22void LogMessage(LogLevel level, std::string_view file, std::size_t line, std::string_view func,
23 std::string_view condition, std::string_view message) noexcept;
24
25} // namespace detail
26} // namespace yaclib
27
28#ifndef YACLIB_FUNC_NAME
29# if defined(__clang__) || defined(__GNUC__)
30# define YACLIB_FUNC_NAME __PRETTY_FUNCTION__
31# elif defined(_MSC_VER)
32# define YACLIB_FUNC_NAME __FUNCSIG__
33# else
34# define YACLIB_FUNC_NAME __func__
35# endif
36#endif
37
38#define YACLIB_LOG_MESSAGE(level, cond, message) \
39 do { \
40 if (!!(cond)) { \
41 ::yaclib::detail::LogMessage(level, __FILE__, __LINE__, YACLIB_FUNC_NAME, #cond, (message)); \
42 } \
43 } while (false)
44
45#define YACLIB_SET_CALLBACK(level, callback) \
46 do { \
47 ::yaclib::detail::SetCallback(level, (callback)); \
48 } while (false)
49
50#ifdef NDEBUG
51# define YACLIB_STUB1(first) ((void)1)
52# define YACLIB_STUB2(first, second) ((void)1)
53#else
54# define YACLIB_STUB1(first) \
55 do { \
56 if (false) { \
57 (void)(first); \
58 } \
59 } while (false)
60# define YACLIB_STUB2(first, second) \
61 do { \
62 if (false) { \
63 (void)(first); \
64 (void)(second); \
65 } \
66 } while (false)
67#endif
68
69#ifdef YACLIB_LOG_WARN
70# define YACLIB_INIT_WARN(callback) YACLIB_SET_CALLBACK(::yaclib::detail::LogLevel::Warn, callback)
71# define YACLIB_WARN(cond, message) YACLIB_LOG_MESSAGE(::yaclib::detail::LogLevel::Warn, cond, message)
72#else
73# define YACLIB_INIT_WARN(callback) YACLIB_STUB1(callback)
74# define YACLIB_WARN(cond, message) YACLIB_STUB2(cond, message)
75#endif
76
77#ifdef YACLIB_LOG_DEBUG
78# define YACLIB_INIT_DEBUG(callback) YACLIB_SET_CALLBACK(::yaclib::detail::LogLevel::Debug, callback)
79# define YACLIB_DEBUG(cond, message) YACLIB_LOG_MESSAGE(::yaclib::detail::LogLevel::Debug, cond, message)
80# define YACLIB_ASSERT(cond) YACLIB_LOG_MESSAGE(::yaclib::detail::LogLevel::Debug, !(cond), "")
81# define YACLIB_PURE_VIRTUAL() YACLIB_LOG_MESSAGE(::yaclib::detail::LogLevel::Debug, true, "Pure virtual call!")
82#else
83# define YACLIB_INIT_DEBUG(callback) YACLIB_STUB1(callback)
84# define YACLIB_DEBUG(cond, message) YACLIB_STUB2(cond, message)
85# define YACLIB_ASSERT(cond) YACLIB_STUB1(!(cond))
86# define YACLIB_PURE_VIRTUAL() YACLIB_STUB1(true)
87#endif
auto SetCallback(ResultCorePtr< Arg, E > &core, IExecutor *executor, Func &&f)
Definition core.hpp:304
void LogMessage(LogLevel level, std::string_view file, std::size_t line, std::string_view func, std::string_view condition, std::string_view message) noexcept
Definition log.cpp:10
Contract< V, E > MakeContract()
Creates related future and promise.
Definition contract.hpp:25
void(*)(std::string_view file, std::size_t line, std::string_view function, std::string_view condition, std::string_view message) noexcept LogCallback
Definition log.hpp:10