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