- err = json_object_to_file (session->config->configfile, AFBConfig);
- json_object_put (AFBConfig); // decrease reference count to free the json object
- if (err < 0) {
- fprintf(stderr, "AFB: Fail to save config on disk [%s]\n ", session->config->configfile);
- }
-}
-
-
-PUBLIC AFB_session *configInit () {
-
- AFB_session *session;
- AFB_config *config;
- int idx, verbosesav;
-
-
- 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;
-
- AFBJsonType = 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++) {
- AFB_Error[idx].level = idx;
- AFB_Error[idx].label = ERROR_LABEL [idx];
- AFB_Error[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 = AFB_Error[level].json;
- json_object_get (target);
-
- return (target);
-}
-
-// get AFB object type with adequate usage count
-PUBLIC json_object *jsonNewjtype (void) {
- json_object_get (AFBJsonType); // increase reference count
- return (AFBJsonType);
-}
-
-// 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]: ", AFB_Error [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));
- }