From 8ff36c67097860b836bb07f958be37b4fa077d6e Mon Sep 17 00:00:00 2001 From: White Date: Thu, 26 Feb 2015 15:37:36 +1030 Subject: [PATCH] Adds a queue to the SQL log and fixes some issues pointed out --- TShockAPI/ILog.cs | 5 --- TShockAPI/Log.cs | 8 ----- TShockAPI/SqlLog.cs | 75 ++++++++++++++++++++++++++++++++++++--------- TShockAPI/TShock.cs | 10 +++--- 4 files changed, 65 insertions(+), 33 deletions(-) diff --git a/TShockAPI/ILog.cs b/TShockAPI/ILog.cs index 906498ce..6ef0e29e 100644 --- a/TShockAPI/ILog.cs +++ b/TShockAPI/ILog.cs @@ -42,11 +42,6 @@ namespace TShockAPI /// string Name { get; } - /// - /// Returns true if the ILog is using SQL or false if not - /// - bool Sql { get; } - /// /// Checks whether the log level contains the specified flag. /// diff --git a/TShockAPI/Log.cs b/TShockAPI/Log.cs index 782dbe36..cab2a1e4 100644 --- a/TShockAPI/Log.cs +++ b/TShockAPI/Log.cs @@ -175,14 +175,6 @@ namespace TShockAPI Debug(String.Format(format, args)); } - /// - /// Disposes objects that are being used. - /// - public static void Dispose() - { - TShock.Log.Dispose(); - } - /// /// Internal method which writes a message directly to the log file. /// diff --git a/TShockAPI/SqlLog.cs b/TShockAPI/SqlLog.cs index 8aed014f..3655149f 100644 --- a/TShockAPI/SqlLog.cs +++ b/TShockAPI/SqlLog.cs @@ -17,13 +17,29 @@ along with this program. If not, see . */ using System; +using System.Collections.Generic; using System.Data; using System.Diagnostics; using System.Globalization; +using System.Linq; using TShockAPI.DB; namespace TShockAPI { + struct LogInfo + { + public string timestamp; + public string message; + public string caller; + public LogLevel logLevel; + + public override string ToString() + { + return String.Format("Message: {0}: {1}: {2}", + caller, logLevel.ToString().ToUpper(), message); + } + } + /// /// Class inheriting ILog for writing logs to TShock's SQL database /// @@ -32,7 +48,7 @@ namespace TShockAPI private readonly LogLevel _logLevel; private readonly IDbConnection _database; private readonly TextLog _backupLog; - private int _failures; + private readonly List _failures = new List(TShock.Config.RevertToTextLogsOnSqlFailures); private bool _useTextLog; public string Name @@ -40,11 +56,6 @@ namespace TShockAPI get { return "SQL Log Writer"; } } - public bool Sql - { - get { return true; } - } - public SqlLog(LogLevel logLevel, IDbConnection db, string textlogFilepath, bool clearTextLog) { _logLevel = logLevel; @@ -218,25 +229,61 @@ namespace TShockAPI _backupLog.Write(message, level); 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); - if (_failures > 0) - _failures--; + if (_failures.Count > 0) + { + var info = _failures.Last(); + var success = true; + + try + { + _database.Query("INSERT INTO Logs (LogLevel, TimeStamp, Caller, Message) VALUES (@0, @1, @2, @3)", + info.logLevel, info.timestamp, info.caller, info.message); + } + catch (Exception ex) + { + success = false; + _failures.Add(new LogInfo + { + caller = "TShock", + logLevel = LogLevel.Error, + message = String.Format("SQL Log insert query failed: {0}", ex), + timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture) + }); + } + + if (success) + _failures.RemoveAt(_failures.Count - 1); + } } catch (Exception ex) { _backupLog.ConsoleError("SQL Log insert query failed: {0}", ex); - _failures++; - _backupLog.Error("SQL logging will revert to text logging if {0} more failures occur.", - TShock.Config.RevertToTextLogsOnSqlFailures - _failures); - if (_failures >= TShock.Config.RevertToTextLogsOnSqlFailures) + _failures.Add(new LogInfo { - _useTextLog = true; - _backupLog.ConsoleError("SQL Logging disabled due to errors. Reverting to text logging."); + logLevel = level, + message = message, + caller = caller, + timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture) + }); + } + + if (_failures.Count >= TShock.Config.RevertToTextLogsOnSqlFailures) + { + _useTextLog = true; + _backupLog.ConsoleError("SQL Logging disabled due to errors. Reverting to text logging."); + + for (var i = _failures.Count - 1; i >= 0; --i) + { + _backupLog.Write(String.Format("SQL log failed at: {0}. {1}", _failures[i].timestamp, _failures[i]), + LogLevel.Error); } + _failures.Clear(); } } diff --git a/TShockAPI/TShock.cs b/TShockAPI/TShock.cs index 5c0df0b5..b61b88bc 100755 --- a/TShockAPI/TShock.cs +++ b/TShockAPI/TShock.cs @@ -157,9 +157,9 @@ namespace TShockAPI { logPathSetupWarning = "Could not apply the given log path / log format, defaults will be used. Exception details:\n" + ex; - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(logPathSetupWarning); - Console.ForegroundColor = ConsoleColor.Gray; + + ServerApi.LogWriter.PluginWriteLine(this, logPathSetupWarning, TraceLevel.Error); + // Problem with the log path or format use the default logFilename = Path.Combine(LogPathDefault, now.ToString(LogFormatDefault) + ".log"); } @@ -197,9 +197,7 @@ namespace TShockAPI } catch (MySqlException ex) { - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(ex); - Console.ResetColor(); + ServerApi.LogWriter.PluginWriteLine(this, ex.ToString(), TraceLevel.Error); throw new Exception("MySql not setup correctly"); } }