From be8c0c03e5d784ff7d2909d120cda6566ac2cea6 Mon Sep 17 00:00:00 2001 From: Rusty Wagner Date: Fri, 1 Aug 2025 19:03:16 -0400 Subject: Add log functions for logging the current stack trace without an active exception --- log.cpp | 232 +++++++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 150 insertions(+), 82 deletions(-) (limited to 'log.cpp') diff --git a/log.cpp b/log.cpp index 709ad112..2329e503 100644 --- a/log.cpp +++ b/log.cpp @@ -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()); } -- cgit v1.3.1