• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 #include <sys/time.h>
16 #include <iomanip>
17 #include <map>
18 #include <securec.h>
19 #include <hilog/log.h>
20 
21 #include "hilog_common.h"
22 #include "log_utils.h"
23 #include "log_print.h"
24 
25 namespace OHOS {
26 namespace HiviewDFX {
27 using namespace std;
28 
29 static constexpr int COLOR_BLUE = 75;
30 static constexpr int COLOR_DEFAULT = 231;
31 static constexpr int COLOR_GREEN = 40;
32 static constexpr int COLOR_ORANGE = 166;
33 static constexpr int COLOR_RED = 196;
34 static constexpr int COLOR_YELLOW = 226;
35 static constexpr int TM_YEAR_BASE = 1900;
36 static constexpr int DT_WIDTH = 2;
37 static constexpr long long NS2US = 1000LL;
38 static constexpr long long NS2MS = 1000000LL;
39 static constexpr int MONO_WIDTH = 8;
40 static constexpr int EPOCH_WIDTH = 10;
41 static constexpr int MSEC_WIDTH = 3;
42 static constexpr int USEC_WIDTH = 6;
43 static constexpr int NSEC_WIDTH = 9;
44 static constexpr int PID_WIDTH = 5;
45 static constexpr int DOMAIN_WIDTH = 5;
46 static constexpr int DOMAIN_SHORT_MASK = 0xFFFFF;
47 
GetColor(uint16_t level)48 static inline int GetColor(uint16_t level)
49 {
50     switch (LogLevel(level)) {
51         case LOG_DEBUG: return COLOR_BLUE;
52         case LOG_INFO: return COLOR_GREEN;
53         case LOG_WARN: return COLOR_ORANGE;
54         case LOG_ERROR: return COLOR_RED;
55         case LOG_FATAL: return COLOR_YELLOW;
56         default: return COLOR_DEFAULT;
57     }
58 }
59 
GetLogTypePrefix(uint16_t type)60 static inline const char* GetLogTypePrefix(uint16_t type)
61 {
62     switch (LogType(type)) {
63         case LOG_APP: return "A";
64         case LOG_INIT: return "I";
65         case LOG_CORE: return "C";
66         case LOG_KMSG: return "K";
67         default: return " ";
68     }
69 }
70 
ShortDomain(uint32_t d)71 static inline uint32_t ShortDomain(uint32_t d)
72 {
73     return (d) & DOMAIN_SHORT_MASK;
74 }
75 
PrintLogPrefix(const LogContent & content,const LogFormat & format,std::ostream & out)76 static void PrintLogPrefix(const LogContent& content, const LogFormat& format, std::ostream& out)
77 {
78     // 1. print day & time
79     if (format.timeFormat == FormatTime::TIME) {
80         struct tm tl;
81         time_t time = content.tv_sec;
82 #if (defined( __WINDOWS__ ))
83         if (localtime_s(&tl, &time) != 0) {
84             return;
85         }
86 #else
87         if (localtime_r(&time, &tl) == nullptr) {
88             return;
89         }
90         if (format.zone) {
91             out << tl.tm_zone << " ";
92         }
93 #endif
94         if (format.year) {
95             out << (tl.tm_year + TM_YEAR_BASE) << "-";
96         }
97         out << setfill('0');
98         out << setw(DT_WIDTH) << (tl.tm_mon + 1) << "-" << setw(DT_WIDTH) << tl.tm_mday << " ";
99         out << setw(DT_WIDTH) << tl.tm_hour << ":" << setw(DT_WIDTH) << tl.tm_min << ":";
100         out << setw(DT_WIDTH) << tl.tm_sec;
101     } else if (format.timeFormat == FormatTime::MONOTONIC) {
102         out << setfill(' ');
103         out << setw(MONO_WIDTH) << content.mono_sec;
104     } else if (format.timeFormat == FormatTime::EPOCH) {
105         out << setfill(' ');
106         out << setw(EPOCH_WIDTH) << content.tv_sec;
107     } else {
108         out << "Invalid time format" << endl;
109         return;
110     }
111     // 1.1 print msec/usec/nsec
112     out << ".";
113     out << setfill('0');
114     if (format.timeAccuFormat == FormatTimeAccu::MSEC) {
115         out << setw(MSEC_WIDTH) << (content.tv_nsec / NS2MS);
116     } else if (format.timeAccuFormat == FormatTimeAccu::USEC) {
117         out << setw(USEC_WIDTH) << (content.tv_nsec / NS2US);
118     } else if (format.timeAccuFormat == FormatTimeAccu::NSEC) {
119         out << setw(NSEC_WIDTH) << content.tv_nsec;
120     } else {
121         out << "Invalid time accuracy format" << endl;
122         return;
123     }
124     // The kmsg logs are taken from /dev/kmsg, cannot obtain pid, tid or domain information
125     // The kmsg log printing format: 08-06 16:51:04.945 <6> [4294.967295] hungtask_base whitelist[0]-init-1
126     if (content.type != LOG_KMSG) {
127         out << setfill(' ');
128         // 2. print pid/tid
129         out << " " << setw(PID_WIDTH) << content.pid << " " << setw(PID_WIDTH) << content.tid;
130         // 3. print level
131         out << " " << LogLevel2ShortStr(content.level) << " ";
132         // 4. print log type
133         out << GetLogTypePrefix(content.type);
134         // 5. print domain
135         out << setfill('0');
136         out << hex << setw(DOMAIN_WIDTH) << ShortDomain(content.domain) << dec;
137         // 5. print tag & log
138         out << "/" << content.tag << ": ";
139     } else {
140         out << " " << content.tag << " ";
141     }
142 }
143 
LogPrintWithFormat(const LogContent & content,const LogFormat & format,std::ostream & out)144 void LogPrintWithFormat(const LogContent& content, const LogFormat& format, std::ostream& out)
145 {
146     // set colorful log
147     if (format.colorful) {
148         out << "\x1B[38;5;" << GetColor(content.level) << "m";
149     }
150 
151     const char *pHead = content.log;
152     const char *pScan = content.log;
153     // split the log content by '\n', and add log prefix(datetime, pid, tid....) to each new line
154     while (*pScan != '\0') {
155         if (*pScan == '\n') {
156             char tmp[MAX_LOG_LEN];
157             int len = static_cast<int>(pScan - pHead);
158             errno_t ret = memcpy_s(tmp, MAX_LOG_LEN - 1, pHead, len);
159             if (ret != EOK) {
160                 break;
161             }
162             tmp[(MAX_LOG_LEN - 1) > len ? len : (MAX_LOG_LEN -1)] = '\0';
163             if (tmp[0] != '\0') {
164                 PrintLogPrefix(content, format, out);
165                 out << tmp << endl;
166             }
167             pHead = pScan + 1;
168         }
169         pScan++;
170     }
171     if (pHead[0] != '\0') {
172         PrintLogPrefix(content, format, out);
173         out << pHead;
174     }
175 
176     // restore color
177     if (format.colorful) {
178         out << "\x1B[0m";
179     }
180     if (pHead[0] != '\0') {
181         out << endl;
182     }
183     return;
184 }
185 } // namespace HiviewDFX
186 } // namespace OHOS