Skip to content

Commit

Permalink
helper now formats message; fix compilation errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Exortions committed Dec 28, 2024
1 parent 829db0f commit 536166f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
23 changes: 22 additions & 1 deletion include/lemlog/logger/logger.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "fmtlib/core.h"
#include <string>

namespace logger {
Expand Down Expand Up @@ -87,6 +88,21 @@ enum class Level {
ERROR,
};

/**
* @brief log a message with a level and topic
*
* Messages with a logging level of DEBUG must have the topic whitelisted,
* while all other messages are sent by default unless the topic is blacklisted
*
* @param level the level of the message, e.g INFO
* @param topic the topic of the message, e.g "lemlib/motions/boomerang"
* @param format the pre-formatted message, e.g. "Hello, {}!"
* @param args the arguments to be formatted into the message, e.g. "world"
*
* @tparam Args the types of the arguments to be formatted into the message
*/
static void log(Level level, std::string topic, std::string message);

/**
* @brief Logger Helper class. Used to send messages to all sinks
*
Expand All @@ -108,6 +124,7 @@ class Helper {
* @endcode
*/
Helper(std::string topic);

/**
* @brief Send a message to all sinks
*
Expand All @@ -127,7 +144,11 @@ class Helper {
*/
template <typename... Args>
void log(Level level, const std::string& format, Args&&... args) {
logger::log(level, m_topic, format, args...);
// format the message into a string using the provided arguments
std::string message =
fmt::format(format, std::forward<Args>(args)...);

logger::log(level, m_topic, message);
}
private:
const std::string m_topic;
Expand Down
19 changes: 1 addition & 18 deletions src/lemlog/logger/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,7 @@ void addBlacklist(std::string s) { blacklist.push_back(s); }

void removeBlacklist(std::string s) { blacklist.remove(s); }

/**
* @brief log a message with a level and topic
*
* Messages with a logging level of DEBUG must have the topic whitelisted,
* while all other messages are sent by default unless the topic is blacklisted
*
* @param level the level of the message, e.g INFO
* @param topic the topic of the message, e.g "lemlib/motions/boomerang"
* @param format the pre-formatted message, e.g. "Hello, {}!"
* @param args the arguments to be formatted into the message, e.g. "world"
*
* @tparam Args the types of the arguments to be formatted into the message
*/
template <typename... Args>
static void log(Level level, std::string topic, std::string& format, Args&&... args) {
// format the message into a string using the provided arguments
std::string message = fmt::format(format, std::forward<Args>(args)...);

static void log(Level level, std::string topic, std::string message) {
// is the message a debug message?
if (level == Level::DEBUG) {
// is it whitelisted?
Expand Down

0 comments on commit 536166f

Please sign in to comment.