Skip to content

Commit

Permalink
Android: implement chained log callbacks (buffer and terminal output)
Browse files Browse the repository at this point in the history
Query the libraries' default log handlers before registering the
application's own handler. Queue log messages in the application's
internal buffer _and_ hand them to the default handlers which send
the text to stdout as well.
  • Loading branch information
gsigh authored and uwehermann committed Aug 30, 2018
1 parent 4b92340 commit e2e4a66
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions android/loghandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@

namespace pv {

static sr_log_callback prev_sr_log_cb;
static void *prev_sr_log_cb_data;

#ifdef ENABLE_DECODE
static srd_log_callback prev_srd_log_cb;
static void *prev_srd_log_cb_data;
#endif

int AndroidLogHandler::sr_callback(void *cb_data, int loglevel, const char *format, va_list args)
{
static const int prio[] = {
Expand All @@ -40,11 +48,18 @@ int AndroidLogHandler::sr_callback(void *cb_data, int loglevel, const char *form
[SR_LOG_DBG] = ANDROID_LOG_DEBUG,
[SR_LOG_SPEW] = ANDROID_LOG_VERBOSE,
};
va_list args2;
int ret;

/* This specific log callback doesn't need the void pointer data. */
(void)cb_data;

/* Call the previously registered log callback (library's default). */
va_copy(args2, args);
if (prev_sr_log_cb)
prev_sr_log_cb(prev_sr_log_cb_data, loglevel, format, args2);
va_end(args2);

/* Only output messages of at least the selected loglevel(s). */
if (loglevel > sr_log_loglevel_get())
return SR_OK;
Expand All @@ -70,11 +85,18 @@ int AndroidLogHandler::srd_callback(void *cb_data, int loglevel, const char *for
[SRD_LOG_DBG] = ANDROID_LOG_DEBUG,
[SRD_LOG_SPEW] = ANDROID_LOG_VERBOSE,
};
va_list args2;
int ret;

/* This specific log callback doesn't need the void pointer data. */
(void)cb_data;

/* Call the previously registered log callback (library's default). */
va_copy(args2, args);
if (prev_srd_log_cb)
prev_srd_log_cb(prev_srd_log_cb_data, loglevel, format, args2);
va_end(args2);

/* Only output messages of at least the selected loglevel(s). */
if (loglevel > srd_log_loglevel_get())
return SRD_OK;
Expand All @@ -94,8 +116,10 @@ int AndroidLogHandler::srd_callback(void *cb_data, int loglevel, const char *for

void AndroidLogHandler::install_callbacks()
{
sr_log_callback_get(&prev_sr_log_cb, &prev_sr_log_cb_data);
sr_log_callback_set(sr_callback, nullptr);
#ifdef ENABLE_DECODE
srd_log_callback_get(&prev_srd_log_cb, &prev_srd_log_cb_data);
srd_log_callback_set(srd_callback, nullptr);
#endif
}
Expand Down

0 comments on commit e2e4a66

Please sign in to comment.