Skip to content

Commit

Permalink
Added commandline option to set default log level
Browse files Browse the repository at this point in the history
  • Loading branch information
naxuroqa committed Jul 1, 2014
1 parent e6e4c5d commit 7b905f1
Showing 1 changed file with 32 additions and 13 deletions.
45 changes: 32 additions & 13 deletions src/Main.vala
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,49 @@
*/

namespace Venom {

public class Main : GLib.Object {
public class Main : GLib.Object {
private static string? datafile = null;
private static bool offline = false;
private static bool textview = false;
private static bool version = false;
private const GLib.OptionEntry[] options = {
{ "datafile", 'n', 0, GLib.OptionArg.FILENAME, ref datafile, "Set the location of the tox data file", "<file>" },
{ "offline", 0, 0, GLib.OptionArg.NONE, ref offline, "Start in offline mode", null },
{ "textview", 0, 0, GLib.OptionArg.NONE, ref textview, "Use textview to display messages", null },
{ "version", 'V', 0, GLib.OptionArg.NONE, ref version, "Display version number", null },
private const OptionEntry[] option_entries = {
{ "file" , 'f', 0, OptionArg.FILENAME, ref datafile , "Set the location of the tox data file", "<file>" },
{ "loglevel", 'l', 0, OptionArg.CALLBACK, (void*) parse_loglevel, "Set level of messages to log", "<loglevel>" },
{ "offline" , 0 , 0, OptionArg.NONE , ref offline , "Start in offline mode", null },
{ "textview", 0 , 0, OptionArg.NONE , ref textview , "Use textview to display messages", null },
{ "version" , 'V', 0, OptionArg.NONE , ref version , "Display version number", null },
{ null }
};

public static bool parse_loglevel (string name, string? val, ref OptionError error) throws OptionError {
if (val == null) {
return true;
}
Regex regex;
try {
regex = new GLib.Regex("^[0-9]+$");
} catch {
Logger.log(LogLevel.FATAL, "could not create regex needed for number parsing");
return true;
}

if (!regex.match (val, 0, null)) {
throw new OptionError.BAD_VALUE (_("'%s' not a positive number"), val);
}
Logger.displayed_level = (LogLevel)int.parse(val);
return true;
}

public static int main (string[] args) {
GLib.Intl.setlocale(GLib.LocaleCategory.MESSAGES, "");
GLib.Intl.textdomain(Config.GETTEXT_PACKAGE);
GLib.Intl.bind_textdomain_codeset(Config.GETTEXT_PACKAGE, "utf-8");
//FIXME see if this is needed on windows
//GLib.Intl.bindtextdomain(Config.GETTEXT_PACKAGE, Config.GETTEXT_PATH);
GLib.OptionContext option_context = new GLib.OptionContext("");
option_context.set_help_enabled(true);
option_context.add_main_entries(option_entries, null);
try {
GLib.OptionContext option_context = new GLib.OptionContext("");
option_context.set_help_enabled(true);
option_context.add_main_entries(options, null);
option_context.parse(ref args);
} catch (GLib.OptionError e) {
stdout.printf(_("error: %s\n"), e.message);
Expand All @@ -57,17 +76,17 @@ public class Main : GLib.Object {
}

if(datafile != null) {
stdout.printf(_("Using data file \"%s\"\n"), datafile);
Logger.log(LogLevel.INFO, "Using data file \"" + datafile + "\"");
ResourceFactory.instance.data_filename = datafile;
}

if(textview) {
stdout.printf(_("Using Gtk.TextView to display messages\n"));
Logger.log(LogLevel.INFO, "Using Gtk.TextView to display messages");
ResourceFactory.instance.textview_mode = true;
}

if(offline) {
stdout.printf(_("Starting in offline mode\n"));
Logger.log(LogLevel.INFO, "Starting in offline mode");
ResourceFactory.instance.offline_mode = true;
}

Expand Down

0 comments on commit 7b905f1

Please sign in to comment.