Skip to content

Commit

Permalink
Error logs with file name and line in debug build
Browse files Browse the repository at this point in the history
  • Loading branch information
arkq committed Jan 9, 2021
1 parent 74b4d2f commit b4cf31b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 34 deletions.
33 changes: 5 additions & 28 deletions src/shared/log.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* BlueALSA - log.c
* Copyright (c) 2016-2020 Arkadiusz Bokowy
* Copyright (c) 2016-2021 Arkadiusz Bokowy
*
* This file is a part of bluez-alsa.
*
Expand Down Expand Up @@ -95,36 +95,13 @@ static void vlog(int priority, const char *format, va_list ap) {

}

void error(const char *format, ...) {
void log_message(int priority, const char *format, ...) {
va_list ap;
va_start(ap, format);
vlog(LOG_ERR, format, ap);
vlog(priority, format, ap);
va_end(ap);
}

void warn(const char *format, ...) {
va_list ap;
va_start(ap, format);
vlog(LOG_WARNING, format, ap);
va_end(ap);
}

void info(const char *format, ...) {
va_list ap;
va_start(ap, format);
vlog(LOG_INFO, format, ap);
va_end(ap);
}

#if DEBUG
void _debug(const char *format, ...) {
va_list ap;
va_start(ap, format);
vlog(LOG_DEBUG, format, ap);
va_end(ap);
}
#endif

#if DEBUG
/**
* Dump current thread's call stack. */
Expand Down Expand Up @@ -167,7 +144,7 @@ void callstackdump(const char *label) {

#endif

_debug("%s: %s", label, buffer);
log_message(LOG_DEBUG, "%s: %s", label, buffer);

}
#endif
Expand All @@ -189,7 +166,7 @@ void hexdump(const char *label, const void *mem, size_t len) {
mem = ((unsigned char *)mem) + 1;
}

_debug("%s:%s", label, buf);
log_message(LOG_DEBUG, "%s:%s", label, buf);
free(buf);
}
#endif
16 changes: 10 additions & 6 deletions src/shared/log.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* BlueALSA - log.h
* Copyright (c) 2016-2020 Arkadiusz Bokowy
* Copyright (c) 2016-2021 Arkadiusz Bokowy
*
* This file is a part of bluez-alsa.
*
Expand All @@ -17,6 +17,7 @@

#include <stdbool.h>
#include <stddef.h>
#include <syslog.h>

#if DEBUG_TIME
# define BLUEALSA_LOGTIME true
Expand All @@ -25,14 +26,17 @@
#endif

void log_open(const char *ident, bool syslog, bool time);
void error(const char *format, ...) __attribute__ ((format(printf, 1, 2)));
void warn(const char *format, ...) __attribute__ ((format(printf, 1, 2)));
void info(const char *format, ...) __attribute__ ((format(printf, 1, 2)));
void log_message(int priority, const char *format, ...) __attribute__ ((format(printf, 2, 3)));

#if DEBUG
void _debug(const char *format, ...) __attribute__ ((format(printf, 1, 2)));
# define debug(M, ...) _debug("%s:%d: " M, __FILE__, __LINE__, ## __VA_ARGS__)
# define error(M, ...) log_message(LOG_ERR, "%s:%d: " M, __FILE__, __LINE__, ## __VA_ARGS__)
# define warn(M, ...) log_message(LOG_WARNING, "%s:%d: " M, __FILE__, __LINE__, ## __VA_ARGS__)
# define info(M, ...) log_message(LOG_INFO, "%s:%d: " M, __FILE__, __LINE__, ## __VA_ARGS__)
# define debug(M, ...) log_message(LOG_DEBUG, "%s:%d: " M, __FILE__, __LINE__, ## __VA_ARGS__)
#else
# define error(M, ...) log_message(LOG_ERR, M, ## __VA_ARGS__)
# define warn(M, ...) log_message(LOG_WARNING, M, ## __VA_ARGS__)
# define info(M, ...) log_message(LOG_INFO, M, ## __VA_ARGS__)
# define debug(M, ...) do {} while (0)
#endif

Expand Down

0 comments on commit b4cf31b

Please sign in to comment.