• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved.
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 
16 #ifndef INCLUDE_TS_BASE_LOGGING_H_
17 #define INCLUDE_TS_BASE_LOGGING_H_
18 
19 #include <cinttypes>
20 #include <string>
21 
22 // namespace SysTuning {
23 // namespace base {
24 #define TS_CRASH                 \
25     do {                         \
26         __builtin_trap();        \
27         __builtin_unreachable(); \
28     } while (0)
29 enum LogLevel { LOG_DEBUG = 0, LOG_INFO, LOG_WARN, LOG_ERROR, LOG_FATAL, LOG_OFF };
30 extern enum LogLevel g_curLogLevel;
31 bool SetLogLevel(const std::string &level);
32 #define LOGWITHLEVEL(level, motify, fmt, ...)                                                           \
33     do {                                                                                                \
34         if (level >= g_curLogLevel) {                                                                   \
35             fprintf(stdout, "[-%c][%s][%d]: " fmt "\n", motify, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
36             if (level == LOG_FATAL) {                                                                   \
37                 TS_CRASH;                                                                               \
38             }                                                                                           \
39         }                                                                                               \
40     } while (0)
41 #define TS_LOGE(fmt, ...) LOGWITHLEVEL(LOG_ERROR, 'E', fmt, ##__VA_ARGS__)
42 #define TS_LOGF(fmt, ...) LOGWITHLEVEL(LOG_FATAL, 'F', fmt, ##__VA_ARGS__)
43 #define TS_LOGD(fmt, ...) LOGWITHLEVEL(LOG_DEBUG, 'D', fmt, ##__VA_ARGS__)
44 #define TS_LOGI(fmt, ...) LOGWITHLEVEL(LOG_INFO, 'I', fmt, ##__VA_ARGS__)
45 #define TS_LOGW(fmt, ...) LOGWITHLEVEL(LOG_WARN, 'W', fmt, ##__VA_ARGS__)
46 
47 #define TS_ASSERT(x)  \
48     do {              \
49         if (!(x)) {   \
50             TS_CRASH; \
51         }             \
52     } while (0)
53 
54 #define TS_CHECK_TRUE_RET(expression, retval, ...) \
55     do {                                           \
56         if (!(expression)) {                       \
57             return retval;                         \
58         }                                          \
59     } while (0)
60 
61 #define TS_CHECK_TRUE(expression, retval, formate, ...)                                \
62     do {                                                                               \
63         if (!(expression)) {                                                           \
64             TS_LOGW("TS_CHECK_TRUE(%s) FAILED, " formate, #expression, ##__VA_ARGS__); \
65             return retval;                                                             \
66         }                                                                              \
67     } while (0)
68 // } // namespace base
69 // } // namespace SysTuning
70 
71 #endif // INCLUDE_TS_BASE_LOGGING_H_
72