diff --git a/amicli.c b/amicli.c index 25d282c..9163b94 100644 --- a/amicli.c +++ b/amicli.c @@ -24,6 +24,8 @@ #include +static int debug = 0; + /* * This is a simple program that will use C AMI to log in, * and then accepts AMI commands on STDIN @@ -37,10 +39,12 @@ static void ami_callback(struct ami_session *ami, struct ami_event *event) { const char *eventname = ami_keyvalue(event, "Event"); (void) ami; - printf("(Callback) Event Received: %s\n", eventname); -#ifdef PRINT_EVENTS - ami_dump_event(event); /* Do something with event */ -#endif + if (debug >= 1) { + printf("(Callback) Event Received: %s\n", eventname); + } + if (debug >= 2) { + ami_dump_event(event); /* Do something with event */ + } ami_event_free(event); /* Free event when done with it */ } @@ -133,7 +137,9 @@ static int single_ami_command(struct ami_session *ami) fprintf(stderr, "AMI action '%s' failed\n", action); return -1; } - ami_dump_response(resp); + if (debug >= 1) { + ami_dump_response(resp); + } ami_resp_free(resp); /* Free response when done with it (just LF or CR LF) */ return 1; } @@ -145,7 +151,6 @@ int main(int argc,char *argv[]) char ami_host[92] = "127.0.0.1"; /* Default to localhost */ char ami_username[64] = ""; char ami_password[64] = ""; - int debug = 0; struct ami_session *ami; while ((c = getopt(argc, argv, getopt_settings)) != (char) -1) { @@ -198,11 +203,14 @@ int main(int argc,char *argv[]) return -1; } + ami_set_debug_level(NULL, debug); + ami_set_debug(NULL, STDERR_FILENO); ami = ami_connect(ami_host, 0, ami_callback, ami_disconnect_callback); if (!ami) { fprintf(stderr, "Failed to connect to %s\n", ami_host); return -1; } + ami_set_debug_level(ami, debug); ami_set_debug(ami, STDERR_FILENO); ami_set_discard_on_failure(ami, 0); @@ -211,7 +219,9 @@ int main(int argc,char *argv[]) return -1; } - fprintf(stderr, "*** Successfully logged in to AMI on %s (%s) ***\n", ami_host, ami_username); + if (debug >= 1) { + fprintf(stderr, "*** Successfully logged in to AMI on %s (%s) ***\n", ami_host, ami_username); + } while (single_ami_command(ami) > 0); ami_disconnect(ami); ami_destroy(ami); diff --git a/cami.c b/cami.c index 4177373..0e6f0fe 100644 --- a/cami.c +++ b/cami.c @@ -582,6 +582,11 @@ void ami_set_debug(struct ami_session *ami, int fd) } } +int ami_debug_level(struct ami_session *ami) +{ + return ami ? ami->debug_level : ami_initial_debug_level; +} + int ami_set_debug_level(struct ami_session *ami, int level) { int old_level = ami ? ami->debug_level : ami_initial_debug_level; diff --git a/include/cami.h b/include/cami.h index 9c5d982..1c63c3a 100644 --- a/include/cami.h +++ b/include/cami.h @@ -56,9 +56,15 @@ struct ami_response { */ void ami_set_debug(struct ami_session *ami, int fd); +/*! + * \brief Get debug logging level + * \param ami The AMI session. If NULL, sets the debug level prior to session creation (e.g. in ami_connect) + * \retval Current debug Level. 0 will disable logging, 10 is the most granular. Default is 0. + */ +int ami_debug_level(struct ami_session *ami); + /*! * \brief Set debug logging level - * \param ami * \param ami The AMI session. If NULL, sets the debug level prior to session creation (e.g. in ami_connect) * \param level Level between 0 and 10. 0 will disable logging, 10 is the most granular. Default is 0. * \note A log level of 1 is recommended for production use: this will log all errors and warnings. Use a greater log level for debugging.