diff --git a/TShockAPI/Log.cs b/TShockAPI/Log.cs
new file mode 100644
index 00000000..d2d6f354
--- /dev/null
+++ b/TShockAPI/Log.cs
@@ -0,0 +1,157 @@
+using System;
+using System.Collections.Generic;
+using System.Diagnostics;
+using System.Globalization;
+using System.Linq;
+using System.IO;
+using System.Text;
+using System.Threading;
+
+namespace TShockAPI
+{
+ public enum LogLevel
+ {
+ None = 0,
+ Debug = 1,
+ Info = 2,
+ Warning = 4,
+ Error = 8,
+ Data = 16,
+ All = 31
+ }
+
+ public static class Log
+ {
+ private static string _filename;
+ private static LogLevel _logLevel;
+ private static StreamWriter _logWriter;
+
+ ///
+ /// 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 static void Initialize(string filename, LogLevel logLevel, bool clear)
+ {
+ _filename = filename;
+ _logLevel = logLevel;
+
+ _logWriter = new StreamWriter(filename, !clear);
+ }
+
+ ///
+ /// Internal method which writes a message directly to the log file.
+ ///
+ private static void Write(String message, LogLevel level)
+ {
+ StackTrace trace = new StackTrace();
+ StackFrame frame = null;
+
+ frame = trace.GetFrame(2);
+
+ string caller = "TShock: ";
+
+ /*if (frame != null && frame.GetMethod().DeclaringType != null)
+ {
+ caller = frame.GetMethod().DeclaringType.Name + ": ";
+ }*/
+
+ switch (level)
+ {
+ case LogLevel.Debug:
+ message = "DEBUG: " + message;
+ break;
+ case LogLevel.Info:
+ message = "INFO: " + message;
+ break;
+ case LogLevel.Warning:
+ message = "WARNING: " + message;
+ break;
+ case LogLevel.Error:
+ message = "ERROR: " + message;
+ break;
+ }
+
+ /*try
+ {
+ _logWriter = new StreamWriter(_filename, true);
+ }
+ catch (IOException)
+ {
+ _logWriter = new StreamWriter(_filename + "." + Process.GetCurrentProcess().Id.ToString(), true);
+ }*/
+
+ String text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture) + " - " + caller + message;
+
+ Console.WriteLine(text);
+
+ if (!MayWriteType(level))
+ {
+ return;
+ }
+
+ _logWriter.WriteLine(text);
+ _logWriter.Flush();
+ }
+
+ ///
+ /// Checks whether the log level contains the specified flag.
+ ///
+ /// The value to check.
+ private static bool MayWriteType(LogLevel type)
+ {
+ return ((_logLevel & type) == type);
+ }
+
+ ///
+ /// Writes data to the log file.
+ ///
+ /// The message to be written.
+ public static void Data(String message)
+ {
+ Write(message, LogLevel.Data);
+ }
+
+ ///
+ /// Writes an error to the log file.
+ ///
+ /// The message to be written.
+ public static void Error(String message)
+ {
+ Write(message, LogLevel.Error);
+ }
+
+ ///
+ /// Writes a warning to the log file.
+ ///
+ /// The message to be written.
+ public static void Warn(String message)
+ {
+ Write(message, LogLevel.Warning);
+ }
+
+ ///
+ /// Writes an informative string to the log file.
+ ///
+ /// The message to be written.
+ public static void Info(String message)
+ {
+ Write(message, LogLevel.Info);
+ }
+
+ ///
+ /// Writes a debug string to the log file.
+ ///
+ /// The message to be written.
+ public static void Debug(String message)
+ {
+ if (!MayWriteType(LogLevel.Debug))
+ {
+ return;
+ }
+
+ Write(message, LogLevel.Debug);
+ }
+ }
+}
diff --git a/TShockAPI/TShock.cs b/TShockAPI/TShock.cs
index 6b83874b..c65c2257 100644
--- a/TShockAPI/TShock.cs
+++ b/TShockAPI/TShock.cs
@@ -103,6 +103,8 @@ namespace TShockAPI
public override void Initialize()
{
+ Log.Initialize(FileTools.SaveDir + "log.txt", LogLevel.Error, true);
+ Log.Info("Starting...");
GameHooks.OnPreInitialize += OnPreInit;
GameHooks.OnPostInitialize += OnPostInit;
GameHooks.OnUpdate += new Action(OnUpdate);
@@ -113,7 +115,9 @@ namespace TShockAPI
NetHooks.OnGreetPlayer += new NetHooks.GreetPlayerD(OnGreetPlayer);
NpcHooks.OnStrikeNpc += new NpcHooks.StrikeNpcD(NpcHooks_OnStrikeNpc);
ServerHooks.OnCommand += new ServerHooks.CommandD(ServerHooks_OnCommand);
+ Log.Info("Hooks initialized");
Commands.InitCommands();
+ Log.Info("Commands initialized");
}
void ServerHooks_OnCommand(string cmd, HandledEventArgs e)
@@ -129,6 +133,7 @@ namespace TShockAPI
GameHooks.OnLoadContent -= new Action(OnLoadContent);
ServerHooks.OnChat -= new Action(OnChat);
ServerHooks.OnJoin -= new Action(OnJoin);
+ ServerHooks.OnCommand -= new ServerHooks.CommandD(ServerHooks_OnCommand);
NetHooks.OnPreGetData -= GetData;
NetHooks.OnGreetPlayer -= new NetHooks.GreetPlayerD(OnGreetPlayer);
NpcHooks.OnStrikeNpc -= new NpcHooks.StrikeNpcD(NpcHooks_OnStrikeNpc);
diff --git a/TShockAPI/Tools.cs b/TShockAPI/Tools.cs
index 312f1697..31b62775 100644
--- a/TShockAPI/Tools.cs
+++ b/TShockAPI/Tools.cs
@@ -70,6 +70,7 @@ namespace TShockAPI
{
SendMessage(i, msg);
}
+ Log.Info("Broadcast: " + msg);
}
///
@@ -81,6 +82,7 @@ namespace TShockAPI
public static void SendMessage(int ply, string msg, float[] color)
{
NetMessage.SendData(0x19, ply, -1, msg, 255, color[0], color[1], color[2]);
+ Log.Info("Said: " + msg + " - To: " + Tools.FindPlayer(ply));
}
///
@@ -91,6 +93,7 @@ namespace TShockAPI
public static void SendMessage(int ply, string message)
{
NetMessage.SendData(0x19, ply, -1, message, 255, 0f, 255f, 0f);
+ Log.Info("Said: " + message + " - To: " + Tools.FindPlayer(ply));
}
///
@@ -231,6 +234,7 @@ namespace TShockAPI
public static void Kick(int ply, string reason)
{
NetMessage.SendData(0x2, ply, -1, reason, 0x0, 0f, 0f, 0f);
+ Log.Info("Kicked " + Tools.FindPlayer(ply) + " for : " + reason);
}
///