Added some basic logging.

This commit is contained in:
Deathmax 2011-06-04 11:27:35 +08:00
parent 0b06bbd692
commit 35b584ab94
3 changed files with 166 additions and 0 deletions

157
TShockAPI/Log.cs Normal file
View file

@ -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;
/// <summary>
/// Creates the log file stream and sets the initial log level.
/// </summary>
/// <param name="filename">The output filename. This file will be overwritten if 'clear' is set.</param>
/// <param name="logLevel">The <see cref="LogLevel" /> value which sets the type of messages to output.</param>
/// <param name="clear">Whether or not to clear the log file on initialization.</param>
public static void Initialize(string filename, LogLevel logLevel, bool clear)
{
_filename = filename;
_logLevel = logLevel;
_logWriter = new StreamWriter(filename, !clear);
}
/// <summary>
/// Internal method which writes a message directly to the log file.
/// </summary>
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();
}
/// <summary>
/// Checks whether the log level contains the specified flag.
/// </summary>
/// <param name="type">The <see cref="LogLevel" /> value to check.</param>
private static bool MayWriteType(LogLevel type)
{
return ((_logLevel & type) == type);
}
/// <summary>
/// Writes data to the log file.
/// </summary>
/// <param name="message">The message to be written.</param>
public static void Data(String message)
{
Write(message, LogLevel.Data);
}
/// <summary>
/// Writes an error to the log file.
/// </summary>
/// <param name="message">The message to be written.</param>
public static void Error(String message)
{
Write(message, LogLevel.Error);
}
/// <summary>
/// Writes a warning to the log file.
/// </summary>
/// <param name="message">The message to be written.</param>
public static void Warn(String message)
{
Write(message, LogLevel.Warning);
}
/// <summary>
/// Writes an informative string to the log file.
/// </summary>
/// <param name="message">The message to be written.</param>
public static void Info(String message)
{
Write(message, LogLevel.Info);
}
/// <summary>
/// Writes a debug string to the log file.
/// </summary>
/// <param name="message">The message to be written.</param>
public static void Debug(String message)
{
if (!MayWriteType(LogLevel.Debug))
{
return;
}
Write(message, LogLevel.Debug);
}
}
}

View file

@ -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<Microsoft.Xna.Framework.GameTime>(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<Microsoft.Xna.Framework.Content.ContentManager>(OnLoadContent);
ServerHooks.OnChat -= new Action<int, string, HandledEventArgs>(OnChat);
ServerHooks.OnJoin -= new Action<int, AllowEventArgs>(OnJoin);
ServerHooks.OnCommand -= new ServerHooks.CommandD(ServerHooks_OnCommand);
NetHooks.OnPreGetData -= GetData;
NetHooks.OnGreetPlayer -= new NetHooks.GreetPlayerD(OnGreetPlayer);
NpcHooks.OnStrikeNpc -= new NpcHooks.StrikeNpcD(NpcHooks_OnStrikeNpc);

View file

@ -70,6 +70,7 @@ namespace TShockAPI
{
SendMessage(i, msg);
}
Log.Info("Broadcast: " + msg);
}
/// <summary>
@ -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));
}
/// <summary>
@ -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));
}
/// <summary>
@ -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);
}
/// <summary>