154aa6d63Sopenharmony_ci/* 254aa6d63Sopenharmony_ci * Copyright (c) 2024-2024 Huawei Device Co., Ltd. 354aa6d63Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 454aa6d63Sopenharmony_ci * you may not use this file except in compliance with the License. 554aa6d63Sopenharmony_ci * You may obtain a copy of the License at 654aa6d63Sopenharmony_ci * 754aa6d63Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 854aa6d63Sopenharmony_ci * 954aa6d63Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1054aa6d63Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1154aa6d63Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1254aa6d63Sopenharmony_ci * See the License for the specific language governing permissions and 1354aa6d63Sopenharmony_ci * limitations under the License. 1454aa6d63Sopenharmony_ci */ 1554aa6d63Sopenharmony_ci#ifndef SIGNATURETOOLS_SIGNATRUE_TOOLS_LOG_H 1654aa6d63Sopenharmony_ci#define SIGNATURETOOLS_SIGNATRUE_TOOLS_LOG_H 1754aa6d63Sopenharmony_ci#include <stdio.h> 1854aa6d63Sopenharmony_ci#include <iostream> 1954aa6d63Sopenharmony_ci#include <time.h> 2054aa6d63Sopenharmony_ci#include <chrono> 2154aa6d63Sopenharmony_ci#include <ctime> 2254aa6d63Sopenharmony_ci#include <iomanip> 2354aa6d63Sopenharmony_ci#include <sstream> 2454aa6d63Sopenharmony_ci 2554aa6d63Sopenharmony_ci#include "signature_tools_errno.h" 2654aa6d63Sopenharmony_ci 2754aa6d63Sopenharmony_cinamespace OHOS { 2854aa6d63Sopenharmony_cinamespace SignatureTools { 2954aa6d63Sopenharmony_ci 3054aa6d63Sopenharmony_cistatic const char POINT = '.'; 3154aa6d63Sopenharmony_cistatic const char PLACEHOLDER = '0'; 3254aa6d63Sopenharmony_cistatic const int PLACEHOLDERLEN = 3; 3354aa6d63Sopenharmony_cistatic const int SCALE = 1000; 3454aa6d63Sopenharmony_ci 3554aa6d63Sopenharmony_ci#define SIGNATURE_LOG(level, fmt, ...) \ 3654aa6d63Sopenharmony_ci printf("[%s] [%s] [%s] [%d] " fmt "\n", level, __FILE_NAME__, __FUNCTION__, __LINE__, ##__VA_ARGS__) \ 3754aa6d63Sopenharmony_ci 3854aa6d63Sopenharmony_ci#ifdef SIGNATURE_LOG_DEBUG 3954aa6d63Sopenharmony_ci#define SIGNATURE_TOOLS_LOGI(fmt, ...) SIGNATURE_LOG("Info", fmt, ##__VA_ARGS__) 4054aa6d63Sopenharmony_ci#define SIGNATURE_TOOLS_LOGD(fmt, ...) SIGNATURE_LOG("Debug", fmt, ##__VA_ARGS__) 4154aa6d63Sopenharmony_ci#define SIGNATURE_TOOLS_LOGW(fmt, ...) SIGNATURE_LOG("Warn", fmt, ##__VA_ARGS__) 4254aa6d63Sopenharmony_ci#else 4354aa6d63Sopenharmony_ci#define SIGNATURE_TOOLS_LOGI(fmt, ...) 4454aa6d63Sopenharmony_ci#define SIGNATURE_TOOLS_LOGD(fmt, ...) 4554aa6d63Sopenharmony_ci#define SIGNATURE_TOOLS_LOGW(fmt, ...) 4654aa6d63Sopenharmony_ci#endif 4754aa6d63Sopenharmony_ci 4854aa6d63Sopenharmony_ci#define SIGNATURE_TOOLS_LOGF(fmt, ...) SIGNATURE_LOG("Fatal", fmt, ##__VA_ARGS__) 4954aa6d63Sopenharmony_ci#define SIGNATURE_TOOLS_LOGE(fmt, ...) SIGNATURE_LOG("Error", fmt, ##__VA_ARGS__) 5054aa6d63Sopenharmony_ci 5154aa6d63Sopenharmony_ciinline std::string GetSystemTime() 5254aa6d63Sopenharmony_ci{ 5354aa6d63Sopenharmony_ci std::string timeBuffer; 5454aa6d63Sopenharmony_ci std::stringstream ss; 5554aa6d63Sopenharmony_ci auto now = std::chrono::system_clock::now(); 5654aa6d63Sopenharmony_ci std::time_t nowTime = std::chrono::system_clock::to_time_t(now); 5754aa6d63Sopenharmony_ci std::tm* localTime = std::localtime(&nowTime); 5854aa6d63Sopenharmony_ci auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) % SCALE; 5954aa6d63Sopenharmony_ci ss << std::put_time(localTime, "%m-%d %H:%M:%S"); 6054aa6d63Sopenharmony_ci ss << POINT << std::setfill(PLACEHOLDER) << std::setw(PLACEHOLDERLEN) << ms.count(); 6154aa6d63Sopenharmony_ci timeBuffer = ss.str(); 6254aa6d63Sopenharmony_ci return timeBuffer; 6354aa6d63Sopenharmony_ci} 6454aa6d63Sopenharmony_ci 6554aa6d63Sopenharmony_ci/* 6654aa6d63Sopenharmony_ci* Function: Print the error code and error message to the terminal. 6754aa6d63Sopenharmony_ci* Parametric Description: command, code, details 6854aa6d63Sopenharmony_ci* command: Error code variable name as a string. 6954aa6d63Sopenharmony_ci* code: Error code. 7054aa6d63Sopenharmony_ci* details: Error description information. 7154aa6d63Sopenharmony_ci**/ 7254aa6d63Sopenharmony_ciinline void PrintErrorNumberMsg(const std::string& command, const int code, const std::string& details) 7354aa6d63Sopenharmony_ci{ 7454aa6d63Sopenharmony_ci std::cerr << GetSystemTime() << " ERROR - " << command << ", code: " 7554aa6d63Sopenharmony_ci << code << ". Details: " << details << std::endl; 7654aa6d63Sopenharmony_ci} 7754aa6d63Sopenharmony_ci 7854aa6d63Sopenharmony_ci/* 7954aa6d63Sopenharmony_ci* Function: Print a prompt to the terminal. 8054aa6d63Sopenharmony_ci* Parametric Description: message 8154aa6d63Sopenharmony_ci* message: Prompt Description Information. 8254aa6d63Sopenharmony_ci**/ 8354aa6d63Sopenharmony_ciinline void PrintMsg(const std::string& message) 8454aa6d63Sopenharmony_ci{ 8554aa6d63Sopenharmony_ci std::cout << GetSystemTime() << " INFO - " << message << std::endl; 8654aa6d63Sopenharmony_ci} 8754aa6d63Sopenharmony_ci} // namespace SignatureTools 8854aa6d63Sopenharmony_ci} // namespace OHOS 8954aa6d63Sopenharmony_ci#endif // SIGNATURETOOLS_SIGNATRUE_TOOLS_LOG_H