1 /*
2 Copyright (c) 2013 Advanced Micro Devices, Inc.
3 
4 This software is provided 'as-is', without any express or implied warranty.
5 In no event will the authors be held liable for any damages arising from the use of this software.
6 Permission is granted to anyone to use this software for any purpose,
7 including commercial applications, and to alter it and redistribute it freely,
8 subject to the following restrictions:
9 
10 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
11 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
12 3. This notice may not be removed or altered from any source distribution.
13 */
14 //Originally written by Erwin Coumans
15 
16 #include "b3Logging.h"
17 
18 #include <stdio.h>
19 #include <stdarg.h>
20 
21 #ifdef _WIN32
22 #include <windows.h>
23 #endif  //_WIN32
24 
b3PrintfFuncDefault(const char * msg)25 void b3PrintfFuncDefault(const char* msg)
26 {
27 #ifdef _WIN32
28 	OutputDebugStringA(msg);
29 #endif
30 	printf("%s", msg);
31 	//is this portable?
32 	fflush(stdout);
33 }
34 
b3WarningMessageFuncDefault(const char * msg)35 void b3WarningMessageFuncDefault(const char* msg)
36 {
37 #ifdef _WIN32
38 	OutputDebugStringA(msg);
39 #endif
40 	printf("%s", msg);
41 	//is this portable?
42 	fflush(stdout);
43 }
44 
b3ErrorMessageFuncDefault(const char * msg)45 void b3ErrorMessageFuncDefault(const char* msg)
46 {
47 #ifdef _WIN32
48 	OutputDebugStringA(msg);
49 #endif
50 	printf("%s", msg);
51 
52 	//is this portable?
53 	fflush(stdout);
54 }
55 
56 static b3PrintfFunc* b3s_printfFunc = b3PrintfFuncDefault;
57 static b3WarningMessageFunc* b3s_warningMessageFunc = b3WarningMessageFuncDefault;
58 static b3ErrorMessageFunc* b3s_errorMessageFunc = b3ErrorMessageFuncDefault;
59 
60 ///The developer can route b3Printf output using their own implementation
b3SetCustomPrintfFunc(b3PrintfFunc * printfFunc)61 void b3SetCustomPrintfFunc(b3PrintfFunc* printfFunc)
62 {
63 	b3s_printfFunc = printfFunc;
64 }
b3SetCustomWarningMessageFunc(b3PrintfFunc * warningMessageFunc)65 void b3SetCustomWarningMessageFunc(b3PrintfFunc* warningMessageFunc)
66 {
67 	b3s_warningMessageFunc = warningMessageFunc;
68 }
b3SetCustomErrorMessageFunc(b3PrintfFunc * errorMessageFunc)69 void b3SetCustomErrorMessageFunc(b3PrintfFunc* errorMessageFunc)
70 {
71 	b3s_errorMessageFunc = errorMessageFunc;
72 }
73 
74 //#define B3_MAX_DEBUG_STRING_LENGTH 2048
75 #define B3_MAX_DEBUG_STRING_LENGTH 32768
76 
b3OutputPrintfVarArgsInternal(const char * str,...)77 void b3OutputPrintfVarArgsInternal(const char* str, ...)
78 {
79 	char strDebug[B3_MAX_DEBUG_STRING_LENGTH] = {0};
80 	va_list argList;
81 	va_start(argList, str);
82 #ifdef _MSC_VER
83 	vsprintf_s(strDebug, B3_MAX_DEBUG_STRING_LENGTH, str, argList);
84 #else
85 	vsnprintf(strDebug, B3_MAX_DEBUG_STRING_LENGTH, str, argList);
86 #endif
87 	(b3s_printfFunc)(strDebug);
88 	va_end(argList);
89 }
b3OutputWarningMessageVarArgsInternal(const char * str,...)90 void b3OutputWarningMessageVarArgsInternal(const char* str, ...)
91 {
92 	char strDebug[B3_MAX_DEBUG_STRING_LENGTH] = {0};
93 	va_list argList;
94 	va_start(argList, str);
95 #ifdef _MSC_VER
96 	vsprintf_s(strDebug, B3_MAX_DEBUG_STRING_LENGTH, str, argList);
97 #else
98 	vsnprintf(strDebug, B3_MAX_DEBUG_STRING_LENGTH, str, argList);
99 #endif
100 	(b3s_warningMessageFunc)(strDebug);
101 	va_end(argList);
102 }
b3OutputErrorMessageVarArgsInternal(const char * str,...)103 void b3OutputErrorMessageVarArgsInternal(const char* str, ...)
104 {
105 	char strDebug[B3_MAX_DEBUG_STRING_LENGTH] = {0};
106 	va_list argList;
107 	va_start(argList, str);
108 #ifdef _MSC_VER
109 	vsprintf_s(strDebug, B3_MAX_DEBUG_STRING_LENGTH, str, argList);
110 #else
111 	vsnprintf(strDebug, B3_MAX_DEBUG_STRING_LENGTH, str, argList);
112 #endif
113 	(b3s_errorMessageFunc)(strDebug);
114 	va_end(argList);
115 }
116 
b3EnterProfileZoneDefault(const char * name)117 void b3EnterProfileZoneDefault(const char* name)
118 {
119 }
b3LeaveProfileZoneDefault()120 void b3LeaveProfileZoneDefault()
121 {
122 }
123 static b3EnterProfileZoneFunc* b3s_enterFunc = b3EnterProfileZoneDefault;
124 static b3LeaveProfileZoneFunc* b3s_leaveFunc = b3LeaveProfileZoneDefault;
b3EnterProfileZone(const char * name)125 void b3EnterProfileZone(const char* name)
126 {
127 	(b3s_enterFunc)(name);
128 }
b3LeaveProfileZone()129 void b3LeaveProfileZone()
130 {
131 	(b3s_leaveFunc)();
132 }
133 
b3SetCustomEnterProfileZoneFunc(b3EnterProfileZoneFunc * enterFunc)134 void b3SetCustomEnterProfileZoneFunc(b3EnterProfileZoneFunc* enterFunc)
135 {
136 	b3s_enterFunc = enterFunc;
137 }
b3SetCustomLeaveProfileZoneFunc(b3LeaveProfileZoneFunc * leaveFunc)138 void b3SetCustomLeaveProfileZoneFunc(b3LeaveProfileZoneFunc* leaveFunc)
139 {
140 	b3s_leaveFunc = leaveFunc;
141 }
142 
143 #ifndef _MSC_VER
144 #undef vsprintf_s
145 #endif
146