make logging to file optional
This commit is contained in:
@@ -1,25 +1,26 @@
|
|||||||
#ifndef LOG_HPP
|
#ifndef LOG_HPP
|
||||||
#define LOG_HPP
|
#define LOG_HPP
|
||||||
|
|
||||||
#define log _log // comment to disable logging
|
#define log _log // comment to disable logging
|
||||||
|
// #define LOG_FILE_LOCATION "linuxSmartie.log.gz"
|
||||||
|
|
||||||
#ifdef log
|
#ifdef log
|
||||||
#include <zlib.h>
|
|
||||||
|
|
||||||
#define LOG_FILE_LOCATION "linuxSmartie.log.gz"
|
|
||||||
|
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <print>
|
#include <print>
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
|
|
||||||
inline std::mutex cmtx; // console logging mutex
|
#ifdef LOG_FILE_LOCATION
|
||||||
|
#include <zlib.h>
|
||||||
inline std::mutex fmtx; // file logging mutex
|
inline std::mutex fmtx; // file logging mutex
|
||||||
|
#endif
|
||||||
|
|
||||||
|
inline std::mutex cmtx; // console logging mutex
|
||||||
enum class LOG { INF, WRN, ERR, DBG, START };
|
enum class LOG { INF, WRN, ERR, DBG, START };
|
||||||
|
|
||||||
template <typename... Args>
|
template <typename... Args>
|
||||||
inline void _log(LOG level, std::string_view tag, std::format_string<Args...> str, Args&&... args) {
|
inline void _log(LOG level, std::string_view tag, std::format_string<Args...> str, Args&&... args) {
|
||||||
constexpr bool logToFile = true;
|
|
||||||
constexpr std::string_view reset = "\033[0m";
|
constexpr std::string_view reset = "\033[0m";
|
||||||
constexpr std::string_view pattern = "[{}{}{}] ";
|
constexpr std::string_view pattern = "[{}{}{}] ";
|
||||||
std::string_view color = "";
|
std::string_view color = "";
|
||||||
@@ -48,21 +49,23 @@ inline void _log(LOG level, std::string_view tag, std::format_string<Args...> st
|
|||||||
std::println(str, std::forward<Args>(args)...);
|
std::println(str, std::forward<Args>(args)...);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef LOG_FILE_LOCATION
|
||||||
/* log to file */
|
/* log to file */
|
||||||
if (logToFile) {
|
{
|
||||||
auto now = std::chrono::system_clock::now();
|
auto now = std::chrono::system_clock::now();
|
||||||
auto local = std::chrono::current_zone()->to_local(now);
|
auto local = std::chrono::current_zone()->to_local(now);
|
||||||
std::string msg = std::format("{:%x %H:%M:%S }", floor<std::chrono::milliseconds>(local)); // timestamp
|
std::string msg = std::format("{:%x %H:%M:%S }", floor<std::chrono::milliseconds>(local)); // timestamp
|
||||||
std::format_to(std::back_inserter(msg), pattern, color, tag, reset); // tag
|
std::format_to(std::back_inserter(msg), pattern, color, tag, reset); // tag
|
||||||
std::format_to(std::back_inserter(msg), str, std::forward<Args>(args)...); // actual log message
|
std::format_to(std::back_inserter(msg), str, std::forward<Args>(args)...); // actual log message
|
||||||
|
|
||||||
std::scoped_lock lock (fmtx);
|
std::scoped_lock lock(fmtx);
|
||||||
gzFile file = gzopen(LOG_FILE_LOCATION, "ab");
|
gzFile file = gzopen(LOG_FILE_LOCATION, "ab");
|
||||||
if (file) {
|
if (file) {
|
||||||
gzprintf(file, "%s\n", msg.c_str());
|
gzprintf(file, "%s\n", msg.c_str());
|
||||||
gzclose(file);
|
gzclose(file);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
#else
|
#else
|
||||||
|
|||||||
Reference in New Issue
Block a user