mirror of
https://github.com/AxioDL/logvisor.git
synced 2025-05-31 19:51:20 +00:00
Fix crashing on windows when registering a file logger after a console logger, windows runtime bugs annoy the hell out of me >:(
This commit is contained in:
parent
81fb4e4c2d
commit
8913e55e34
@ -17,6 +17,7 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
extern "C" void logvisorBp();
|
extern "C" void logvisorBp();
|
||||||
|
#define log_typeid(type) std::hash<std::string>()(#type)
|
||||||
|
|
||||||
namespace logvisor {
|
namespace logvisor {
|
||||||
|
|
||||||
@ -43,6 +44,10 @@ enum Level {
|
|||||||
* @brief Backend interface for receiving app-wide log events
|
* @brief Backend interface for receiving app-wide log events
|
||||||
*/
|
*/
|
||||||
struct ILogger {
|
struct ILogger {
|
||||||
|
private:
|
||||||
|
uint64_t m_typeHash;
|
||||||
|
public:
|
||||||
|
ILogger(uint64_t typeHash) : m_typeHash(typeHash) {}
|
||||||
virtual ~ILogger() = default;
|
virtual ~ILogger() = default;
|
||||||
virtual void report(const char* modName, Level severity, fmt::string_view format, fmt::format_args args) = 0;
|
virtual void report(const char* modName, Level severity, fmt::string_view format, fmt::format_args args) = 0;
|
||||||
virtual void report(const char* modName, Level severity, fmt::wstring_view format, fmt::wformat_args args) = 0;
|
virtual void report(const char* modName, Level severity, fmt::wstring_view format, fmt::wformat_args args) = 0;
|
||||||
@ -50,6 +55,8 @@ struct ILogger {
|
|||||||
fmt::string_view format, fmt::format_args args) = 0;
|
fmt::string_view format, fmt::format_args args) = 0;
|
||||||
virtual void reportSource(const char* modName, Level severity, const char* file, unsigned linenum,
|
virtual void reportSource(const char* modName, Level severity, const char* file, unsigned linenum,
|
||||||
fmt::wstring_view format, fmt::wformat_args args) = 0;
|
fmt::wstring_view format, fmt::wformat_args args) = 0;
|
||||||
|
|
||||||
|
[[nodiscard]] uint64_t getTypeId() const { return m_typeHash; }
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -536,7 +536,7 @@ static const char* Term = nullptr;
|
|||||||
#endif
|
#endif
|
||||||
bool XtermColor = false;
|
bool XtermColor = false;
|
||||||
struct ConsoleLogger : public ILogger {
|
struct ConsoleLogger : public ILogger {
|
||||||
ConsoleLogger() {
|
ConsoleLogger() : ILogger(log_typeid(ConsoleLogger)) {
|
||||||
#if _WIN32
|
#if _WIN32
|
||||||
#if !WINDOWS_STORE
|
#if !WINDOWS_STORE
|
||||||
const char* conemuANSI = getenv("ConEmuANSI");
|
const char* conemuANSI = getenv("ConEmuANSI");
|
||||||
@ -555,6 +555,7 @@ struct ConsoleLogger : public ILogger {
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
~ConsoleLogger() override = default;
|
||||||
|
|
||||||
static void _reportHead(const char* modName, const char* sourceInfo, Level severity) {
|
static void _reportHead(const char* modName, const char* sourceInfo, Level severity) {
|
||||||
/* Clear current line out */
|
/* Clear current line out */
|
||||||
@ -738,8 +739,10 @@ void RegisterStandardExceptions() {
|
|||||||
|
|
||||||
struct FileLogger : public ILogger {
|
struct FileLogger : public ILogger {
|
||||||
FILE* fp;
|
FILE* fp;
|
||||||
|
FileLogger(uint64_t typeHash) : ILogger(typeHash) {}
|
||||||
virtual void openFile() = 0;
|
virtual void openFile() = 0;
|
||||||
virtual void closeFile() { std::fclose(fp); }
|
virtual void closeFile() { std::fclose(fp); }
|
||||||
|
virtual ~FileLogger() = default;
|
||||||
|
|
||||||
void _reportHead(const char* modName, const char* sourceInfo, Level severity) {
|
void _reportHead(const char* modName, const char* sourceInfo, Level severity) {
|
||||||
const std::chrono::steady_clock::duration tm = CurrentUptime();
|
const std::chrono::steady_clock::duration tm = CurrentUptime();
|
||||||
@ -820,8 +823,9 @@ struct FileLogger : public ILogger {
|
|||||||
|
|
||||||
struct FileLogger8 : public FileLogger {
|
struct FileLogger8 : public FileLogger {
|
||||||
const char* m_filepath;
|
const char* m_filepath;
|
||||||
FileLogger8(const char* filepath) : m_filepath(filepath) {}
|
FileLogger8(const char* filepath) : FileLogger(log_typeid(FileLogger8)), m_filepath(filepath) {}
|
||||||
void openFile() override { fp = std::fopen(m_filepath, "a"); }
|
void openFile() override { fp = std::fopen(m_filepath, "a"); }
|
||||||
|
~FileLogger8() override = default;
|
||||||
};
|
};
|
||||||
|
|
||||||
void RegisterFileLogger(const char* filepath) {
|
void RegisterFileLogger(const char* filepath) {
|
||||||
@ -833,17 +837,19 @@ void RegisterFileLogger(const char* filepath) {
|
|||||||
|
|
||||||
struct FileLogger16 : public FileLogger {
|
struct FileLogger16 : public FileLogger {
|
||||||
const wchar_t* m_filepath;
|
const wchar_t* m_filepath;
|
||||||
FileLogger16(const wchar_t* filepath) : m_filepath(filepath) {}
|
FileLogger16(const wchar_t* filepath) : FileLogger(log_typeid(FileLogger16)), m_filepath(filepath) {}
|
||||||
void openFile() override { fp = _wfopen(m_filepath, L"a"); }
|
void openFile() override { fp = _wfopen(m_filepath, L"a"); }
|
||||||
|
~FileLogger16() override = default;
|
||||||
};
|
};
|
||||||
|
|
||||||
void RegisterFileLogger(const wchar_t* filepath) {
|
void RegisterFileLogger(const wchar_t* filepath) {
|
||||||
/* Determine if file logger already added */
|
/* Determine if file logger already added */
|
||||||
for (auto& logger : MainLoggers) {
|
for (const auto& logger : MainLoggers) {
|
||||||
FileLogger16* filelogger = dynamic_cast<FileLogger16*>(logger.get());
|
if (logger->getTypeId() == log_typeid(FileLogger16)) {
|
||||||
if (filelogger) {
|
const auto* fl = static_cast<const FileLogger16*>(logger.get());
|
||||||
if (!wcscmp(filepath, filelogger->m_filepath))
|
if (!wcscmp(filepath, fl->m_filepath)) {
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user