Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions TShockAPI/SqlLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ You should have received a copy of the GNU General Public License
using System.Data;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using MySql.Data.MySqlClient;
using TShockAPI.DB;
using TShockAPI.DB.Queries;
Expand Down Expand Up @@ -274,15 +273,7 @@ public void Write(string message, TraceLevel level)
if (!MayWriteType(level))
return;

var caller = "TShock";

var frame = new StackTrace().GetFrame(2);
if (frame != null)
{
var meth = frame.GetMethod();
if (meth != null && meth.DeclaringType != null)
caller = meth.DeclaringType.Name;
}
var caller = ResolveCaller(level);

try
{
Expand All @@ -298,7 +289,7 @@ public void Write(string message, TraceLevel level)
var success = true;
while (_failures.Count > 0 && success)
{
var info = _failures.First();
var info = _failures[0];

try
{
Expand Down Expand Up @@ -348,6 +339,19 @@ public void Write(string message, TraceLevel level)
}
}

private static string ResolveCaller(TraceLevel level)
{
if (level >= TraceLevel.Info)
return "TShock";

var caller = "TShock";
var frame = new StackFrame(3, false);
var meth = frame.GetMethod();
if (meth != null && meth.DeclaringType != null)
caller = meth.DeclaringType.Name;
return caller;
}

public void Dispose()
{
_backupLog.Dispose();
Expand Down
80 changes: 52 additions & 28 deletions TShockAPI/TextLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ namespace TShockAPI
public class TextLog : ILog, IDisposable
{
private readonly bool ClearFile;
private readonly object _writeLock = new object();
private static readonly TimeSpan FlushInterval = TimeSpan.FromSeconds(1);
private const int FlushBatchSize = 32;
private StreamWriter _logWriter;
private DateTime _lastFlushUtc = DateTime.UtcNow;
private int _writesSinceFlush;

/// <summary>
/// File name of the Text log
Expand Down Expand Up @@ -248,44 +253,63 @@ public void Write(string message, TraceLevel level)
{
if (!MayWriteType(level))
return;
if (_logWriter is null)
lock (_writeLock)
{
_logWriter = new StreamWriter(FileName, !ClearFile);
}
if (_logWriter is null)
{
_logWriter = new StreamWriter(FileName, !ClearFile);
}

var caller = "TShock";
var caller = ResolveCaller(level);
var logEntry = string.Format("{0} - {1}: {2}: {3}",
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture),
caller, level.ToString().ToUpper(), message);

var frame = new StackTrace().GetFrame(2);
if (frame != null)
{
var meth = frame.GetMethod();
if (meth != null && meth.DeclaringType != null)
caller = meth.DeclaringType.Name;
}
try
{
_logWriter.WriteLine(logEntry);
_writesSinceFlush++;

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
{
_logWriter.WriteLine(logEntry);
_logWriter.Flush();
}
catch (ObjectDisposedException)
{
ServerApi.LogWriter.PluginWriteLine(TShock.instance, logEntry, TraceLevel.Error);
Console.WriteLine("Unable to write to log as log has been disposed.");
Console.WriteLine("{0} - {1}: {2}: {3}",
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture),
caller, level.ToString().ToUpper(), message);
if (level <= TraceLevel.Warning || _writesSinceFlush >= FlushBatchSize || DateTime.UtcNow - _lastFlushUtc >= FlushInterval)
{
_logWriter.Flush();
_writesSinceFlush = 0;
_lastFlushUtc = DateTime.UtcNow;
}
}
catch (ObjectDisposedException)
{
ServerApi.LogWriter.PluginWriteLine(TShock.instance, logEntry, TraceLevel.Error);
Console.WriteLine("Unable to write to log as log has been disposed.");
Console.WriteLine("{0} - {1}: {2}: {3}",
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture),
caller, level.ToString().ToUpper(), message);
}
}
}

private static string ResolveCaller(TraceLevel level)
{
if (level >= TraceLevel.Info)
return "TShock";

var caller = "TShock";
var frame = new StackFrame(3, false);
var meth = frame.GetMethod();
if (meth != null && meth.DeclaringType != null)
caller = meth.DeclaringType.Name;
return caller;
}

public void Dispose()
{
if (_logWriter != null)
lock (_writeLock)
{
_logWriter.Dispose();
if (_logWriter != null)
{
_logWriter.Flush();
_logWriter.Dispose();
}
}
}
}
Expand Down