Browse Source

Removing duplicated code.

Branimir Karadžić 9 years ago
parent
commit
335c1ae320
3 changed files with 9 additions and 101 deletions
  1. 7 7
      examples/07-callback/callback.cpp
  2. 0 87
      examples/common/entry/dbg.cpp
  3. 2 7
      examples/common/entry/dbg.h

+ 7 - 7
examples/07-callback/callback.cpp

@@ -126,7 +126,7 @@ struct BgfxCallback : public bgfx::CallbackI
 	virtual void fatal(bgfx::Fatal::Enum _code, const char* _str) BX_OVERRIDE
 	{
 		// Something unexpected happened, inform user and bail out.
-		dbgPrintf("Fatal error: 0x%08x: %s", _code, _str);
+		bx::debugPrintf("Fatal error: 0x%08x: %s", _code, _str);
 
 		// Must terminate, continuing will cause crash anyway.
 		abort();
@@ -134,8 +134,8 @@ struct BgfxCallback : public bgfx::CallbackI
 
 	virtual void traceVargs(const char* _filePath, uint16_t _line, const char* _format, va_list _argList) BX_OVERRIDE
 	{
-		dbgPrintf("%s (%d): ", _filePath, _line);
-		dbgPrintfVargs(_format, _argList);
+		bx::debugPrintf("%s (%d): ", _filePath, _line);
+		bx::debugPrintfVargs(_format, _argList);
 	}
 
 	virtual uint32_t cacheReadSize(uint64_t _id) BX_OVERRIDE
@@ -257,7 +257,7 @@ public:
 			{
 				if (BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT >= _align)
 				{
-					dbgPrintf("%s(%d): FREE %p\n", _file, _line, _ptr);
+					bx::debugPrintf("%s(%d): FREE %p\n", _file, _line, _ptr);
 					::free(_ptr);
 					--m_numBlocks;
 				}
@@ -274,7 +274,7 @@ public:
 			if (BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT >= _align)
 			{
 				void* ptr = ::malloc(_size);
-				dbgPrintf("%s(%d): ALLOC %p of %d byte(s)\n", _file, _line, ptr, _size);
+				bx::debugPrintf("%s(%d): ALLOC %p of %d byte(s)\n", _file, _line, ptr, _size);
 				++m_numBlocks;
 				m_maxBlocks = bx::uint32_max(m_maxBlocks, m_numBlocks);
 				return ptr;
@@ -286,7 +286,7 @@ public:
 		if (BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT >= _align)
 		{
 			void* ptr = ::realloc(_ptr, _size);
-			dbgPrintf("%s(%d): REALLOC %p (old %p) of %d byte(s)\n", _file, _line, ptr, _ptr, _size);
+			bx::debugPrintf("%s(%d): REALLOC %p (old %p) of %d byte(s)\n", _file, _line, ptr, _ptr, _size);
 
 			if (NULL == _ptr)
 			{
@@ -302,7 +302,7 @@ public:
 
 	void dumpStats() const
 	{
-		dbgPrintf("Allocator stats: num blocks %d (peak: %d)\n", m_numBlocks, m_maxBlocks);
+		bx::debugPrintf("Allocator stats: num blocks %d (peak: %d)\n", m_numBlocks, m_maxBlocks);
 	}
 
 private:

+ 0 - 87
examples/common/entry/dbg.cpp

@@ -1,87 +0,0 @@
-/*
- * Copyright 2011-2017 Branimir Karadzic. All rights reserved.
- * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
- */
-
-#include <stdio.h>
-#include <stdint.h>
-#include <inttypes.h>
-#include <ctype.h> // isprint
-
-#include "dbg.h"
-#include <bx/string.h>
-#include <bx/debug.h>
-
-void dbgPrintfVargs(const char* _format, va_list _argList)
-{
-	char temp[8192];
-	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';
-	bx::debugOutput(out);
-}
-
-void dbgPrintf(const char* _format, ...)
-{
-	va_list argList;
-	va_start(argList, _format);
-	dbgPrintfVargs(_format, argList);
-	va_end(argList);
-}
-
-#define DBG_ADDRESS "%" PRIxPTR
-
-void dbgPrintfData(const void* _data, uint32_t _size, const char* _format, ...)
-{
-#define HEX_DUMP_WIDTH 16
-#define HEX_DUMP_SPACE_WIDTH 48
-#define HEX_DUMP_FORMAT "%-" DBG_STRINGIZE(HEX_DUMP_SPACE_WIDTH) "." DBG_STRINGIZE(HEX_DUMP_SPACE_WIDTH) "s"
-
-	va_list argList;
-	va_start(argList, _format);
-	dbgPrintfVargs(_format, argList);
-	va_end(argList);
-
-	dbgPrintf("\ndata: " DBG_ADDRESS ", size: %d\n", _data, _size);
-
-	if (NULL != _data)
-	{
-		const uint8_t* data = reinterpret_cast<const uint8_t*>(_data);
-		char hex[HEX_DUMP_WIDTH*3+1];
-		char ascii[HEX_DUMP_WIDTH+1];
-		uint32_t hexPos = 0;
-		uint32_t asciiPos = 0;
-		for (uint32_t ii = 0; ii < _size; ++ii)
-		{
-			bx::snprintf(&hex[hexPos], sizeof(hex)-hexPos, "%02x ", data[asciiPos]);
-			hexPos += 3;
-
-			ascii[asciiPos] = isprint(data[asciiPos]) ? data[asciiPos] : '.';
-			asciiPos++;
-
-			if (HEX_DUMP_WIDTH == asciiPos)
-			{
-				ascii[asciiPos] = '\0';
-				dbgPrintf("\t" DBG_ADDRESS "\t" HEX_DUMP_FORMAT "\t%s\n", data, hex, ascii);
-				data += asciiPos;
-				hexPos = 0;
-				asciiPos = 0;
-			}
-		}
-
-		if (0 != asciiPos)
-		{
-			ascii[asciiPos] = '\0';
-			dbgPrintf("\t" DBG_ADDRESS "\t" HEX_DUMP_FORMAT "\t%s\n", data, hex, ascii);
-		}
-	}
-
-#undef HEX_DUMP_WIDTH
-#undef HEX_DUMP_SPACE_WIDTH
-#undef HEX_DUMP_FORMAT
-}

+ 2 - 7
examples/common/entry/dbg.h

@@ -6,16 +6,11 @@
 #ifndef DBG_H_HEADER_GUARD
 #define DBG_H_HEADER_GUARD
 
-#include <stdarg.h> // va_list
-#include <stdint.h>
+#include <bx/debug.h>
 
 #define DBG_STRINGIZE(_x) DBG_STRINGIZE_(_x)
 #define DBG_STRINGIZE_(_x) #_x
 #define DBG_FILE_LINE_LITERAL "" __FILE__ "(" DBG_STRINGIZE(__LINE__) "): "
-#define DBG(_format, ...) dbgPrintf(DBG_FILE_LINE_LITERAL "" _format "\n", ##__VA_ARGS__)
-
-extern void dbgPrintfVargs(const char* _format, va_list _argList);
-extern void dbgPrintf(const char* _format, ...);
-extern void dbgPrintfData(const void* _data, uint32_t _size, const char* _format, ...);
+#define DBG(_format, ...) bx::debugPrintf(DBG_FILE_LINE_LITERAL "" _format "\n", ##__VA_ARGS__)
 
 #endif // DBG_H_HEADER_GUARD