mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2025-08-04 09:44:10 +00:00
feat: Abstract Logger and simplify code (#1207)
* Logger: Rename logger to Logger from dLogger * Logger: Add compile time filename Fix include issues Add writers Add macros Add macro to force compilation * Logger: Replace calls with macros Allows for filename and line number to be logged * Logger: Add comments and remove extra define Logger: Replace with unique_ptr also flush console at exit. regular file writer should be flushed on file close. Logger: Remove constexpr on variable * Logger: Simplify code * Update Logger.cpp
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
#define __AMF3__H__
|
||||
|
||||
#include "dCommonVars.h"
|
||||
#include "dLogger.h"
|
||||
#include "Logger.h"
|
||||
#include "Game.h"
|
||||
|
||||
#include <unordered_map>
|
||||
|
@@ -1,7 +1,7 @@
|
||||
#include "AmfSerialize.h"
|
||||
|
||||
#include "Game.h"
|
||||
#include "dLogger.h"
|
||||
#include "Logger.h"
|
||||
|
||||
// Writes an AMFValue pointer to a RakNet::BitStream
|
||||
template<>
|
||||
@@ -29,7 +29,7 @@ void RakNet::BitStream::Write<AMFBaseValue&>(AMFBaseValue& value) {
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
Game::logger->Log("AmfSerialize", "Encountered unwritable AMFType %i!", type);
|
||||
LOG("Encountered unwritable AMFType %i!", type);
|
||||
}
|
||||
case eAmf::Undefined:
|
||||
case eAmf::Null:
|
||||
|
@@ -9,7 +9,7 @@
|
||||
#include "Database.h"
|
||||
#include "Game.h"
|
||||
#include "ZCompression.h"
|
||||
#include "dLogger.h"
|
||||
#include "Logger.h"
|
||||
|
||||
//! Forward declarations
|
||||
|
||||
@@ -59,14 +59,14 @@ uint32_t BrickByBrickFix::TruncateBrokenBrickByBrickXml() {
|
||||
completeUncompressedModel.append((char*)uncompressedChunk.get());
|
||||
completeUncompressedModel.resize(previousSize + actualUncompressedSize);
|
||||
} else {
|
||||
Game::logger->Log("BrickByBrickFix", "Failed to inflate chunk %i for model %llu. Error: %i", chunkCount, modelId, err);
|
||||
LOG("Failed to inflate chunk %i for model %llu. Error: %i", chunkCount, modelId, err);
|
||||
break;
|
||||
}
|
||||
chunkCount++;
|
||||
}
|
||||
std::unique_ptr<tinyxml2::XMLDocument> document = std::make_unique<tinyxml2::XMLDocument>();
|
||||
if (!document) {
|
||||
Game::logger->Log("BrickByBrickFix", "Failed to initialize tinyxml document. Aborting.");
|
||||
LOG("Failed to initialize tinyxml document. Aborting.");
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -75,8 +75,7 @@ uint32_t BrickByBrickFix::TruncateBrokenBrickByBrickXml() {
|
||||
"</LXFML>",
|
||||
completeUncompressedModel.length() >= 15 ? completeUncompressedModel.length() - 15 : 0) == std::string::npos
|
||||
) {
|
||||
Game::logger->Log("BrickByBrickFix",
|
||||
"Brick-by-brick model %llu will be deleted!", modelId);
|
||||
LOG("Brick-by-brick model %llu will be deleted!", modelId);
|
||||
ugcModelToDelete->setInt64(1, modelsToTruncate->getInt64(1));
|
||||
pcModelToDelete->setInt64(1, modelsToTruncate->getInt64(1));
|
||||
ugcModelToDelete->execute();
|
||||
@@ -85,8 +84,7 @@ uint32_t BrickByBrickFix::TruncateBrokenBrickByBrickXml() {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Game::logger->Log("BrickByBrickFix",
|
||||
"Brick-by-brick model %llu will be deleted!", modelId);
|
||||
LOG("Brick-by-brick model %llu will be deleted!", modelId);
|
||||
ugcModelToDelete->setInt64(1, modelsToTruncate->getInt64(1));
|
||||
pcModelToDelete->setInt64(1, modelsToTruncate->getInt64(1));
|
||||
ugcModelToDelete->execute();
|
||||
@@ -140,12 +138,10 @@ uint32_t BrickByBrickFix::UpdateBrickByBrickModelsToSd0() {
|
||||
insertionStatement->setInt64(2, modelId);
|
||||
try {
|
||||
insertionStatement->executeUpdate();
|
||||
Game::logger->Log("BrickByBrickFix", "Updated model %i to sd0", modelId);
|
||||
LOG("Updated model %i to sd0", modelId);
|
||||
updatedModels++;
|
||||
} catch (sql::SQLException exception) {
|
||||
Game::logger->Log(
|
||||
"BrickByBrickFix",
|
||||
"Failed to update model %i. This model should be inspected manually to see why."
|
||||
LOG("Failed to update model %i. This model should be inspected manually to see why."
|
||||
"The database error is %s", modelId, exception.what());
|
||||
}
|
||||
}
|
||||
|
@@ -4,7 +4,7 @@ set(DCOMMON_SOURCES
|
||||
"BinaryIO.cpp"
|
||||
"dConfig.cpp"
|
||||
"Diagnostics.cpp"
|
||||
"dLogger.cpp"
|
||||
"Logger.cpp"
|
||||
"GeneralUtils.cpp"
|
||||
"LDFFormat.cpp"
|
||||
"MD5.cpp"
|
||||
|
@@ -1,6 +1,6 @@
|
||||
#include "Diagnostics.h"
|
||||
#include "Game.h"
|
||||
#include "dLogger.h"
|
||||
#include "Logger.h"
|
||||
|
||||
// If we're on Win32, we'll include our minidump writer
|
||||
#ifdef _WIN32
|
||||
@@ -9,7 +9,7 @@
|
||||
#include <Dbghelp.h>
|
||||
|
||||
#include "Game.h"
|
||||
#include "dLogger.h"
|
||||
#include "Logger.h"
|
||||
|
||||
void make_minidump(EXCEPTION_POINTERS* e) {
|
||||
auto hDbgHelp = LoadLibraryA("dbghelp");
|
||||
@@ -28,7 +28,7 @@ void make_minidump(EXCEPTION_POINTERS* e) {
|
||||
"_%4d%02d%02d_%02d%02d%02d.dmp",
|
||||
t.wYear, t.wMonth, t.wDay, t.wHour, t.wMinute, t.wSecond);
|
||||
}
|
||||
Game::logger->Log("Diagnostics", "Creating crash dump %s", name);
|
||||
LOG("Creating crash dump %s", name);
|
||||
auto hFile = CreateFileA(name, GENERIC_WRITE, FILE_SHARE_READ, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
|
||||
if (hFile == INVALID_HANDLE_VALUE)
|
||||
return;
|
||||
@@ -83,7 +83,7 @@ struct bt_ctx {
|
||||
|
||||
static inline void Bt(struct backtrace_state* state) {
|
||||
std::string fileName = Diagnostics::GetOutDirectory() + "crash_" + Diagnostics::GetProcessName() + "_" + std::to_string(getpid()) + ".log";
|
||||
Game::logger->Log("Diagnostics", "backtrace is enabled, crash dump located at %s", fileName.c_str());
|
||||
LOG("backtrace is enabled, crash dump located at %s", fileName.c_str());
|
||||
FILE* file = fopen(fileName.c_str(), "w+");
|
||||
if (file != nullptr) {
|
||||
backtrace_print(state, 2, file);
|
||||
@@ -118,7 +118,7 @@ void CatchUnhandled(int sig) {
|
||||
#ifndef __include_backtrace__
|
||||
|
||||
std::string fileName = Diagnostics::GetOutDirectory() + "crash_" + Diagnostics::GetProcessName() + "_" + std::to_string(getpid()) + ".log";
|
||||
Game::logger->Log("Diagnostics", "Encountered signal %i, creating crash dump %s", sig, fileName.c_str());
|
||||
LOG("Encountered signal %i, creating crash dump %s", sig, fileName.c_str());
|
||||
if (Diagnostics::GetProduceMemoryDump()) {
|
||||
GenerateDump();
|
||||
}
|
||||
@@ -154,7 +154,7 @@ void CatchUnhandled(int sig) {
|
||||
}
|
||||
}
|
||||
|
||||
Game::logger->Log("Diagnostics", "[%02zu] %s", i, functionName.c_str());
|
||||
LOG("[%02zu] %s", i, functionName.c_str());
|
||||
}
|
||||
# else // defined(__GNUG__)
|
||||
backtrace_symbols_fd(array, size, STDOUT_FILENO);
|
||||
|
@@ -9,7 +9,7 @@
|
||||
#include "CDClientDatabase.h"
|
||||
#include "GeneralUtils.h"
|
||||
#include "Game.h"
|
||||
#include "dLogger.h"
|
||||
#include "Logger.h"
|
||||
#include "AssetManager.h"
|
||||
|
||||
#include "eSqliteDataType.h"
|
||||
@@ -44,7 +44,7 @@ bool FdbToSqlite::Convert::ConvertDatabase(AssetMemoryBuffer& buffer) {
|
||||
|
||||
CDClientDatabase::ExecuteQuery("COMMIT;");
|
||||
} catch (CppSQLite3Exception& e) {
|
||||
Game::logger->Log("FdbToSqlite", "Encountered error %s converting FDB to SQLite", e.errorMessage());
|
||||
LOG("Encountered error %s converting FDB to SQLite", e.errorMessage());
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@@ -3,7 +3,7 @@
|
||||
#include <random>
|
||||
|
||||
class dServer;
|
||||
class dLogger;
|
||||
class Logger;
|
||||
class InstanceManager;
|
||||
class dChatFilter;
|
||||
class dConfig;
|
||||
@@ -14,7 +14,7 @@ class EntityManager;
|
||||
class dZoneManager;
|
||||
|
||||
namespace Game {
|
||||
extern dLogger* logger;
|
||||
extern Logger* logger;
|
||||
extern dServer* server;
|
||||
extern InstanceManager* im;
|
||||
extern dChatFilter* chatFilter;
|
||||
|
@@ -13,7 +13,7 @@
|
||||
#include "NiPoint3.h"
|
||||
|
||||
#include "Game.h"
|
||||
#include "dLogger.h"
|
||||
#include "Logger.h"
|
||||
|
||||
enum eInventoryType : uint32_t;
|
||||
enum class eObjectBits : size_t;
|
||||
|
@@ -4,7 +4,7 @@
|
||||
#include "GeneralUtils.h"
|
||||
|
||||
#include "Game.h"
|
||||
#include "dLogger.h"
|
||||
#include "Logger.h"
|
||||
|
||||
// C++
|
||||
#include <string_view>
|
||||
@@ -48,7 +48,7 @@ LDFBaseData* LDFBaseData::DataFromString(const std::string_view& format) {
|
||||
try {
|
||||
type = static_cast<eLDFType>(strtol(ldfTypeAndValue.first.data(), &storage, 10));
|
||||
} catch (std::exception) {
|
||||
Game::logger->Log("LDFFormat", "Attempted to process invalid ldf type (%s) from string (%s)", ldfTypeAndValue.first.data(), format.data());
|
||||
LOG("Attempted to process invalid ldf type (%s) from string (%s)", ldfTypeAndValue.first.data(), format.data());
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@@ -63,7 +63,7 @@ LDFBaseData* LDFBaseData::DataFromString(const std::string_view& format) {
|
||||
case LDF_TYPE_S32: {
|
||||
int32_t data;
|
||||
if (!GeneralUtils::TryParse(ldfTypeAndValue.second.data(), data)) {
|
||||
Game::logger->Log("LDFFormat", "Warning: Attempted to process invalid int32 value (%s) from string (%s)", ldfTypeAndValue.second.data(), format.data());
|
||||
LOG("Warning: Attempted to process invalid int32 value (%s) from string (%s)", ldfTypeAndValue.second.data(), format.data());
|
||||
return nullptr;
|
||||
}
|
||||
returnValue = new LDFData<int32_t>(key, data);
|
||||
@@ -74,7 +74,7 @@ LDFBaseData* LDFBaseData::DataFromString(const std::string_view& format) {
|
||||
case LDF_TYPE_FLOAT: {
|
||||
float data;
|
||||
if (!GeneralUtils::TryParse(ldfTypeAndValue.second.data(), data)) {
|
||||
Game::logger->Log("LDFFormat", "Warning: Attempted to process invalid float value (%s) from string (%s)", ldfTypeAndValue.second.data(), format.data());
|
||||
LOG("Warning: Attempted to process invalid float value (%s) from string (%s)", ldfTypeAndValue.second.data(), format.data());
|
||||
return nullptr;
|
||||
}
|
||||
returnValue = new LDFData<float>(key, data);
|
||||
@@ -84,7 +84,7 @@ LDFBaseData* LDFBaseData::DataFromString(const std::string_view& format) {
|
||||
case LDF_TYPE_DOUBLE: {
|
||||
double data;
|
||||
if (!GeneralUtils::TryParse(ldfTypeAndValue.second.data(), data)) {
|
||||
Game::logger->Log("LDFFormat", "Warning: Attempted to process invalid double value (%s) from string (%s)", ldfTypeAndValue.second.data(), format.data());
|
||||
LOG("Warning: Attempted to process invalid double value (%s) from string (%s)", ldfTypeAndValue.second.data(), format.data());
|
||||
return nullptr;
|
||||
}
|
||||
returnValue = new LDFData<double>(key, data);
|
||||
@@ -101,7 +101,7 @@ LDFBaseData* LDFBaseData::DataFromString(const std::string_view& format) {
|
||||
data = 0;
|
||||
} else {
|
||||
if (!GeneralUtils::TryParse(ldfTypeAndValue.second.data(), data)) {
|
||||
Game::logger->Log("LDFFormat", "Warning: Attempted to process invalid uint32 value (%s) from string (%s)", ldfTypeAndValue.second.data(), format.data());
|
||||
LOG("Warning: Attempted to process invalid uint32 value (%s) from string (%s)", ldfTypeAndValue.second.data(), format.data());
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
@@ -119,7 +119,7 @@ LDFBaseData* LDFBaseData::DataFromString(const std::string_view& format) {
|
||||
data = false;
|
||||
} else {
|
||||
if (!GeneralUtils::TryParse(ldfTypeAndValue.second.data(), data)) {
|
||||
Game::logger->Log("LDFFormat", "Warning: Attempted to process invalid bool value (%s) from string (%s)", ldfTypeAndValue.second.data(), format.data());
|
||||
LOG("Warning: Attempted to process invalid bool value (%s) from string (%s)", ldfTypeAndValue.second.data(), format.data());
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
@@ -131,7 +131,7 @@ LDFBaseData* LDFBaseData::DataFromString(const std::string_view& format) {
|
||||
case LDF_TYPE_U64: {
|
||||
uint64_t data;
|
||||
if (!GeneralUtils::TryParse(ldfTypeAndValue.second.data(), data)) {
|
||||
Game::logger->Log("LDFFormat", "Warning: Attempted to process invalid uint64 value (%s) from string (%s)", ldfTypeAndValue.second.data(), format.data());
|
||||
LOG("Warning: Attempted to process invalid uint64 value (%s) from string (%s)", ldfTypeAndValue.second.data(), format.data());
|
||||
return nullptr;
|
||||
}
|
||||
returnValue = new LDFData<uint64_t>(key, data);
|
||||
@@ -141,7 +141,7 @@ LDFBaseData* LDFBaseData::DataFromString(const std::string_view& format) {
|
||||
case LDF_TYPE_OBJID: {
|
||||
LWOOBJID data;
|
||||
if (!GeneralUtils::TryParse(ldfTypeAndValue.second.data(), data)) {
|
||||
Game::logger->Log("LDFFormat", "Warning: Attempted to process invalid LWOOBJID value (%s) from string (%s)", ldfTypeAndValue.second.data(), format.data());
|
||||
LOG("Warning: Attempted to process invalid LWOOBJID value (%s) from string (%s)", ldfTypeAndValue.second.data(), format.data());
|
||||
return nullptr;
|
||||
}
|
||||
returnValue = new LDFData<LWOOBJID>(key, data);
|
||||
@@ -155,12 +155,12 @@ LDFBaseData* LDFBaseData::DataFromString(const std::string_view& format) {
|
||||
}
|
||||
|
||||
case LDF_TYPE_UNKNOWN: {
|
||||
Game::logger->Log("LDFFormat", "Warning: Attempted to process invalid unknown value (%s) from string (%s)", ldfTypeAndValue.second.data(), format.data());
|
||||
LOG("Warning: Attempted to process invalid unknown value (%s) from string (%s)", ldfTypeAndValue.second.data(), format.data());
|
||||
break;
|
||||
}
|
||||
|
||||
default: {
|
||||
Game::logger->Log("LDFFormat", "Warning: Attempted to process invalid LDF type (%d) from string (%s)", type, format.data());
|
||||
LOG("Warning: Attempted to process invalid LDF type (%d) from string (%s)", type, format.data());
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
96
dCommon/Logger.cpp
Normal file
96
dCommon/Logger.cpp
Normal file
@@ -0,0 +1,96 @@
|
||||
#include "Logger.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <ctime>
|
||||
#include <filesystem>
|
||||
#include <stdarg.h>
|
||||
|
||||
Writer::~Writer() {
|
||||
// Dont try to close stdcout...
|
||||
if (!m_Outfile || m_IsConsoleWriter) return;
|
||||
|
||||
fclose(m_Outfile);
|
||||
m_Outfile = NULL;
|
||||
}
|
||||
|
||||
void Writer::Log(const char* time, const char* message) {
|
||||
if (!m_Outfile) return;
|
||||
|
||||
fputs(time, m_Outfile);
|
||||
fputs(message, m_Outfile);
|
||||
}
|
||||
|
||||
void Writer::Flush() {
|
||||
if (!m_Outfile) return;
|
||||
fflush(m_Outfile);
|
||||
}
|
||||
|
||||
FileWriter::FileWriter(const char* outpath) {
|
||||
m_Outfile = fopen(outpath, "wt");
|
||||
if (!m_Outfile) printf("Couldn't open %s for writing!\n", outpath);
|
||||
m_Outpath = outpath;
|
||||
m_IsConsoleWriter = false;
|
||||
}
|
||||
|
||||
ConsoleWriter::ConsoleWriter(bool enabled) {
|
||||
m_Enabled = enabled;
|
||||
m_Outfile = stdout;
|
||||
m_IsConsoleWriter = true;
|
||||
}
|
||||
|
||||
Logger::Logger(const std::string& outpath, bool logToConsole, bool logDebugStatements) {
|
||||
m_logDebugStatements = logDebugStatements;
|
||||
std::filesystem::path outpathPath(outpath);
|
||||
if (!std::filesystem::exists(outpathPath.parent_path())) std::filesystem::create_directories(outpathPath.parent_path());
|
||||
m_Writers.push_back(std::make_unique<FileWriter>(outpath));
|
||||
m_Writers.push_back(std::make_unique<ConsoleWriter>(logToConsole));
|
||||
}
|
||||
|
||||
void Logger::vLog(const char* format, va_list args) {
|
||||
time_t t = time(NULL);
|
||||
struct tm* time = localtime(&t);
|
||||
char timeStr[70];
|
||||
strftime(timeStr, sizeof(timeStr), "[%d-%m-%y %H:%M:%S ", time);
|
||||
char message[2048];
|
||||
vsnprintf(message, 2048, format, args);
|
||||
for (const auto& writer : m_Writers) {
|
||||
writer->Log(timeStr, message);
|
||||
}
|
||||
}
|
||||
|
||||
void Logger::Log(const char* className, const char* format, ...) {
|
||||
va_list args;
|
||||
std::string log = std::string(className) + "] " + std::string(format) + "\n";
|
||||
va_start(args, format);
|
||||
vLog(log.c_str(), args);
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
void Logger::LogDebug(const char* className, const char* format, ...) {
|
||||
if (!m_logDebugStatements) return;
|
||||
va_list args;
|
||||
std::string log = std::string(className) + "] " + std::string(format) + "\n";
|
||||
va_start(args, format);
|
||||
vLog(log.c_str(), args);
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
void Logger::Flush() {
|
||||
for (const auto& writer : m_Writers) {
|
||||
writer->Flush();
|
||||
}
|
||||
}
|
||||
|
||||
void Logger::SetLogToConsole(bool logToConsole) {
|
||||
for (const auto& writer : m_Writers) {
|
||||
if (writer->IsConsoleWriter()) writer->SetEnabled(logToConsole);
|
||||
}
|
||||
}
|
||||
|
||||
bool Logger::GetLogToConsole() const {
|
||||
bool toReturn = false;
|
||||
for (const auto& writer : m_Writers) {
|
||||
if (writer->IsConsoleWriter()) toReturn |= writer->GetEnabled();
|
||||
}
|
||||
return toReturn;
|
||||
}
|
89
dCommon/Logger.h
Normal file
89
dCommon/Logger.h
Normal file
@@ -0,0 +1,89 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#define STRINGIFY_IMPL(x) #x
|
||||
|
||||
#define STRINGIFY(x) STRINGIFY_IMPL(x)
|
||||
|
||||
#define GET_FILE_NAME(x, y) GetFileNameFromAbsolutePath(__FILE__ x y)
|
||||
|
||||
#define FILENAME_AND_LINE GET_FILE_NAME(":", STRINGIFY(__LINE__))
|
||||
|
||||
// Calculate the filename at compile time from the path.
|
||||
// We just do this by scanning the path for the last '/' or '\' character and returning the string after it.
|
||||
constexpr const char* GetFileNameFromAbsolutePath(const char* path) {
|
||||
const char* file = path;
|
||||
while (*path) {
|
||||
char nextChar = *path++;
|
||||
if (nextChar == '/' || nextChar == '\\') {
|
||||
file = path;
|
||||
}
|
||||
}
|
||||
return file;
|
||||
}
|
||||
|
||||
// These have to have a constexpr variable to store the filename_and_line result in a local variable otherwise
|
||||
// they will not be valid constexpr and will be evaluated at runtime instead of compile time!
|
||||
// The full string is still stored in the binary, however the offset of the filename in the absolute paths
|
||||
// is used in the instruction instead of the start of the absolute path.
|
||||
#define LOG(message, ...) do { auto str = FILENAME_AND_LINE; Game::logger->Log(str, message, ##__VA_ARGS__); } while(0)
|
||||
#define LOG_DEBUG(message, ...) do { auto str = FILENAME_AND_LINE; Game::logger->LogDebug(str, message, ##__VA_ARGS__); } while(0)
|
||||
|
||||
// Writer class for writing data to files.
|
||||
class Writer {
|
||||
public:
|
||||
Writer(bool enabled = true) : m_Enabled(enabled) {};
|
||||
virtual ~Writer();
|
||||
|
||||
virtual void Log(const char* time, const char* message);
|
||||
virtual void Flush();
|
||||
|
||||
void SetEnabled(bool disabled) { m_Enabled = disabled; }
|
||||
bool GetEnabled() const { return m_Enabled; }
|
||||
|
||||
bool IsConsoleWriter() { return m_IsConsoleWriter; }
|
||||
public:
|
||||
bool m_Enabled = true;
|
||||
bool m_IsConsoleWriter = false;
|
||||
FILE* m_Outfile;
|
||||
};
|
||||
|
||||
// FileWriter class for writing data to a file on a disk.
|
||||
class FileWriter : public Writer {
|
||||
public:
|
||||
FileWriter(const char* outpath);
|
||||
FileWriter(const std::string& outpath) : FileWriter(outpath.c_str()) {};
|
||||
private:
|
||||
std::string m_Outpath;
|
||||
};
|
||||
|
||||
// ConsoleWriter class for writing data to the console.
|
||||
class ConsoleWriter : public Writer {
|
||||
public:
|
||||
ConsoleWriter(bool enabled);
|
||||
};
|
||||
|
||||
class Logger {
|
||||
public:
|
||||
Logger() = delete;
|
||||
Logger(const std::string& outpath, bool logToConsole, bool logDebugStatements);
|
||||
|
||||
void Log(const char* filenameAndLine, const char* format, ...);
|
||||
void LogDebug(const char* filenameAndLine, const char* format, ...);
|
||||
|
||||
void Flush();
|
||||
|
||||
bool GetLogToConsole() const;
|
||||
void SetLogToConsole(bool logToConsole);
|
||||
|
||||
void SetLogDebugStatements(bool logDebugStatements) { m_logDebugStatements = logDebugStatements; }
|
||||
|
||||
private:
|
||||
void vLog(const char* format, va_list args);
|
||||
|
||||
bool m_logDebugStatements;
|
||||
std::vector<std::unique_ptr<Writer>> m_Writers;
|
||||
};
|
@@ -2,7 +2,7 @@
|
||||
|
||||
#include "AssetManager.h"
|
||||
#include "Game.h"
|
||||
#include "dLogger.h"
|
||||
#include "Logger.h"
|
||||
|
||||
#include <zlib.h>
|
||||
|
||||
|
@@ -3,6 +3,7 @@
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
|
||||
#pragma pack(push, 1)
|
||||
struct PackRecord {
|
||||
|
@@ -1,7 +1,7 @@
|
||||
#include "PackIndex.h"
|
||||
#include "BinaryIO.h"
|
||||
#include "Game.h"
|
||||
#include "dLogger.h"
|
||||
#include "Logger.h"
|
||||
|
||||
PackIndex::PackIndex(const std::filesystem::path& filePath) {
|
||||
m_FileStream = std::ifstream(filePath / "versions" / "primary.pki", std::ios::in | std::ios::binary);
|
||||
@@ -34,7 +34,7 @@ PackIndex::PackIndex(const std::filesystem::path& filePath) {
|
||||
m_PackFileIndices.push_back(packFileIndex);
|
||||
}
|
||||
|
||||
Game::logger->Log("PackIndex", "Loaded pack catalog with %i pack files and %i files", m_PackPaths.size(), m_PackFileIndices.size());
|
||||
LOG("Loaded pack catalog with %i pack files and %i files", m_PackPaths.size(), m_PackFileIndices.size());
|
||||
|
||||
for (auto& item : m_PackPaths) {
|
||||
std::replace(item.begin(), item.end(), '\\', '/');
|
||||
|
@@ -1,110 +0,0 @@
|
||||
#include "dLogger.h"
|
||||
|
||||
dLogger::dLogger(const std::string& outpath, bool logToConsole, bool logDebugStatements) {
|
||||
m_logToConsole = logToConsole;
|
||||
m_logDebugStatements = logDebugStatements;
|
||||
m_outpath = outpath;
|
||||
|
||||
#ifdef _WIN32
|
||||
mFile = std::ofstream(m_outpath);
|
||||
if (!mFile) { printf("Couldn't open %s for writing!\n", outpath.c_str()); }
|
||||
#else
|
||||
fp = fopen(outpath.c_str(), "wt");
|
||||
if (fp == NULL) { printf("Couldn't open %s for writing!\n", outpath.c_str()); }
|
||||
#endif
|
||||
}
|
||||
|
||||
dLogger::~dLogger() {
|
||||
#ifdef _WIN32
|
||||
mFile.close();
|
||||
#else
|
||||
if (fp != nullptr) {
|
||||
fclose(fp);
|
||||
fp = nullptr;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void dLogger::vLog(const char* format, va_list args) {
|
||||
#ifdef _WIN32
|
||||
time_t t = time(NULL);
|
||||
struct tm time;
|
||||
localtime_s(&time, &t);
|
||||
char timeStr[70];
|
||||
strftime(timeStr, sizeof(timeStr), "%d-%m-%y %H:%M:%S", &time);
|
||||
char message[2048];
|
||||
vsnprintf(message, 2048, format, args);
|
||||
|
||||
if (m_logToConsole) std::cout << "[" << timeStr << "] " << message;
|
||||
mFile << "[" << timeStr << "] " << message;
|
||||
#else
|
||||
time_t t = time(NULL);
|
||||
struct tm* time = localtime(&t);
|
||||
char timeStr[70];
|
||||
strftime(timeStr, sizeof(timeStr), "%d-%m-%y %H:%M:%S", time);
|
||||
char message[2048];
|
||||
vsnprintf(message, 2048, format, args);
|
||||
|
||||
if (m_logToConsole) {
|
||||
fputs("[", stdout);
|
||||
fputs(timeStr, stdout);
|
||||
fputs("] ", stdout);
|
||||
fputs(message, stdout);
|
||||
}
|
||||
|
||||
if (fp != nullptr) {
|
||||
fputs("[", fp);
|
||||
fputs(timeStr, fp);
|
||||
fputs("] ", fp);
|
||||
fputs(message, fp);
|
||||
} else {
|
||||
printf("Logger not initialized!\n");
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void dLogger::LogBasic(const char* format, ...) {
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
vLog(format, args);
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
void dLogger::LogBasic(const std::string& message) {
|
||||
LogBasic(message.c_str());
|
||||
}
|
||||
|
||||
void dLogger::Log(const char* className, const char* format, ...) {
|
||||
va_list args;
|
||||
std::string log = "[" + std::string(className) + "] " + std::string(format) + "\n";
|
||||
va_start(args, format);
|
||||
vLog(log.c_str(), args);
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
void dLogger::Log(const std::string& className, const std::string& message) {
|
||||
Log(className.c_str(), message.c_str());
|
||||
}
|
||||
|
||||
void dLogger::LogDebug(const char* className, const char* format, ...) {
|
||||
if (!m_logDebugStatements) return;
|
||||
va_list args;
|
||||
std::string log = "[" + std::string(className) + "] " + std::string(format) + "\n";
|
||||
va_start(args, format);
|
||||
vLog(log.c_str(), args);
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
void dLogger::LogDebug(const std::string& className, const std::string& message) {
|
||||
LogDebug(className.c_str(), message.c_str());
|
||||
}
|
||||
|
||||
void dLogger::Flush() {
|
||||
#ifdef _WIN32
|
||||
mFile.flush();
|
||||
#else
|
||||
if (fp != nullptr) {
|
||||
std::fflush(fp);
|
||||
}
|
||||
#endif
|
||||
}
|
@@ -1,38 +0,0 @@
|
||||
#pragma once
|
||||
#include <ctime>
|
||||
#include <cstdarg>
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
|
||||
class dLogger {
|
||||
public:
|
||||
dLogger(const std::string& outpath, bool logToConsole, bool logDebugStatements);
|
||||
~dLogger();
|
||||
|
||||
void SetLogToConsole(bool logToConsole) { m_logToConsole = logToConsole; }
|
||||
void SetLogDebugStatements(bool logDebugStatements) { m_logDebugStatements = logDebugStatements; }
|
||||
void vLog(const char* format, va_list args);
|
||||
|
||||
void LogBasic(const std::string& message);
|
||||
void LogBasic(const char* format, ...);
|
||||
void Log(const char* className, const char* format, ...);
|
||||
void Log(const std::string& className, const std::string& message);
|
||||
void LogDebug(const std::string& className, const std::string& message);
|
||||
void LogDebug(const char* className, const char* format, ...);
|
||||
|
||||
void Flush();
|
||||
|
||||
const bool GetIsLoggingToConsole() const { return m_logToConsole; }
|
||||
|
||||
private:
|
||||
bool m_logDebugStatements;
|
||||
bool m_logToConsole;
|
||||
std::string m_outpath;
|
||||
std::ofstream mFile;
|
||||
|
||||
#ifndef _WIN32
|
||||
//Glorious linux can run with SPEED:
|
||||
FILE* fp = nullptr;
|
||||
#endif
|
||||
};
|
Reference in New Issue
Block a user