From 0f7d421bc696905d3e9fa9bdcc9c5f61c7c5051d Mon Sep 17 00:00:00 2001 From: White Date: Mon, 23 Mar 2015 12:00:37 +1030 Subject: [PATCH 1/4] SQL log actually ensures its table exists --- TShockAPI/SqlLog.cs | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/TShockAPI/SqlLog.cs b/TShockAPI/SqlLog.cs index 50a7e1bc..f2af1510 100644 --- a/TShockAPI/SqlLog.cs +++ b/TShockAPI/SqlLog.cs @@ -22,6 +22,7 @@ using System.Data; using System.Diagnostics; using System.Globalization; using System.Linq; +using MySql.Data.MySqlClient; using TShockAPI.DB; namespace TShockAPI @@ -55,7 +56,7 @@ namespace TShockAPI /// /// Sets the database connection and the initial log level. /// - /// + /// Database connection /// File path to a backup text log in case the SQL log fails /// public SqlLog(IDbConnection db, string textlogFilepath, bool clearTextLog) @@ -63,6 +64,20 @@ namespace TShockAPI FileName = string.Format("{0}://database", db.GetSqlType()); _database = db; _backupLog = new TextLog(textlogFilepath, clearTextLog); + + var table = new SqlTable("Logs", + new SqlColumn("ID", MySqlDbType.Int32) {AutoIncrement = true, Primary = true}, + new SqlColumn("TimeStamp", MySqlDbType.Text), + new SqlColumn("LogLevel", MySqlDbType.Int32), + new SqlColumn("Caller", MySqlDbType.Text), + new SqlColumn("Message", MySqlDbType.Text) + ); + + var creator = new SqlTableCreator(db, + db.GetSqlType() == SqlType.Sqlite + ? (IQueryBuilder)new SqliteQueryCreator() + : new MysqlQueryCreator()); + creator.EnsureTableStructure(table); } public bool MayWriteType(TraceLevel type) @@ -232,9 +247,8 @@ namespace TShockAPI return; } - _database.Query("INSERT INTO Logs (LogLevel, TimeStamp, Caller, Message) VALUES (@0, @1, @2, @3)", - level, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture), - caller, message); + _database.Query("INSERT INTO Logs (TimeStamp, Caller, LogLevel, Message) VALUES (@0, @1, @2, @3)", + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture), caller, (int)level, message); var success = true; while (_failures.Count > 0 && success) @@ -243,8 +257,8 @@ namespace TShockAPI try { - _database.Query("INSERT INTO Logs (LogLevel, TimeStamp, Caller, Message) VALUES (@0, @1, @2, @3)", - info.logLevel, info.timestamp, info.caller, info.message); + _database.Query("INSERT INTO Logs (TimeStamp, Caller, LogLevel, Message) VALUES (@0, @1, @2, @3)", + info.timestamp, info.caller, (int)info.logLevel, info.message); } catch (Exception ex) { From 5b246ce6410787beebd82ea84e165187a82d3ce2 Mon Sep 17 00:00:00 2001 From: White Date: Mon, 23 Mar 2015 13:27:07 +1030 Subject: [PATCH 2/4] Format fix --- TShockAPI/SqlLog.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/TShockAPI/SqlLog.cs b/TShockAPI/SqlLog.cs index f2af1510..ad4950d6 100644 --- a/TShockAPI/SqlLog.cs +++ b/TShockAPI/SqlLog.cs @@ -74,9 +74,9 @@ namespace TShockAPI ); var creator = new SqlTableCreator(db, - db.GetSqlType() == SqlType.Sqlite - ? (IQueryBuilder)new SqliteQueryCreator() - : new MysqlQueryCreator()); + db.GetSqlType() == SqlType.Sqlite + ? (IQueryBuilder) new SqliteQueryCreator() + : new MysqlQueryCreator()); creator.EnsureTableStructure(table); } From e6d07f2e83d2e865e813d7d20df35193100edc24 Mon Sep 17 00:00:00 2001 From: White Date: Mon, 23 Mar 2015 14:20:59 +1030 Subject: [PATCH 3/4] Update to DateTime.UtcNow --- TShockAPI/SqlLog.cs | 6 +++--- TShockAPI/TextLog.cs | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/TShockAPI/SqlLog.cs b/TShockAPI/SqlLog.cs index ad4950d6..ef0ceba7 100644 --- a/TShockAPI/SqlLog.cs +++ b/TShockAPI/SqlLog.cs @@ -248,7 +248,7 @@ namespace TShockAPI } _database.Query("INSERT INTO Logs (TimeStamp, Caller, LogLevel, Message) VALUES (@0, @1, @2, @3)", - DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture), caller, (int)level, message); + DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture), caller, (int)level, message); var success = true; while (_failures.Count > 0 && success) @@ -268,7 +268,7 @@ namespace TShockAPI caller = "TShock", logLevel = TraceLevel.Error, message = string.Format("SQL Log insert query failed: {0}", ex), - timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture) + timestamp = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture) }); } @@ -285,7 +285,7 @@ namespace TShockAPI logLevel = level, message = message, caller = caller, - timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture) + timestamp = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture) }); } diff --git a/TShockAPI/TextLog.cs b/TShockAPI/TextLog.cs index aef72b7a..bd256c75 100644 --- a/TShockAPI/TextLog.cs +++ b/TShockAPI/TextLog.cs @@ -212,7 +212,7 @@ namespace TShockAPI } var logEntry = string.Format("{0} - {1}: {2}: {3}", - DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture), + DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture), caller, level.ToString().ToUpper(), message); try { @@ -224,7 +224,7 @@ namespace TShockAPI ServerApi.LogWriter.PluginWriteLine(TShock.instance, logEntry, TraceLevel.Error); Console.WriteLine("Unable to write to log as log has been disposed."); Console.WriteLine("{0} - {1}: {2}: {3}", - DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture), + DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture), caller, level.ToString().ToUpper(), message); } } From f610fefc838d9f93bd786d6489d1f9bb2f968832 Mon Sep 17 00:00:00 2001 From: White Date: Tue, 24 Mar 2015 14:13:40 +1030 Subject: [PATCH 4/4] Revert UtcNow change in TextLog.cs --- TShockAPI/TextLog.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/TShockAPI/TextLog.cs b/TShockAPI/TextLog.cs index bd256c75..aef72b7a 100644 --- a/TShockAPI/TextLog.cs +++ b/TShockAPI/TextLog.cs @@ -212,7 +212,7 @@ namespace TShockAPI } var logEntry = string.Format("{0} - {1}: {2}: {3}", - DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture), + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture), caller, level.ToString().ToUpper(), message); try { @@ -224,7 +224,7 @@ namespace TShockAPI ServerApi.LogWriter.PluginWriteLine(TShock.instance, logEntry, TraceLevel.Error); Console.WriteLine("Unable to write to log as log has been disposed."); Console.WriteLine("{0} - {1}: {2}: {3}", - DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture), + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture), caller, level.ToString().ToUpper(), message); } }