Browse Source

Removing duplicated code.

Branimir Karadžić 9 years ago
parent
commit
053f9303ec
4 changed files with 89 additions and 94 deletions
  1. 10 0
      include/bx/debug.h
  2. 77 1
      src/debug.cpp
  3. 0 86
      tests/dbg.cpp
  4. 2 7
      tests/dbg.h

+ 10 - 0
include/bx/debug.h

@@ -7,6 +7,7 @@
 #define BX_DEBUG_H_HEADER_GUARD
 #define BX_DEBUG_H_HEADER_GUARD
 
 
 #include "bx.h"
 #include "bx.h"
+#include <stdarg.h> // va_list
 
 
 namespace bx
 namespace bx
 {
 {
@@ -16,6 +17,15 @@ namespace bx
 	///
 	///
 	void debugOutput(const char* _out);
 	void debugOutput(const char* _out);
 
 
+	///
+	void debugPrintfVargs(const char* _format, va_list _argList);
+
+	///
+	void debugPrintf(const char* _format, ...);
+
+	///
+	void debugPrintfData(const void* _data, uint32_t _size, const char* _format, ...);
+
 } // namespace bx
 } // namespace bx
 
 
 #endif // BX_DEBUG_H_HEADER_GUARD
 #endif // BX_DEBUG_H_HEADER_GUARD

+ 77 - 1
src/debug.cpp

@@ -3,7 +3,9 @@
  * License: https://github.com/bkaradzic/bx#license-bsd-2-clause
  * License: https://github.com/bkaradzic/bx#license-bsd-2-clause
  */
  */
 
 
-#include <bx/bx.h>
+#include <bx/debug.h>
+#include <bx/string.h> // isPrint
+#include <inttypes.h>  // PRIx*
 
 
 #if BX_PLATFORM_ANDROID
 #if BX_PLATFORM_ANDROID
 #	include <android/log.h>
 #	include <android/log.h>
@@ -64,4 +66,78 @@ namespace bx
 #endif // BX_PLATFORM_
 #endif // BX_PLATFORM_
 	}
 	}
 
 
+	void debugPrintfVargs(const char* _format, va_list _argList)
+	{
+		char temp[8192];
+		char* out = temp;
+		int32_t len = vsnprintf(out, sizeof(temp), _format, _argList);
+		if ( (int32_t)sizeof(temp) < len)
+		{
+			out = (char*)alloca(len+1);
+			len = vsnprintf(out, len, _format, _argList);
+		}
+		out[len] = '\0';
+		debugOutput(out);
+	}
+
+	void debugPrintf(const char* _format, ...)
+	{
+		va_list argList;
+		va_start(argList, _format);
+		debugPrintfVargs(_format, argList);
+		va_end(argList);
+	}
+
+#define DBG_ADDRESS "%" PRIxPTR
+
+	void debugPrintfData(const void* _data, uint32_t _size, const char* _format, ...)
+	{
+#define HEX_DUMP_WIDTH 16
+#define HEX_DUMP_SPACE_WIDTH 48
+#define HEX_DUMP_FORMAT "%-" BX_STRINGIZE(HEX_DUMP_SPACE_WIDTH) "." BX_STRINGIZE(HEX_DUMP_SPACE_WIDTH) "s"
+
+		va_list argList;
+		va_start(argList, _format);
+		debugPrintfVargs(_format, argList);
+		va_end(argList);
+
+		debugPrintf("\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)
+			{
+				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';
+					debugPrintf("\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';
+				debugPrintf("\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
+	}
+
 } // namespace bx
 } // namespace bx

+ 0 - 86
tests/dbg.cpp

@@ -1,86 +0,0 @@
-/*
- * Copyright 2011-2017 Branimir Karadzic. All rights reserved.
- * License: https://github.com/bkaradzic/bx#license-bsd-2-clause
- */
-
-#include <stdio.h>
-#include <stdint.h>
-#include <inttypes.h>
-
-#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] = bx::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
tests/dbg.h

@@ -6,16 +6,11 @@
 #ifndef DBG_H_HEADER_GUARD
 #ifndef DBG_H_HEADER_GUARD
 #define 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) DBG_STRINGIZE_(_x)
 #define DBG_STRINGIZE_(_x) #_x
 #define DBG_STRINGIZE_(_x) #_x
 #define DBG_FILE_LINE_LITERAL "" __FILE__ "(" DBG_STRINGIZE(__LINE__) "): "
 #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
 #endif // DBG_H_HEADER_GUARD