• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 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 FI_LOG_H
17 #define FI_LOG_H
18 
19 #include <cinttypes>
20 #include <functional>
21 #include <future>
22 #include <string>
23 #include <sstream>
24 
25 #include "hilog/log.h"
26 
27 #ifdef LOG_DOMAIN
28 #undef LOG_DOMAIN
29 #endif
30 #define LOG_DOMAIN 0XD002220
31 
32 #ifndef FI_FUNC_FMT
33 #define FI_FUNC_FMT "in %{public}s, "
34 #endif
35 
36 #ifndef FI_FUNC_INFO
37 #define FI_FUNC_INFO __FUNCTION__
38 #endif
39 
40 #ifndef FI_FILE_NAME
41 #define FI_FILE_NAME (strrchr((__FILE__), '/') ? strrchr((__FILE__), '/') + 1 : (__FILE__))
42 #endif
43 
44 #ifndef FI_LINE_INFO
45 #define FI_LINE_INFO FI_FILE_NAME, __LINE__
46 #endif
47 
48 #define FI_HILOGD(fmt, ...) do { \
49     if (HiLogIsLoggable(LOG_DOMAIN, LOG_TAG, LOG_DEBUG)) { \
50         HILOG_DEBUG(LOG_CORE, FI_FUNC_FMT fmt, FI_FUNC_INFO, ##__VA_ARGS__); \
51     } \
52 } while (0)
53 #define FI_HILOGI(fmt, ...) do { \
54     HILOG_INFO(LOG_CORE, FI_FUNC_FMT fmt, FI_FUNC_INFO, ##__VA_ARGS__); \
55 } while (0)
56 #define FI_HILOGW(fmt, ...) do { \
57     HILOG_WARN(LOG_CORE, FI_FUNC_FMT fmt, FI_FUNC_INFO, ##__VA_ARGS__); \
58 } while (0)
59 #define FI_HILOGE(fmt, ...) do { \
60     HILOG_ERROR(LOG_CORE, FI_FUNC_FMT fmt, FI_FUNC_INFO, ##__VA_ARGS__); \
61 } while (0)
62 #define FI_HILOGF(fmt, ...) do { \
63     HILOG_FATAL(LOG_CORE, FI_FUNC_FMT fmt, FI_FUNC_INFO, ##__VA_ARGS__); \
64 } while (0)
65 
66 namespace OHOS {
67 namespace Msdp {
68 class InnerFunctionTracer {
69 public:
InnerFunctionTracer(LogLevel level,const char * tag,const char * func)70     InnerFunctionTracer(LogLevel level, const char* tag, const char* func)
71         : level_ { level }, tag_ { tag }, func_ { func }
72     {
73         if (HiLogIsLoggable(LOG_DOMAIN, tag_, level_)) {
74             if (func_ != nullptr) {
75                 HILOG_IMPL(LOG_CORE, level_, LOG_DOMAIN, tag_, "in %{public}s, enter", func_);
76             }
77         }
78     }
~InnerFunctionTracer()79     ~InnerFunctionTracer()
80     {
81         if (HiLogIsLoggable(LOG_DOMAIN, tag_, level_)) {
82             if (func_ != nullptr) {
83                 HILOG_IMPL(LOG_CORE, level_, LOG_DOMAIN, tag_, "in %{public}s, leave", func_);
84             }
85         }
86     }
87 private:
88     LogLevel level_ { LOG_LEVEL_MIN };
89     const char* tag_ { nullptr };
90     const char* func_ { nullptr };
91 };
92 } // namespace Msdp
93 } // namespace OHOS
94 
95 #define CALL_DEBUG_ENTER InnerFunctionTracer __innerFuncTracer_Debug___ { LOG_DEBUG, LOG_TAG, __FUNCTION__ }
96 #define CALL_INFO_TRACE InnerFunctionTracer ___innerFuncTracer_Info___ { LOG_INFO, LOG_TAG, __FUNCTION__ }
97 #define CALL_TEST_DEBUG InnerFunctionTracer ___innerFuncTracer_Info___ { LOG_DEBUG, LOG_TAG, \
98     test_info_ == nullptr ? "TestBody" : test_info_->name() }
99 #endif // FI_LOG_H
100