Skip to content

Commit 8962fc6

Browse files
committed
Improved debugging
- 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 8962fc6

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

amicli.c

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,12 @@ static void ami_callback(struct ami_session *ami, struct ami_event *event)
3737
{
3838
const char *eventname = ami_keyvalue(event, "Event");
3939
(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
40+
if (ami_debug_level(ami) >= 1) {
41+
printf("(Callback) Event Received: %s\n", eventname);
42+
}
43+
if (ami_debug_level(ami) >= 2) {
44+
ami_dump_event(event); /* Do something with event */
45+
}
4446
ami_event_free(event); /* Free event when done with it */
4547
}
4648

@@ -133,7 +135,9 @@ static int single_ami_command(struct ami_session *ami)
133135
fprintf(stderr, "AMI action '%s' failed\n", action);
134136
return -1;
135137
}
136-
ami_dump_response(resp);
138+
if (ami_debug_level(ami) >= 1) {
139+
ami_dump_response(resp);
140+
}
137141
ami_resp_free(resp); /* Free response when done with it (just LF or CR LF) */
138142
return 1;
139143
}
@@ -198,11 +202,14 @@ int main(int argc,char *argv[])
198202
return -1;
199203
}
200204

205+
ami_set_debug_level(NULL, debug);
206+
ami_set_debug(NULL, STDERR_FILENO);
201207
ami = ami_connect(ami_host, 0, ami_callback, ami_disconnect_callback);
202208
if (!ami) {
203209
fprintf(stderr, "Failed to connect to %s\n", ami_host);
204210
return -1;
205211
}
212+
206213
ami_set_debug_level(ami, debug);
207214
ami_set_debug(ami, STDERR_FILENO);
208215
ami_set_discard_on_failure(ami, 0);
@@ -211,7 +218,9 @@ int main(int argc,char *argv[])
211218
return -1;
212219
}
213220

214-
fprintf(stderr, "*** Successfully logged in to AMI on %s (%s) ***\n", ami_host, ami_username);
221+
if (ami_debug_level(ami) >= 1) {
222+
fprintf(stderr, "*** Successfully logged in to AMI on %s (%s) ***\n", ami_host, ami_username);
223+
}
215224
while (single_ami_command(ami) > 0);
216225
ami_disconnect(ami);
217226
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)