From 98f3ef2689de06e8ab8b46a91acfa7dd2056a3a6 Mon Sep 17 00:00:00 2001 From: Alessandro Ghedini Date: Mon, 5 Mar 2012 12:19:59 +0100 Subject: Imported Upstream version 0.5.1 --- src/utilities/config.vala | 5 ++ src/utilities/logger.vala | 168 +++++++++++++++++++++++++++++++++++++--------- src/utilities/paths.vala | 44 +++++++++++- 3 files changed, 184 insertions(+), 33 deletions(-) (limited to 'src/utilities') diff --git a/src/utilities/config.vala b/src/utilities/config.vala index 5790eef..cc776d5 100644 --- a/src/utilities/config.vala +++ b/src/utilities/config.vala @@ -55,6 +55,7 @@ public class Config : GLib.Object { public double refresh_rate { get; set; default = 60.0; } public double global_scale { get; set; default = 1.0; } public bool show_indicator { get; set; default = true; } + public bool show_captions { get; set; default = true; } public bool auto_start { get; set; default = false; } public Gee.ArrayList themes { get; private set; } @@ -70,6 +71,7 @@ public class Config : GLib.Object { writer.write_attribute("refresh_rate", refresh_rate.to_string()); writer.write_attribute("global_scale", global_scale.to_string()); writer.write_attribute("show_indicator", show_indicator ? "true" : "false"); + writer.write_attribute("show_captions", show_captions ? "true" : "false"); writer.end_element(); writer.end_document(); } @@ -112,6 +114,9 @@ public class Config : GLib.Object { case "show_indicator": show_indicator = bool.parse(attr_content); break; + case "show_captions": + show_captions = bool.parse(attr_content); + break; default: warning("Invalid setting \"" + attr_name + "\" in gnome-pie.conf!"); break; diff --git a/src/utilities/logger.vala b/src/utilities/logger.vala index 3108ba3..5334920 100644 --- a/src/utilities/logger.vala +++ b/src/utilities/logger.vala @@ -17,7 +17,7 @@ this program. If not, see . namespace GnomePie { -///////////////////////////////////////////////////////////////////////// +///////////////////////////////////////////////////////////////////////// /// A static class which beautifies the messages of the default logger. /// Some of this code is inspired by plank's written by Robert Dyer. /// Thanks a lot for this project! @@ -29,22 +29,33 @@ public class Logger { /// If these are set to false, the according messages are not shown ///////////////////////////////////////////////////////////////////// - public static bool display_info { get; set; default = true; } - public static bool display_debug { get; set; default = true; } - public static bool display_warning { get; set; default = true; } - public static bool display_error { get; set; default = true; } + private static const bool display_debug = true; + private static const bool display_warning = true; + private static const bool display_error = true; + private static const bool display_message = true; + + ///////////////////////////////////////////////////////////////////// + /// If these are set to false, the according messages are not logged + ///////////////////////////////////////////////////////////////////// + + private static const bool log_debug = false; + private static const bool log_warning = true; + private static const bool log_error = true; + private static const bool log_message = true; ///////////////////////////////////////////////////////////////////// /// If true, a time stamp is shown in each message. ///////////////////////////////////////////////////////////////////// - public static bool display_time { get; set; default = true; } + private static const bool display_time = false; + private static const bool log_time = true; ///////////////////////////////////////////////////////////////////// /// If true, the origin of the message is shown. In form file:line ///////////////////////////////////////////////////////////////////// - public static bool display_file { get; set; default = false; } + private static const bool display_file = false; + private static const bool log_file = false; ///////////////////////////////////////////////////////////////////// /// A regex, used to format the standard message. @@ -52,6 +63,16 @@ public class Logger { private static Regex regex = null; + ///////////////////////////////////////////////////////////////////// + /// Limit log and statistics size to roughly 1 MB. + ///////////////////////////////////////////////////////////////////// + + private static const int max_log_length = 1000000; + private static const int max_stats_length = 1000000; + + private static int log_length; + private static int stats_length; + ///////////////////////////////////////////////////////////////////// /// Possible terminal colors. ///////////////////////////////////////////////////////////////////// @@ -72,20 +93,84 @@ public class Logger { ///////////////////////////////////////////////////////////////////// public static void init() { + log_length = -1; + stats_length = -1; + try { regex = new Regex("""(.*)\.vala(:\d+): (.*)"""); } catch {} - GLib.Log.set_default_handler(log_func); + GLib.Log.set_handler(null, GLib.LogLevelFlags.LEVEL_MASK, log_func); + } + + ///////////////////////////////////////////////////////////////////// + /// Appends a line to the statistics file + ///////////////////////////////////////////////////////////////////// + + public static void stats(string line) { + var stats = GLib.FileStream.open(Paths.stats, "a"); + + if (stats != null) { + if (stats_length == -1) + stats_length = (int)stats.tell(); + + string final_line = "[" + get_time() + "] " + line + "\n"; + stats.puts(final_line); + stats_length += final_line.length; + } + + if (stats_length > max_stats_length) { + string content = ""; + + try { + GLib.FileUtils.get_contents(Paths.stats, out content); + int split_index = content.index_of_char('\n', stats_length - (int)(max_stats_length*0.9)); + GLib.FileUtils.set_contents(Paths.stats, content.substring(split_index+1)); + + stats_length -= (split_index+1); + } catch (GLib.FileError e) {} + } + } + + ///////////////////////////////////////////////////////////////////// + /// Appends a line to the log file + ///////////////////////////////////////////////////////////////////// + + private static void write_log_line(string line) { + var log = GLib.FileStream.open(Paths.log, "a"); + + if (log != null) { + if (log_length == -1) + log_length = (int)log.tell(); + + log.puts(line); + log_length += line.length; + } + + if (log_length > max_log_length) { + string content = ""; + + try { + GLib.FileUtils.get_contents(Paths.log, out content); + int split_index = content.index_of_char('\n', log_length - (int)(max_log_length*0.9)); + GLib.FileUtils.set_contents(Paths.log, content.substring(split_index+1)); + + log_length -= (split_index+1); + } catch (GLib.FileError e) {} + } } ///////////////////////////////////////////////////////////////////// - /// Displays an Info message. + /// Displays a message. ///////////////////////////////////////////////////////////////////// - private static void info(string message) { - if (display_info) { - stdout.printf(set_color(Color.GREEN, false) + "[" + get_time() + "MESSAGE]" + message); + private static void message(string message, string message_log) { + if (display_message) { + stdout.printf(set_color(Color.GREEN, false) + "[" + (display_time ? get_time() + " " : "") + "MESSAGE]" + message); + } + + if (log_message) { + write_log_line("[" + (log_time ? get_time() + " " : "") + "MESSAGE]" + message_log); } } @@ -93,9 +178,13 @@ public class Logger { /// Displays a Debug message. ///////////////////////////////////////////////////////////////////// - private static void debug(string message) { + private static void debug(string message, string message_log) { if (display_debug) { - stdout.printf(set_color(Color.BLUE, false) + "[" + get_time() + " DEBUG ]" + message); + stdout.printf(set_color(Color.BLUE, false) + "[" + (display_time ? get_time() + " " : "") + " DEBUG ]" + message); + } + + if (log_debug) { + write_log_line("[" + (log_time ? get_time() + " " : "") + " DEBUG ]" + message_log); } } @@ -103,9 +192,13 @@ public class Logger { /// Displays a Warning message. ///////////////////////////////////////////////////////////////////// - private static void warning(string message) { + private static void warning(string message, string message_log) { if (display_warning) { - stdout.printf(set_color(Color.YELLOW, false) + "[" + get_time() + "WARNING]" + message); + stdout.printf(set_color(Color.YELLOW, false) + "[" + (display_time ? get_time() + " " : "") + "WARNING]" + message); + } + + if (log_warning) { + write_log_line("[" + (log_time ? get_time() + " " : "") + "WARNING]" + message_log); } } @@ -113,9 +206,13 @@ public class Logger { /// Displays a Error message. ///////////////////////////////////////////////////////////////////// - private static void error(string message) { + private static void error(string message, string message_log) { if (display_error) { - stdout.printf(set_color(Color.RED, false) + "[" + get_time() + " ERROR ]" + message); + stdout.printf(set_color(Color.RED, false) + "[" + (display_time ? get_time() + " " : "") + " ERROR ]" + message); + } + + if (log_error) { + write_log_line("[" + (log_time ? get_time() + " " : "") + " ERROR ]" + message_log); } } @@ -141,12 +238,8 @@ public class Logger { ///////////////////////////////////////////////////////////////////// private static string get_time() { - if (display_time) { - var now = new DateTime.now_local (); - return "%.2d:%.2d:%.2d:%.6d ".printf (now.get_hour (), now.get_minute (), now.get_second (), now.get_microsecond ()); - } else { - return ""; - } + var now = new DateTime.now_local(); + return "%.4d:%.2d:%.2d:%.2d:%.2d:%.2d:%.6d".printf(now.get_year(), now.get_month(), now.get_day_of_month(), now.get_hour(), now.get_minute(), now.get_second(), now.get_microsecond()); } ///////////////////////////////////////////////////////////////////// @@ -165,27 +258,42 @@ public class Logger { } } + ///////////////////////////////////////////////////////////////////// + /// Helper method to format the message for logging. + ///////////////////////////////////////////////////////////////////// + + private static string create_log_message(string message) { + if (log_file && regex != null && regex.match(message)) { + var parts = regex.split(message); + return " [%s%s] %s\n".printf(parts[1], parts[2], parts[3]); + } else if (regex != null && regex.match(message)) { + var parts = regex.split(message); + return " %s\n".printf(parts[3]); + } else { + return " " + message + "\n"; + } + } + ///////////////////////////////////////////////////////////////////// /// The handler function. ///////////////////////////////////////////////////////////////////// - private static void log_func(string? d, LogLevelFlags flags, string message) { - + private static void log_func(string? d, LogLevelFlags flags, string text) { switch (flags) { case LogLevelFlags.LEVEL_ERROR: case LogLevelFlags.LEVEL_CRITICAL: - error(create_message(message)); + error(create_message(text), create_log_message(text)); break; case LogLevelFlags.LEVEL_INFO: case LogLevelFlags.LEVEL_MESSAGE: - info(create_message(message)); + message(create_message(text), create_log_message(text)); break; case LogLevelFlags.LEVEL_DEBUG: - debug(create_message(message)); + debug(create_message(text), create_log_message(text)); break; case LogLevelFlags.LEVEL_WARNING: default: - warning(create_message(message)); + warning(create_message(text), create_log_message(text)); break; } } diff --git a/src/utilities/paths.vala b/src/utilities/paths.vala index 589cc36..bc3e9b1 100644 --- a/src/utilities/paths.vala +++ b/src/utilities/paths.vala @@ -23,16 +23,30 @@ namespace GnomePie { ///////////////////////////////////////////////////////////////////////// public class Paths : GLib.Object { + + ///////////////////////////////////////////////////////////////////// + /// The log file, + /// usually ~/.config/gnome-pie/gnome-pie.log. + ///////////////////////////////////////////////////////////////////// + + public static string log { get; private set; default=""; } + + ///////////////////////////////////////////////////////////////////// + /// The statistics file, + /// usually ~/.config/gnome-pie/gnome-pie.stats. + ///////////////////////////////////////////////////////////////////// + + public static string stats { get; private set; default=""; } ///////////////////////////////////////////////////////////////////// - /// The file settings file, + /// The settings file, /// usually ~/.config/gnome-pie/gnome-pie.conf. ///////////////////////////////////////////////////////////////////// public static string settings { get; private set; default=""; } ///////////////////////////////////////////////////////////////////// - /// The file pie configuration file + /// The pie configuration file /// usually ~/.config/gnome-pie/pies.conf. ///////////////////////////////////////////////////////////////////// @@ -186,6 +200,24 @@ public class Paths : GLib.Object { pie_config = config_file.get_path(); settings = config_dir.get_path() + "/gnome-pie.conf"; + log = config_dir.get_path() + "/gnome-pie.log"; + stats = config_dir.get_path() + "/gnome-pie.stats"; + + if (!GLib.File.new_for_path(log).query_exists()) { + try { + FileUtils.set_contents(log, ""); + } catch (GLib.FileError e) { + error(e.message); + } + } + + if (!GLib.File.new_for_path(stats).query_exists()) { + try { + FileUtils.set_contents(stats, ""); + } catch (GLib.FileError e) { + error(e.message); + } + } // autostart file name autostart = GLib.Path.build_filename(GLib.Environment.get_user_config_dir(), @@ -196,7 +228,13 @@ public class Paths : GLib.Object { warning("Failed to find pie configuration file \"pies.conf\"! (This should only happen when Gnome-Pie is started for the first time...)"); if (!GLib.File.new_for_path(settings).query_exists()) - warning("Failed to find settings file \"gnome-pie.conf\"!"); + warning("Failed to find settings file \"gnome-pie.conf\"! (This should only happen when Gnome-Pie is started for the first time...)"); + + if (!GLib.File.new_for_path(log).query_exists()) + warning("Failed to find log file \"gnome-pie.log\"!"); + + if (!GLib.File.new_for_path(stats).query_exists()) + warning("Failed to find statistics file \"gnome-pie.stats\"!"); if (!GLib.File.new_for_path(local_themes).query_exists()) warning("Failed to find local themes directory!"); -- cgit v1.2.3