1/*
2 * Copyright (c) 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 BATTERY_LOG_H
17#define BATTERY_LOG_H
18
19#include "beget_ext.h"
20#include <string>
21
22#define CHARGER_LOG_FILE  "charger.log"
23#define FEATURE_CHARGING  "charger: "
24
25inline void ReplaceHolder(std::string& str, const std::string& holder)
26{
27    size_t index = 0;
28    size_t holderLen = holder.size();
29    while ((index = str.find(holder, index)) != std::string::npos) {
30        str = str.replace(index, holderLen, "");
31        index++;
32    }
33}
34
35inline std::string ReplaceHolders(const char* fmt)
36{
37    std::string str(fmt);
38    ReplaceHolder(str, "{public}");
39    ReplaceHolder(str, "{private}");
40    return "[%s:%d] %s# " + str + "\n";
41}
42
43#define BATTERY_HILOGE(label, fmt, ...)                                                                            \
44    do {                                                                                                           \
45        InitLogPrint(INIT_LOG_PATH CHARGER_LOG_FILE, INIT_ERROR, label, (ReplaceHolders(fmt).c_str()),             \
46            (STARTUP_FILE_NAME), (__LINE__), (__FUNCTION__), ##__VA_ARGS__);                                       \
47    } while (0)
48#define BATTERY_HILOGW(label, fmt, ...)                                                                            \
49    do {                                                                                                           \
50        InitLogPrint(INIT_LOG_PATH CHARGER_LOG_FILE, INIT_WARN, label, (ReplaceHolders(fmt).c_str()),              \
51            (STARTUP_FILE_NAME), (__LINE__), (__FUNCTION__), ##__VA_ARGS__);                                       \
52    } while (0)
53#define BATTERY_HILOGI(label, fmt, ...)                                                                            \
54    do {                                                                                                           \
55        InitLogPrint(INIT_LOG_PATH CHARGER_LOG_FILE, INIT_INFO, label, (ReplaceHolders(fmt).c_str()),              \
56            (STARTUP_FILE_NAME), (__LINE__), (__FUNCTION__), ##__VA_ARGS__);                                       \
57    } while (0)
58#define BATTERY_HILOGD(label, fmt, ...)                                                                            \
59    do {                                                                                                           \
60        InitLogPrint(INIT_LOG_PATH CHARGER_LOG_FILE, INIT_DEBUG, label, (ReplaceHolders(fmt).c_str()),             \
61            (STARTUP_FILE_NAME), (__LINE__), (__FUNCTION__), ##__VA_ARGS__);                                       \
62    } while (0)
63
64#endif // BATTERY_LOG_H
65