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