• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-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 LIBLLVMBACKEND_LLVM_LOGGER_H
17 #define LIBLLVMBACKEND_LLVM_LOGGER_H
18 
19 #include "utils/logger.h"
20 #include <bitset>
21 #include <vector>
22 
23 namespace ark::llvmbackend {
24 #include "llvm_logger_components.inc"
25 
26 enum LLVMLoggerComponents : uint8_t {
27 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
28 #define DEF(COMPONENT, ...) COMPONENT,
29     LLVM_LOG_COMPONENTS(DEF)
30 #undef DEF
31         LOG_COMPONENTS_NUM
32 };
33 
34 class LLVMLogger {
35     NO_COPY_SEMANTIC(LLVMLogger);
36     NO_MOVE_SEMANTIC(LLVMLogger);
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(LLVMLoggerComponents comp,Logger::Level level)48     static inline bool IsComponentEnabled(LLVMLoggerComponents comp, Logger::Level level)
49     {
50         return components_.test(comp) || level == Logger::Level::FATAL;
51     }
52 
EnableComponent(LLVMLoggerComponents comp)53     static void EnableComponent(LLVMLoggerComponents comp)
54     {
55         components_.set(comp);
56     }
57 
58 private:
59     LLVMLogger() = default;
60     ~LLVMLogger() = default;
61 
62 private:
63     static std::bitset<LLVMLoggerComponents::LOG_COMPONENTS_NUM> components_;
64 };
65 
66 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
67 #define LLVM_LOG(level, comp)                                                                 \
68     ark::llvmbackend::LLVMLogger::IsComponentEnabled(                                         \
69         ark::llvmbackend::LLVMLoggerComponents::comp, /* CC-OFF(G.PRE.02) namespace member */ \
70         ark::Logger::Level::level) &&                 /* CC-OFF(G.PRE.02) namespace member */ \
71         LOG(level, LLVM) << "[" #comp "] "
72 
73 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
74 #define LLVM_LOG_IF(cond, level, comp) (cond) && LLVM_LOG(level, comp)
75 
76 }  // namespace ark::llvmbackend
77 
78 #endif  // LIBLLVMBACKEND_LLVM_LOGGER_H
79