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 BATTERY_LOG_H
17 #define BATTERY_LOG_H
18
19 #ifndef ENABLE_INIT_LOG
20
21 #include "hilog/log.h"
22
23 namespace OHOS {
24 namespace HDI {
25 namespace Battery {
26 #define FILE_NAME (__builtin_strrchr(__FILE__, '/') ? __builtin_strrchr(__FILE__, '/') + 1 : __FILE__)
27 #define FORMAT(fmt, ...) "[%{public}s:%{public}d] %{public}s# " fmt, FILE_NAME, __LINE__, __FUNCTION__, ##__VA_ARGS__
28
29 #ifdef BATTERY_HILOGF
30 #undef BATTERY_HILOGF
31 #endif
32
33 #ifdef BATTERY_HILOGE
34 #undef BATTERY_HILOGE
35 #endif
36
37 #ifdef BATTERY_HILOGW
38 #undef BATTERY_HILOGW
39 #endif
40
41 #ifdef BATTERY_HILOGI
42 #undef BATTERY_HILOGI
43 #endif
44
45 #ifdef BATTERY_HILOGD
46 #undef BATTERY_HILOGD
47 #endif
48
49 namespace {
50 // Battery manager reserved domain id range
51 constexpr unsigned int BATTERY_DOMAIN_ID_START = 0xD002920;
52 constexpr unsigned int BATTERY_DOMAIN_ID_END = BATTERY_DOMAIN_ID_START + 32;
53 constexpr unsigned int TEST_DOMAIN_ID = 0xD000F00;
54 }
55
56 enum BatteryManagerLogLabel {
57 // Component labels, you can add if needed
58 COMP_APP = 0,
59 COMP_FWK = 1,
60 COMP_SVC = 2,
61 COMP_HDI = 3,
62 COMP_DRV = 4,
63 // Feature labels, use to mark major features
64 FEATURE_CHARGING,
65 FEATURE_BATT_INFO,
66 // Test label
67 LABEL_TEST,
68 // The end of labels, max to the domain id range length 32
69 LABEL_END,
70 };
71
72 enum BatteryManagerLogDomain {
73 DOMAIN_APP = BATTERY_DOMAIN_ID_START + COMP_APP, // 0xD002920
74 DOMAIN_FRAMEWORK, // 0xD002921
75 DOMAIN_SERVICE, // 0xD002922
76 DOMAIN_HDI, // 0xD002923
77 DOMAIN_DRIVER, // 0xD002924
78 DOMAIN_FEATURE_CHARGING,
79 DOMAIN_FEATURE_BATT_INFO,
80 DOMAIN_TEST = TEST_DOMAIN_ID, // 0xD000F00
81 DOMAIN_END = BATTERY_DOMAIN_ID_END, // Max to 0xD002940, keep the sequence and length same as BatteryManagerLogLabel
82 };
83
84 // Keep the sequence and length same as BatteryManagerLogDomain
85 static constexpr OHOS::HiviewDFX::HiLogLabel BATTERY_LABEL[LABEL_END] = {
86 {LOG_CORE, DOMAIN_APP, "BatteryApp"},
87 {LOG_CORE, DOMAIN_FRAMEWORK, "BatteryFwk"},
88 {LOG_CORE, DOMAIN_SERVICE, "BatterySvc"},
89 {LOG_CORE, DOMAIN_HDI, "BatteryHdi"},
90 {LOG_CORE, DOMAIN_DRIVER, "BatteryDrv"},
91 {LOG_CORE, DOMAIN_FEATURE_CHARGING, "BatteryCharging"},
92 {LOG_CORE, DOMAIN_FEATURE_BATT_INFO, "BatteryInfo"},
93 {LOG_CORE, DOMAIN_TEST, "BatteryTest"},
94 };
95
96 #define BATTERY_HILOGF(domain, ...) (void)OHOS::HiviewDFX::HiLog::Fatal(BATTERY_LABEL[domain], FORMAT(__VA_ARGS__))
97 #define BATTERY_HILOGE(domain, ...) (void)OHOS::HiviewDFX::HiLog::Error(BATTERY_LABEL[domain], FORMAT(__VA_ARGS__))
98 #define BATTERY_HILOGW(domain, ...) (void)OHOS::HiviewDFX::HiLog::Warn(BATTERY_LABEL[domain], FORMAT(__VA_ARGS__))
99 #define BATTERY_HILOGI(domain, ...) (void)OHOS::HiviewDFX::HiLog::Info(BATTERY_LABEL[domain], FORMAT(__VA_ARGS__))
100 #define BATTERY_HILOGD(domain, ...) (void)OHOS::HiviewDFX::HiLog::Debug(BATTERY_LABEL[domain], FORMAT(__VA_ARGS__))
101 } // namespace Battery
102 } // namespace HDI
103 } // namespace OHOS
104
105 #else
106
107 #include <string>
108 #include "beget_ext.h"
109
110 #define CHARGER_LOG_FILE "charger.log"
111 #define FEATURE_CHARGING "charger: "
112 #define FEATURE_BATT_INFO FEATURE_CHARGING
113 #define COMP_HDI FEATURE_CHARGING
114
ReplaceHolder(std::string & str,const std::string & holder)115 inline void ReplaceHolder(std::string& str, const std::string& holder)
116 {
117 size_t index = 0;
118 size_t holderLen = holder.size();
119 while ((index = str.find(holder, index)) != std::string::npos) {
120 str = str.replace(index, holderLen, "");
121 index++;
122 }
123 }
124
ReplaceHolders(const char * fmt)125 inline std::string ReplaceHolders(const char* fmt)
126 {
127 std::string str(fmt);
128 ReplaceHolder(str, "{public}");
129 ReplaceHolder(str, "{private}");
130 return "[%s:%d] %s# " + str + "\n";
131 }
132
133 #define BATTERY_HILOGE(label, fmt, ...) \
134 do { \
135 InitLogPrint(INIT_LOG_PATH CHARGER_LOG_FILE, INIT_ERROR, label, (ReplaceHolders(fmt).c_str()), \
136 (FILE_NAME), (__LINE__), (__FUNCTION__), ##__VA_ARGS__); \
137 } while (0)
138 #define BATTERY_HILOGW(label, fmt, ...) \
139 do { \
140 InitLogPrint(INIT_LOG_PATH CHARGER_LOG_FILE, INIT_WARN, label, (ReplaceHolders(fmt).c_str()), \
141 (FILE_NAME), (__LINE__), (__FUNCTION__), ##__VA_ARGS__); \
142 } while (0)
143 #define BATTERY_HILOGI(label, fmt, ...) \
144 do { \
145 InitLogPrint(INIT_LOG_PATH CHARGER_LOG_FILE, INIT_INFO, label, (ReplaceHolders(fmt).c_str()), \
146 (FILE_NAME), (__LINE__), (__FUNCTION__), ##__VA_ARGS__); \
147 } while (0)
148 #define BATTERY_HILOGD(label, fmt, ...) \
149 do { \
150 InitLogPrint(INIT_LOG_PATH CHARGER_LOG_FILE, INIT_DEBUG, label, (ReplaceHolders(fmt).c_str()), \
151 (FILE_NAME), (__LINE__), (__FUNCTION__), ##__VA_ARGS__); \
152 } while (0)
153
154 #endif // ENABLE_INIT_LOG
155
156 #endif // BATTERY_LOG_H
157