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