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