19 std::string python_caller_location()
23 py::object frame = py::module_::import(
"sys").attr(
"_getframe")();
24 const auto file = py::cast<std::string>(frame.attr(
"f_code").attr(
"co_filename"));
25 const auto line = py::cast<int>(frame.attr(
"f_lineno"));
26 return "[" + file +
":" + std::to_string(line) +
"] ";
28 catch (
const py::error_already_set&)
34 void log_message(spdlog::level::level_enum level,
const std::string& channel,
const std::string& message,
bool with_location)
36 const std::string text = with_location ? python_caller_location() + message : message;
48 [](
const std::string& channel,
const std::string& message) { log_message(spdlog::level::trace, channel, message,
true); },
52 Log a message into a specific channel with severity ``trace``.
53 The message is prefixed with the file name and line number of the calling python code.
55 :param str channel: The log channel's name.
56 :param str message: The message to log.
61 [](
const std::string& channel,
const std::string& message) { log_message(spdlog::level::debug, channel, message,
true); },
65 Log a message into a specific channel with severity ``debug``.
66 The message is prefixed with the file name and line number of the calling python code.
68 :param str channel: The log channel's name.
69 :param str message: The message to log.
74 [](
const std::string& channel,
const std::string& message) { log_message(spdlog::level::info, channel, message,
false); },
78 Log a message into a specific channel with severity ``info``.
80 :param str channel: The log channel's name.
81 :param str message: The message to log.
86 [](
const std::string& channel,
const std::string& message) { log_message(spdlog::level::warn, channel, message,
true); },
90 Log a message into a specific channel with severity ``warning``.
91 The message is prefixed with the file name and line number of the calling python code.
93 :param str channel: The log channel's name.
94 :param str message: The message to log.
99 [](
const std::string& channel,
const std::string& message) { log_message(spdlog::level::err, channel, message,
true); },
103 Log a message into a specific channel with severity ``error``.
104 The message is prefixed with the file name and line number of the calling python code.
106 :param str channel: The log channel's name.
107 :param str message: The message to log.
112 [](
const std::string& channel,
const std::string& message) { log_message(spdlog::level::critical, channel, message,
true); },
116 Log a message into a specific channel with severity ``critical``.
117 The message is prefixed with the file name and line number of the calling python code.
119 :param str channel: The log channel's name.
120 :param str message: The message to log.
123 auto log_manager = py::class_<LogManager, RawPtrWrapper<LogManager>>(m,
"LogManager", R
"(
124 The log manager takes care of the log channels of HAL and the sinks (e.g., stdout, log file, GUI) that they write to.
127 log_manager.def(py::init([]() {
133 Set the log file name.
134 If file_name is empty, the default log file will be used.
136 :param str file_name: The desired log file.
140 Set a channel's severity level.
142 :param str channel_name: Name of the channel.
143 :param str level: The severity level.
146 log_manager.def_static(
148 Ensure that a channel with the given name exists, creating it if it does not exist yet.
149 Note that, unlike its C++ counterpart, this does not return the channel itself.
151 :param str channel_name: Name of the channel.
155 Returns all channels' names.
157 :returns: A set of channel names.
162 Get all available severity levels.
164 :returns: A set of severity levels.
170 By default all channels are active.
172 :param str channel_name: The name of the channel.
176 Activate all logging channels.
180 Deactivate a channel suppressing all output.
182 :param str channel_name: The name of the channel.
186 Deactivate all logging channels.
190 Get the list of default sinks that are added to each newly created logger by default.
192 :returns: The default sinks.
193 :rtype: list[hal_py.log_sink]
197 Remove the specified sink type from the list of default sinks if present.
199 :param str sink_type: The type of sink to remove from default sinks.
202 auto log_sink = py::class_<LogManager::log_sink, RawPtrWrapper<LogManager::log_sink>>(log_manager,
"log_sink");
205 The type of the sink.
211 Boolean indication whether sink is a file sink.
217 Truncate option passed to sink.
223 The file path incase the sink is a file sink.
void set_level_of_channel(const std::string &channel_name, const std::string &level)
void activate_channel(const std::string &channel_name)
void deactivate_all_channels()
void deactivate_channel(const std::string &channel_name)
void activate_all_channels()
std::vector< std::shared_ptr< hal::LogManager::log_sink > > get_default_sinks()
std::set< std::string > get_channels() const
std::shared_ptr< spdlog::logger > get_channel(const std::string &channel_name="stdout")
void remove_sink_from_default(const std::string &sink_type)
static LogManager * get_instance(const std::filesystem::path &file_name="")
std::set< std::string > get_available_log_levels() const
void set_file_name(const std::filesystem::path &file_name)
std::unique_ptr< T, py::nodelete > RawPtrWrapper
void log_init(py::module &m)
const Module * module(const Gate *g, const NodeBoxes &boxes)
std::filesystem::path path