Skip to content

Commit

Permalink
fix: reduce scope of macros
Browse files Browse the repository at this point in the history
  • Loading branch information
c-dilks committed Aug 8, 2024
1 parent 88dd8fd commit d0e3733
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 11 deletions.
4 changes: 3 additions & 1 deletion src/iguana/algorithms/clas12/EventBuilderFilter/Algorithm.cc
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#include "Algorithm.h"
#include "iguana/services/NewLogger.h"

namespace iguana::clas12 {

REGISTER_IGUANA_ALGORITHM(EventBuilderFilter);

void EventBuilderFilter::Start(hipo::banklist& banks)
{
m_level = LogLevel::trace; // FIXME

// define options, their default values, and cache them
ParseYAMLConfig();
Expand Down Expand Up @@ -53,7 +55,7 @@ namespace iguana::clas12 {

void EventBuilderFilter::Stop()
{
NEWLOG("test newlog {}", 7);
WARN("test newlog {}", 7);
}

}
15 changes: 15 additions & 0 deletions src/iguana/services/NewLogger.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#define WARN(fmt_str, ...) \
if(m_level <= LogLevel::warn) \
PrintLog( \
stderr, \
fmt::format("{}", fmt::styled(fmt::format("[warn] [{}]", m_name), fmt::emphasis::bold | fmt::fg(fmt::terminal_color::magenta))), \
FMT_STRING(fmt_str), \
__VA_ARGS__);

#define ERROR(fmt_str, ...) \
if(m_level <= LogLevel::error) \
PrintLog( \
stderr, \
fmt::format("{}", fmt::styled(fmt::format("[error] [{}]", m_name), fmt::emphasis::bold | fmt::fg(fmt::terminal_color::red))), \
FMT_STRING(fmt_str), \
__VA_ARGS__);
35 changes: 25 additions & 10 deletions src/iguana/services/Object.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,24 @@ namespace iguana {

public:

/// These are the available log levels, from lowest to highest:
/// - `trace`: the most verbose level, used for fine-grained printouts for each event
/// - `debug`: less verbose printout, expected to be less frequent than `trace`
/// - `info`: the least verbose printout; this is the default level
/// - `quiet`: use this level to only allow warnings and errors, silencing all other printouts
/// - `warn`: an issue that may or may not be critical
/// - `error`: an issue that is likely critical
/// - `silent`: use this level to silence **all** printouts (use it at your own risk!)
enum LogLevel {
trace,
debug,
info,
quiet,
warn,
error,
silent
};

/// @param name the name of this object
Object(std::string_view name = "");
~Object() {}
Expand Down Expand Up @@ -45,25 +63,22 @@ namespace iguana {
/// The name of this object
std::string m_name;

/// The current log level for this instance
LogLevel m_level;

/// `Logger` instance for this object
std::unique_ptr<Logger> m_log;

void PrintLogV(std::string_view name, fmt::string_view fmt_str, fmt::format_args fmt_args)
void PrintLogV(FILE* out, std::string_view prefix, fmt::string_view fmt_str, fmt::format_args fmt_args)
{
fmt::print("<{}> {}\n", name, fmt::vformat(fmt_str, fmt_args));
fmt::print(out, "{} {}\n", prefix, fmt::vformat(fmt_str, fmt_args));
}

template <typename... ARG_TYPES>
void PrintLog(std::string_view name, fmt::format_string<ARG_TYPES...> fmt_str, ARG_TYPES&&... fmt_args)
void PrintLog(FILE* out, std::string_view prefix, fmt::format_string<ARG_TYPES...> fmt_str, ARG_TYPES&&... fmt_args)
{
PrintLogV(name, fmt_str, fmt::make_format_args(fmt_args...));
PrintLogV(out, prefix, fmt_str, fmt::make_format_args(fmt_args...));
}

};
}

#define NEWLOG(fmt_str, ...) \
PrintLog( \
fmt::format("[{}]", fmt::styled(m_name, fmt::emphasis::bold | fmt::fg(fmt::terminal_color::magenta))), \
FMT_STRING(fmt_str), \
__VA_ARGS__);

0 comments on commit d0e3733

Please sign in to comment.