VART-X  0.4.0
vart_logger.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2024-2026 Advanced Micro Devices Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
24 #include <syslog.h>
25 #include <chrono>
26 #include <cstdarg>
27 #include <cstdio>
28 #include <fstream>
29 #include <iostream>
30 #include <map>
31 #include <memory>
32 #include <mutex>
33 #include <sstream>
34 #include <string>
35 #include "vart_logger_types.hpp"
36 
39 namespace vart {
40 
41 // Below 'obj' object is created as static object and shared by all the
42 // components when header file is included
43 #define VART_LOG_REGISTER(x, y) obj.mod_register(x, y);
44 
45 #define VART_LOG_ERR_OBJ(x, ...) \
46  do { \
47  if (x) { \
48  obj.vart_logger_log_obj(LogLevel::LOG_LEVEL_ERROR, x, __FILE__, __func__, __LINE__, __VA_ARGS__); \
49  } \
50  } while (0)
51 
52 #define VART_LOG_WARN_OBJ(x, ...) \
53  do { \
54  if (x) { \
55  obj.vart_logger_log_obj(LogLevel::LOG_LEVEL_WARNING, x, __FILE__, __func__, __LINE__, __VA_ARGS__); \
56  } \
57  } while (0)
58 
59 #define VART_LOG_INFO_OBJ(x, ...) \
60  do { \
61  if (x) { \
62  obj.vart_logger_log_obj(LogLevel::LOG_LEVEL_INFO, x, __FILE__, __func__, __LINE__, __VA_ARGS__); \
63  } \
64  } while (0)
65 
66 #define VART_LOG_DEBUG_OBJ(x, ...) \
67  do { \
68  if (x) { \
69  obj.vart_logger_log_obj(LogLevel::LOG_LEVEL_DEBUG, x, __FILE__, __func__, __LINE__, __VA_ARGS__); \
70  } \
71  } while (0)
72 
73 static const int sys_log_level[] = {0, LOG_ERR, LOG_WARNING, LOG_NOTICE, LOG_INFO, LOG_DEBUG};
78 class Logger {
79  public:
101  static Logger& get_instance() {
102  static Logger log_obj;
103  return log_obj;
104  }
105 
130  std::shared_ptr<struct log_context> mod_register(const char* modname, bool need_id);
131 
156  std::shared_ptr<struct log_context> ctx,
157  const char* filename,
158  const char* func,
159  uint32_t line,
160  const char* fmt,
161  ...);
162 
174  void set_backend(const LogBackend *backend);
175 
176  private:
177  Logger();
178  ~Logger();
179  Logger(const Logger&) = delete;
180  void operator=(const Logger&) = delete;
181  LogLevel global_log_level;
182  LogLevel level;
183  LogType type;
184  std::mutex reg_mutex;
185  std::mutex log_mutex;
186  std::map<std::string, std::tuple<int16_t, uint16_t>> vart_components;
187  std::chrono::high_resolution_clock::time_point logger_start_time;
188  std::FILE* log_file;
189  LogBackend backend;
190  bool backend_installed = false;
191 };
192 
193 static Logger& obj = Logger::get_instance();
194 
195 } // namespace vart
The logger module provides the logging support for VART modules. Supports logging to console,...
Definition: vart_logger.hpp:78
std::shared_ptr< struct log_context > mod_register(const char *modname, bool need_id)
mod_register() - Registers a module with logger object
void vart_logger_log_obj(LogLevel, std::shared_ptr< struct log_context > ctx, const char *filename, const char *func, uint32_t line, const char *fmt,...)
vart_logger_log_obj() - Logs a module's message along with metadata like filename,...
void set_backend(const LogBackend *backend)
set_backend() - Install or remove a process-wide external log backend.
static Logger & get_instance()
get_instance() - Creates a logger() instance if not created earlier, else return an existing instance...
Definition: vart_logger.hpp:101
VART (Vitis AI Runtime) public API namespace.
LogLevel
Definition: vart_logger_types.hpp:54
LogType
Definition: vart_logger_types.hpp:66
@ FILE
Logs to file specified with VART_LOG_FILE_PATH environment variable.
Definition: vart_logger_types.hpp:69
Definition: vart_logger_types.hpp:105