From cb6cee9dd12f6d6489bacdaa2e9df6e7c5d4fef8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Fri, 24 Jul 2015 18:02:17 -0700 Subject: [PATCH] Updated trace callback. --- examples/07-callback/callback.cpp | 5 +++-- include/bgfx.c99.h | 3 ++- include/bgfx.h | 11 ++++++++--- src/bgfx.cpp | 23 +++++++---------------- src/bgfx_p.h | 4 ++-- 5 files changed, 22 insertions(+), 24 deletions(-) diff --git a/examples/07-callback/callback.cpp b/examples/07-callback/callback.cpp index 0d256ead..fa0b0329 100644 --- a/examples/07-callback/callback.cpp +++ b/examples/07-callback/callback.cpp @@ -136,9 +136,10 @@ struct BgfxCallback : public bgfx::CallbackI abort(); } - virtual void trace(const char* _str) BX_OVERRIDE + virtual void traceVargs(const char* _filePath, uint16_t _line, const char* _format, va_list _argList) BX_OVERRIDE { - dbgPrintf("%s", _str); + dbgPrintf("%s (%d): ", _filePath, _line); + dbgPrintfVargs(_format, _argList); } virtual uint32_t cacheReadSize(uint64_t _id) BX_OVERRIDE diff --git a/include/bgfx.c99.h b/include/bgfx.c99.h index 04b18889..9193e157 100644 --- a/include/bgfx.c99.h +++ b/include/bgfx.c99.h @@ -8,6 +8,7 @@ #ifndef BGFX_C99_H_HEADER_GUARD #define BGFX_C99_H_HEADER_GUARD +#include // va_list #include // bool #include // uint32_t #include // size_t @@ -367,7 +368,7 @@ typedef struct bgfx_callback_interface typedef struct bgfx_callback_vtbl { void (*fatal)(bgfx_callback_interface_t* _this, bgfx_fatal_t _code, const char* _str); - void (*trace)(bgfx_callback_interface_t* _this, const char* _str); + void (*trace_vargs)(bgfx_callback_interface_t* _this, const char* _filePath, uint16_t _line, const char* _format, va_list _argList); uint32_t (*cache_read_size)(bgfx_callback_interface_t* _this, uint64_t _id); bool (*cache_read)(bgfx_callback_interface_t* _this, uint64_t _id, void* _data, uint32_t _size); void (*cache_write)(bgfx_callback_interface_t* _this, uint64_t _id, const void* _data, uint32_t _size); diff --git a/include/bgfx.h b/include/bgfx.h index 576a9645..e3e0244e 100644 --- a/include/bgfx.h +++ b/include/bgfx.h @@ -6,6 +6,7 @@ #ifndef BGFX_H_HEADER_GUARD #define BGFX_H_HEADER_GUARD +#include // va_list #include // uint32_t #include // size_t @@ -275,14 +276,18 @@ namespace bgfx /// Print debug message. /// - /// @param[in] _str Message. + /// @param[in] _filePath File path where debug message was generated. + /// @param[in] _line Line where debug message was generated. + /// @param[in] _format `printf` style format. + /// @param[in] _argList Variable arguments list initialized with + /// `va_start`. /// /// @remarks /// Not thread safe and it can be called from any thread. /// - /// @attention C99 equivalent is `bgfx_callback_vtbl.trace`. + /// @attention C99 equivalent is `bgfx_callback_vtbl.trace_vargs`. /// - virtual void trace(const char* _str) = 0; + virtual void traceVargs(const char* _filePath, uint16_t _line, const char* _format, va_list _argList) = 0; /// Return size of for cached item. Return 0 if no cached item was /// found. diff --git a/src/bgfx.cpp b/src/bgfx.cpp index 42a1e4ca..2d71223f 100644 --- a/src/bgfx.cpp +++ b/src/bgfx.cpp @@ -40,9 +40,10 @@ namespace bgfx { } - virtual void trace(const char* _str) BX_OVERRIDE + virtual void traceVargs(const char* _filePath, uint16_t _line, const char* _format, va_list _argList) BX_OVERRIDE { - bx::debugOutput(_str); + dbgPrintf("%s (%d): ", _filePath, _line); + dbgPrintfVargs(_format, _argList); } virtual void fatal(Fatal::Enum _code, const char* _str) BX_OVERRIDE @@ -254,23 +255,13 @@ namespace bgfx g_callback->fatal(_code, out); } - void trace(const char* _format, ...) + void trace(const char* _filePath, uint16_t _line, const char* _format, ...) { - char temp[8192]; - va_list argList; va_start(argList, _format); - char* out = temp; - int32_t len = bx::vsnprintf(out, sizeof(temp), _format, argList); - if ( (int32_t)sizeof(temp) < len) - { - out = (char*)alloca(len+1); - len = bx::vsnprintf(out, len, _format, argList); - } - out[len] = '\0'; + g_callback->traceVargs(_filePath, _line, _format, argList); va_end(argList); - g_callback->trace(out); } #include "charset.h" @@ -3106,9 +3097,9 @@ namespace bgfx m_interface->vtbl->fatal(m_interface, (bgfx_fatal_t)_code, _str); } - virtual void trace(const char* _str) BX_OVERRIDE + virtual void traceVargs(const char* _filePath, uint16_t _line, const char* _format, va_list _argList) BX_OVERRIDE { - m_interface->vtbl->trace(m_interface, _str); + m_interface->vtbl->trace_vargs(m_interface, _filePath, _line, _format, _argList); } virtual uint32_t cacheReadSize(uint64_t _id) BX_OVERRIDE diff --git a/src/bgfx_p.h b/src/bgfx_p.h index 446d307f..76237360 100644 --- a/src/bgfx_p.h +++ b/src/bgfx_p.h @@ -57,7 +57,7 @@ namespace bgfx void fatal(Fatal::Enum _code, const char* _format, ...); #endif // BX_COMPILER_CLANG_ANALYZER - void trace(const char* _format, ...); + void trace(const char* _filePath, uint16_t _line, const char* _format, ...); void dbgPrintfVargs(const char* _format, va_list _argList); void dbgPrintf(const char* _format, ...); @@ -65,7 +65,7 @@ namespace bgfx #define _BX_TRACE(_format, ...) \ BX_MACRO_BLOCK_BEGIN \ - bgfx::trace(BX_FILE_LINE_LITERAL "BGFX " _format "\n", ##__VA_ARGS__); \ + bgfx::trace(__FILE__, uint16_t(__LINE__), "BGFX " _format "\n", ##__VA_ARGS__); \ BX_MACRO_BLOCK_END #define _BX_WARN(_condition, _format, ...) \