1 /*
2  * Copyright (c) 2022 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 DISTRIBUTEDDATA_LOG_PRINT_H
17 #define DISTRIBUTEDDATA_LOG_PRINT_H
18 
19 #include <cinttypes>
20 #include <cstdarg>
21 #include <cstdio>
22 #include <string>
23 
24 #define LOG_TAG_KV "DistributedDB_DATA"
25 
26 class Logger {
27 public:
28     enum class Level {
29         LEVEL_DEBUG,
30         LEVEL_INFO,
31         LEVEL_WARN,
32         LEVEL_ERROR,
33         LEVEL_FATAL
34     };
35 
~Logger()36     virtual ~Logger() {};
37     static Logger *GetInstance();
38     static void RegisterLogger(Logger *logger);
39     static void Log(Level level, const std::string &tag, const char *func, int line, const char *format, ...);
40 
41 private:
42     void Print(Level level,  const char *func, int line, const std::string &tag, const std::string &msg);
43     static void PreparePrivateLog(const char *format, std::string &outStrFormat);
44     static Logger *logHandler;
45     static const std::string PRIVATE_TAG;
46     static const std::string PUBLIC_TAG;
47 };
48 
49 #define NO_LOG(...) // No log in normal and release. Used for convenience when deep debugging
50 #define LOGD(...) Logger::Log(Logger::Level::LEVEL_DEBUG, LOG_TAG_KV, __FILE_NAME__, __LINE__, __VA_ARGS__)
51 #define LOGI(...) Logger::Log(Logger::Level::LEVEL_INFO, LOG_TAG_KV, __FILE_NAME__, __LINE__, __VA_ARGS__)
52 #define LOGW(...) Logger::Log(Logger::Level::LEVEL_WARN, LOG_TAG_KV, __FILE_NAME__, __LINE__, __VA_ARGS__)
53 #define LOGE(...) Logger::Log(Logger::Level::LEVEL_ERROR, LOG_TAG_KV, __FILE_NAME__, __LINE__, __VA_ARGS__)
54 #define LOGF(...) Logger::Log(Logger::Level::LEVEL_FATAL, LOG_TAG_KV, __FILE_NAME__, __LINE__, __VA_ARGS__)
55 #define ZLOGD(...) Logger::Log(Logger::Level::LEVEL_DEBUG, LOG_TAG_KV, __FILE_NAME__, __LINE__, __VA_ARGS__)
56 #define ZLOGI(...) Logger::Log(Logger::Level::LEVEL_INFO, LOG_TAG_KV, __FILE_NAME__, __LINE__, __VA_ARGS__)
57 #define ZLOGW(...) Logger::Log(Logger::Level::LEVEL_WARN, LOG_TAG_KV, __FILE_NAME__, __LINE__, __VA_ARGS__)
58 #define ZLOGE(...) Logger::Log(Logger::Level::LEVEL_ERROR, LOG_TAG_KV, __FILE_NAME__, __LINE__, __VA_ARGS__)
59 #define ZLOGF(...) Logger::Log(Logger::Level::LEVEL_FATAL, LOG_TAG_KV, __FILE_NAME__, __LINE__, __VA_ARGS__)
60 #endif // DISTRIBUTEDDB_LOG_PRINT_H
61