2#include <boost/asio.hpp>
3#include <boost/system/error_code.hpp>
4#include <boost/lockfree/spsc_queue.hpp>
53 std::optional<std::chrono::system_clock::time_point>
timestamp;
79 result += std::format(
"[{:%Y-%m-%d %H:%M:%S}] ", std::chrono::floor<std::chrono::milliseconds>(
timestamp.value()));
81 if (
level.has_value())
85 result +=
"[from=" + std::to_string(
isFrom.value()) +
"] ";
94 result +=
":" + std::to_string(
remotePort.value());
97 result +=
" [error " + std::to_string(
errorValue.value()) +
"]";
123 std::filesystem::path
folder =
"LOG_FOLDER";
134 std::ofstream CURRENT_LOG_FILE_;
135 std::filesystem::path LOG_FOLDER_PATH_;
136 boost::asio::strand<boost::asio::io_context::executor_type> loggerStrand_;
137 boost::lockfree::spsc_queue<Log, boost::lockfree::capacity<LOG_BUFFER_SIZE>> logBuffer_;
139 std::filesystem::path newLogFilePath()
const {
140 std::string name = std::format(
"LOG_FILE-{:%Y-%m-%d_%H-%M-%S}", std::chrono::floor<std::chrono::seconds>(std::chrono::system_clock::now()));
143 std::filesystem::path path = LOG_FOLDER_PATH_ / (name +
".log");
144 for(
int i = 1; std::filesystem::exists(path); ++i){
145 path = LOG_FOLDER_PATH_ / (name +
"-" + std::to_string(i) +
".log");
150 void rotateLogFile(){
151 std::ofstream newLogFile(newLogFilePath());
152 if(!newLogFile.is_open()) {
156 CURRENT_LOG_FILE_.close();
157 CURRENT_LOG_FILE_ = std::move(newLogFile);
160 void unsafeFlush(
bool checkSize =
false){
161 if(!CURRENT_LOG_FILE_.is_open())
return;
165 while(logBuffer_.pop(log)){
166 CURRENT_LOG_FILE_ << log.
toString() <<
'\n';
168 CURRENT_LOG_FILE_.flush();
171 if(checkSize &&
static_cast<std::uintmax_t
>(CURRENT_LOG_FILE_.tellp()) >= config_.maxFileSize){
176 void unsafePushLog(
Log log){
178 log.
timestamp = std::chrono::system_clock::now();
180 if(!logBuffer_.push(log)){
182 logBuffer_.push(std::move(log));
193 Logger(boost::asio::io_context& ioc,
LoggerConfig config = {}) : config_(std::move(config)), LOG_FOLDER_PATH_(config_.folder), loggerStrand_(ioc.get_executor()) {
196 if(LOG_FOLDER_PATH_.empty()){
197 LOG_FOLDER_PATH_ =
"LOG_FOLDER";
199 std::filesystem::create_directories(LOG_FOLDER_PATH_);
200 LOG_FOLDER_PATH_ = std::filesystem::absolute(LOG_FOLDER_PATH_);
202 CURRENT_LOG_FILE_.open(newLogFilePath());
219 boost::asio::post(loggerStrand_, [
this](){unsafeFlush(
true);});
224 if(!config_.enabled)
return;
226 boost::asio::post(loggerStrand_, [
this, log = std::move(log)]()
mutable {unsafePushLog(std::move(log));});
void push(Log log)
Adds an entry to the log. Returns immediately. Ignored when logging is disabled.
Definition logger.h:223
void flush()
Asks the logger to write buffered entries to disk. Returns immediately.
Definition logger.h:218
Logger(boost::asio::io_context &ioc, LoggerConfig config={})
Creates a logger. If config.enabled is false it does nothing.
Definition logger.h:193
void unsafeFlushNow()
Writes all buffered entries to disk immediately on the calling thread. Only safe when no other thread...
Definition logger.h:213
Everything in the framework lives in this namespace.
std::string levelToString(LogLevel level)
Converts a LogLevel to its upper case name, e.g. "WARN".
Definition logger.h:32
std::uintmax_t MAX_LOG_FILE_SIZE
Default size in bytes at which a log file is rotated. Change with setMaxLogFileSize().
Definition protocol.h:22
LogLevel
Severity of a log entry.
Definition logger.h:19
@ WARN
Definition logger.h:25
@ INFO
Definition logger.h:23
@ ERROR
Definition logger.h:27
@ DEBUG
Definition logger.h:21
One log entry. Every field is optional and only the fields that are set appear in the output.
Definition logger.h:46
std::optional< std::string > errorMessage
Definition logger.h:61
std::optional< std::chrono::system_clock::time_point > timestamp
Definition logger.h:53
std::optional< uint16_t > localPort
Definition logger.h:67
std::optional< int > errorValue
Definition logger.h:59
std::optional< std::string > localAddress
Definition logger.h:65
std::optional< uint16_t > remotePort
Definition logger.h:71
std::optional< std::string > message
Definition logger.h:55
std::optional< uint64_t > isFrom
Definition logger.h:49
std::optional< std::string > remoteAddress
Definition logger.h:69
std::string toString() const
Formats the entry as a single line of text.
Definition logger.h:74
std::optional< LogLevel > level
Definition logger.h:51
Logging settings passed to acsf::Server and acsf::Client.
Definition logger.h:118
std::filesystem::path folder
Definition logger.h:123
std::uintmax_t maxFileSize
Definition logger.h:126
bool enabled
Definition logger.h:120