From 58fcf4e587c0bf11afce4c355e6054407b6057e5 Mon Sep 17 00:00:00 2001 From: "Zeeshan Ali (Khattak)" Date: Wed, 9 Feb 2011 19:44:56 +0200 Subject: [PATCH] core: Seprate log level for each domain We now allow you to specify log levels separately for each log domain. --- data/rygel.conf | 7 ++--- src/rygel/rygel-cmdline-config.vala | 18 ++++++++----- src/rygel/rygel-configuration.vala | 2 +- src/rygel/rygel-environment-config.vala | 8 +++--- src/rygel/rygel-log-handler.vala | 46 +++++++++++++++++++++++++-------- src/rygel/rygel-meta-config.vala | 6 ++--- src/rygel/rygel-user-config.vala | 9 +++---- 7 files changed, 60 insertions(+), 36 deletions(-) diff --git a/data/rygel.conf b/data/rygel.conf index 77ae571..dcfd415 100644 --- a/data/rygel.conf +++ b/data/rygel.conf @@ -33,15 +33,16 @@ interface= # The port to run HTTP server on. 0 means dynamic. port=0 -# The log level +# Comma-separated list of domain:level pairs to specify log level thresholds for +# individual domains. domain could be either 'rygel', name of a plugin or '*' +# for all domains. Allowed levels are: # # 1=critical # 2=error # 3=warning # 4=message/info # 5=debug -# -log-level=4 +log-levels=*:4 # Allow upload of media files? allow-upload=true diff --git a/src/rygel/rygel-cmdline-config.vala b/src/rygel/rygel-cmdline-config.vala index e5b1f6a..53643a5 100644 --- a/src/rygel/rygel-cmdline-config.vala +++ b/src/rygel/rygel-cmdline-config.vala @@ -45,7 +45,7 @@ internal class Rygel.CmdlineConfig : GLib.Object, Configuration { private static bool disallow_upload; private static bool disallow_deletion; - private static LogLevel log_level = LogLevel.INVALID; + private static string log_levels; private static string plugin_path; @@ -87,9 +87,13 @@ internal class Rygel.CmdlineConfig : GLib.Object, Configuration { ref disallow_upload, "Disallow upload", null }, { "disallow-deletion", 'D', 0, OptionArg.NONE, ref disallow_deletion, "Disallow deletion", null }, - { "log-level", 'g', 0, OptionArg.INT, ref log_level, - "Log level. 1=critical,2=error,3=warning,4=message/info,5=debug", - "N" }, + { "log-levels", 'g', 0, OptionArg.STRING, ref log_levels, + "Comma-separated list of domain:level pairs to specify log level " + + "thresholds for individual domains. domain could be either " + + "'rygel', name of a plugin or '*' for all domains. " + + " Allowed levels are: " + + "0=critical,2=error,3=warning,4=message/info,5=debug.", + "DOMAIN1:LEVEL1[,DOMAIN2:LEVEL2,..]" }, { "plugin-path", 'u', 0, OptionArg.STRING, ref plugin_path, "Plugin Path", "PLUGIN_PATH" }, { "disable-plugin", 'd', 0, OptionArg.STRING_ARRAY, @@ -208,12 +212,12 @@ internal class Rygel.CmdlineConfig : GLib.Object, Configuration { } } - public LogLevel get_log_level () throws GLib.Error { - if (this.log_level == LogLevel.INVALID) { + public string get_log_levels () throws GLib.Error { + if (log_levels == null) { throw new ConfigurationError.NO_VALUE_SET (_("No value available")); } - return log_level; + return log_levels; } public string get_plugin_path () throws GLib.Error { diff --git a/src/rygel/rygel-configuration.vala b/src/rygel/rygel-configuration.vala index 13bf08b..b67d732 100644 --- a/src/rygel/rygel-configuration.vala +++ b/src/rygel/rygel-configuration.vala @@ -51,7 +51,7 @@ public interface Rygel.Configuration : GLib.Object { public abstract bool get_allow_deletion () throws GLib.Error; - public abstract LogLevel get_log_level () throws GLib.Error; + public abstract string get_log_levels () throws GLib.Error; public abstract string get_plugin_path () throws GLib.Error; diff --git a/src/rygel/rygel-environment-config.vala b/src/rygel/rygel-environment-config.vala index afb158e..332adb1 100644 --- a/src/rygel/rygel-environment-config.vala +++ b/src/rygel/rygel-environment-config.vala @@ -43,7 +43,7 @@ internal class Rygel.EnvironmentConfig : GLib.Object, Configuration { private static string WMV_TRANSCODING_ENV = DISABLE_PREFIX + "_WMV_TRANS"; private static string DISALLOW_UPLOAD_ENV = DISABLE_PREFIX + "_UPLOAD"; private static string DISALLOW_DELETION_ENV = DISABLE_PREFIX + "_DELETION"; - private static string LOG_LEVEL_ENV = RYGEL_PREFIX + "_LOG"; + private static string LOG_LEVELS_ENV = RYGEL_PREFIX + "_LOG"; private static string PLUGIN_PATH_ENV = RYGEL_PREFIX + "_PLUGIN_PATH"; // Our singleton @@ -97,10 +97,8 @@ internal class Rygel.EnvironmentConfig : GLib.Object, Configuration { return !this.get_bool_variable (DISALLOW_DELETION_ENV); } - public LogLevel get_log_level () throws GLib.Error { - return (LogLevel) this.get_int_variable (LOG_LEVEL_ENV, - LogLevel.CRITICAL, - LogLevel.DEBUG); + public string get_log_levels () throws GLib.Error { + return this.get_string_variable (LOG_LEVELS_ENV); } public string get_plugin_path () throws GLib.Error { diff --git a/src/rygel/rygel-log-handler.vala b/src/rygel/rygel-log-handler.vala index 51fd9d8..514d908 100644 --- a/src/rygel/rygel-log-handler.vala +++ b/src/rygel/rygel-log-handler.vala @@ -21,6 +21,8 @@ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ +using Gee; + public enum Rygel.LogLevel { INVALID = 0, CRITICAL = 1, @@ -32,13 +34,14 @@ public enum Rygel.LogLevel { } public class Rygel.LogHandler : GLib.Object { - private const LogLevelFlags DEFAULT_LEVELS = LogLevelFlags.LEVEL_WARNING | - LogLevelFlags.LEVEL_CRITICAL | - LogLevelFlags.LEVEL_ERROR | - LogLevelFlags.LEVEL_MESSAGE | - LogLevelFlags.LEVEL_INFO; + private const string DEFAULT_LEVELS = "*:4"; + private const LogLevelFlags DEFAULT_FLAGS = LogLevelFlags.LEVEL_WARNING | + LogLevelFlags.LEVEL_CRITICAL | + LogLevelFlags.LEVEL_ERROR | + LogLevelFlags.LEVEL_MESSAGE | + LogLevelFlags.LEVEL_INFO; - public LogLevelFlags levels; // Current log levels + private HashMap log_level_hash; private static LogHandler log_handler; // Singleton @@ -51,32 +54,53 @@ public class Rygel.LogHandler : GLib.Object { } private LogHandler () { + this.log_level_hash = new HashMap (); + // Get the allowed log levels from the config var config = MetaConfig.get_default (); + string log_levels; try { - this.levels = this.log_level_to_flags (config.get_log_level ()); + log_levels = config.get_log_levels (); } catch (Error err) { - this.levels = DEFAULT_LEVELS; + log_levels = DEFAULT_LEVELS; warning (_("Failed to get log level from configuration: %s"), err.message); } + foreach (var pair in log_levels.split (",")) { + var tokens = pair.split (":"); + if (unlikely (tokens.length < 2)) { + break; + } + + var domain = tokens[0]; + var levels = (LogLevel) tokens[1].to_int (); + var flags = this.log_level_to_flags (levels); + + this.log_level_hash[domain] = flags; + } + Log.set_default_handler (this.log_func); } private void log_func (string? log_domain, LogLevelFlags log_levels, string message) { - if (log_levels in this.levels) { + var flags = this.log_level_hash[log_domain]; + if (flags == 0) { + flags = this.log_level_hash["*"]; + } + + if (log_levels in flags) { // Forward the message to default domain Log.default_handler (log_domain, log_levels, message); } } private LogLevelFlags log_level_to_flags (LogLevel level) { - LogLevelFlags flags = DEFAULT_LEVELS; + LogLevelFlags flags = DEFAULT_FLAGS; switch (level) { case LogLevel.CRITICAL: @@ -107,7 +131,7 @@ public class Rygel.LogHandler : GLib.Object { LogLevelFlags.LEVEL_DEBUG; break; default: - flags = DEFAULT_LEVELS; + flags = DEFAULT_FLAGS; break; } diff --git a/src/rygel/rygel-meta-config.vala b/src/rygel/rygel-meta-config.vala index a421606..ead0400 100644 --- a/src/rygel/rygel-meta-config.vala +++ b/src/rygel/rygel-meta-config.vala @@ -248,13 +248,13 @@ public class Rygel.MetaConfig : GLib.Object, Configuration { return val; } - public LogLevel get_log_level () throws GLib.Error { - LogLevel val = LogLevel.DEFAULT; + public string get_log_levels () throws GLib.Error { + string val = null; bool unavailable = true; foreach (var config in this.configs) { try { - val = config.get_log_level (); + val = config.get_log_levels (); unavailable = false; break; } catch (GLib.Error err) {} diff --git a/src/rygel/rygel-user-config.vala b/src/rygel/rygel-user-config.vala index 7ee7679..3356d80 100644 --- a/src/rygel/rygel-user-config.vala +++ b/src/rygel/rygel-user-config.vala @@ -40,7 +40,7 @@ public class Rygel.UserConfig : GLib.Object, Configuration { public static const string WMV_TRANSCODER_KEY = "enable-wmv-transcoder"; public static const string ALLOW_UPLOAD_KEY = "allow-upload"; public static const string ALLOW_DELETION_KEY = "allow-deletion"; - public static const string LOG_LEVEL_KEY = "log-level"; + public static const string LOG_LEVELS_KEY = "log-levels"; public static const string PLUGIN_PATH_KEY = "plugin-path"; // Our singleton @@ -88,11 +88,8 @@ public class Rygel.UserConfig : GLib.Object, Configuration { return this.get_bool ("general", ALLOW_DELETION_KEY); } - public LogLevel get_log_level () throws GLib.Error { - return (LogLevel) this.get_int ("general", - LOG_LEVEL_KEY, - LogLevel.INVALID, - LogLevel.DEBUG); + public string get_log_levels () throws GLib.Error { + return this.get_string ("general", LOG_LEVELS_KEY); } public string get_plugin_path () throws GLib.Error { -- 2.7.4