Implemented logging utilities
This commit is contained in:
11
source/log/CMakeLists.txt
Normal file
11
source/log/CMakeLists.txt
Normal file
@@ -0,0 +1,11 @@
|
||||
project(UglyLogLib)
|
||||
|
||||
add_library(${PROJECT_NAME}
|
||||
./src/globals.cpp
|
||||
./src/TimestampLog.cpp
|
||||
)
|
||||
|
||||
target_include_directories(${PROJECT_NAME}
|
||||
PUBLIC ./include
|
||||
PRIVATE ./src
|
||||
)
|
||||
13
source/log/include/ILogFacility.hpp
Normal file
13
source/log/include/ILogFacility.hpp
Normal file
@@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include <format>
|
||||
#include <string_view>
|
||||
|
||||
namespace ugly {
|
||||
|
||||
class ILogFacility {
|
||||
public:
|
||||
virtual void vlog(std::string_view fmt, std::format_args args) = 0;
|
||||
};
|
||||
|
||||
}
|
||||
24
source/log/include/LogUtils.hpp
Normal file
24
source/log/include/LogUtils.hpp
Normal file
@@ -0,0 +1,24 @@
|
||||
#pragma once
|
||||
|
||||
#include "ILogFacility.hpp"
|
||||
|
||||
#include <format>
|
||||
|
||||
namespace ugly {
|
||||
|
||||
class LogAlias {
|
||||
public:
|
||||
ILogFacility *mpLog;
|
||||
|
||||
template<typename ...Args>
|
||||
LogAlias &operator()(std::format_string<Args...> format, Args &&...args) {
|
||||
if(mpLog) {
|
||||
mpLog->vlog(format.get(), std::make_format_args(args...));
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
|
||||
extern LogAlias log;
|
||||
|
||||
}
|
||||
24
source/log/include/TimestampLog.hpp
Normal file
24
source/log/include/TimestampLog.hpp
Normal file
@@ -0,0 +1,24 @@
|
||||
#pragma once
|
||||
|
||||
#include "ILogFacility.hpp"
|
||||
|
||||
#include <chrono>
|
||||
#include <format>
|
||||
#include <ostream>
|
||||
#include <string_view>
|
||||
#include <string>
|
||||
|
||||
namespace ugly {
|
||||
|
||||
class TimestampLog: virtual public ILogFacility {
|
||||
private:
|
||||
std::ostream &mrOutputStream;
|
||||
std::string mDescription;
|
||||
std::chrono::high_resolution_clock::time_point mStart;
|
||||
|
||||
public:
|
||||
TimestampLog(std::ostream &os, std::string_view description);
|
||||
void vlog(std::string_view fmt, std::format_args args) override;
|
||||
};
|
||||
|
||||
}
|
||||
18
source/log/src/TimestampLog.cpp
Normal file
18
source/log/src/TimestampLog.cpp
Normal file
@@ -0,0 +1,18 @@
|
||||
#include "TimestampLog.hpp"
|
||||
|
||||
#include <format>
|
||||
#include <chrono>
|
||||
|
||||
|
||||
ugly::TimestampLog::TimestampLog(std::ostream &os, std::string_view description):
|
||||
mrOutputStream{os},
|
||||
mDescription{description},
|
||||
mStart{std::chrono::high_resolution_clock::now()} {}
|
||||
|
||||
|
||||
void ugly::TimestampLog::vlog(std::string_view fmt, std::format_args args) {
|
||||
auto now = std::chrono::high_resolution_clock::now();
|
||||
auto timestamp = std::chrono::duration_cast<std::chrono::duration<float>>(now - mStart).count();
|
||||
auto message = std::vformat(fmt, args);
|
||||
mrOutputStream << std::format("[{:9.4f}] {}: {}\n", timestamp, mDescription, message);
|
||||
}
|
||||
3
source/log/src/globals.cpp
Normal file
3
source/log/src/globals.cpp
Normal file
@@ -0,0 +1,3 @@
|
||||
#include "LogUtils.hpp"
|
||||
|
||||
ugly::LogAlias ugly::log{nullptr};
|
||||
Reference in New Issue
Block a user