Skip to content

Commit

Permalink
Merge pull request #1327 from lplewa/common-log
Browse files Browse the repository at this point in the history
Add Legacy compatibility to Logger
  • Loading branch information
pbalcer authored Feb 13, 2024
2 parents 78ef1ca + fb07a39 commit f15234d
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 9 deletions.
32 changes: 32 additions & 0 deletions source/common/logger/ur_logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,44 @@ inline void always(const char *format, Args &&...args) {
get_logger().always(format, std::forward<Args>(args)...);
}

template <typename... Args>
inline void debug(const logger::LegacyMessage &p, const char *format,
Args &&...args) {
get_logger().log(p, logger::Level::DEBUG, format,
std::forward<Args>(args)...);
}

template <typename... Args>
inline void info(logger::LegacyMessage p, const char *format, Args &&...args) {
get_logger().log(p, logger::Level::INFO, format,
std::forward<Args>(args)...);
}

template <typename... Args>
inline void warning(logger::LegacyMessage p, const char *format,
Args &&...args) {
get_logger().log(p, logger::Level::WARN, format,
std::forward<Args>(args)...);
}

template <typename... Args>
inline void error(logger::LegacyMessage p, const char *format, Args &&...args) {
get_logger().log(p, logger::Level::ERR, format,
std::forward<Args>(args)...);
}

inline void setLevel(logger::Level level) { get_logger().setLevel(level); }

inline void setFlushLevel(logger::Level level) {
get_logger().setFlushLevel(level);
}

template <typename T> inline std::string toHex(T t) {
std::stringstream s;
s << std::hex << t;
return s.str();
}

/// @brief Create an instance of the logger with parameters obtained from the respective
/// environment variable or with default configuration if the env var is empty,
/// not set, or has the wrong format.
Expand Down
54 changes: 51 additions & 3 deletions source/common/logger/ur_logger_details.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@

namespace logger {

struct LegacyMessage {
LegacyMessage(const char *p) : message(p){};
const char *message;
};

class Logger {
public:
Logger(std::unique_ptr<logger::Sink> sink) : sink(std::move(sink)) {
Expand Down Expand Up @@ -59,20 +64,63 @@ class Logger {
}
}

template <typename... Args>
void debug(const logger::LegacyMessage &p, const char *format,
Args &&...args) {
log(p, logger::Level::DEBUG, format, std::forward<Args>(args)...);
}

template <typename... Args>
void info(const logger::LegacyMessage &p, const char *format,
Args &&...args) {
log(p, logger::Level::INFO, format, std::forward<Args>(args)...);
}

template <typename... Args>
void warning(const logger::LegacyMessage &p, const char *format,
Args &&...args) {
log(p, logger::Level::WARN, format, std::forward<Args>(args)...);
}

template <typename... Args>
void error(const logger::LegacyMessage &p, const char *format,
Args &&...args) {
log(p, logger::Level::ERR, format, std::forward<Args>(args)...);
}

template <typename... Args>
void log(logger::Level level, const char *format, Args &&...args) {
if (level < this->level) {
log(logger::LegacyMessage(format), level, format,
std::forward<Args>(args)...);
}

template <typename... Args>
void log(const logger::LegacyMessage &p, logger::Level level,
const char *format, Args &&...args) {
if (!sink) {
return;
}

if (sink) {
sink->log(level, format, std::forward<Args>(args)...);
if (isLegacySink) {
sink->log(level, p.message, std::forward<Args>(args)...);
return;
}
if (level < this->level) {
return;
}

sink->log(level, format, std::forward<Args>(args)...);
}

void setLegacySink(std::unique_ptr<logger::Sink> legacySink) {
this->isLegacySink = true;
this->sink = std::move(legacySink);
}

private:
logger::Level level;
std::unique_ptr<logger::Sink> sink;
bool isLegacySink = false;
};

} // namespace logger
Expand Down
15 changes: 9 additions & 6 deletions source/common/logger/ur_sinks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,7 @@ class Sink {
}

format(buffer, fmt, std::forward<Args &&>(args)...);

std::scoped_lock<std::mutex> lock(output_mutex);
*ostream << buffer.str();
if (level >= flush_level) {
ostream->flush();
}
print(level, buffer.str());
}

void setFlushLevel(logger::Level level) { this->flush_level = level; }
Expand All @@ -50,6 +45,14 @@ class Sink {
flush_level = logger::Level::ERR;
}

virtual void print(logger::Level level, const std::string &msg) {
std::scoped_lock<std::mutex> lock(output_mutex);
*ostream << msg;
if (level >= flush_level) {
ostream->flush();
}
}

private:
std::string logger_name;
bool skip_prefix;
Expand Down

0 comments on commit f15234d

Please sign in to comment.