1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 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 <iostream> 20 #include <string.h> 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, LOG_INFO, LOG_WARN, LOG_ERROR, LOG_FATAL}; 30 const enum LogLevel g_currentLogLevel = LOG_DEBUG; 31 extern bool g_cleanMode; 32 #define LOGWITHLEVEL(level, motify, fmt, ...) \ 33 do { \ 34 if (level >= g_currentLogLevel) { \ 35 if (!g_cleanMode) { \ 36 fprintf(stdout, "[-%c][%s][%d]: " fmt "\n", motify, __FUNCTION__, \ 37 __LINE__, ##__VA_ARGS__); \ 38 } \ 39 if (level == LOG_FATAL) { \ 40 TS_CRASH; \ 41 } \ 42 } \ 43 } while (0) 44 #define TS_LOGE(fmt, ...) LOGWITHLEVEL(LOG_ERROR, 'E', fmt, ##__VA_ARGS__) 45 #define TS_LOGF(fmt, ...) LOGWITHLEVEL(LOG_FATAL, 'F', fmt, ##__VA_ARGS__) 46 #define TS_LOGI(fmt, ...) LOGWITHLEVEL(LOG_INFO, 'I', fmt, ##__VA_ARGS__) 47 #ifdef NDEBUG 48 #define TS_LOGD(format, ...) 49 #define TS_LOGW(format, ...) 50 #define TS_ASSERT(x) 51 #else 52 #define TS_LOGD(fmt, ...) LOGWITHLEVEL(LOG_DEBUG, 'D', fmt, ##__VA_ARGS__) 53 #define TS_LOGW(fmt, ...) LOGWITHLEVEL(LOG_WARN, 'W', fmt, ##__VA_ARGS__) 54 55 #define TS_ASSERT(x) \ 56 do { \ 57 if (!(x)) { \ 58 TS_CRASH; \ 59 } \ 60 } while (0) 61 62 #endif 63 // } // namespace base 64 // } // namespace SysTuning 65 66 #endif // INCLUDE_TS_BASE_LOGGING_H_ 67