- session = malloc (sizeof (AFB_session));
- memset (session,0, sizeof (AFB_session));
-
- // create config handle
- config = malloc (sizeof (AFB_config));
- memset (config,0, sizeof (AFB_config));
-
- // stack config handle into session
- session->config = config;
-
- jTypeStatic = json_object_new_string ("AFB_message");
-
- // initialise JSON constant messages and increase reference count to make them permanent
- verbosesav = verbose;
- verbose = 0; // run initialisation in silent mode
-
-
- for (idx = 0; idx <= AFB_SUCCESS; idx++) {
- AFBerr[idx].level = idx;
- AFBerr[idx].label = ERROR_LABEL [idx];
- AFBerr[idx].json = jsonNewMessage (idx, NULL);
- }
- verbose = verbosesav;
-
- return (session);
-}
-
-
-// get JSON object from error level and increase its reference count
-PUBLIC json_object *jsonNewStatus (AFB_error level) {
-
- json_object *target = AFBerr[level].json;
- json_object_get (target);
-
- return (target);
-}
-
-// get AFB object type with adequate usage count
-PUBLIC json_object *jsonNewjtype (void) {
- json_object_get (jTypeStatic); // increase reference count
- return (jTypeStatic);
-}
-
-// build an ERROR message and return it as a valid json object
-PUBLIC json_object *jsonNewMessage (AFB_error level, char* format, ...) {
- static int count = 0;
- json_object * AFBResponse;
- va_list args;
- char message [512];
-
- // format message
- if (format != NULL) {
- va_start(args, format);
- vsnprintf (message, sizeof (message), format, args);
- va_end(args);
- }
-
- AFBResponse = json_object_new_object();
- json_object_object_add (AFBResponse, "jtype", jsonNewjtype ());
- json_object_object_add (AFBResponse, "status" , json_object_new_string (ERROR_LABEL[level]));
- if (format != NULL) {
- json_object_object_add (AFBResponse, "info" , json_object_new_string (message));
- }
- if (verbose) {
- fprintf (stderr, "AFB:%-6s [%3d]: ", AFBerr [level].label, count++);
- if (format != NULL) {
- fprintf (stderr, "%s", message);
- } else {
- fprintf (stderr, "No Message");
- }
- fprintf (stderr, "\n");
- }
-
- return (AFBResponse);
-}
-
-// Dump a message on stderr
-PUBLIC void jsonDumpObject (json_object * jObject) {
-
- if (verbose) {
- fprintf (stderr, "AFB:dump [%s]\n", json_object_to_json_string(jObject));
- }