Skip to content

Commit

Permalink
implement fmt into logger
Browse files Browse the repository at this point in the history
  • Loading branch information
Exortions committed Dec 26, 2024
1 parent 99118a1 commit f5f2488
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
8 changes: 5 additions & 3 deletions include/lemlog/logger/logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,19 +112,21 @@ class Helper {
* @brief Send a message to all sinks
*
* @param level the logging level of the message
* @param message the message to send
* @param format the message to be sent
* @param args the arguments to be formatted into the message
*
* @b Example:
* @code {.cpp}
* void doSomething() {
* // create a Helper
* logger::Helper helper("doSomething");
* // log an info message
* helper.log(logger::Level::INFO, "Did something!");
* helper.log(logger::Level::INFO, "Motor temperature: {}", 42);
* }
* @endcode
*/
void log(Level level, std::string message);
template <typename... Args>
void log(Level level, const std::string& format, Args&&... args);
private:
const std::string m_topic;
};
Expand Down
17 changes: 13 additions & 4 deletions src/lemlog/logger/logger.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "lemlog/logger/logger.hpp"
#include "fmt/core.h"
#include <list>

namespace logger {
Expand All @@ -24,9 +25,16 @@ void removeBlacklist(std::string s) { blacklist.remove(s); }
*
* @param level the level of the message, e.g INFO
* @param topic the topic of the message, e.g "lemlib/motions/boomerang"
* @param message the message to send, e.g "Hello World!"
* @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) {
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)...);

// is the message a debug message?
if (level == Level::DEBUG) {
// is it whitelisted?
Expand All @@ -53,8 +61,9 @@ static void log(Level level, std::string topic, std::string message) {
Helper::Helper(std::string topic)
: m_topic(topic) {}

void Helper::log(Level level, std::string message) {
logger::log(level, m_topic, message);
template <typename... Args>
void Helper::log(Level level, const std::string& format, Args&&... args) {
logger::log(level, m_topic, format, args...);
}

Sink::Sink() { sinks.push_back(this); }
Expand Down

0 comments on commit f5f2488

Please sign in to comment.