Adds a queue to the SQL log and fixes some issues pointed out
This commit is contained in:
parent
0459dfca11
commit
8ff36c6709
4 changed files with 65 additions and 33 deletions
|
|
@ -42,11 +42,6 @@ namespace TShockAPI
|
|||
/// </summary>
|
||||
string Name { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if the ILog is using SQL or false if not
|
||||
/// </summary>
|
||||
bool Sql { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether the log level contains the specified flag.
|
||||
/// </summary>
|
||||
|
|
|
|||
|
|
@ -175,14 +175,6 @@ namespace TShockAPI
|
|||
Debug(String.Format(format, args));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Disposes objects that are being used.
|
||||
/// </summary>
|
||||
public static void Dispose()
|
||||
{
|
||||
TShock.Log.Dispose();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Internal method which writes a message directly to the log file.
|
||||
/// </summary>
|
||||
|
|
|
|||
|
|
@ -17,13 +17,29 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Class inheriting ILog for writing logs to TShock's SQL database
|
||||
/// </summary>
|
||||
|
|
@ -32,7 +48,7 @@ namespace TShockAPI
|
|||
private readonly LogLevel _logLevel;
|
||||
private readonly IDbConnection _database;
|
||||
private readonly TextLog _backupLog;
|
||||
private int _failures;
|
||||
private readonly List<LogInfo> _failures = new List<LogInfo>(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();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue