ffx_assert.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // This file is part of the FidelityFX SDK.
  2. //
  3. // Copyright (c) 2022-2023 Advanced Micro Devices, Inc. All rights reserved.
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. #include "ffx_assert.h"
  22. #include <stdlib.h> // for malloc()
  23. #ifdef _WIN32
  24. #ifndef WIN32_LEAN_AND_MEAN
  25. #define WIN32_LEAN_AND_MEAN
  26. #endif
  27. #include <windows.h> // required for OutputDebugString()
  28. #include <stdio.h> // required for sprintf_s
  29. #endif // #ifndef _WIN32
  30. static FfxAssertCallback s_assertCallback;
  31. // set the printing callback function
  32. void ffxAssertSetPrintingCallback(FfxAssertCallback callback)
  33. {
  34. s_assertCallback = callback;
  35. return;
  36. }
  37. // implementation of assert reporting
  38. bool ffxAssertReport(const char* file, int32_t line, const char* condition, const char* message)
  39. {
  40. if (!file) {
  41. return true;
  42. }
  43. #ifdef _WIN32
  44. // form the final assertion string and output to the TTY.
  45. const size_t bufferSize = static_cast<size_t>(snprintf(nullptr, 0, "%s(%d): ASSERTION FAILED. %s\n", file, line, message ? message : condition)) + 1;
  46. char* tempBuf = static_cast<char*>(malloc(bufferSize));
  47. if (!tempBuf) {
  48. return true;
  49. }
  50. if (!message) {
  51. sprintf_s(tempBuf, bufferSize, "%s(%d): ASSERTION FAILED. %s\n", file, line, condition);
  52. } else {
  53. sprintf_s(tempBuf, bufferSize, "%s(%d): ASSERTION FAILED. %s\n", file, line, message);
  54. }
  55. if (!s_assertCallback) {
  56. OutputDebugStringA(tempBuf);
  57. } else {
  58. s_assertCallback(tempBuf);
  59. }
  60. // free the buffer.
  61. free(tempBuf);
  62. #else
  63. FFX_UNUSED(line);
  64. FFX_UNUSED(condition);
  65. FFX_UNUSED(message);
  66. #endif
  67. return true;
  68. }