diff --git a/TShockAPI/ILog.cs b/TShockAPI/ILog.cs index a8c65eaf..cae6755b 100644 --- a/TShockAPI/ILog.cs +++ b/TShockAPI/ILog.cs @@ -17,36 +17,25 @@ along with this program. If not, see . */ using System; +using System.Diagnostics; namespace TShockAPI { - [Flags] - public enum LogLevel - { - None = 0, - Debug = 1, - Info = 2, - Warning = 4, - Error = 8, - Data = 16, - All = 31 - } - -/// -/// Logging interface -/// + /// + /// Logging interface + /// public interface ILog { /// - /// Log name + /// Log file name /// - string Name { get; } + string FileName { get; set; } /// /// 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. @@ -57,7 +46,7 @@ namespace TShockAPI /// /// Writes an informative string to the log and to the console. /// - /// The format of the message to be written. + /// The format of the message to be written. /// The format arguments. void ConsoleInfo(string format, params object[] args); @@ -92,6 +81,7 @@ namespace TShockAPI /// /// The message to be written. void Error(string message); + /// /// Writes an error to the log. /// @@ -130,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. @@ -150,4 +140,4 @@ namespace TShockAPI /// void Dispose(); } -} +} \ No newline at end of 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 bb3987c0..50a7e1bc 100644 --- a/TShockAPI/SqlLog.cs +++ b/TShockAPI/SqlLog.cs @@ -31,11 +31,11 @@ namespace TShockAPI public string timestamp; public string message; public string caller; - public LogLevel logLevel; + public TraceLevel logLevel; public override string ToString() { - return String.Format("Message: {0}: {1}: {2}", + return string.Format("Message: {0}: {1}: {2}", caller, logLevel.ToString().ToUpper(), message); } } @@ -45,36 +45,38 @@ 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); private bool _useTextLog; - public string Name - { - get { return "SQL Log Writer"; } - } + public string FileName { get; set; } - public SqlLog(LogLevel logLevel, IDbConnection db, string textlogFilepath, bool clearTextLog) + /// + /// Sets the database connection and the initial log level. + /// + /// + /// File path to a backup text log in case the SQL log fails + /// + public SqlLog(IDbConnection db, string textlogFilepath, bool clearTextLog) { - _logLevel = logLevel; + FileName = string.Format("{0}://database", db.GetSqlType()); _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; } /// /// 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); } /// @@ -84,16 +86,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); } /// @@ -103,19 +105,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); } /// @@ -125,16 +127,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); } /// @@ -144,16 +146,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); } /// @@ -163,19 +165,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); } /// @@ -185,16 +187,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); } /// @@ -204,10 +206,10 @@ namespace TShockAPI /// The format arguments. public void Debug(string format, params object[] args) { - Debug(String.Format(format, args)); + Debug(string.Format(format, args)); } - public void Write(string message, LogLevel level) + public void Write(string message, TraceLevel level) { if (!MayWriteType(level)) return; @@ -238,7 +240,7 @@ namespace TShockAPI while (_failures.Count > 0 && success) { var info = _failures.First(); - + try { _database.Query("INSERT INTO Logs (LogLevel, TimeStamp, Caller, Message) VALUES (@0, @1, @2, @3)", @@ -250,8 +252,8 @@ namespace TShockAPI _failures.Add(new LogInfo { caller = "TShock", - logLevel = LogLevel.Error, - message = String.Format("SQL Log insert query failed: {0}", ex), + 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) }); } @@ -278,10 +280,10 @@ namespace TShockAPI _useTextLog = true; _backupLog.ConsoleError("SQL Logging disabled due to errors. Reverting to text logging."); - foreach(var logInfo in _failures) + foreach (var logInfo in _failures) { - _backupLog.Write(String.Format("SQL log failed at: {0}. {1}", logInfo.timestamp, logInfo), - LogLevel.Error); + _backupLog.Write(string.Format("SQL log failed at: {0}. {1}", logInfo.timestamp, logInfo), + 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 9812e168..aef72b7a 100644 --- a/TShockAPI/TextLog.cs +++ b/TShockAPI/TextLog.cs @@ -30,51 +30,35 @@ namespace TShockAPI public class TextLog : ILog, IDisposable { private readonly StreamWriter _logWriter; - private readonly LogLevel _logLevel; /// - /// Log file name + /// File name of the Text log /// - public static string fileName { get; private set; } - - /// - /// Name of the TextLog - /// - public string Name - { - get { return "Text Log Writer"; } - } - - public bool Sql - { - get { return false; } - } + public string FileName { get; set; } /// /// 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; + FileName = filename; _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); } /// @@ -84,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); } /// @@ -103,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); } /// @@ -125,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); } /// @@ -144,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); } /// @@ -163,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); } /// @@ -185,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); } /// @@ -204,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)); } /// @@ -212,7 +196,7 @@ namespace TShockAPI /// /// /// - public void Write(string message, LogLevel level) + public void Write(string message, TraceLevel level) { if (!MayWriteType(level)) return; @@ -227,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