using System;
using System.IO;
using Woodpecker.Data;
using Woodpecker.Net.Game.Messages;
namespace Woodpecker.Core
{
///
/// Provides various functions for logging events to the main form and optionally to disk.
///
public static class Logging
{
#region Declares
///
/// A reference to the main form.
///
public static frmMain mainForm;
///
/// Specifies if to save logs to disk.
///
public static bool saveLogsToDisk = true;
///
/// Specifies if to log info messages.
///
public static bool logInfoMessages = true;
///
/// Specifies if to log warnings.
///
public static bool logWarnings = true;
///
/// Specifies if to log errors.
///
public static bool logErrors = true;
///
/// Specifies if to log client>server messages.
///
public static bool logClientMessages = false;
///
/// Specifies if to log server>client messages.
///
public static bool logServerMessages = false;
#endregion
public static void logHolyInfo(string Text)
{
System.Windows.Forms.Application.DoEvents();
mainForm.logInfo(Text);
}
///
/// Logs a plain line of text with a >, which is never written to logfile.
///
/// The text to log.
public static void logInfo(string Text)
{
if(logInfoMessages)
mainForm.logInfo(">> " + Text);
}
///
/// Logs a warning message with timestamp and assembly name.
///
/// The text to log.
public static void logWarning(string Text)
{
if (logWarnings)
mainForm.logWarning(Text);
}
///
/// Logs an error message with timestamp and assembly name.
///
/// The text to log.
public static void logError(string Text)
{
if (logErrors)
mainForm.logError(Text);
}
public static void logClientMessage(int connectionID, clientMessage Message)
{
if (logClientMessages)
mainForm.logClientMessage(connectionID, Message);
}
public static void logServerMessage(int connectionID, serverMessage Message)
{
if (logServerMessages)
mainForm.logServerMessage(connectionID, Message);
}
///
/// Creates a TextWriter instance and writes a string to the current log file in the /logs/ folder of the application.
///
/// The string to write.
public static void writeLogToFile(string s)
{
try
{
TextWriter Writer = new StreamWriter(Filesystem.startupPath + "/logs/" + DateTime.Now.ToShortDateString() + ".txt", true);
Writer.Write(s);
Writer.Flush();
Writer.Close();
Writer.Dispose();
}
catch { }
}
}
}