diff options
| -rw-r--r-- | common/log.c | 51 | ||||
| -rw-r--r-- | include/libseat.h | 25 | ||||
| -rw-r--r-- | include/log.h | 10 | 
3 files changed, 60 insertions, 26 deletions
| diff --git a/common/log.c b/common/log.c index 7e5e8d4..646480d 100644 --- a/common/log.c +++ b/common/log.c @@ -10,7 +10,10 @@  const long NSEC_PER_SEC = 1000000000; +static void log_stderr(enum libseat_log_level level, const char *fmt, va_list args); +  static enum libseat_log_level current_log_level; +static libseat_log_func current_log_handler = log_stderr;  static struct timespec start_time = {-1, -1};  static bool colored = false; @@ -37,22 +40,7 @@ static void timespec_sub(struct timespec *r, const struct timespec *a, const str  	}  } -void log_init(enum libseat_log_level level) { -	if (start_time.tv_sec >= 0) { -		return; -	} -	clock_gettime(CLOCK_MONOTONIC, &start_time); -	current_log_level = level; -	colored = isatty(STDERR_FILENO); -} - -void _logf(enum libseat_log_level level, const char *fmt, ...) { -	int stored_errno = errno; -	va_list args; -	if (level > current_log_level) { -		return; -	} - +static void log_stderr(enum libseat_log_level level, const char *fmt, va_list args) {  	struct timespec ts = {0};  	clock_gettime(CLOCK_MONOTONIC, &ts);  	timespec_sub(&ts, &ts, &start_time); @@ -71,10 +59,37 @@ void _logf(enum libseat_log_level level, const char *fmt, ...) {  	fprintf(stderr, "%02d:%02d:%02d.%03ld %s ", (int)(ts.tv_sec / 60 / 60),  		(int)(ts.tv_sec / 60 % 60), (int)(ts.tv_sec % 60), ts.tv_nsec / 1000000, prefix); -	va_start(args, fmt);  	vfprintf(stderr, fmt, args); -	va_end(args);  	fprintf(stderr, "%s", postfix); +} + +void log_init(enum libseat_log_level level) { +	if (start_time.tv_sec >= 0) { +		return; +	} +	clock_gettime(CLOCK_MONOTONIC, &start_time); +	current_log_level = level; +	colored = isatty(STDERR_FILENO); +} + +void _logf(enum libseat_log_level level, const char *fmt, ...) { +	int stored_errno = errno; +	va_list args; +	if (level > current_log_level) { +		return; +	} + +	va_start(args, fmt); +	current_log_handler(level, fmt, args); +	va_end(args); +  	errno = stored_errno;  } + +void libseat_set_log_handler(libseat_log_func handler) { +	if (handler == NULL) { +		handler = log_stderr; +	} +	current_log_handler = handler; +} diff --git a/include/libseat.h b/include/libseat.h index ba8a1ac..be19c43 100644 --- a/include/libseat.h +++ b/include/libseat.h @@ -135,4 +135,29 @@ int libseat_get_fd(struct libseat *seat);   */  int libseat_dispatch(struct libseat *seat, int timeout); +/* + * A log level. + */ +enum libseat_log_level { +	LIBSEAT_LOG_LEVEL_SILENT = 0, +	LIBSEAT_LOG_LEVEL_ERROR = 1, +	LIBSEAT_LOG_LEVEL_INFO = 2, +	LIBSEAT_LOG_LEVEL_DEBUG = 3, +	LIBSEAT_LOG_LEVEL_LAST, +}; + +/* + * A function that handles log messages. + */ +typedef void (*libseat_log_func)(enum libseat_log_level level, const char *format, va_list args); + +/* + * Sets the handler for log messages. + * + * The handler will be called for each message whose level is lower or equal + * to the current log level. If the handler is NULL, the handler is reset to + * the default. + */ +void libseat_set_log_handler(libseat_log_func handler); +  #endif diff --git a/include/log.h b/include/log.h index ff354f8..d4bfd89 100644 --- a/include/log.h +++ b/include/log.h @@ -3,6 +3,8 @@  #include <stdarg.h> +#include "libseat.h" +  #ifdef __GNUC__  #define ATTRIB_PRINTF(start, end) __attribute__((format(printf, start, end)))  #else @@ -41,14 +43,6 @@  #define log_debug(str)  #endif -enum libseat_log_level { -	LIBSEAT_LOG_LEVEL_SILENT = 0, -	LIBSEAT_LOG_LEVEL_ERROR = 1, -	LIBSEAT_LOG_LEVEL_INFO = 2, -	LIBSEAT_LOG_LEVEL_DEBUG = 3, -	LIBSEAT_LOG_LEVEL_LAST, -}; -  void log_init(enum libseat_log_level level);  void _logf(enum libseat_log_level level, const char *fmt, ...) ATTRIB_PRINTF(2, 3); | 
