• 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 "logger.h"
17 
18 #include <set>
19 
20 #ifdef PLATFORM_HAS_JAVA
21 #include <os/java/java_internal.h>
22 #endif
23 #include <securec.h>
24 
25 #include <core/namespace.h>
26 #include <core/plugin/intf_plugin_register.h>
27 
28 #include "log/logger_output.h"
29 
30 CORE_BEGIN_NAMESPACE()
31 using BASE_NS::string;
32 using BASE_NS::string_view;
33 using BASE_NS::Uid;
34 
35 namespace {
36 // Note: these must match the LogLevel enum.
37 constexpr int LOG_LEVEL_COUNT = 7;
38 constexpr const char* LOG_LEVEL_NAMES[LOG_LEVEL_COUNT] = {
39     "Verbose",
40     "Debug",
41     "Info",
42     "Warning",
43     "Error",
44     "Fatal",
45     "None",
46 };
47 
48 constexpr const char* LOG_LEVEL_NAMES_SHORT[LOG_LEVEL_COUNT] = {
49     "V",
50     "D",
51     "I",
52     "W",
53     "E",
54     "F",
55     "N",
56 };
57 constexpr const size_t MAX_BUFFER_SIZE = 1024;
58 } // namespace
59 
GetLogLevelName(LogLevel logLevel,bool shortName)60 string_view Logger::GetLogLevelName(LogLevel logLevel, bool shortName)
61 {
62     const int level = static_cast<int>(logLevel);
63     CORE_ASSERT(level >= 0 && level < LOG_LEVEL_COUNT);
64 
65     return shortName ? LOG_LEVEL_NAMES_SHORT[level] : LOG_LEVEL_NAMES[level];
66 }
67 
Logger(bool defaultOutputs)68 Logger::Logger(bool defaultOutputs)
69 #ifdef NDEBUG
70     : logLevel_(LogLevel::LOG_INFO)
71 #endif
72 {
73     if (defaultOutputs) {
74 #if CORE_LOG_TO_CONSOLE == 1
75         AddOutput(CreateLoggerConsoleOutput());
76 #endif
77 
78 #if CORE_LOG_TO_DEBUG_OUTPUT == 1
79         AddOutput(CreateLoggerDebugOutput());
80 #endif
81 
82 #if CORE_LOG_TO_FILE == 1
83         AddOutput(CreateLoggerFileOutput("./logfile.txt"));
84 #endif
85     }
86 }
87 
88 Logger::~Logger() = default;
89 
VLog(LogLevel logLevel,const string_view filename,int lineNumber,const string_view format,va_list args)90 void Logger::VLog(LogLevel logLevel, const string_view filename, int lineNumber, const string_view format, va_list args)
91 {
92     CORE_ASSERT_MSG(logLevel != LogLevel::LOG_NONE, "'None' is not a valid log level for writing to the log.");
93 
94     if (logLevel_ > logLevel) {
95         return;
96     }
97 
98     // we need to make a copy of the args, since the va_list can be in an undefined state after use.
99     va_list tmp;
100     va_copy(tmp, args);
101 
102     // use vsnprintf to calculate the required size (not supported by the _s variant)
103     const int sizeNeeded = vsnprintf(nullptr, 0, format.data(), args) + 1;
104     va_end(args);
105 
106     std::lock_guard guard(loggerMutex_);
107 
108     if (sizeNeeded > 0 && static_cast<size_t>(sizeNeeded) > buffer_.size()) {
109         buffer_.resize(static_cast<size_t>(sizeNeeded));
110     }
111 
112     int ret = vsnprintf_s(buffer_.data(), buffer_.size(), buffer_.size() - 1, format.data(), tmp);
113     va_end(tmp);
114     if (ret < 0) {
115         return;
116     }
117 
118     for (auto& output : outputs_) {
119         output->Write(logLevel, filename, lineNumber, buffer_.data());
120     }
121 }
122 
VLogOnce(const string_view id,LogLevel logLevel,const string_view filename,int lineNumber,const string_view format,va_list args)123 void Logger::VLogOnce(const string_view id, LogLevel logLevel, const string_view filename, int lineNumber,
124     const string_view format, va_list args)
125 {
126     std::lock_guard<std::mutex> guard(onceMutex_);
127 
128     auto const [pos, inserted] = registeredOnce_.insert(string(id));
129     if (inserted) {
130         VLog(logLevel, filename, lineNumber, format, args);
131     }
132 }
133 
VLogAssert(const string_view filename,int lineNumber,bool expression,const string_view expressionString,const string_view format,va_list args)134 bool Logger::VLogAssert(const string_view filename, int lineNumber, bool expression, const string_view expressionString,
135     const string_view format, va_list args)
136 {
137     if (!expression) {
138         char buffer[MAX_BUFFER_SIZE];
139         const int numWritten = vsnprintf_s(buffer, MAX_BUFFER_SIZE, MAX_BUFFER_SIZE - 1, format.data(), args);
140         if (numWritten >= 0) {
141             buffer[numWritten] = '\0';
142         } else {
143             buffer[0] = '\0';
144         }
145 
146         Log(LogLevel::LOG_FATAL, filename, lineNumber, "Assert failed (%s). %s", expressionString.data(), buffer);
147 
148 #ifdef PLATFORM_HAS_JAVA
149         // Print also a java trace if available
150         Log(LogLevel::LOG_FATAL, filename, lineNumber, "Java trace:");
151         JNIEnv* env = java_internal::GetJavaEnv();
152         if (env) {
153             jclass cls = env->FindClass("java/lang/Exception");
154             if (cls) {
155                 jmethodID constructor = env->GetMethodID(cls, "<init>", "()V");
156                 jobject exception = env->NewObject(cls, constructor);
157                 jmethodID printStackTrace = env->GetMethodID(cls, "printStackTrace", "()V");
158                 env->CallVoidMethod(exception, printStackTrace);
159                 env->DeleteLocalRef(exception);
160             }
161         }
162 #endif
163     }
164 
165     return expression;
166 }
167 
CheckOnceReset()168 void Logger::CheckOnceReset()
169 {
170     std::lock_guard<std::mutex> guard(onceMutex_);
171     registeredOnce_.clear();
172 }
173 
174 FORMAT_FUNC(5, 6)
Log(LogLevel logLevel,const string_view filename,int lineNumber,FORMAT_ATTRIBUTE const char * format,...)175 void Logger::Log(
176     LogLevel logLevel, const string_view filename, int lineNumber, FORMAT_ATTRIBUTE const char* format, ...)
177 {
178     va_list vl;
179     va_start(vl, format);
180     VLog(logLevel, filename, lineNumber, format, vl);
181     va_end(vl);
182 }
183 
184 FORMAT_FUNC(6, 7)
LogAssert(const string_view filename,int lineNumber,bool expression,const string_view expressionString,FORMAT_ATTRIBUTE const char * format,...)185 bool Logger::LogAssert(const string_view filename, int lineNumber, bool expression, const string_view expressionString,
186     FORMAT_ATTRIBUTE const char* format, ...)
187 {
188     if (!expression) {
189         va_list vl;
190         va_start(vl, format);
191         VLogAssert(filename, lineNumber, expression, expressionString, format, vl);
192         va_end(vl);
193     }
194     return expression;
195 }
196 
GetLogLevel() const197 ILogger::LogLevel Logger::GetLogLevel() const
198 {
199     return logLevel_;
200 }
201 
SetLogLevel(LogLevel logLevel)202 void Logger::SetLogLevel(LogLevel logLevel)
203 {
204     logLevel_ = logLevel;
205 }
206 
AddOutput(IOutput::Ptr output)207 void Logger::AddOutput(IOutput::Ptr output)
208 {
209     if (output) {
210         std::lock_guard<std::mutex> guard(loggerMutex_);
211         outputs_.push_back(move(output));
212     }
213 }
214 
GetInterface(const Uid & uid) const215 const IInterface* Logger::GetInterface(const Uid& uid) const
216 {
217     if (uid == ILogger::UID) {
218         return this;
219     }
220     return nullptr;
221 }
222 
GetInterface(const Uid & uid)223 IInterface* Logger::GetInterface(const Uid& uid)
224 {
225     if (uid == ILogger::UID) {
226         return this;
227     }
228     return nullptr;
229 }
230 
Ref()231 void Logger::Ref() {}
232 
Unref()233 void Logger::Unref() {}
234 CORE_END_NAMESPACE()
235