1/**
2 * Copyright (c) 2021-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
16#ifndef COMPILER_COMPILER_LOGGER_H
17#define COMPILER_COMPILER_LOGGER_H
18
19#include <bitset>
20#include <vector>
21#include "utils/logger.h"
22
23namespace panda::compiler {
24#include "compiler_logger_components.inc"
25
26enum CompilerLoggerComponents : uint8_t {
27// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
28#define DEF(COMPONENT, ...) COMPONENT,
29    COMPILER_LOG_COMPONENTS(DEF)
30#undef DEF
31        LOG_COMPONENTS_NUM
32};
33
34class CompilerLogger {
35    NO_COPY_SEMANTIC(CompilerLogger);
36    NO_MOVE_SEMANTIC(CompilerLogger);
37
38public:
39    static void SetComponents(const std::vector<std::string> &args);
40
41    static void Init([[maybe_unused]] const std::vector<std::string> &args)
42    {
43#ifndef NDEBUG
44        SetComponents(args);
45#endif
46    }
47
48    static inline bool IsComponentEnabled(CompilerLoggerComponents comp)
49    {
50        return components.test(comp);
51    }
52
53    static void EnableComponent(CompilerLoggerComponents comp)
54    {
55        components.set(comp);
56    }
57
58private:
59    CompilerLogger() = default;
60    ~CompilerLogger() = default;
61
62private:
63    static std::bitset<CompilerLoggerComponents::LOG_COMPONENTS_NUM> components;
64};
65
66#ifndef NDEBUG
67
68// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
69#define COMPILER_LOG(level, comp) \
70    CompilerLogger::IsComponentEnabled(CompilerLoggerComponents::comp) && LOG(level, COMPILER) << "[" #comp "] "
71
72#else
73
74// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
75#define COMPILER_LOG(level, comp) \
76    CompilerLogger::IsComponentEnabled(CompilerLoggerComponents::comp) && LOG(level, COMPILER) << "[" #comp "] "
77
78#endif
79}  // namespace panda::compiler
80
81#endif  // COMPILER_COMPILER_LOGGER_H
82