• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "mpl_logging.h"
17 #include <unistd.h>
18 #ifndef _WIN32
19 #include <sys/syscall.h>
20 #endif
21 #include "securec.h"
22 
23 namespace {
24 constexpr uint32_t kMaxLogLen = 10000;
25 }
26 
27 namespace maple {
28 LogInfo logInfo;
29 LogInfo &log = logInfo;
30 
31 const char *kLongLogLevels[] = {
32     [kLlDbg] = "D", [kLlLog] = "L", [kLlInfo] = "Info ", [kLlWarn] = "Warn ", [kLlErr] = "Error", [kLlFatal] = "Fatal"};
33 
34 const char *tags[] = {
35     [kLtThread] = "TR",
36     [kLtLooper] = "LP",
37 };
38 
39 SECUREC_ATTRIBUTE(4, 5)
EmitLogForUser(enum LogNumberCode num,enum LogLevel ll,const char * fmt,...) const40 void LogInfo::EmitLogForUser(enum LogNumberCode num, enum LogLevel ll, const char *fmt, ...) const
41 {
42     char buf[kMaxLogLen];
43     int len = snprintf_s(buf, kMaxLogLen, kMaxLogLen - 1, "%s %02d: ", kLongLogLevels[ll], num);
44     if (len == -1) {
45         WARN(kLncWarn, "snprintf_s failed");
46         return;
47     }
48     if (outMode) {
49         va_list l;
50         va_start(l, fmt);
51         int eNum = vsnprintf_s(buf + len, kMaxLogLen - len, static_cast<size_t>(kMaxLogLen - len - 1), fmt, l);
52         if (eNum == -1) {
53             WARN(kLncWarn, "vsnprintf_s failed");
54             va_end(l);
55             return;
56         }
57         va_end(l);
58     }
59     std::cerr << buf << '\n';
60     return;
61 }
62 
EmitLogForUser(enum LogNumberCode num,enum LogLevel ll,const std::string & message) const63 void LogInfo::EmitLogForUser(enum LogNumberCode num, enum LogLevel ll, const std::string &message) const
64 {
65     EmitLogForUser(num, ll, "%s", message.c_str());
66 }
67 
68 SECUREC_ATTRIBUTE(5, 6)
EmitErrorMessage(const std::string & cond,const std::string & file,unsigned int line,const char * fmt,...) const69 void LogInfo::EmitErrorMessage(const std::string &cond, const std::string &file, unsigned int line, const char *fmt,
70                                ...) const
71 {
72     char buf[kMaxLogLen];
73 #ifdef _WIN32
74     int len = snprintf_s(buf, kMaxLogLen, kMaxLogLen - 1, "CHECK/CHECK_FATAL failure: %s at [%s:%d] ", cond.c_str(),
75                          file.c_str(), line);
76 #else
77     pid_t tid = syscall(SYS_gettid);
78     int len = snprintf_s(buf, kMaxLogLen, kMaxLogLen - 1, "Tid(%d): CHECK/CHECK_FATAL failure: %s at [%s:%d] ", tid,
79                          cond.c_str(), file.c_str(), line);
80 #endif
81     if (len == -1) {
82         WARN(kLncWarn, "snprintf_s failed");
83         return;
84     }
85     va_list l;
86     va_start(l, fmt);
87     int eNum = vsnprintf_s(buf + len, kMaxLogLen - len, static_cast<size_t>(kMaxLogLen - len - 1), fmt, l);
88     if (eNum == -1) {
89         WARN(kLncWarn, "vsnprintf_s failed");
90         va_end(l);
91         return;
92     }
93     va_end(l);
94     std::cerr << buf;
95 }
96 
MapleLogger(LogLevel level)97 std::ostream &LogInfo::MapleLogger(LogLevel level)
98 {
99     if (level == kLlLog) {
100         return std::cout;
101     }
102     return std::cerr;
103 }
104 }  // namespace maple
105