RDK Documentation (Open Sourced RDK Components)
logger.h
1 /*
2  * If not stated otherwise in this file or this component's Licenses.txt file the
3  * following copyright and licenses apply:
4  *
5  * Copyright 2018 RDK Management
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18 */
19 #ifndef _LOGGER_H_
20 #define _LOGGER_H_
21 
22 #include <cstdio>
23 #include <cassert>
24 #include <cstring>
25 
26 #include "safec_lib.h"
27 
28 /**
29  * Enable rdk_logger. If not defined, fallback to stdout logging.
30  */
31 // #define USE_RDK_LOGGER
32 
33 #ifdef USE_RDK_LOGGER
34 #undef USE_RDK_LOGGER
35 #endif
36 
37 /**
38  * @brief Logging level with an increasing order of refinement
39  * (TRACE_LEVEL = Finest logging).
40  * It is essental to start with 0 and increase w/o gaps as the value
41  * can be used for indexing in a mapping table.
42  */
43 enum LogLevel {
44  FATAL_LEVEL = 0,
45  ERROR_LEVEL,
46  WARNING_LEVEL,
47  METRIC_LEVEL,
48  INFO_LEVEL,
49  VERBOSE_LEVEL,
50  TRACE_LEVEL,
51 };
52 
53 /**
54  * @brief Init logging
55  * Should be called once per program run before calling log-functions
56  */
57 void log_init();
58 
59 /**
60  * @brief Log a message
61  * The function is defined by logging backend.
62  * Currently 2 variants are supported: rdk_logger (USE_RDK_LOGGER),
63  * stdout(default)
64  */
65 void log (LogLevel level,
66  const char* func,
67  const char* file,
68  int line,
69  const char* format, ...);
70 
71 #define LOG(LEVEL, FORMAT, ...) \
72  log( LEVEL, \
73  __func__, __FILE__, __LINE__, \
74  FORMAT, \
75  ##__VA_ARGS__)
76 
77 #define LOG_INIT log_init
78 #define LOG_TRACE(FMT, ...) LOG(TRACE_LEVEL, FMT, ##__VA_ARGS__)
79 #define LOG_VERBOSE(FMT, ...) LOG(VERBOSE_LEVEL, FMT, ##__VA_ARGS__)
80 #define LOG_METRIC(FMT, ...) LOG(METRIC_LEVEL, FMT, ##__VA_ARGS__)
81 #define LOG_INFO(FMT, ...) LOG(INFO_LEVEL, FMT, ##__VA_ARGS__)
82 #define LOG_WARNING(FMT, ...) LOG(WARNING_LEVEL, FMT, ##__VA_ARGS__)
83 #define LOG_ERROR(FMT, ...) LOG(ERROR_LEVEL, FMT, ##__VA_ARGS__)
84 #define LOG_FATAL(FMT, ...) LOG(FATAL_LEVEL, FMT, ##__VA_ARGS__)
85 
86 #ifndef NDEBUG
87 #define LOG_ASSERT(FMT, ...) LOG(FATAL_LEVEL, FMT, ##__VA_ARGS__)
88 #else
89 #define LOG_ASSERT(FMT, ...) LOG(ERROR_LEVEL, FMT, ##__VA_ARGS__)
90 #endif
91 
92 #define ASSERT(x) \
93  ((x) \
94  ? (void)(0) \
95  : LOG_ASSERT("'%s' check failed.", #x) ) \
96 
97 
98 #endif // _LOGGER_H_