Skip to content

Commit

Permalink
added logger class to replace direct calls to stdout/stderr, removed …
Browse files Browse the repository at this point in the history
…some unecessary translation of log messages, default log level is >=warning which hides most of the current log messages
  • Loading branch information
naxuroqa committed Jul 1, 2014
1 parent 288df43 commit e6e4c5d
Show file tree
Hide file tree
Showing 22 changed files with 176 additions and 131 deletions.
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ SET( VENOM_SRC
core/FileTransfer.vala
core/GroupChat.vala
core/GroupChatContact.vala
core/Logger.vala
core/MessageLog.vala
core/Message.vala
core/ResourceFactory.vala
Expand Down
4 changes: 2 additions & 2 deletions src/core/ContactStorage.vala
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ namespace Venom {
try {
SqliteTools.put_text(select_statement, TABLE_KEY, key);
} catch (SqliteStatementError e) {
stderr.printf("Error retrieving contact from sqlite database: %s\n", e.message);
Logger.log(LogLevel.ERROR, "Error retrieving contact from sqlite database: " + e.message);
return;
}

Expand All @@ -85,7 +85,7 @@ namespace Venom {
SqliteTools.put_int (insert_statement, TABLE_ISBLOCKED, c.is_blocked ? 1 : 0);
SqliteTools.put_text(insert_statement, TABLE_GROUP, c.group);
} catch (SqliteStatementError e) {
stderr.printf(_("Error writing contact to sqlite database: %s\n"), e.message);
Logger.log(LogLevel.ERROR, "Error writing contact to sqlite database: " + e.message);
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/DhtNode.vala
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namespace Venom {
this.is_ipv6 = true;
}
public string to_string() {
return _("%s:%u%s %s").printf(host, port, is_ipv6 ? _(" (ipv6)") : "", Tools.bin_to_hexstring(pub_key));
return "%s:%u%s %s".printf(host, port, is_ipv6 ? " (ipv6)" : "", Tools.bin_to_hexstring(pub_key));
}
}
}
2 changes: 1 addition & 1 deletion src/core/DhtNodeStorage.vala
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ namespace Venom {
SqliteTools.put_text(insert_statement, TABLE_LOCATION, node.location);
SqliteTools.put_int (insert_statement, TABLE_ISBLOCKED, node.is_blocked ? 1 : 0);
} catch (SqliteStatementError e) {
stderr.printf("Error writing dht node to sqlite database: %s\n", e.message);
Logger.log(LogLevel.ERROR, "Error writing dht node to sqlite database: " + e.message);
return;
}

Expand Down
57 changes: 57 additions & 0 deletions src/core/Logger.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Logger.vala
*
* Copyright (C) 2013-2014 Venom authors and contributors
*
* This file is part of Venom.
*
* Venom is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Venom is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Venom. If not, see <http://www.gnu.org/licenses/>.
*/

namespace Venom {
public enum LogLevel {
DEBUG,
INFO,
WARNING,
ERROR,
FATAL;

public string to_string() {
switch(this) {
case DEBUG:
return "DBG";
case INFO:
return "INFO";
case WARNING:
return "WARN";
case ERROR:
return "ERROR";
case FATAL:
return "FATAL";
default:
return "UNKOWN";
}
}
}
public class Logger : GLib.Object {
public static LogLevel displayed_level {get; set; default = LogLevel.WARNING;}
public static void log(LogLevel level, string message) {
if(level < displayed_level) {
return;
}
unowned FileStream s = level < LogLevel.ERROR ? stdout : stderr;
s.printf("[%s] %s\n", level.to_string(), message);
}
}
}
38 changes: 14 additions & 24 deletions src/core/MessageLog.vala
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,14 @@ namespace Venom {
);
""";

public SqliteMessageLog(Sqlite.Database db) {
public SqliteMessageLog(Sqlite.Database db) throws SqliteDbError {
this.db = db;
init_db ();
stdout.printf (_("SQLite database created.\n"));
Logger.log(LogLevel.DEBUG, "SQLite database created.");
}

~LocalStorage() {
stdout.printf (_("SQLite database closed.\n"));
Logger.log(LogLevel.DEBUG, "SQLite database closed.");
}

public void connect_to(ToxSession session) {
Expand Down Expand Up @@ -115,7 +115,7 @@ namespace Venom {
SqliteTools.put_int64(insert_statement, TABLE_TIME, nowTime.to_unix());
SqliteTools.put_int(insert_statement, TABLE_SENDER, issender ? 1 : 0);
} catch (SqliteStatementError e) {
stderr.printf(_("Error writing message to sqlite database: %s\n"), e.message);
Logger.log(LogLevel.ERROR, "Error writing message to sqlite database: " + e.message);
return;
}

Expand All @@ -132,7 +132,7 @@ namespace Venom {
SqliteTools.put_text(select_statement , TABLE_CONTACT, cId);
SqliteTools.put_int64(select_statement, TABLE_OLDEST, earliestTime.to_unix());
} catch (SqliteStatementError e) {
stderr.printf(_("Error retrieving logs from sqlite database: %s\n"), e.message);
Logger.log(LogLevel.ERROR, "Error retrieving logs from sqlite database: " + e.message);
return null;
}

Expand All @@ -159,41 +159,31 @@ namespace Venom {
//TODO
}

private int init_db() {

private void init_db() throws SqliteDbError {
string errmsg;

//create table and index if needed
int ec = db.exec (QUERY_TABLE_HISTORY, null, out errmsg);
if (ec != Sqlite.OK) {
stderr.printf (_("Error: %s\n"), errmsg);
return -1;
if (db.exec (QUERY_TABLE_HISTORY, null, out errmsg) != Sqlite.OK) {
throw new SqliteDbError.QUERY(_("Error creating message log table: %s\n"), errmsg);
}

const string index_query = """
CREATE UNIQUE INDEX IF NOT EXISTS main_index ON History (userHash, contactHash, timestamp);
""";

ec = db.exec (index_query, null, out errmsg);
if (ec != Sqlite.OK) {
stderr.printf (_("Error: %s\n"), errmsg);
return -1;
if (db.exec (index_query, null, out errmsg) != Sqlite.OK) {
throw new SqliteDbError.QUERY(_("Error executing index query: %d: %s\n"), db.errcode (), errmsg);
}

//prepare insert statement for adding new history messages
ec = db.prepare_v2 (STATEMENT_INSERT_HISTORY, STATEMENT_INSERT_HISTORY.length, out insert_statement);
if (ec != Sqlite.OK) {
stderr.printf (_("Error: %d: %s\n"), db.errcode (), db.errmsg ());
return -1;
if (db.prepare_v2 (STATEMENT_INSERT_HISTORY, STATEMENT_INSERT_HISTORY.length, out insert_statement) != Sqlite.OK) {
throw new SqliteDbError.QUERY(_("Error creating message insert statement: %d: %s\n"), db.errcode (), db.errmsg());
}

//prepare select statement to get history. Will execute on indexed data
ec = db.prepare_v2 (STATEMENT_SELECT_HISTORY, STATEMENT_SELECT_HISTORY.length, out select_statement);
if (ec != Sqlite.OK) {
stderr.printf (_("Error: %d: %s\n"), db.errcode (), db.errmsg ());
return -1;
if (db.prepare_v2 (STATEMENT_SELECT_HISTORY, STATEMENT_SELECT_HISTORY.length, out select_statement) != Sqlite.OK) {
throw new SqliteDbError.QUERY(_("Error creating message select statement: %d: %s\n"), db.errcode (), db.errmsg());
}
return 0;
}


Expand Down
2 changes: 1 addition & 1 deletion src/core/ResourceFactory.vala
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ namespace Venom {
try {
buf = new Gdk.Pixbuf.from_resource( resourcename );
} catch (Error e) {
stderr.printf(_("Error while loading image from \"%s\":%s\n"), resourcename, e.message );
Logger.log(LogLevel.ERROR, "Error while loading image from \"" + resourcename + "\": " + e.message);
}
return buf;
}
Expand Down
6 changes: 3 additions & 3 deletions src/core/Settings.vala
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,10 @@ namespace Venom {
DataOutputStream os = new DataOutputStream(file.replace(null, false, FileCreateFlags.PRIVATE | FileCreateFlags.REPLACE_DESTINATION ));
generator.to_stream(os);
} catch (Error e) {
stderr.printf(_("Error saving configs:%s\n"), e.message);
Logger.log(LogLevel.ERROR, "Error saving configs: " + e.message);
return;
}
stdout.printf(_("Settings saved.\n"));
Logger.log(LogLevel.INFO, "Settings saved.");
}

public static Settings? load_settings(string path) {
Expand All @@ -109,7 +109,7 @@ namespace Venom {
parser.load_from_file(path);
node = parser.get_root();
} catch (Error e) {
stderr.printf(_("Error reading configs:%s\n"),e.message);
Logger.log(LogLevel.ERROR, "Error reading configs: " + e.message);
return null;
}
return Json.gobject_deserialize (typeof (Settings), node) as Settings;
Expand Down
10 changes: 5 additions & 5 deletions src/core/Tools.vala
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ namespace Venom {
File path = File.new_for_path(pathname);
if(!path.query_exists()) {
DirUtils.create_with_parents(pathname, mode);
stdout.printf("created directory %s\n", pathname);
Logger.log(LogLevel.INFO, "created directory " + pathname);
}
}

Expand Down Expand Up @@ -93,7 +93,7 @@ namespace Venom {
if (u != (unichar) (-1)) {
sb.append_unichar(u);
} else {
stdout.printf("Invalid UTF-8 character detected\n");
Logger.log(LogLevel.WARNING, "Invalid UTF-8 character detected");
}
}
return sb.str;
Expand All @@ -120,7 +120,7 @@ namespace Venom {
try {
ret = Tools.uri_regex.replace(escaped_text, -1, 0, "<a href=\"\\g<u>\">\\g<u></a>");
} catch (GLib.RegexError e) {
stderr.printf(_("Error when doing uri markup: %s"), e.message);
Logger.log(LogLevel.ERROR, "Error when doing uri markup: " + e.message);
return text;
}
return ret;
Expand Down Expand Up @@ -155,7 +155,7 @@ namespace Venom {
try {
_action_regex = new GLib.Regex("^/(?P<action_name>\\S+)(\\s+(?P<action_string>.+))?$");
} catch (GLib.RegexError e) {
stderr.printf(_("Can't create action regex: %s.\n"), e.message);
Logger.log(LogLevel.ERROR, "Can't create action regex: " + e.message);
}
}
return _action_regex;
Expand All @@ -168,7 +168,7 @@ namespace Venom {
try {
_uri_regex = new GLib.Regex("(?<u>[a-z]+://\\S*)");
} catch (GLib.RegexError e) {
stderr.printf(_("Can't create action regex: %s.\n"), e.message);
Logger.log(LogLevel.ERROR, "Can't create uri regex: " + e.message);
}
}
return _uri_regex;
Expand Down
18 changes: 9 additions & 9 deletions src/core/ToxDns.vala
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace Venom {
try {
_tox_dns_record_regex = new GLib.Regex("^v=(?P<v>[^\\W;]+);(id=(?P<id>[^\\W;]+)|pub=(?P<pub>[^\\W;]+);check=(?P<check>[^\\W;]+))");
} catch (GLib.Error e) {
stderr.printf(_("Error creating tox dns regex: %s\n"), e.message);
Logger.log(LogLevel.FATAL, "Error creating tox dns regex: " + e.message);
}
}
return _tox_dns_record_regex;
Expand All @@ -48,7 +48,7 @@ namespace Venom {
//TODO support message pin and xname (ignored for now)
_tox_uri_regex = new GLib.Regex("^((?P<scheme>tox)://)?((?P<tox_id>[[:xdigit:]]{%i})|(?P<authority_user>[[:digit:][:alpha:]]+)(@(?P<authority_host>[[:digit:][:alpha:]]+(\\.[[:digit:][:alpha:]]+)+))?)".printf(Tox.FRIEND_ADDRESS_SIZE * 2));
} catch (GLib.Error e) {
stderr.printf(_("Error creating tox uri regex: %s\n"), e.message);
Logger.log(LogLevel.FATAL, "Error creating tox uri regex: " + e.message);
}
}
return _tox_uri_regex;
Expand All @@ -61,7 +61,7 @@ namespace Venom {

GLib.MatchInfo info = null;
if(tox_uri_regex == null || !tox_uri_regex.match(tox_uri, 0, out info)) {
stderr.printf(_("Invalid tox uri\n"));
Logger.log(LogLevel.ERROR, "Invalid tox uri");
return null;
}

Expand All @@ -78,7 +78,7 @@ namespace Venom {
try {
record = lookup_dns_record(hostname);
} catch (GLib.Error e) {
stderr.printf(_("Error resolving name: %s\n"), e.message);
Logger.log(LogLevel.ERROR, "Error resolving name: " + e.message);
return null;
}

Expand All @@ -92,25 +92,25 @@ namespace Venom {
switch(v) {
case "tox1":
string id = info.fetch_named("id");
stdout.printf(_("tox 1 ID found: %s\n"), id);
Logger.log(LogLevel.INFO, "tox 1 ID found: " + id);
return id;
case "tox2":
string pub = info.fetch_named("pub");
string check = info.fetch_named("check");
stdout.printf(_("tox 2 ID found: %s %s\n"), pub, check);
Logger.log(LogLevel.INFO, "tox 2 ID found: " + pub + " " + check);
string pin = pin_request(tox_dns_id);
if(pin == null || pin == "") {
stderr.printf(_("No pin privided, aborting...\n"));
Logger.log(LogLevel.INFO, "No pin privided, aborting...");
return null;
}
string decoded = pub + Venom.Tools.bin_to_hexstring(Base64.decode(pin + "==")) + check;
stdout.printf("decoded: %s\n", decoded);
Logger.log(LogLevel.INFO, "decoded: " + decoded);
return decoded;
default:
assert_not_reached();
}
} else {
stderr.printf(_("Invalid record\n"));
Logger.log(LogLevel.INFO, "Invalid record");
}
return null;
}
Expand Down
12 changes: 6 additions & 6 deletions src/core/ToxSession.vala
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ namespace Venom {
//unfinished, so replaced with dummy storage
//dht_node_storage = new SqliteDhtNodeStorage(db);
} catch (Error e) {
stderr.printf(_("Error opening database: %s\n"), e.message);
Logger.log(LogLevel.ERROR, "Error opening database: " + e.message);
message_log = new DummyMessageLog();
dht_node_storage = new DummyDhtNodeStorage();
}
Expand Down Expand Up @@ -349,7 +349,7 @@ namespace Venom {
g.peer_count++;
} else if (change == Tox.ChatChange.PEER_DEL) {
if(!g.peers.remove(peernumber)) {
stderr.printf(_("Could not remove peer [%i] from groupchat #%i (no such peer)\n"), peernumber, groupnumber);
Logger.log(LogLevel.ERROR, "Could not remove peer [%i] from groupchat #%i (no such peer)".printf(peernumber, groupnumber));
}
g.peer_count--;
} else { // change == PEER_NAME
Expand Down Expand Up @@ -719,15 +719,15 @@ namespace Venom {

// Background thread main function
private int run() {
stdout.printf(_("Background thread started.\n"));
Logger.log(LogLevel.INFO, "Background thread started.");

if(!bootstrapped) {
stdout.printf(_("Connecting to DHT Nodes:\n"));
Logger.log(LogLevel.INFO, "Connecting to DHT Nodes:");
for(int i = 0; i < dht_nodes.length; ++i) {
// skip ipv6 nodes if we don't support them
if(dht_nodes[i].is_ipv6 && !ipv6)
continue;
stdout.printf(_(" %s\n"), dht_nodes[i].to_string());
Logger.log(LogLevel.INFO, " " + dht_nodes[i].to_string());
lock(handle) {
handle.bootstrap_from_address(
dht_nodes[i].host,
Expand Down Expand Up @@ -758,7 +758,7 @@ namespace Venom {
}
Thread.usleep(handle.do_interval() * 1000);
}
stdout.printf(_("Background thread stopped.\n"));
Logger.log(LogLevel.INFO, "Background thread stopped.");
return 0;
}

Expand Down
4 changes: 2 additions & 2 deletions src/ui/AddContactDialog.vala
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ namespace Venom {
try {
builder.add_from_resource("/org/gtk/venom/add_contact_dialog.ui");
} catch (GLib.Error e) {
stderr.printf(_("Loading add contact window failed!\n"));
Logger.log(LogLevel.FATAL, "Loading add contact window failed: " + e.message);
}

Gtk.Box box = builder.get_object("box") as Gtk.Box;
Expand All @@ -68,7 +68,7 @@ namespace Venom {
try {
id_regex = new GLib.Regex("^[[:xdigit:]]*$");
} catch (RegexError re) {
stderr.printf("Failed to compile regex: %s\n", re.message);
Logger.log(LogLevel.FATAL, "Failed to compile regex:" + re.message);
}

this.add_buttons("_Cancel", Gtk.ResponseType.CANCEL, "_Ok", Gtk.ResponseType.OK, null);
Expand Down

0 comments on commit e6e4c5d

Please sign in to comment.