1f16e0440Sopenharmony_ci/* 2f16e0440Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 3f16e0440Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4f16e0440Sopenharmony_ci * you may not use this file except in compliance with the License. 5f16e0440Sopenharmony_ci * You may obtain a copy of the License at 6f16e0440Sopenharmony_ci * 7f16e0440Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8f16e0440Sopenharmony_ci * 9f16e0440Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10f16e0440Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11f16e0440Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12f16e0440Sopenharmony_ci * See the License for the specific language governing permissions and 13f16e0440Sopenharmony_ci * limitations under the License. 14f16e0440Sopenharmony_ci */ 15f16e0440Sopenharmony_ci 16f16e0440Sopenharmony_ci#ifndef BATTERY_LOG_H 17f16e0440Sopenharmony_ci#define BATTERY_LOG_H 18f16e0440Sopenharmony_ci 19f16e0440Sopenharmony_ci#include "beget_ext.h" 20f16e0440Sopenharmony_ci#include <string> 21f16e0440Sopenharmony_ci 22f16e0440Sopenharmony_ci#define CHARGER_LOG_FILE "charger.log" 23f16e0440Sopenharmony_ci#define FEATURE_CHARGING "charger: " 24f16e0440Sopenharmony_ci 25f16e0440Sopenharmony_ciinline void ReplaceHolder(std::string& str, const std::string& holder) 26f16e0440Sopenharmony_ci{ 27f16e0440Sopenharmony_ci size_t index = 0; 28f16e0440Sopenharmony_ci size_t holderLen = holder.size(); 29f16e0440Sopenharmony_ci while ((index = str.find(holder, index)) != std::string::npos) { 30f16e0440Sopenharmony_ci str = str.replace(index, holderLen, ""); 31f16e0440Sopenharmony_ci index++; 32f16e0440Sopenharmony_ci } 33f16e0440Sopenharmony_ci} 34f16e0440Sopenharmony_ci 35f16e0440Sopenharmony_ciinline std::string ReplaceHolders(const char* fmt) 36f16e0440Sopenharmony_ci{ 37f16e0440Sopenharmony_ci std::string str(fmt); 38f16e0440Sopenharmony_ci ReplaceHolder(str, "{public}"); 39f16e0440Sopenharmony_ci ReplaceHolder(str, "{private}"); 40f16e0440Sopenharmony_ci return "[%s:%d] %s# " + str + "\n"; 41f16e0440Sopenharmony_ci} 42f16e0440Sopenharmony_ci 43f16e0440Sopenharmony_ci#define BATTERY_HILOGE(label, fmt, ...) \ 44f16e0440Sopenharmony_ci do { \ 45f16e0440Sopenharmony_ci InitLogPrint(INIT_LOG_PATH CHARGER_LOG_FILE, INIT_ERROR, label, (ReplaceHolders(fmt).c_str()), \ 46f16e0440Sopenharmony_ci (STARTUP_FILE_NAME), (__LINE__), (__FUNCTION__), ##__VA_ARGS__); \ 47f16e0440Sopenharmony_ci } while (0) 48f16e0440Sopenharmony_ci#define BATTERY_HILOGW(label, fmt, ...) \ 49f16e0440Sopenharmony_ci do { \ 50f16e0440Sopenharmony_ci InitLogPrint(INIT_LOG_PATH CHARGER_LOG_FILE, INIT_WARN, label, (ReplaceHolders(fmt).c_str()), \ 51f16e0440Sopenharmony_ci (STARTUP_FILE_NAME), (__LINE__), (__FUNCTION__), ##__VA_ARGS__); \ 52f16e0440Sopenharmony_ci } while (0) 53f16e0440Sopenharmony_ci#define BATTERY_HILOGI(label, fmt, ...) \ 54f16e0440Sopenharmony_ci do { \ 55f16e0440Sopenharmony_ci InitLogPrint(INIT_LOG_PATH CHARGER_LOG_FILE, INIT_INFO, label, (ReplaceHolders(fmt).c_str()), \ 56f16e0440Sopenharmony_ci (STARTUP_FILE_NAME), (__LINE__), (__FUNCTION__), ##__VA_ARGS__); \ 57f16e0440Sopenharmony_ci } while (0) 58f16e0440Sopenharmony_ci#define BATTERY_HILOGD(label, fmt, ...) \ 59f16e0440Sopenharmony_ci do { \ 60f16e0440Sopenharmony_ci InitLogPrint(INIT_LOG_PATH CHARGER_LOG_FILE, INIT_DEBUG, label, (ReplaceHolders(fmt).c_str()), \ 61f16e0440Sopenharmony_ci (STARTUP_FILE_NAME), (__LINE__), (__FUNCTION__), ##__VA_ARGS__); \ 62f16e0440Sopenharmony_ci } while (0) 63f16e0440Sopenharmony_ci 64f16e0440Sopenharmony_ci#endif // BATTERY_LOG_H 65