• 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 #ifndef CORE_LOG_LOGGER_H
17 #define CORE_LOG_LOGGER_H
18 
19 #include <mutex>
20 #include <set>
21 
22 #include <base/containers/string.h>
23 #include <base/containers/vector.h>
24 #include <core/log.h>
25 #include <core/namespace.h>
26 
CORE_BEGIN_NAMESPACE()27 CORE_BEGIN_NAMESPACE()
28 class Logger : public ILogger {
29 public:
30     static BASE_NS::string_view GetLogLevelName(LogLevel logLevel, bool shortName);
31 
32     explicit Logger(bool defaultOutputs);
33     ~Logger() override;
34 
35     void VLog(LogLevel logLevel, const BASE_NS::string_view filename, int lineNumber, const BASE_NS::string_view format,
36         va_list args) override;
37     void VLogOnce(const BASE_NS::string_view id, LogLevel logLevel, const BASE_NS::string_view filename, int lineNumber,
38         const BASE_NS::string_view format, va_list args) override;
39     bool VLogAssert(const BASE_NS::string_view filename, int lineNumber, bool expression,
40         const BASE_NS::string_view expressionString, const BASE_NS::string_view format, va_list args) override;
41 
42     FORMAT_FUNC(5, 6)
43     void Log(LogLevel logLevel, const BASE_NS::string_view filename, int lineNumber,
44         FORMAT_ATTRIBUTE const char* format, ...) override;
45 
46     FORMAT_FUNC(6, 7)
47     bool LogAssert(const BASE_NS::string_view filename, int lineNumber, bool expression,
48         const BASE_NS::string_view expressionString, FORMAT_ATTRIBUTE const char* format, ...) override;
49 
50     LogLevel GetLogLevel() const override;
51     void SetLogLevel(LogLevel logLevel) override;
52 
53     void AddOutput(IOutput::Ptr output) override;
54     void CheckOnceReset() override;
55 
56     // IInterface
57     const IInterface* GetInterface(const BASE_NS::Uid& uid) const override;
58     IInterface* GetInterface(const BASE_NS::Uid& uid) override;
59     void Ref() override;
60     void Unref() override;
61 
62 private:
63     LogLevel logLevel_ = LogLevel::LOG_VERBOSE;
64     std::mutex loggerMutex_;
65 
66     BASE_NS::vector<char> buffer_;
67     BASE_NS::vector<IOutput::Ptr> outputs_;
68     std::set<BASE_NS::string> registeredOnce_; // Global set of ids used by the CORE_ONCE macro.
69     std::mutex onceMutex_;
70 };
71 CORE_END_NAMESPACE()
72 
73 #endif // CORE_LOG_LOGGER_H
74