farm-ng-core
trace_debug_log.h
Go to the documentation of this file.
1 // Copyright 2022, farm-ng inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 
17 #include "farm_ng/core/enum/enum.h"
20 
21 #include <farm_pp/preprocessor/comma.hpp>
22 #include <farm_pp/preprocessor/empty.hpp>
23 
24 #include <filesystem>
25 #include <iostream>
26 
27 #define FARM_LOG_LEVEL_TRACE 0
28 #define FARM_LOG_LEVEL_DEBUG 1
29 #define FARM_LOG_LEVEL_INFO 2
30 
31 static_assert(FARM_LOG_LEVEL_TRACE == int(::farm_ng::LogLevel::trace));
32 static_assert(FARM_LOG_LEVEL_DEBUG == int(::farm_ng::LogLevel::debug));
33 static_assert(FARM_LOG_LEVEL_INFO == int(::farm_ng::LogLevel::info));
34 
35 // All other log levels (INFO, WARN, CRITICAL,...) and corresponding log macros
36 // LOG_INFO, LOG_WARN are always compiled (unless globally disabled).
37 // In addition for being defined at compile-time, logging must also be enabled
38 // at run-time be specifying the appropriate run-time log level using the
39 // corresponding logger, such as
40 // ``defaultLogger().setLogLevel(LogLevel::trace);``
41 
42 #ifdef FARM_ONLY_INCLUDE_ME_IN_CPP_FILE
43 static_assert(
44  false,
45  "Shoot FARM_ONLY_INCLUDE_ME_IN_CPP_FILE is already define, but it should "
46  "not! You must include trace_debug_log.h only from a *.cpp file and you "
47  "must "
48  "define "
49  "FARM_LOG_LEVEL right above the include.");
50 #endif
51 #define FARM_ONLY_INCLUDE_ME_IN_CPP_FILE 1
52 
53 #ifndef FARM_LOG_LEVEL
54 static_assert(
55  false,
56  "You must include log_macros.h only from a *.cpp file and you must define "
57  "FARM_LOG_LEVEL right above the include.");
58 #else
59 static_assert(
63  "specified level: " FARM_PP_STRINGIZE(FARM_LOG_LEVEL));
64 #endif
65 
66 #define FARM_TRACE(...)
67 #define FARM_DEBUG(...)
68 
69 #ifdef FARM_LOG_ENABLED
70 #if FARM_LOG_LEVEL <= FARM_LOG_LEVEL_TRACE
71 #undef FARM_TRACE
72 #define FARM_TRACE(...) \
73  farm_ng::defaultLogger().log( \
74  farm_ng::LogLevel::trace, \
75  "TRACE", \
76  __FILE__, \
77  __LINE__, \
78  __func__, \
79  __VA_ARGS__)
80 #endif
81 #if FARM_LOG_LEVEL <= FARM_LOG_LEVEL_DEBUG
82 #undef FARM_DEBUG
83 #define FARM_DEBUG(...) \
84  farm_ng::defaultLogger().log( \
85  farm_ng::LogLevel::debug, \
86  "DEBUG", \
87  __FILE__, \
88  __LINE__, \
89  __func__, \
90  __VA_ARGS__)
91 #endif
92 #endif // FARM_LOG_ENABLED
FARM_LOG_LEVEL_TRACE
#define FARM_LOG_LEVEL_TRACE
Definition: trace_debug_log.h:27
FARM_LOG_LEVEL_INFO
#define FARM_LOG_LEVEL_INFO
Definition: trace_debug_log.h:29
logger.h
format.h
enum.h
FARM_LOG_LEVEL_DEBUG
#define FARM_LOG_LEVEL_DEBUG
Definition: trace_debug_log.h:28
FARM_LOG_LEVEL
#define FARM_LOG_LEVEL
Definition: logger_define_debug_variant_test.cpp:15