ReSharper code reformat to match naming conventions and stuff
This commit is contained in:
parent
1147788154
commit
c6abbfe4d2
45 changed files with 11639 additions and 11342 deletions
256
TShockAPI/Log.cs
256
TShockAPI/Log.cs
|
|
@ -22,147 +22,147 @@ using System.IO;
|
|||
|
||||
namespace TShockAPI
|
||||
{
|
||||
public enum LogLevel
|
||||
{
|
||||
None = 0,
|
||||
Debug = 1,
|
||||
Info = 2,
|
||||
Warning = 4,
|
||||
Error = 8,
|
||||
Data = 16,
|
||||
All = 31
|
||||
}
|
||||
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;
|
||||
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;
|
||||
/// <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);
|
||||
}
|
||||
_logWriter = new StreamWriter(filename, !clear);
|
||||
}
|
||||
|
||||
/// <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>
|
||||
/// 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 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 an error to the log file.
|
||||
/// </summary>
|
||||
/// <param name="message">The message to be written.</param>
|
||||
public static void ConsoleError(String message)
|
||||
{
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
Console.WriteLine(message);
|
||||
Console.ForegroundColor = ConsoleColor.Gray;
|
||||
Write(message, LogLevel.Error);
|
||||
}
|
||||
/// <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 error to the log file.
|
||||
/// </summary>
|
||||
/// <param name="message">The message to be written.</param>
|
||||
public static void ConsoleError(String message)
|
||||
{
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
Console.WriteLine(message);
|
||||
Console.ForegroundColor = ConsoleColor.Gray;
|
||||
Write(message, LogLevel.Error);
|
||||
}
|
||||
|
||||
/// <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 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. Also outputs to the console.
|
||||
/// </summary>
|
||||
/// <param name="message">The message to be written.</param>
|
||||
public static void ConsoleInfo(String message)
|
||||
{
|
||||
Console.ForegroundColor = ConsoleColor.Yellow;
|
||||
Console.WriteLine(message);
|
||||
Console.ForegroundColor = ConsoleColor.Gray;
|
||||
Write(message, LogLevel.Info);
|
||||
}
|
||||
/// <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)
|
||||
{
|
||||
Write(message, LogLevel.Debug);
|
||||
}
|
||||
/// <summary>
|
||||
/// Writes an informative string to the log file. Also outputs to the console.
|
||||
/// </summary>
|
||||
/// <param name="message">The message to be written.</param>
|
||||
public static void ConsoleInfo(String message)
|
||||
{
|
||||
Console.ForegroundColor = ConsoleColor.Yellow;
|
||||
Console.WriteLine(message);
|
||||
Console.ForegroundColor = ConsoleColor.Gray;
|
||||
Write(message, LogLevel.Info);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Disposes objects that are being used.
|
||||
/// </summary>
|
||||
public static void Dispose()
|
||||
{
|
||||
_logWriter.Dispose();
|
||||
}
|
||||
/// <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)
|
||||
{
|
||||
Write(message, LogLevel.Debug);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Internal method which writes a message directly to the log file.
|
||||
/// </summary>
|
||||
private static void Write(String message, LogLevel level)
|
||||
{
|
||||
if (!MayWriteType(level))
|
||||
{
|
||||
return;
|
||||
}
|
||||
/// <summary>
|
||||
/// Disposes objects that are being used.
|
||||
/// </summary>
|
||||
public static void Dispose()
|
||||
{
|
||||
_logWriter.Dispose();
|
||||
}
|
||||
|
||||
string caller = "TShock";
|
||||
/// <summary>
|
||||
/// Internal method which writes a message directly to the log file.
|
||||
/// </summary>
|
||||
private static void Write(String message, LogLevel level)
|
||||
{
|
||||
if (!MayWriteType(level))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
StackFrame frame = new StackTrace().GetFrame(2);
|
||||
if (frame != null)
|
||||
{
|
||||
var meth = frame.GetMethod();
|
||||
if (meth != null)
|
||||
caller = meth.DeclaringType.Name;
|
||||
}
|
||||
string caller = "TShock";
|
||||
|
||||
_logWriter.WriteLine(string.Format("{0} - {1}: {2}: {3}",
|
||||
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture),
|
||||
caller, level.ToString().ToUpper(), message));
|
||||
_logWriter.Flush();
|
||||
}
|
||||
}
|
||||
StackFrame frame = new StackTrace().GetFrame(2);
|
||||
if (frame != null)
|
||||
{
|
||||
var meth = frame.GetMethod();
|
||||
if (meth != null)
|
||||
caller = meth.DeclaringType.Name;
|
||||
}
|
||||
|
||||
_logWriter.WriteLine(string.Format("{0} - {1}: {2}: {3}",
|
||||
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture),
|
||||
caller, level.ToString().ToUpper(), message));
|
||||
_logWriter.Flush();
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue