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 "dfx/log/log_base.h"
17 #include <string>
18 #include <cstdarg>
19 #include <iostream>
20 #include <securec.h>
21 #include <chrono>
22 #include "internal_inc/osal.h"
23
24 static const int g_logBufferSize = 2048;
25
GetCurrentTime(void)26 static std::string GetCurrentTime(void)
27 {
28 const int startYear = 1900;
29 auto now = std::chrono::system_clock::now();
30 auto curMs = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch());
31 auto sectime = std::chrono::duration_cast<std::chrono::seconds>(curMs);
32 auto milltime = curMs % 1000;
33 std::time_t timet = sectime.count();
34 struct tm curtime;
35 localtime_r(&timet, &curtime);
36
37 auto year = std::to_string(curtime.tm_year + startYear);
38 auto mon = std::to_string(curtime.tm_mon + 1);
39 auto day = std::to_string(curtime.tm_mday);
40 auto hour = std::to_string(curtime.tm_hour);
41 auto min = std::to_string(curtime.tm_min);
42 auto sec = std::to_string(curtime.tm_sec);
43 auto ms = std::to_string(milltime.count());
44
45 return year + "-" + mon + "-" + day + " " + hour + ":" + min + ":" + sec + "." + ms;
46 }
47
LogOutput(const char * level,const char * log)48 static void LogOutput(const char* level, const char* log)
49 {
50 std::string pid, tid, strBuf;
51 pid = std::to_string(GetPid());
52 tid = std::to_string(syscall(SYS_gettid));
53
54 strBuf = GetCurrentTime() + " ";
55 strBuf += pid + " " + tid + " ";
56 strBuf += level;
57 strBuf += " ffrt : ";
58 strBuf += log;
59
60 std::cout << strBuf;
61 }
62
LogErr(const char * fmt,...)63 void LogErr(const char* fmt, ...)
64 {
65 char errLog[g_logBufferSize];
66 va_list arg;
67 va_start(arg, fmt);
68 int ret = vsnprintf_s(errLog, sizeof(errLog), sizeof(errLog) - 1, fmt, arg);
69 va_end(arg);
70 if (ret < 0) {
71 return;
72 }
73 LogOutput("E", errLog);
74 }
75
LogWarn(const char * fmt,...)76 void LogWarn(const char* fmt, ...)
77 {
78 char warnLog[g_logBufferSize];
79 va_list arg;
80 va_start(arg, fmt);
81 int ret = vsnprintf_s(warnLog, sizeof(warnLog), sizeof(warnLog) - 1, fmt, arg);
82 va_end(arg);
83 if (ret < 0) {
84 return;
85 }
86 LogOutput("W", warnLog);
87 }
88
LogInfo(const char * fmt,...)89 void LogInfo(const char* fmt, ...)
90 {
91 char infoLog[g_logBufferSize];
92 va_list arg;
93 va_start(arg, fmt);
94 int ret = vsnprintf_s(infoLog, sizeof(infoLog), sizeof(infoLog) - 1, fmt, arg);
95 va_end(arg);
96 if (ret < 0) {
97 return;
98 }
99 LogOutput("I", infoLog);
100 }
101
LogDebug(const char * fmt,...)102 void LogDebug(const char* fmt, ...)
103 {
104 char debugLog[g_logBufferSize];
105 va_list arg;
106 va_start(arg, fmt);
107 int ret = vsnprintf_s(debugLog, sizeof(debugLog), sizeof(debugLog) - 1, fmt, arg);
108 va_end(arg);
109 if (ret < 0) {
110 return;
111 }
112 LogOutput("D", debugLog);
113 }