summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2015-11-25 03:40:12 +0100
committerMatthias Schiffer <mschiffer@universe-factory.net>2015-11-25 03:40:12 +0100
commit281fbb005702da662e1658cb1bb4135f66a447bf (patch)
tree4370d46d4b08d06b7bc90832551c679a090c6e2a
parent5b1d1a36df766dd60eb68b24a5edf4229b68e564 (diff)
downloadfastd-281fbb005702da662e1658cb1bb4135f66a447bf.tar
fastd-281fbb005702da662e1658cb1bb4135f66a447bf.zip
log: clean up fastd_logf
Also, return early when no log destination would get the message anyways.
-rw-r--r--src/log.c22
1 files changed, 12 insertions, 10 deletions
diff --git a/src/log.c b/src/log.c
index bd38821..0b319d3 100644
--- a/src/log.c
+++ b/src/log.c
@@ -257,9 +257,13 @@ static inline int get_syslog_level(fastd_loglevel_t log_level) {
/** printf-like function handling different conversion specifiers and using the configured log destinations */
void fastd_logf(fastd_loglevel_t level, const char *format, ...) {
- char buffer[1024];
- char timestr[100] = "";
+ bool log_stderr = !ctx.log_initialized || level <= conf.log_stderr_level;
+ bool log_syslog = ctx.log_initialized && level <= conf.log_syslog_level;
va_list ap;
+ char buffer[1024];
+
+ if (!log_stderr && !log_syslog)
+ return;
va_start(ap, format);
fastd_vsnprintf(buffer, sizeof(buffer), format, ap);
@@ -267,22 +271,20 @@ void fastd_logf(fastd_loglevel_t level, const char *format, ...) {
buffer[sizeof(buffer)-1] = 0;
- if (!ctx.log_initialized || level <= conf.log_stderr_level) {
+ if (log_stderr) {
+ char timestr[100] = "";
time_t t;
struct tm tm;
t = time(NULL);
if (localtime_r(&t, &tm) != NULL) {
if (strftime(timestr, sizeof(timestr), "%F %T %z --- ", &tm) <= 0)
- *timestr = 0;
+ timestr[0] = 0;
}
- }
- if (!ctx.log_initialized || level <= conf.log_stderr_level)
fprintf(stderr, "%s%s%s\n", timestr, get_log_prefix(level), buffer);
-
- if (ctx.log_initialized) {
- if (level <= conf.log_syslog_level)
- syslog(get_syslog_level(level), "%s", buffer);
}
+
+ if (log_syslog)
+ syslog(get_syslog_level(level), "%s", buffer);
}