JSONDebug.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #include "JSONDebug.h"
  2. #ifdef JSON_DEBUG
  3. #ifdef JSON_STDERROR
  4. #include <iostream> //need std::cerr
  5. #else
  6. #include "JSONSingleton.h"
  7. //otherwise, use a callback to tell the end user what happened
  8. json_error_callback_t JSONDebug::register_callback(json_error_callback_t callback) json_nothrow {
  9. json_error_callback_t res = JSONSingleton<json_error_callback_t>::get();
  10. JSONSingleton<json_error_callback_t>::set(callback);
  11. return res;
  12. }
  13. #endif
  14. //Something went wrong or an assert failed
  15. void JSONDebug::_JSON_FAIL(const json_string & msg) json_nothrow {
  16. #ifdef JSON_STDERROR //no callback, just use stderror
  17. #ifndef JSON_UNICODE
  18. std::cerr << msg << std::endl;
  19. #else
  20. std::cerr << std::string(msg.begin(), msg.end()) << std::endl;
  21. #endif
  22. #else
  23. if (json_error_callback_t ErrorCallback = JSONSingleton<json_error_callback_t>::get()){ //only do anything if the callback is registered
  24. #ifdef JSON_LIBRARY
  25. ErrorCallback(msg.c_str());
  26. #else
  27. ErrorCallback(msg);
  28. #endif
  29. }
  30. #endif
  31. }
  32. //asserts that condition is true, more useful than cassert because it lets you keep going
  33. void JSONDebug::_JSON_ASSERT(bool condition, const json_string & msg) json_nothrow {
  34. if (json_unlikely(!condition)){
  35. _JSON_FAIL(msg);
  36. }
  37. }
  38. #endif