diff --git a/TShockAPI/ILog.cs b/TShockAPI/ILog.cs index 2b1c0f66..cae6755b 100644 --- a/TShockAPI/ILog.cs +++ b/TShockAPI/ILog.cs @@ -17,51 +17,10 @@ along with this program. If not, see . */ using System; +using System.Diagnostics; namespace TShockAPI { - /// - /// Flags to define which types of message are logged - /// - [Flags] - public enum LogLevel - { - /// - /// No messages will be logged - /// - None = 0, - - /// - /// Debug messages will be logged - /// - Debug = 1, - - /// - /// Informative messages will be logged - /// - Info = 2, - - /// - /// Warning message will be logged - /// - Warning = 4, - - /// - /// Error messages will be logged - /// - Error = 8, - - /// - /// Data messages will be logged - /// - Data = 16, - - /// - /// All messages will be logged - /// - All = 31 - } - /// /// Logging interface /// @@ -76,7 +35,7 @@ namespace TShockAPI /// Checks whether the log level contains the specified flag. /// /// The value to check. - bool MayWriteType(LogLevel type); + bool MayWriteType(TraceLevel type); /// /// Writes an informative string to the log and to the console. @@ -161,13 +120,13 @@ namespace TShockAPI /// /// Message to write /// LogLevel assosciated with the message - void Write(string message, LogLevel level); + void Write(string message, TraceLevel level); /// /// Writes a debug string to the log file. /// /// The message to be written. - void Debug(String message); + void Debug(string message); /// /// Writes a debug string to the log file. diff --git a/TShockAPI/Log.cs b/TShockAPI/Log.cs index cab2a1e4..93a2d0c1 100644 --- a/TShockAPI/Log.cs +++ b/TShockAPI/Log.cs @@ -17,6 +17,7 @@ along with this program. If not, see . */ using System; +using System.Diagnostics; namespace TShockAPI { @@ -27,9 +28,9 @@ namespace TShockAPI /// /// The message to be written. [Obsolete("Please use TShock.Log.Data")] - public static void Data(String message) + public static void Data(string message) { - Write(message, LogLevel.Data); + Write(message, TraceLevel.Verbose); } /// @@ -40,7 +41,7 @@ namespace TShockAPI [Obsolete("Please use TShock.Log.Data")] public static void Data(string format, params object[] args) { - Data(String.Format(format, args)); + Data(string.Format(format, args)); } /// @@ -48,9 +49,9 @@ namespace TShockAPI /// /// The message to be written. [Obsolete("Please use TShock.Log.Error")] - public static void Error(String message) + public static void Error(string message) { - Write(message, LogLevel.Error); + Write(message, TraceLevel.Error); } /// @@ -61,7 +62,7 @@ namespace TShockAPI [Obsolete("Please use TShock.Log.Error")] public static void Error(string format, params object[] args) { - Error(String.Format(format, args)); + Error(string.Format(format, args)); } /// @@ -69,12 +70,12 @@ namespace TShockAPI /// /// The message to be written. [Obsolete("Please use TShock.Log.ConsoleError")] - public static void ConsoleError(String message) + public static void ConsoleError(string message) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(message); Console.ForegroundColor = ConsoleColor.Gray; - Write(message, LogLevel.Error); + Write(message, TraceLevel.Error); } /// @@ -85,7 +86,7 @@ namespace TShockAPI [Obsolete("Please use TShock.Log.ConsoleError")] public static void ConsoleError(string format, params object[] args) { - ConsoleError(String.Format(format, args)); + ConsoleError(string.Format(format, args)); } /// @@ -93,9 +94,9 @@ namespace TShockAPI /// /// The message to be written. [Obsolete("Please use TShock.Log.Warn")] - public static void Warn(String message) + public static void Warn(string message) { - Write(message, LogLevel.Warning); + Write(message, TraceLevel.Warning); } /// @@ -106,7 +107,7 @@ namespace TShockAPI [Obsolete("Please use TShock.Log.Warn")] public static void Warn(string format, params object[] args) { - Warn(String.Format(format, args)); + Warn(string.Format(format, args)); } /// @@ -114,9 +115,9 @@ namespace TShockAPI /// /// The message to be written. [Obsolete("Please use TShock.Log.Info")] - public static void Info(String message) + public static void Info(string message) { - Write(message, LogLevel.Info); + Write(message, TraceLevel.Info); } /// @@ -127,7 +128,7 @@ namespace TShockAPI [Obsolete("Please use TShock.Log.Info")] public static void Info(string format, params object[] args) { - Info(String.Format(format, args)); + Info(string.Format(format, args)); } /// @@ -135,12 +136,12 @@ namespace TShockAPI /// /// The message to be written. [Obsolete("Please use TShock.Log.ConsoleInfo")] - public static void ConsoleInfo(String message) + public static void ConsoleInfo(string message) { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine(message); Console.ForegroundColor = ConsoleColor.Gray; - Write(message, LogLevel.Info); + Write(message, TraceLevel.Info); } /// @@ -151,7 +152,7 @@ namespace TShockAPI [Obsolete("Please use TShock.Log.ConsoleInfo")] public static void ConsoleInfo(string format, params object[] args) { - ConsoleInfo(String.Format(format, args)); + ConsoleInfo(string.Format(format, args)); } /// @@ -159,9 +160,9 @@ namespace TShockAPI /// /// The message to be written. [Obsolete("Please use TShock.Log.Debug")] - public static void Debug(String message) + public static void Debug(string message) { - Write(message, LogLevel.Debug); + Write(message, TraceLevel.Verbose); } /// @@ -172,13 +173,13 @@ namespace TShockAPI [Obsolete("Please use TShock.Log.Debug")] public static void Debug(string format, params object[] args) { - Debug(String.Format(format, args)); + Debug(string.Format(format, args)); } /// /// Internal method which writes a message directly to the log file. /// - private static void Write(String message, LogLevel level) + private static void Write(string message, TraceLevel level) { TShock.Log.Write(message, level); } diff --git a/TShockAPI/SqlLog.cs b/TShockAPI/SqlLog.cs index 763eca8a..50a7e1bc 100644 --- a/TShockAPI/SqlLog.cs +++ b/TShockAPI/SqlLog.cs @@ -31,7 +31,7 @@ namespace TShockAPI public string timestamp; public string message; public string caller; - public LogLevel logLevel; + public TraceLevel logLevel; public override string ToString() { @@ -45,7 +45,6 @@ namespace TShockAPI /// public class SqlLog : ILog, IDisposable { - private readonly LogLevel _logLevel; private readonly IDbConnection _database; private readonly TextLog _backupLog; private readonly List _failures = new List(TShock.Config.RevertToTextLogsOnSqlFailures); @@ -56,21 +55,19 @@ namespace TShockAPI /// /// Sets the database connection and the initial log level. /// - /// /// /// File path to a backup text log in case the SQL log fails /// - public SqlLog(LogLevel logLevel, IDbConnection db, string textlogFilepath, bool clearTextLog) + public SqlLog(IDbConnection db, string textlogFilepath, bool clearTextLog) { FileName = string.Format("{0}://database", db.GetSqlType()); - _logLevel = logLevel; _database = db; - _backupLog = new TextLog(textlogFilepath, logLevel, clearTextLog); + _backupLog = new TextLog(textlogFilepath, clearTextLog); } - public bool MayWriteType(LogLevel type) + public bool MayWriteType(TraceLevel type) { - return ((_logLevel & type) == type); + return type != TraceLevel.Off; } /// @@ -79,7 +76,7 @@ namespace TShockAPI /// The message to be written. public void Data(string message) { - Write(message, LogLevel.Data); + Write(message, TraceLevel.Verbose); } /// @@ -98,7 +95,7 @@ namespace TShockAPI /// The message to be written. public void Error(string message) { - Write(message, LogLevel.Error); + Write(message, TraceLevel.Error); } /// @@ -120,7 +117,7 @@ namespace TShockAPI Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(message); Console.ForegroundColor = ConsoleColor.Gray; - Write(message, LogLevel.Error); + Write(message, TraceLevel.Error); } /// @@ -139,7 +136,7 @@ namespace TShockAPI /// The message to be written. public void Warn(string message) { - Write(message, LogLevel.Warning); + Write(message, TraceLevel.Warning); } /// @@ -158,7 +155,7 @@ namespace TShockAPI /// The message to be written. public void Info(string message) { - Write(message, LogLevel.Info); + Write(message, TraceLevel.Info); } /// @@ -180,7 +177,7 @@ namespace TShockAPI Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine(message); Console.ForegroundColor = ConsoleColor.Gray; - Write(message, LogLevel.Info); + Write(message, TraceLevel.Info); } /// @@ -199,7 +196,7 @@ namespace TShockAPI /// The message to be written. public void Debug(string message) { - Write(message, LogLevel.Debug); + Write(message, TraceLevel.Verbose); } /// @@ -212,7 +209,7 @@ namespace TShockAPI Debug(string.Format(format, args)); } - public void Write(string message, LogLevel level) + public void Write(string message, TraceLevel level) { if (!MayWriteType(level)) return; @@ -255,7 +252,7 @@ namespace TShockAPI _failures.Add(new LogInfo { caller = "TShock", - logLevel = LogLevel.Error, + 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) }); @@ -286,7 +283,7 @@ namespace TShockAPI foreach (var logInfo in _failures) { _backupLog.Write(string.Format("SQL log failed at: {0}. {1}", logInfo.timestamp, logInfo), - LogLevel.Error); + TraceLevel.Error); } _failures.Clear(); } diff --git a/TShockAPI/TShock.cs b/TShockAPI/TShock.cs index f7f32205..24d2b82a 100755 --- a/TShockAPI/TShock.cs +++ b/TShockAPI/TShock.cs @@ -202,15 +202,10 @@ namespace TShockAPI throw new Exception("Invalid storage type"); } -#if DEBUG - var level = LogLevel.All; -#else - var level = LogLevel.All & ~LogLevel.Debug; -#endif if (Config.UseSqlLogs) - Log = new SqlLog(level, DB, logFilename, LogClear); + Log = new SqlLog(DB, logFilename, LogClear); else - Log = new TextLog(logFilename, level, LogClear); + Log = new TextLog(logFilename, LogClear); if (File.Exists(Path.Combine(SavePath, "tshock.pid"))) { diff --git a/TShockAPI/TextLog.cs b/TShockAPI/TextLog.cs index 1b662ec8..aef72b7a 100644 --- a/TShockAPI/TextLog.cs +++ b/TShockAPI/TextLog.cs @@ -30,7 +30,6 @@ namespace TShockAPI public class TextLog : ILog, IDisposable { private readonly StreamWriter _logWriter; - private readonly LogLevel _logLevel; /// /// File name of the Text log @@ -41,27 +40,25 @@ namespace TShockAPI /// Creates the log file stream and sets the initial log level. /// /// The output filename. This file will be overwritten if 'clear' is set. - /// The value which sets the type of messages to output. /// Whether or not to clear the log file on initialization. - public TextLog(string filename, LogLevel logLevel, bool clear) + public TextLog(string filename, bool clear) { FileName = filename; - _logLevel = logLevel; _logWriter = new StreamWriter(filename, !clear); } - public bool MayWriteType(LogLevel type) + public bool MayWriteType(TraceLevel type) { - return ((_logLevel & type) == type); + return type != TraceLevel.Off; } /// /// Writes data to the log file. /// /// The message to be written. - public void Data(String message) + public void Data(string message) { - Write(message, LogLevel.Data); + Write(message, TraceLevel.Verbose); } /// @@ -71,16 +68,16 @@ namespace TShockAPI /// The format arguments. public void Data(string format, params object[] args) { - Data(String.Format(format, args)); + Data(string.Format(format, args)); } /// /// Writes an error to the log file. /// /// The message to be written. - public void Error(String message) + public void Error(string message) { - Write(message, LogLevel.Error); + Write(message, TraceLevel.Error); } /// @@ -90,19 +87,19 @@ namespace TShockAPI /// The format arguments. public void Error(string format, params object[] args) { - Error(String.Format(format, args)); + Error(string.Format(format, args)); } /// /// Writes an error to the log file. /// /// The message to be written. - public void ConsoleError(String message) + public void ConsoleError(string message) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(message); Console.ForegroundColor = ConsoleColor.Gray; - Write(message, LogLevel.Error); + Write(message, TraceLevel.Error); } /// @@ -112,16 +109,16 @@ namespace TShockAPI /// The format arguments. public void ConsoleError(string format, params object[] args) { - ConsoleError(String.Format(format, args)); + ConsoleError(string.Format(format, args)); } /// /// Writes a warning to the log file. /// /// The message to be written. - public void Warn(String message) + public void Warn(string message) { - Write(message, LogLevel.Warning); + Write(message, TraceLevel.Warning); } /// @@ -131,16 +128,16 @@ namespace TShockAPI /// The format arguments. public void Warn(string format, params object[] args) { - Warn(String.Format(format, args)); + Warn(string.Format(format, args)); } /// /// Writes an informative string to the log file. /// /// The message to be written. - public void Info(String message) + public void Info(string message) { - Write(message, LogLevel.Info); + Write(message, TraceLevel.Info); } /// @@ -150,19 +147,19 @@ namespace TShockAPI /// The format arguments. public void Info(string format, params object[] args) { - Info(String.Format(format, args)); + Info(string.Format(format, args)); } /// /// Writes an informative string to the log file. Also outputs to the console. /// /// The message to be written. - public void ConsoleInfo(String message) + public void ConsoleInfo(string message) { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine(message); Console.ForegroundColor = ConsoleColor.Gray; - Write(message, LogLevel.Info); + Write(message, TraceLevel.Info); } /// @@ -172,16 +169,16 @@ namespace TShockAPI /// The format arguments. public void ConsoleInfo(string format, params object[] args) { - ConsoleInfo(String.Format(format, args)); + ConsoleInfo(string.Format(format, args)); } /// /// Writes a debug string to the log file. /// /// The message to be written. - public void Debug(String message) + public void Debug(string message) { - Write(message, LogLevel.Debug); + Write(message, TraceLevel.Verbose); } /// @@ -191,7 +188,7 @@ namespace TShockAPI /// The format arguments. public void Debug(string format, params object[] args) { - Debug(String.Format(format, args)); + Debug(string.Format(format, args)); } /// @@ -199,7 +196,7 @@ namespace TShockAPI /// /// /// - public void Write(string message, LogLevel level) + public void Write(string message, TraceLevel level) { if (!MayWriteType(level)) return; @@ -214,7 +211,7 @@ namespace TShockAPI caller = meth.DeclaringType.Name; } - var logEntry = String.Format("{0} - {1}: {2}: {3}", + var logEntry = string.Format("{0} - {1}: {2}: {3}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture), caller, level.ToString().ToUpper(), message); try