Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal to add hts_log callback function and hts_log_set_logger() #1414

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions hts.c
Original file line number Diff line number Diff line change
Expand Up @@ -4754,19 +4754,32 @@ static char get_severity_tag(enum htsLogLevel severity)
return '*';
}

static hts_log_func *hts_logger = NULL;
static void *hts_logger_data = NULL;

void hts_log_set_logger(hts_log_func *logger, void *data)
{
hts_logger = logger;
hts_logger_data = data;
}

void hts_log(enum htsLogLevel severity, const char *context, const char *format, ...)
{
int save_errno = errno;
if (severity <= hts_verbose) {
va_list argptr;

fprintf(stderr, "[%c::%s] ", get_severity_tag(severity), context);

va_start(argptr, format);
vfprintf(stderr, format, argptr);
if (hts_logger) {
hts_logger(hts_logger_data, severity, get_severity_tag(severity), context, format, argptr);
}
else {
fprintf(stderr, "[%c::%s] ", get_severity_tag(severity), context);
vfprintf(stderr, format, argptr);
fprintf(stderr, "\n");
}
va_end(argptr);

fprintf(stderr, "\n");
}
errno = save_errno;
}
6 changes: 6 additions & 0 deletions htslib/hts_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ SOFTWARE.
#ifndef HTS_LOG_H
#define HTS_LOG_H

#include <stdarg.h>
#include "hts_defs.h"

#ifdef __cplusplus
Expand Down Expand Up @@ -89,6 +90,11 @@ HTS_FORMAT(HTS_PRINTF_FMT, 3, 4);
/*! Logs an event with severity HTS_LOG_TRACE and default context. Parameters: format, ... */
#define hts_log_trace(...) hts_log(HTS_LOG_TRACE, __func__, __VA_ARGS__)

typedef void hts_log_func(void *data, enum htsLogLevel severity, char severity_tag, const char *context, const char *format, va_list args);

HTSLIB_EXPORT
void hts_log_set_logger(hts_log_func *logger, void *data);

#ifdef __cplusplus
}
#endif
Expand Down
32 changes: 32 additions & 0 deletions test/sam.c
Original file line number Diff line number Diff line change
Expand Up @@ -1874,6 +1874,37 @@ static void test_mempolicy(void)
}
}

static void my_logger(void *data, enum htsLogLevel severity, char severity_tag, const char *context, const char *format, va_list args)
{
char msg[200];
int *nptr = (int *) data;
(*nptr)++;

if (severity != HTS_LOG_ERROR) fail("my_logger saw the wrong message");

vsprintf(msg, format, args);
if (strcmp(msg, "YOU SHOULD NOT SEE THIS MESSAGE") != 0)
fail("my_logger message was incorrect: \"%s\"", msg);
}

static void test_logging(void)
{
int old_level = hts_get_log_level();
int n = 0;

hts_set_log_level(HTS_LOG_WARNING);

hts_log_set_logger(my_logger, &n);
hts_log_error("YOU SHOULD NOT SEE %s", "THIS MESSAGE");
if (n != 1) fail("test_logging message one was not redirected");

hts_log_set_logger(NULL, NULL);
hts_log_warning("(expect to see this message)");
if (n != 1) fail("test_logging message two was redirected");

hts_set_log_level(old_level);
}

static void test_bam_set1_minimal()
{
int r;
Expand Down Expand Up @@ -2227,6 +2258,7 @@ int main(int argc, char **argv)
check_big_ref(1);
test_parse_decimal();
test_mempolicy();
test_logging();
set_qname();
for (i = 1; i < argc; i++) faidx1(argv[i]);

Expand Down