CustomAssert.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright (c) 2008, Power of Two Games LLC
  3. * 2013, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of Power of Two Games LLC nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY POWER OF TWO GAMES LLC ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL POWER OF TWO GAMES LLC BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "CustomAssert.h"
  28. #include <cstdio>
  29. #include <cstdarg>
  30. namespace assert_ns
  31. {
  32. namespace
  33. {
  34. Assert::FailBehavior DefaultHandler(const char* condition,
  35. const char* msg,
  36. const char* file,
  37. const int line)
  38. {
  39. std::printf("%s(%d): Assert Failure: ", file, line);
  40. if (condition != NULL)
  41. std::printf("'%s' ", condition);
  42. if (msg != NULL)
  43. std::printf("%s", msg);
  44. std::printf("\n");
  45. return Assert::Halt;
  46. }
  47. Assert::Handler& GetAssertHandlerInstance()
  48. {
  49. static Assert::Handler s_handler = &DefaultHandler;
  50. return s_handler;
  51. }
  52. }
  53. Assert::Handler Assert::GetHandler()
  54. {
  55. return GetAssertHandlerInstance();
  56. }
  57. void Assert::SetHandler(Assert::Handler newHandler)
  58. {
  59. GetAssertHandlerInstance() = newHandler;
  60. }
  61. Assert::FailBehavior Assert::ReportFailure(const char* condition,
  62. const char* file,
  63. const int line,
  64. const char* msg, ...)
  65. {
  66. const char* message = NULL;
  67. if (msg != NULL)
  68. {
  69. char messageBuffer[1024];
  70. {
  71. va_list args;
  72. va_start(args, msg);
  73. #if defined(_MSC_VER)
  74. vsnprintf_s(messageBuffer, 1024, 1024, msg, args);
  75. #else
  76. vsnprintf(messageBuffer, 1024, msg, args);
  77. #endif
  78. va_end(args);
  79. }
  80. message = messageBuffer;
  81. }
  82. return GetAssertHandlerInstance()(condition, message, file, line);
  83. }
  84. }