• 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     out << setfill(' ');
125     // 2. print pid/tid
126     out << " " << setw(PID_WIDTH) << content.pid << " " << setw(PID_WIDTH) << content.tid;
127     // 3. print level
128     out << " " << LogLevel2ShortStr(content.level) << " ";
129     // 4. print log type
130     out << GetLogTypePrefix(content.type);
131     // 5. print domain
132     out << setfill('0');
133     out << hex << setw(DOMAIN_WIDTH) << ShortDomain(content.domain) << dec;
134     // 5. print tag & log
135     out << "/" << content.tag << ": ";
136 }
137 
LogPrintWithFormat(const LogContent & content,const LogFormat & format,std::ostream & out)138 void LogPrintWithFormat(const LogContent& content, const LogFormat& format, std::ostream& out)
139 {
140     // set colorful log
141     if (format.colorful) {
142         out << "\x1B[38;5;" << GetColor(content.level) << "m";
143     }
144 
145     const char *pHead = content.log;
146     const char *pScan = content.log;
147     // split the log content by '\n', and add log prefix(datetime, pid, tid....) to each new line
148     while (*pScan != '\0') {
149         if (*pScan == '\n') {
150             char tmp[MAX_LOG_LEN];
151             int len = static_cast<int>(pScan - pHead);
152             errno_t ret = memcpy_s(tmp, MAX_LOG_LEN - 1, pHead, len);
153             if (ret != EOK) {
154                 break;
155             }
156             tmp[(MAX_LOG_LEN - 1) > len ? len : (MAX_LOG_LEN -1)] = '\0';
157             if (tmp[0] != '\0') {
158                 PrintLogPrefix(content, format, out);
159                 out << tmp << endl;
160             }
161             pHead = pScan + 1;
162         }
163         pScan++;
164     }
165     if (pHead[0] != '\0') {
166         PrintLogPrefix(content, format, out);
167         out << pHead;
168     }
169 
170     // restore color
171     if (format.colorful) {
172         out << "\x1B[0m";
173     }
174     if (pHead[0] != '\0') {
175         out << endl;
176     }
177     return;
178 }
179 } // namespace HiviewDFX
180 } // namespace OHOS