chiark / gitweb /
log: read log settings from the environment
authorLennart Poettering <lennart@poettering.net>
Tue, 6 Apr 2010 21:38:32 +0000 (23:38 +0200)
committerLennart Poettering <lennart@poettering.net>
Tue, 6 Apr 2010 21:38:32 +0000 (23:38 +0200)
log.c
log.h

diff --git a/log.c b/log.c
index 879cb537336e8461fec1c02ea79bceb79b41eeab..ffca41c2702e45c4e4fe6ddd0561011d8af48bed 100644 (file)
--- a/log.c
+++ b/log.c
@@ -272,3 +272,43 @@ void log_meta(
 
         errno = saved_errno;
 }
+
+int log_set_target_from_string(const char *e) {
+        LogTarget t;
+
+        if ((t = log_target_from_string(e)) < 0)
+                return -EINVAL;
+
+        log_set_target(t);
+        return 0;
+}
+
+int log_set_max_level_from_string(const char *e) {
+        int t;
+
+        if ((t = log_level_from_string(e)) < 0)
+                return -EINVAL;
+
+        log_set_max_level(t);
+        return 0;
+}
+
+void log_parse_environment(void) {
+        const char *e;
+
+        if ((e = getenv("SYSTEMD_LOG_TARGET")))
+                if (log_set_target_from_string(e) < 0)
+                        log_warning("Failed to parse log target %s. Ignoring.", e);
+
+        if ((e = getenv("SYSTEMD_LOG_LEVEL")))
+                if (log_set_max_level_from_string(e) < 0)
+                        log_warning("Failed to parse log level %s. Ignoring.", e);
+}
+
+static const char *const log_target_table[] = {
+        [LOG_TARGET_CONSOLE] = "console",
+        [LOG_TARGET_SYSLOG] = "syslog",
+        [LOG_TARGET_KMSG] = "kmsg",
+};
+
+DEFINE_STRING_TABLE_LOOKUP(log_target, LogTarget);
diff --git a/log.h b/log.h
index d6aa472a24b171b4e1bca5a173362815a366ba00..abea126b916e3a81b5c8c20e69bd799b2cdb25b9 100644 (file)
--- a/log.h
+++ b/log.h
@@ -37,11 +37,16 @@ typedef enum LogTarget{
 void log_set_target(LogTarget target);
 void log_set_max_level(int level);
 
+int log_set_target_from_string(const char *e);
+int log_set_max_level_from_string(const char *e);
+
 void log_close_kmsg(void);
 int log_open_kmsg(void);
 void log_close_syslog(void);
 int log_open_syslog(void);
 
+void log_parse_environment(void);
+
 void log_meta(
         int level,
         const char*file,
@@ -55,4 +60,7 @@ void log_meta(
 #define log_warning(...) log_meta(LOG_WARNING, __FILE__, __LINE__, __func__, __VA_ARGS__)
 #define log_error(...)   log_meta(LOG_ERR,     __FILE__, __LINE__, __func__, __VA_ARGS__)
 
+const char *log_target_to_string(LogTarget target);
+LogTarget log_target_from_string(const char *s);
+
 #endif