Commit af151784 authored by Pascal Bühler's avatar Pascal Bühler

Add log handler api to receive log messages from libSRTP

This address the second half of #230
parent 086bd298
......@@ -82,6 +82,10 @@ typedef enum {
srtp_err_status_t srtp_err_reporting_init();
typedef void (srtp_err_report_handler_func_t)(srtp_err_reporting_level_t level, const char * msg);
srtp_err_status_t srtp_install_err_report_handler(srtp_err_report_handler_func_t func);
/*
* srtp_err_report reports a 'printf' formatted error
* string, followed by a an arg list. The level argument
......
......@@ -67,12 +67,32 @@ srtp_err_status_t srtp_err_reporting_init ()
return srtp_err_status_ok;
}
static srtp_err_report_handler_func_t * srtp_err_report_handler = NULL;
srtp_err_status_t srtp_install_err_report_handler(srtp_err_report_handler_func_t func)
{
srtp_err_report_handler = func;
}
void srtp_err_report (srtp_err_reporting_level_t level, const char *format, ...)
{
va_list args;
va_start(args, format);
if (srtp_err_file != NULL) {
va_start(args, format);
vfprintf(srtp_err_file, format, args);
va_end(args);
}
if (srtp_err_report_handler != NULL) {
va_start(args, format);
char msg[512];
if (vsnprintf(msg, sizeof(msg), format, args) > 0) {
/* strip trailing \n, callback should not have one */
size_t l = strlen(msg);
if (l && msg[l-1] == '\n') {
msg[l-1] = '\0';
}
srtp_err_report_handler(level, msg);
}
va_end(args);
}
va_end(args);
}
......@@ -1680,6 +1680,46 @@ srtp_err_status_t srtp_set_debug_module(char *mod_name, int v);
*/
srtp_err_status_t srtp_list_debug_modules(void);
/**
* @brief srtp_log_level_t defines log levels.
*
* The enumeration srtp_log_level_t defines log levels reported
* in the srtp_log_handler_func_t.
*
*/
typedef enum {
srtp_log_level_error, /**< log level is reporting an error message */
srtp_log_level_warning, /**< log level is reporting a warning message */
srtp_log_level_info, /**< log level is reporting an info message */
srtp_log_level_debug /**< log level is reporting a debug message */
} srtp_log_level_t;
/**
* @brief srtp_log_handler_func_t is the function prototype for
* the log handler.
*
* The typedef srtp_event_handler_func_t is the prototype for the
* event handler function. It has as srtp_log_level_t and log
* message as arguments.
* There can only be a single, global handler for all log messages in
* libSRTP.
*/
typedef void (srtp_log_handler_func_t)(srtp_log_level_t level, const char * msg);
/**
* @brief sets the log handler to the function supplied by the caller.
*
* The function call srtp_install_log_handler(func) sets the log
* handler function to the value func. The value NULL is acceptable
* as an argument; in this case, log messages will be ignored.
* This function can be called before srtp_init() inorder to capture
* any logging during start up.
*
* @param func is a pointer to a fuction of type srtp_log_handler_func_t.
* This function will be used by libSRTP to output log messages.
*/
srtp_err_status_t srtp_install_log_handler(srtp_log_handler_func_t func);
/**
* @brief srtp_get_protect_trailer_length(session, use_mki, mki_index, length)
*
......
......@@ -4322,3 +4322,44 @@ srtp_err_status_t srtp_list_debug_modules(void)
return srtp_crypto_kernel_list_debug_modules();
}
/*
* srtp_log_handler is a global variable holding a pointer to the
* log handler function; this function is called for any log
* output.
*/
static srtp_log_handler_func_t *srtp_log_handler = NULL;
void srtp_err_handler(srtp_err_reporting_level_t level, const char * msg)
{
if (srtp_log_handler) {
srtp_log_level_t log_level;
switch(level) {
case srtp_err_level_error: log_level = srtp_log_level_error; break;
case srtp_err_level_warning: log_level = srtp_log_level_warning; break;
case srtp_err_level_info: log_level = srtp_log_level_info; break;
case srtp_err_level_debug: log_level = srtp_log_level_debug; break;
}
srtp_log_handler(log_level, msg);
}
}
srtp_err_status_t srtp_install_log_handler(srtp_log_handler_func_t func)
{
/*
* note that we accept NULL arguments intentionally - calling this
* function with a NULL arguments removes a log handler that's
* been previously installed
*/
if (srtp_log_handler) {
srtp_install_err_report_handler(NULL);
}
srtp_log_handler = func;
if (srtp_log_handler) {
srtp_install_err_report_handler(srtp_err_handler);
}
return srtp_err_status_ok;
}
......@@ -165,16 +165,30 @@ srtp_master_key_t *test_keys[2] = {
void
usage (char *prog_name)
{
printf("usage: %s [ -t ][ -c ][ -v ][-d <debug_module> ]* [ -l ]\n"
printf("usage: %s [ -t ][ -c ][ -v ][ -o ][-d <debug_module> ]* [ -l ]\n"
" -t run timing test\n"
" -r run rejection timing test\n"
" -c run codec timing test\n"
" -v run validation tests\n"
" -o output logging to stdout\n"
" -d <mod> turn on debugging module <mod>\n"
" -l list debugging modules\n", prog_name);
exit(1);
}
void
log_handler (srtp_log_level_t level, const char * msg)
{
char level_char = '?';
switch(level) {
case srtp_log_level_error: level_char = 'e'; break;
case srtp_log_level_warning: level_char = 'w'; break;
case srtp_log_level_info: level_char = 'i'; break;
case srtp_log_level_debug: level_char = 'd'; break;
}
printf("SRTP-LOG [%c]: %s\n", level_char, msg);
}
/*
* The policy_array is a null-terminated array of policy structs. it
* is declared at the end of this file
......@@ -208,6 +222,7 @@ main (int argc, char *argv[])
unsigned do_codec_timing = 0;
unsigned do_validation = 0;
unsigned do_list_mods = 0;
unsigned do_log_stdout = 0;
srtp_err_status_t status;
/*
......@@ -238,7 +253,7 @@ main (int argc, char *argv[])
/* process input arguments */
while (1) {
q = getopt_s(argc, argv, "trcvld:");
q = getopt_s(argc, argv, "trcvold:");
if (q == -1) {
break;
}
......@@ -255,11 +270,14 @@ main (int argc, char *argv[])
case 'v':
do_validation = 1;
break;
case 'o':
do_log_stdout = 1;
break;
case 'l':
do_list_mods = 1;
break;
case 'd':
status = srtp_crypto_kernel_set_debug_module(optarg_s, 1);
status = srtp_set_debug_module(optarg_s, 1);
if (status) {
printf("error: set debug module (%s) failed\n", optarg_s);
exit(1);
......@@ -275,8 +293,16 @@ main (int argc, char *argv[])
usage(argv[0]);
}
if (do_log_stdout) {
status = srtp_install_log_handler(log_handler);
if (status) {
printf("error: install log handler failed\n");
exit(1);
}
}
if (do_list_mods) {
status = srtp_crypto_kernel_list_debug_modules();
status = srtp_list_debug_modules();
if (status) {
printf("error: list of debug modules failed\n");
exit(1);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment