1fb299fa2Sopenharmony_ci/* 2fb299fa2Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 3fb299fa2Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4fb299fa2Sopenharmony_ci * you may not use this file except in compliance with the License. 5fb299fa2Sopenharmony_ci * You may obtain a copy of the License at 6fb299fa2Sopenharmony_ci * 7fb299fa2Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8fb299fa2Sopenharmony_ci * 9fb299fa2Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10fb299fa2Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11fb299fa2Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12fb299fa2Sopenharmony_ci * See the License for the specific language governing permissions and 13fb299fa2Sopenharmony_ci * limitations under the License. 14fb299fa2Sopenharmony_ci */ 15fb299fa2Sopenharmony_ci#include "log/log.h" 16fb299fa2Sopenharmony_ci#include <chrono> 17fb299fa2Sopenharmony_ci#include <cstdarg> 18fb299fa2Sopenharmony_ci#include <memory> 19fb299fa2Sopenharmony_ci#include <unordered_map> 20fb299fa2Sopenharmony_ci#include <vector> 21fb299fa2Sopenharmony_ci#include <sys/time.h> 22fb299fa2Sopenharmony_ci#include <unistd.h> 23fb299fa2Sopenharmony_ci#include <mutex> 24fb299fa2Sopenharmony_ci#ifndef DIFF_PATCH_SDK 25fb299fa2Sopenharmony_ci#include "hilog_base/log_base.h" 26fb299fa2Sopenharmony_ci#include "vsnprintf_s_p.h" 27fb299fa2Sopenharmony_ci#endif 28fb299fa2Sopenharmony_ci#include "securec.h" 29fb299fa2Sopenharmony_ci 30fb299fa2Sopenharmony_cinamespace Updater { 31fb299fa2Sopenharmony_cistatic std::ofstream g_updaterLog; 32fb299fa2Sopenharmony_cistatic std::mutex g_updaterLogLock; 33fb299fa2Sopenharmony_cistatic std::ofstream g_updaterStage; 34fb299fa2Sopenharmony_cistatic std::ofstream g_errorCode; 35fb299fa2Sopenharmony_cistatic std::ofstream g_nullStream; 36fb299fa2Sopenharmony_cistatic std::string g_logTag; 37fb299fa2Sopenharmony_cistatic int g_logLevel = INFO; 38fb299fa2Sopenharmony_ci#ifndef DIFF_PATCH_SDK 39fb299fa2Sopenharmony_cistatic constexpr unsigned int UPDATER_DOMAIN = 0XD002E01; 40fb299fa2Sopenharmony_ci#endif 41fb299fa2Sopenharmony_ci 42fb299fa2Sopenharmony_civoid InitUpdaterLogger(const std::string &tag, const std::string &logFile, const std::string &stageFile, 43fb299fa2Sopenharmony_ci const std::string &errorCodeFile) 44fb299fa2Sopenharmony_ci{ 45fb299fa2Sopenharmony_ci g_logTag = tag; 46fb299fa2Sopenharmony_ci g_updaterLog.open(logFile.c_str(), std::ios::app | std::ios::out); 47fb299fa2Sopenharmony_ci g_updaterStage.open(stageFile.c_str(), std::ios::app | std::ios::out); 48fb299fa2Sopenharmony_ci g_errorCode.open(errorCodeFile.c_str(), std::ios::app | std::ios::out); 49fb299fa2Sopenharmony_ci} 50fb299fa2Sopenharmony_ci 51fb299fa2Sopenharmony_ciUpdaterLogger::~UpdaterLogger() 52fb299fa2Sopenharmony_ci{ 53fb299fa2Sopenharmony_ci std::string str = oss_.str(); 54fb299fa2Sopenharmony_ci if (g_logLevel > level_) { 55fb299fa2Sopenharmony_ci return; 56fb299fa2Sopenharmony_ci } 57fb299fa2Sopenharmony_ci pid_t tid = 0; 58fb299fa2Sopenharmony_ci#ifndef DIFF_PATCH_SDK 59fb299fa2Sopenharmony_ci HiLogBasePrint(LOG_CORE, (LogLevel)level_, UPDATER_DOMAIN, g_logTag.c_str(), "%{public}s", str.c_str()); 60fb299fa2Sopenharmony_ci tid = gettid(); 61fb299fa2Sopenharmony_ci#endif 62fb299fa2Sopenharmony_ci oss_.str(""); 63fb299fa2Sopenharmony_ci oss_ << std::endl << std::flush; 64fb299fa2Sopenharmony_ci if (g_updaterLog.is_open()) { 65fb299fa2Sopenharmony_ci std::lock_guard<std::mutex> lock(g_updaterLogLock); 66fb299fa2Sopenharmony_ci g_updaterLog << realTime_ << " " << g_logTag << " " << tid << " " 67fb299fa2Sopenharmony_ci << logLevelMap_[level_] << " " << str << std::endl << std::flush; 68fb299fa2Sopenharmony_ci } 69fb299fa2Sopenharmony_ci} 70fb299fa2Sopenharmony_ci 71fb299fa2Sopenharmony_ciStageLogger::~StageLogger() 72fb299fa2Sopenharmony_ci{ 73fb299fa2Sopenharmony_ci if (g_updaterStage.is_open()) { 74fb299fa2Sopenharmony_ci g_updaterStage << std::endl << std::flush; 75fb299fa2Sopenharmony_ci } else { 76fb299fa2Sopenharmony_ci std::cout << std::endl << std::flush; 77fb299fa2Sopenharmony_ci } 78fb299fa2Sopenharmony_ci} 79fb299fa2Sopenharmony_ci 80fb299fa2Sopenharmony_civoid SetLogLevel(int level) 81fb299fa2Sopenharmony_ci{ 82fb299fa2Sopenharmony_ci g_logLevel = level; 83fb299fa2Sopenharmony_ci} 84fb299fa2Sopenharmony_ci 85fb299fa2Sopenharmony_civoid GetFormatTime(char time[], int size) 86fb299fa2Sopenharmony_ci{ 87fb299fa2Sopenharmony_ci#ifndef DIFF_PATCH_SDK 88fb299fa2Sopenharmony_ci struct timeval tv {}; 89fb299fa2Sopenharmony_ci struct tm tm {}; 90fb299fa2Sopenharmony_ci 91fb299fa2Sopenharmony_ci gettimeofday(&tv, nullptr); 92fb299fa2Sopenharmony_ci localtime_r(&tv.tv_sec, &tm); 93fb299fa2Sopenharmony_ci snprintf_s(time, size, size - 1, "%02d-%02d %02d:%02d:%02d.%03d", 94fb299fa2Sopenharmony_ci tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, 95fb299fa2Sopenharmony_ci static_cast<int>(tv.tv_usec / 1000)); // need div 1000 96fb299fa2Sopenharmony_ci#endif 97fb299fa2Sopenharmony_ci} 98fb299fa2Sopenharmony_ci 99fb299fa2Sopenharmony_cistd::ostream& UpdaterLogger::OutputUpdaterLog(const std::string &path, int line) 100fb299fa2Sopenharmony_ci{ 101fb299fa2Sopenharmony_ci GetFormatTime(realTime_, sizeof(realTime_)); 102fb299fa2Sopenharmony_ci if (g_logLevel <= level_) { 103fb299fa2Sopenharmony_ci return oss_ << path << " " << line << " : "; 104fb299fa2Sopenharmony_ci } 105fb299fa2Sopenharmony_ci return g_nullStream; 106fb299fa2Sopenharmony_ci} 107fb299fa2Sopenharmony_ci 108fb299fa2Sopenharmony_cistd::ostream& StageLogger::OutputUpdaterStage() 109fb299fa2Sopenharmony_ci{ 110fb299fa2Sopenharmony_ci std::unordered_map<int, std::string> updaterStageMap = { 111fb299fa2Sopenharmony_ci { UPDATE_STAGE_BEGIN, "BEGIN" }, 112fb299fa2Sopenharmony_ci { UPDATE_STAGE_SUCCESS, "SUCCESS" }, 113fb299fa2Sopenharmony_ci { UPDATE_STAGE_FAIL, "FAIL" }, 114fb299fa2Sopenharmony_ci { UPDATE_STAGE_OUT, "OUT" } 115fb299fa2Sopenharmony_ci }; 116fb299fa2Sopenharmony_ci char realTime[MAX_TIME_SIZE] = {0}; 117fb299fa2Sopenharmony_ci GetFormatTime(realTime, sizeof(realTime)); 118fb299fa2Sopenharmony_ci 119fb299fa2Sopenharmony_ci if (g_updaterLog.is_open()) { 120fb299fa2Sopenharmony_ci if (stage_ == UPDATE_STAGE_OUT) { 121fb299fa2Sopenharmony_ci return g_updaterStage << realTime << " " << g_logTag << " "; 122fb299fa2Sopenharmony_ci } 123fb299fa2Sopenharmony_ci return g_updaterStage << realTime << " " << g_logTag << " status is : " << 124fb299fa2Sopenharmony_ci updaterStageMap[stage_] << ", stage is "; 125fb299fa2Sopenharmony_ci } 126fb299fa2Sopenharmony_ci return std::cout; 127fb299fa2Sopenharmony_ci} 128fb299fa2Sopenharmony_ci 129fb299fa2Sopenharmony_civoid Logger(int level, const char* fileName, int32_t line, const char* format, ...) 130fb299fa2Sopenharmony_ci{ 131fb299fa2Sopenharmony_ci std::vector<char> buff(1024); // 1024 : max length of buff 132fb299fa2Sopenharmony_ci va_list list; 133fb299fa2Sopenharmony_ci va_start(list, format); 134fb299fa2Sopenharmony_ci int size = vsnprintf_s(reinterpret_cast<char*>(buff.data()), buff.capacity(), buff.capacity(), format, list); 135fb299fa2Sopenharmony_ci va_end(list); 136fb299fa2Sopenharmony_ci if (size < EOK) { 137fb299fa2Sopenharmony_ci UpdaterLogger(level).OutputUpdaterLog(fileName, line) << "vsnprintf_s failed"; 138fb299fa2Sopenharmony_ci return; 139fb299fa2Sopenharmony_ci } 140fb299fa2Sopenharmony_ci std::string str(buff.data(), size); 141fb299fa2Sopenharmony_ci UpdaterLogger(level).OutputUpdaterLog(fileName, line) << str; 142fb299fa2Sopenharmony_ci} 143fb299fa2Sopenharmony_ci 144fb299fa2Sopenharmony_ci#ifndef DIFF_PATCH_SDK 145fb299fa2Sopenharmony_ci// used for external module to adapt %{private|public} format log to updater log 146fb299fa2Sopenharmony_civoid UpdaterHiLogger(int level, const char* fileName, int32_t line, const char* format, ...) 147fb299fa2Sopenharmony_ci{ 148fb299fa2Sopenharmony_ci char buf[MAX_LOG_LEN] = {0}; 149fb299fa2Sopenharmony_ci va_list list; 150fb299fa2Sopenharmony_ci va_start(list, format); 151fb299fa2Sopenharmony_ci int size = vsnprintfp_s(buf, MAX_LOG_LEN, MAX_LOG_LEN - 1, true, format, list); 152fb299fa2Sopenharmony_ci va_end(list); 153fb299fa2Sopenharmony_ci if (size < EOK) { 154fb299fa2Sopenharmony_ci UpdaterLogger(level).OutputUpdaterLog(fileName, line) << "vsnprintfp_s failed " << size; 155fb299fa2Sopenharmony_ci } else { 156fb299fa2Sopenharmony_ci UpdaterLogger(level).OutputUpdaterLog(fileName, line) << std::string(buf, size); 157fb299fa2Sopenharmony_ci } 158fb299fa2Sopenharmony_ci} 159fb299fa2Sopenharmony_ci#endif 160fb299fa2Sopenharmony_ci 161fb299fa2Sopenharmony_cistd::ostream& ErrorCode::OutputErrorCode(const std::string &path, int line, UpdaterErrorCode code) 162fb299fa2Sopenharmony_ci{ 163fb299fa2Sopenharmony_ci char realTime[MAX_TIME_SIZE] = {0}; 164fb299fa2Sopenharmony_ci GetFormatTime(realTime, sizeof(realTime)); 165fb299fa2Sopenharmony_ci if (g_errorCode.is_open()) { 166fb299fa2Sopenharmony_ci return g_errorCode << realTime << " " << path << " " << line << " , error code is : " << code << std::endl; 167fb299fa2Sopenharmony_ci } 168fb299fa2Sopenharmony_ci return std::cout; 169fb299fa2Sopenharmony_ci} 170fb299fa2Sopenharmony_ci} // Updater 171