mirror of
https://github.com/Dark98/threeSD.git
synced 2026-07-03 08:39:04 +00:00
Overhauls the logging system
Updated fmt to 8.0.0. This is now more compile-time, and outputs to stderr, file as well as a buffer.
This commit is contained in:
@@ -3,9 +3,20 @@
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#include <chrono>
|
||||
#include <cstdio>
|
||||
#ifdef _WIN32
|
||||
#include <share.h> // For _SH_DENYWR
|
||||
#else
|
||||
#define _SH_DENYWR 0
|
||||
#endif
|
||||
|
||||
#include "common/common_paths.h"
|
||||
#include "common/file_util.h"
|
||||
#include "common/logging/log.h"
|
||||
#include "common/string_util.h"
|
||||
|
||||
namespace Common::Logging {
|
||||
|
||||
std::uint64_t GetLoggingTime() {
|
||||
static auto time_origin = std::chrono::steady_clock::now();
|
||||
return std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now() -
|
||||
@@ -13,6 +24,38 @@ std::uint64_t GetLoggingTime() {
|
||||
.count();
|
||||
}
|
||||
|
||||
std::string StandardizeLogClass(const std::string& log_class) {
|
||||
return Common::ReplaceAll(log_class, "_", ".");
|
||||
// _SH_DENYWR allows read-only access for other programs. For non-Windows it's defined to 0.
|
||||
static FileUtil::IOFile g_log_file{LOG_FILE, "w", _SH_DENYWR};
|
||||
|
||||
static std::array<Entry, 3> g_error_buffer{};
|
||||
static int g_error_buffer_pos = 0;
|
||||
|
||||
void WriteLog(Entry entry) {
|
||||
// stderr
|
||||
fmt::print(stderr, entry.style, entry.message);
|
||||
|
||||
// log file
|
||||
g_log_file.WriteString(entry.message);
|
||||
if (entry.level >= Level::Error) {
|
||||
g_log_file.Flush(); // Do not flush the file too often
|
||||
}
|
||||
|
||||
// log buffer
|
||||
if (entry.level >= Level::Error) {
|
||||
g_error_buffer[g_error_buffer_pos] = std::move(entry);
|
||||
g_error_buffer_pos = (g_error_buffer_pos + 1) % g_error_buffer.size();
|
||||
}
|
||||
}
|
||||
|
||||
std::string GetLastErrors() {
|
||||
std::string output;
|
||||
for (std::size_t i = 0; i < g_error_buffer.size(); ++i) {
|
||||
const std::size_t pos = (g_error_buffer_pos + i) % g_error_buffer.size();
|
||||
if (g_error_buffer[pos].level != Level::Invalid) {
|
||||
output.append(g_error_buffer[pos].message);
|
||||
}
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
} // namespace Common::Logging
|
||||
|
||||
Reference in New Issue
Block a user