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