summaryrefslogtreecommitdiffstats
path: root/src/printf.c
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2012-05-18 08:48:24 +0200
committerMatthias Schiffer <mschiffer@universe-factory.net>2012-05-18 08:48:24 +0200
commite3ba3e8f66cf6d32a7408fe3e3666805ddf82578 (patch)
tree51e24e03d53c3d642c54b4bae27cd80c2290a55c /src/printf.c
parent6c6398d355d91c56c2a0637ca659b013ae82355a (diff)
downloadfastd-e3ba3e8f66cf6d32a7408fe3e3666805ddf82578.tar
fastd-e3ba3e8f66cf6d32a7408fe3e3666805ddf82578.zip
Uninline pr_log
Diffstat (limited to 'src/printf.c')
-rw-r--r--src/printf.c54
1 files changed, 54 insertions, 0 deletions
diff --git a/src/printf.c b/src/printf.c
index bf2bc35..c5a8209 100644
--- a/src/printf.c
+++ b/src/printf.c
@@ -147,3 +147,57 @@ int fastd_vsnprintf(const fastd_context *ctx, char *buffer, size_t size, const c
return buffer-buffer_start;
}
+
+static inline const char* get_log_prefix(int log_level) {
+ switch(log_level) {
+ case LOG_CRIT:
+ return "Fatal: ";
+ case LOG_ERR:
+ return "Error: ";
+ case LOG_WARNING:
+ return "Warning: ";
+ case LOG_NOTICE:
+ return "Info: ";
+ case LOG_INFO:
+ return "Verbose: ";
+ case LOG_DEBUG:
+ return "DEBUG: ";
+ default:
+ return "";
+ }
+}
+
+void fastd_logf(const fastd_context *ctx, int level, const char *format, ...) {
+ char buffer[1024];
+ char timestr[100] = "";
+ va_list ap;
+
+ va_start(ap, format);
+ fastd_vsnprintf(ctx, buffer, sizeof(buffer), format, ap);
+ va_end(ap);
+
+ buffer[sizeof(buffer)-1] = 0;
+
+ if (ctx->conf == NULL || level <= ctx->conf->log_stderr_level || ctx->conf->log_files) {
+ 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;
+ }
+ }
+
+ if (ctx->conf == NULL || level <= ctx->conf->log_stderr_level)
+ fprintf(stderr, "%s%s%s\n", timestr, get_log_prefix(level), buffer);
+
+ if (level <= ctx->conf->log_syslog_level)
+ syslog(level, "%s", buffer);
+
+ fastd_log_fd *file;
+ for (file = ctx->log_files; file; file = file->next) {
+ if (level <= file->config->level)
+ dprintf(file->fd, "%s%s%s\n", timestr, get_log_prefix(level), buffer);
+ }
+}