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#include "PreviewerEngineLog.h" 17 18#include <cstdio> 19#include <iostream> 20#ifdef _WIN32 21#include <windows.h> 22#else 23#include "unistd.h" 24#endif 25 26#include "LocalDate.h" 27#include "TimeTool.h" 28#include "securec.h" 29 30void PrintLog(const char* level, const char* file, const char* func, int line, const char* fmt, ...) 31{ 32#ifdef NDEBUG 33 std::string levelStr(level); 34 if (levelStr == "DEBUG") { 35 return; 36 } 37#endif 38 static char output[1024] = { 0 }; 39 va_list argsList; 40 if (fmt == nullptr || *fmt == '\0') { 41 std::cerr << "PrintLog error: Format string is null or empty" << std::endl; 42 return; 43 } 44 va_start(argsList, fmt); 45 std::string fileStr(file); 46 int idx = fileStr.find_last_of("/"); 47 fileStr = fileStr.substr(idx + 1); 48 int ret = vsnprintf_s(output, sizeof(output), sizeof(output), fmt, argsList); 49 if (ret == -1) { 50 std::cout << "PrintLog function error"; 51 return; 52 } 53 if (stdout != nullptr) { 54 fprintf(stdout, "[%s][%s][%s][%d]%s:%s\n", level, fileStr.c_str(), func, line, 55 TimeTool::GetFormatTime().c_str(), output); 56 fflush(stdout); 57 } 58 va_end(argsList); 59}