diff options
| author | Rusty Wagner <rusty.wagner@gmail.com> | 2025-08-01 19:03:16 -0400 |
|---|---|---|
| committer | Rusty Wagner <rusty.wagner@gmail.com> | 2025-08-01 20:56:38 -0400 |
| commit | be8c0c03e5d784ff7d2909d120cda6566ac2cea6 (patch) | |
| tree | 8e463b2dd62f2478529ea25e3f6599f12007d394 /log.cpp | |
| parent | 15c635b873105eda2861430d062ea3f6bdfe9d9b (diff) | |
Add log functions for logging the current stack trace without an active exception
Diffstat (limited to 'log.cpp')
| -rw-r--r-- | log.cpp | 232 |
1 files changed, 150 insertions, 82 deletions
@@ -161,6 +161,49 @@ static void PerformLogForException(size_t session, BNLogLevel level, const strin } +static void PerformLogWithStackTrace(size_t session, BNLogLevel level, const string& logger_name, size_t tid, + const char* fmt, va_list args) +{ + #if defined(_MSC_VER) + int len = _vscprintf(fmt, args); + if (len < 0) + return; + char* msg = (char*)malloc(len + 1); + if (!msg) + return; + if (vsnprintf(msg, len + 1, fmt, args) >= 0) + { + char* stackTrace = BNGetCurrentStackTraceString(); + if (stackTrace) + { + BNLogWithStackTrace(session, level, logger_name.c_str(), tid, stackTrace, "%s", msg); + BNFreeString(stackTrace); + } + else + { + BNLog(session, level, logger_name.c_str(), tid, "%s", msg); + } + } + free(msg); + #else + char* msg; + if (vasprintf(&msg, fmt, args) < 0) + return; + char* stackTrace = BNGetCurrentStackTraceString(); + if (stackTrace) + { + BNLogWithStackTrace(session, level, logger_name.c_str(), tid, stackTrace, "%s", msg); + BNFreeString(stackTrace); + } + else + { + BNLog(session, level, logger_name.c_str(), tid, "%s", msg); + } + free(msg); + #endif +} + + void BinaryNinja::Log(BNLogLevel level, const char* fmt, ...) { va_list args; @@ -291,102 +334,111 @@ void BinaryNinja::LogAlertForException(const std::exception& e, const char* fmt, } -void BinaryNinja::LogFV(BNLogLevel level, fmt::string_view format, fmt::format_args args) -{ - std::string value = fmt::vformat(format, args); - Log(level, "%s", value.c_str()); -} - - -void BinaryNinja::LogTraceFV(fmt::string_view format, fmt::format_args args) +void BinaryNinja::LogWithStackTrace(BNLogLevel level, const char* fmt, ...) { - std::string value = fmt::vformat(format, args); - LogTrace("%s", value.c_str()); + va_list args; + va_start(args, fmt); + PerformLogWithStackTrace(0, level, "", 0, fmt, args); + va_end(args); } -void BinaryNinja::LogDebugFV(fmt::string_view format, fmt::format_args args) +void BinaryNinja::LogTraceWithStackTrace(const char* fmt, ...) { - std::string value = fmt::vformat(format, args); - LogDebug("%s", value.c_str()); + #ifdef _DEBUG + va_list args; + va_start(args, fmt); + PerformLogWithStackTrace(0, DebugLog, "", 0, fmt, args); + va_end(args); + #endif } -void BinaryNinja::LogInfoFV(fmt::string_view format, fmt::format_args args) +void BinaryNinja::LogDebugWithStackTrace(const char* fmt, ...) { - std::string value = fmt::vformat(format, args); - LogInfo("%s", value.c_str()); + va_list args; + va_start(args, fmt); + PerformLogWithStackTrace(0, DebugLog, "", 0, fmt, args); + va_end(args); } -void BinaryNinja::LogWarnFV(fmt::string_view format, fmt::format_args args) +void BinaryNinja::LogInfoWithStackTrace(const char* fmt, ...) { - std::string value = fmt::vformat(format, args); - LogWarn("%s", value.c_str()); + va_list args; + va_start(args, fmt); + PerformLogWithStackTrace(0, InfoLog, "", 0, fmt, args); + va_end(args); } -void BinaryNinja::LogErrorFV(fmt::string_view format, fmt::format_args args) +void BinaryNinja::LogWarnWithStackTrace(const char* fmt, ...) { - std::string value = fmt::vformat(format, args); - LogError("%s", value.c_str()); + va_list args; + va_start(args, fmt); + PerformLogWithStackTrace(0, WarningLog, "", 0, fmt, args); + va_end(args); } -void BinaryNinja::LogAlertFV(fmt::string_view format, fmt::format_args args) +void BinaryNinja::LogErrorWithStackTrace(const char* fmt, ...) { - std::string value = fmt::vformat(format, args); - LogAlert("%s", value.c_str()); + va_list args; + va_start(args, fmt); + PerformLogWithStackTrace(0, ErrorLog, "", 0, fmt, args); + va_end(args); } -void BinaryNinja::LogForExceptionFV( - BNLogLevel level, const std::exception& e, fmt::string_view format, fmt::format_args args) +void BinaryNinja::LogAlertWithStackTrace(const char* fmt, ...) { - std::string value = fmt::vformat(format, args); - LogForException(level, e, "%s", value.c_str()); + va_list args; + va_start(args, fmt); + PerformLogWithStackTrace(0, AlertLog, "", 0, fmt, args); + va_end(args); } -void BinaryNinja::LogTraceForExceptionFV(const std::exception& e, fmt::string_view format, fmt::format_args args) +void BinaryNinja::LogFV(BNLogLevel level, fmt::string_view format, fmt::format_args args) { std::string value = fmt::vformat(format, args); - LogTraceForException(e, "%s", value.c_str()); + Log(level, "%s", value.c_str()); } -void BinaryNinja::LogDebugForExceptionFV(const std::exception& e, fmt::string_view format, fmt::format_args args) +void BinaryNinja::LogTraceFV(fmt::string_view format, fmt::format_args args) { std::string value = fmt::vformat(format, args); - LogDebugForException(e, "%s", value.c_str()); + LogTrace("%s", value.c_str()); } -void BinaryNinja::LogInfoForExceptionFV(const std::exception& e, fmt::string_view format, fmt::format_args args) +void BinaryNinja::LogForExceptionFV( + BNLogLevel level, const std::exception& e, fmt::string_view format, fmt::format_args args) { std::string value = fmt::vformat(format, args); - LogInfoForException(e, "%s", value.c_str()); + LogForException(level, e, "%s", value.c_str()); } -void BinaryNinja::LogWarnForExceptionFV(const std::exception& e, fmt::string_view format, fmt::format_args args) +void BinaryNinja::LogTraceForExceptionFV(const std::exception& e, fmt::string_view format, fmt::format_args args) { std::string value = fmt::vformat(format, args); - LogWarnForException(e, "%s", value.c_str()); + LogTraceForException(e, "%s", value.c_str()); } -void BinaryNinja::LogErrorForExceptionFV(const std::exception& e, fmt::string_view format, fmt::format_args args) +void BinaryNinja::LogWithStackTraceFV(BNLogLevel level, fmt::string_view format, fmt::format_args args) { std::string value = fmt::vformat(format, args); - LogErrorForException(e, "%s", value.c_str()); + LogWithStackTrace(level, "%s", value.c_str()); } -void BinaryNinja::LogAlertForExceptionFV(const std::exception& e, fmt::string_view format, fmt::format_args args) +void BinaryNinja::LogTraceWithStackTraceFV(fmt::string_view format, fmt::format_args args) { std::string value = fmt::vformat(format, args); - LogAlertForException(e, "%s", value.c_str()); + LogTraceWithStackTrace("%s", value.c_str()); } @@ -567,102 +619,118 @@ void Logger::LogAlertForException(const std::exception& e, const char* fmt, ...) } -void Logger::LogFV(BNLogLevel level, fmt::string_view format, fmt::format_args args) -{ - std::string value = fmt::vformat(format, args); - Log(level, "%s", value.c_str()); -} - - -void Logger::LogTraceFV(fmt::string_view format, fmt::format_args args) +void Logger::LogWithStackTrace(BNLogLevel level, const char* fmt, ...) { - std::string value = fmt::vformat(format, args); - LogTrace("%s", value.c_str()); + va_list args; + va_start(args, fmt); + PerformLogWithStackTrace( + GetSessionId(), level, GetName(), GetThreadId(), fmt::format("{}{}", GetIndent(), fmt).c_str(), args); + va_end(args); } -void Logger::LogDebugFV(fmt::string_view format, fmt::format_args args) +void Logger::LogTraceWithStackTrace(const char* fmt, ...) { - std::string value = fmt::vformat(format, args); - LogDebug("%s", value.c_str()); + #ifdef _DEBUG + va_list args; + va_start(args, fmt); + PerformLogWithStackTrace( + GetSessionId(), DebugLog, GetName(), GetThreadId(), fmt::format("{}{}", GetIndent(), fmt).c_str(), args); + va_end(args); + #endif } -void Logger::LogInfoFV(fmt::string_view format, fmt::format_args args) +void Logger::LogDebugWithStackTrace(const char* fmt, ...) { - std::string value = fmt::vformat(format, args); - LogInfo("%s", value.c_str()); + va_list args; + va_start(args, fmt); + PerformLogWithStackTrace( + GetSessionId(), DebugLog, GetName(), GetThreadId(), fmt::format("{}{}", GetIndent(), fmt).c_str(), args); + va_end(args); } -void Logger::LogWarnFV(fmt::string_view format, fmt::format_args args) +void Logger::LogInfoWithStackTrace(const char* fmt, ...) { - std::string value = fmt::vformat(format, args); - LogWarn("%s", value.c_str()); + va_list args; + va_start(args, fmt); + PerformLogWithStackTrace( + GetSessionId(), InfoLog, GetName(), GetThreadId(), fmt::format("{}{}", GetIndent(), fmt).c_str(), args); + va_end(args); } -void Logger::LogErrorFV(fmt::string_view format, fmt::format_args args) +void Logger::LogWarnWithStackTrace(const char* fmt, ...) { - std::string value = fmt::vformat(format, args); - LogError("%s", value.c_str()); + va_list args; + va_start(args, fmt); + PerformLogWithStackTrace( + GetSessionId(), WarningLog, GetName(), GetThreadId(), fmt::format("{}{}", GetIndent(), fmt).c_str(), args); + va_end(args); } -void Logger::LogAlertFV(fmt::string_view format, fmt::format_args args) +void Logger::LogErrorWithStackTrace(const char* fmt, ...) { - std::string value = fmt::vformat(format, args); - LogAlert("%s", value.c_str()); + va_list args; + va_start(args, fmt); + PerformLogWithStackTrace( + GetSessionId(), ErrorLog, GetName(), GetThreadId(), fmt::format("{}{}", GetIndent(), fmt).c_str(), args); + va_end(args); } -void Logger::LogForExceptionFV( - BNLogLevel level, const std::exception& e, fmt::string_view format, fmt::format_args args) +void Logger::LogAlertWithStackTrace(const char* fmt, ...) { - std::string value = fmt::vformat(format, args); - LogForException(level, e, "%s", value.c_str()); + va_list args; + va_start(args, fmt); + PerformLogWithStackTrace( + GetSessionId(), AlertLog, GetName(), GetThreadId(), fmt::format("{}{}", GetIndent(), fmt).c_str(), args); + va_end(args); } -void Logger::LogTraceForExceptionFV(const std::exception& e, fmt::string_view format, fmt::format_args args) +void Logger::LogFV(BNLogLevel level, fmt::string_view format, fmt::format_args args) { std::string value = fmt::vformat(format, args); - LogTraceForException(e, "%s", value.c_str()); + Log(level, "%s", value.c_str()); } -void Logger::LogDebugForExceptionFV(const std::exception& e, fmt::string_view format, fmt::format_args args) +void Logger::LogTraceFV(fmt::string_view format, fmt::format_args args) { std::string value = fmt::vformat(format, args); - LogDebugForException(e, "%s", value.c_str()); + LogTrace("%s", value.c_str()); } -void Logger::LogInfoForExceptionFV(const std::exception& e, fmt::string_view format, fmt::format_args args) +void Logger::LogForExceptionFV( + BNLogLevel level, const std::exception& e, fmt::string_view format, fmt::format_args args) { std::string value = fmt::vformat(format, args); - LogInfoForException(e, "%s", value.c_str()); + LogForException(level, e, "%s", value.c_str()); } -void Logger::LogWarnForExceptionFV(const std::exception& e, fmt::string_view format, fmt::format_args args) +void Logger::LogTraceForExceptionFV(const std::exception& e, fmt::string_view format, fmt::format_args args) { std::string value = fmt::vformat(format, args); - LogWarnForException(e, "%s", value.c_str()); + LogTraceForException(e, "%s", value.c_str()); } -void Logger::LogErrorForExceptionFV(const std::exception& e, fmt::string_view format, fmt::format_args args) +void Logger::LogWithStackTraceFV(BNLogLevel level, fmt::string_view format, fmt::format_args args) { std::string value = fmt::vformat(format, args); - LogErrorForException(e, "%s", value.c_str()); + LogWithStackTrace(level, "%s", value.c_str()); } -void Logger::LogAlertForExceptionFV(const std::exception& e, fmt::string_view format, fmt::format_args args) +void Logger::LogTraceWithStackTraceFV(fmt::string_view format, fmt::format_args args) { std::string value = fmt::vformat(format, args); - LogAlertForException(e, "%s", value.c_str()); + LogTraceWithStackTrace("%s", value.c_str()); } |
