106f6ba60Sopenharmony_ci/* 206f6ba60Sopenharmony_ci * Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved. 306f6ba60Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 406f6ba60Sopenharmony_ci * you may not use this file except in compliance with the License. 506f6ba60Sopenharmony_ci * You may obtain a copy of the License at 606f6ba60Sopenharmony_ci * 706f6ba60Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 806f6ba60Sopenharmony_ci * 906f6ba60Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1006f6ba60Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1106f6ba60Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1206f6ba60Sopenharmony_ci * See the License for the specific language governing permissions and 1306f6ba60Sopenharmony_ci * limitations under the License. 1406f6ba60Sopenharmony_ci */ 1506f6ba60Sopenharmony_ci 1606f6ba60Sopenharmony_ci#include "sp_log.h" 1706f6ba60Sopenharmony_ci 1806f6ba60Sopenharmony_ci#include "securec.h" 1906f6ba60Sopenharmony_ci 2006f6ba60Sopenharmony_ci#ifdef HI_LOG_ENABLE 2106f6ba60Sopenharmony_ci#include "hilog/log.h" 2206f6ba60Sopenharmony_ci#include <string> 2306f6ba60Sopenharmony_ci#include <sstream> 2406f6ba60Sopenharmony_ci#include <iostream> 2506f6ba60Sopenharmony_ci#include <cstring> 2606f6ba60Sopenharmony_ci#include <cstdint> 2706f6ba60Sopenharmony_ci#include <unistd.h> 2806f6ba60Sopenharmony_ci#else 2906f6ba60Sopenharmony_ci#include <cstdio> 3006f6ba60Sopenharmony_ci#endif 3106f6ba60Sopenharmony_ci 3206f6ba60Sopenharmony_cinamespace OHOS { 3306f6ba60Sopenharmony_cinamespace SmartPerf { 3406f6ba60Sopenharmony_ciconst int32_t LOG_MAX_LEN = 10000; 3506f6ba60Sopenharmony_ci 3606f6ba60Sopenharmony_cistatic void SpLogOut(SpLogLevel logLevel, const char *logBuf) 3706f6ba60Sopenharmony_ci{ 3806f6ba60Sopenharmony_ci#ifdef HI_LOG_ENABLE 3906f6ba60Sopenharmony_ci LogLevel hiLogLevel = LOG_INFO; 4006f6ba60Sopenharmony_ci switch (logLevel) { 4106f6ba60Sopenharmony_ci case SP_LOG_DEBUG: 4206f6ba60Sopenharmony_ci hiLogLevel = LOG_DEBUG; 4306f6ba60Sopenharmony_ci break; 4406f6ba60Sopenharmony_ci case SP_LOG_INFO: 4506f6ba60Sopenharmony_ci hiLogLevel = LOG_INFO; 4606f6ba60Sopenharmony_ci break; 4706f6ba60Sopenharmony_ci case SP_LOG_WARN: 4806f6ba60Sopenharmony_ci hiLogLevel = LOG_WARN; 4906f6ba60Sopenharmony_ci break; 5006f6ba60Sopenharmony_ci case SP_LOG_ERROR: 5106f6ba60Sopenharmony_ci hiLogLevel = LOG_ERROR; 5206f6ba60Sopenharmony_ci break; 5306f6ba60Sopenharmony_ci default: 5406f6ba60Sopenharmony_ci break; 5506f6ba60Sopenharmony_ci } 5606f6ba60Sopenharmony_ci (void)HiLogPrint(LOG_CORE, hiLogLevel, LOG_DOMAIN, "SP_daemon", "%{public}s", logBuf); 5706f6ba60Sopenharmony_ci#else 5806f6ba60Sopenharmony_ci switch (logLevel) { 5906f6ba60Sopenharmony_ci case SP_LOG_DEBUG: 6006f6ba60Sopenharmony_ci printf("[D]%s\n", logBuf); 6106f6ba60Sopenharmony_ci break; 6206f6ba60Sopenharmony_ci case SP_LOG_INFO: 6306f6ba60Sopenharmony_ci printf("[I]%s\n", logBuf); 6406f6ba60Sopenharmony_ci break; 6506f6ba60Sopenharmony_ci case SP_LOG_WARN: 6606f6ba60Sopenharmony_ci printf("[W]%s\n", logBuf); 6706f6ba60Sopenharmony_ci break; 6806f6ba60Sopenharmony_ci case SP_LOG_ERROR: 6906f6ba60Sopenharmony_ci printf("[E]%s\n", logBuf); 7006f6ba60Sopenharmony_ci break; 7106f6ba60Sopenharmony_ci default: 7206f6ba60Sopenharmony_ci break; 7306f6ba60Sopenharmony_ci } 7406f6ba60Sopenharmony_ci#endif 7506f6ba60Sopenharmony_ci} 7606f6ba60Sopenharmony_ci 7706f6ba60Sopenharmony_civoid SpLog(SpLogLevel logLevel, const char *fmt, ...) 7806f6ba60Sopenharmony_ci{ 7906f6ba60Sopenharmony_ci char logBuf[LOG_MAX_LEN] = {0}; 8006f6ba60Sopenharmony_ci int32_t ret = 0; 8106f6ba60Sopenharmony_ci va_list arg; 8206f6ba60Sopenharmony_ci ret = memset_s(&arg, sizeof(va_list), 0, sizeof(va_list)); 8306f6ba60Sopenharmony_ci if (ret != 0) { 8406f6ba60Sopenharmony_ci SpLogOut(logLevel, "SP log memset_s error."); 8506f6ba60Sopenharmony_ci return; 8606f6ba60Sopenharmony_ci } 8706f6ba60Sopenharmony_ci va_start(arg, fmt); 8806f6ba60Sopenharmony_ci ret = vsprintf_s(logBuf, sizeof(logBuf), fmt, arg); 8906f6ba60Sopenharmony_ci va_end(arg); 9006f6ba60Sopenharmony_ci if (ret < 0) { 9106f6ba60Sopenharmony_ci SpLogOut(logLevel, "SP log length error."); 9206f6ba60Sopenharmony_ci return; 9306f6ba60Sopenharmony_ci } 9406f6ba60Sopenharmony_ci SpLogOut(logLevel, logBuf); 9506f6ba60Sopenharmony_ci} 9606f6ba60Sopenharmony_ci} // namespace SmartPerf 9706f6ba60Sopenharmony_ci} // namespace OHOS