50 lines
1.7 KiB
C++
50 lines
1.7 KiB
C++
//
|
|
// Created by maxine on 6/16/25.
|
|
//
|
|
|
|
#pragma once
|
|
|
|
#include <iostream>
|
|
#include <sstream>
|
|
#include <functional>
|
|
#include <format>
|
|
|
|
namespace Mutil::Logging {
|
|
/// Logger object for constructing loggers.
|
|
class LoggerObj : public std::ostream {
|
|
private:
|
|
/// Output stream, default std::clog.
|
|
std::ostream &os = std::clog;
|
|
/// Lambda function on message, can be used for formatting
|
|
std::function<std::string(const std::string_view)> λMessage;
|
|
public:
|
|
/// Default constructor, just simple messages to be written to console stderr
|
|
LoggerObj() {};
|
|
/// Construct with specified output stream
|
|
LoggerObj(std::ostream &os) : os(os) {};
|
|
/// Full construction
|
|
LoggerObj(std::ostream &os, std::function<std::string(const std::string_view)> λMessage) : os(os), λMessage(λMessage) {};
|
|
public:
|
|
/// Send a simple log message
|
|
void operator()(const std::string_view message) {
|
|
λMessage ? os << λMessage(message) : os << message;
|
|
os << std::endl;
|
|
}
|
|
/// Send a formatted/complex log message
|
|
template<typename... Args>
|
|
void operator()(const std::format_string<Args...> fmt, Args&&... args) {
|
|
std::string m = std::vformat(fmt.get(), std::make_format_args(args...));
|
|
λMessage ? os << λMessage(m) : os << m;
|
|
os << std::endl;
|
|
};
|
|
/// Send log messages using logger as a stream
|
|
template<typename In>
|
|
LoggerObj& operator<<(In message) {
|
|
λMessage ? os << λMessage(message) : os << message;
|
|
os << std::endl;
|
|
return *this;
|
|
};
|
|
};
|
|
}
|
|
|