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
30 using namespace std;
31
32
PrintLog(const char * level,const char * file,const char * func,int line,const char * fmt,...)33 void PrintLog(const char* level, const char* file, const char* func, int line, const char* fmt, ...)
34 {
35 #ifdef NDEBUG
36 string levelStr(level);
37 if (levelStr == "DEBUG") {
38 return;
39 }
40 #endif
41 static char output[1024] = { 0 };
42 va_list argsList;
43 va_start(argsList, fmt);
44 string fileStr(file);
45 int idx = fileStr.find_last_of("/");
46 fileStr = fileStr.substr(idx + 1);
47 int ret = vsnprintf_s(output, sizeof(output), sizeof(output), fmt, argsList);
48 if (ret == -1) {
49 cout << "PrintLog function error";
50 return;
51 }
52 if (stdout != nullptr) {
53 fprintf(stdout, "[%s][%s][%s][%d]%s:%s\n", level, fileStr.c_str(), func, line,
54 TimeTool::GetFormatTime().c_str(), output);
55 fflush(stdout);
56 }
57 va_end(argsList);
58 }