b3Logging.cpp 3.8 KB

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