Skip to content

Commit 3662256

Browse files
authored
amicli.c: Guard debug events based on debug level (#11)
- Allow one to control the debug levels before creating a session - Update what logs are generated based on the debug level (start off quiet with more info as the debug level is increased)
1 parent b070821 commit 3662256

3 files changed

Lines changed: 29 additions & 8 deletions

File tree

amicli.c

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
#include <cami/cami.h>
2626

27+
static int debug = 0;
28+
2729
/*
2830
* This is a simple program that will use C AMI to log in,
2931
* and then accepts AMI commands on STDIN
@@ -37,10 +39,12 @@ static void ami_callback(struct ami_session *ami, struct ami_event *event)
3739
{
3840
const char *eventname = ami_keyvalue(event, "Event");
3941
(void) ami;
40-
printf("(Callback) Event Received: %s\n", eventname);
41-
#ifdef PRINT_EVENTS
42-
ami_dump_event(event); /* Do something with event */
43-
#endif
42+
if (debug >= 1) {
43+
printf("(Callback) Event Received: %s\n", eventname);
44+
}
45+
if (debug >= 2) {
46+
ami_dump_event(event); /* Do something with event */
47+
}
4448
ami_event_free(event); /* Free event when done with it */
4549
}
4650

@@ -133,7 +137,9 @@ static int single_ami_command(struct ami_session *ami)
133137
fprintf(stderr, "AMI action '%s' failed\n", action);
134138
return -1;
135139
}
136-
ami_dump_response(resp);
140+
if (debug >= 1) {
141+
ami_dump_response(resp);
142+
}
137143
ami_resp_free(resp); /* Free response when done with it (just LF or CR LF) */
138144
return 1;
139145
}
@@ -145,7 +151,6 @@ int main(int argc,char *argv[])
145151
char ami_host[92] = "127.0.0.1"; /* Default to localhost */
146152
char ami_username[64] = "";
147153
char ami_password[64] = "";
148-
int debug = 0;
149154
struct ami_session *ami;
150155

151156
while ((c = getopt(argc, argv, getopt_settings)) != (char) -1) {
@@ -198,11 +203,14 @@ int main(int argc,char *argv[])
198203
return -1;
199204
}
200205

206+
ami_set_debug_level(NULL, debug);
207+
ami_set_debug(NULL, STDERR_FILENO);
201208
ami = ami_connect(ami_host, 0, ami_callback, ami_disconnect_callback);
202209
if (!ami) {
203210
fprintf(stderr, "Failed to connect to %s\n", ami_host);
204211
return -1;
205212
}
213+
206214
ami_set_debug_level(ami, debug);
207215
ami_set_debug(ami, STDERR_FILENO);
208216
ami_set_discard_on_failure(ami, 0);
@@ -211,7 +219,9 @@ int main(int argc,char *argv[])
211219
return -1;
212220
}
213221

214-
fprintf(stderr, "*** Successfully logged in to AMI on %s (%s) ***\n", ami_host, ami_username);
222+
if (debug >= 1) {
223+
fprintf(stderr, "*** Successfully logged in to AMI on %s (%s) ***\n", ami_host, ami_username);
224+
}
215225
while (single_ami_command(ami) > 0);
216226
ami_disconnect(ami);
217227
ami_destroy(ami);

cami.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,11 @@ void ami_set_debug(struct ami_session *ami, int fd)
582582
}
583583
}
584584

585+
int ami_debug_level(struct ami_session *ami)
586+
{
587+
return ami ? ami->debug_level : ami_initial_debug_level;
588+
}
589+
585590
int ami_set_debug_level(struct ami_session *ami, int level)
586591
{
587592
int old_level = ami ? ami->debug_level : ami_initial_debug_level;

include/cami.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,15 @@ struct ami_response {
5656
*/
5757
void ami_set_debug(struct ami_session *ami, int fd);
5858

59+
/*!
60+
* \brief Get debug logging level
61+
* \param ami The AMI session. If NULL, sets the debug level prior to session creation (e.g. in ami_connect)
62+
* \retval Current debug Level. 0 will disable logging, 10 is the most granular. Default is 0.
63+
*/
64+
int ami_debug_level(struct ami_session *ami);
65+
5966
/*!
6067
* \brief Set debug logging level
61-
* \param ami
6268
* \param ami The AMI session. If NULL, sets the debug level prior to session creation (e.g. in ami_connect)
6369
* \param level Level between 0 and 10. 0 will disable logging, 10 is the most granular. Default is 0.
6470
* \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.

0 commit comments

Comments
 (0)