Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lazy initialize TextLog._logWriter #3023

Merged
merged 1 commit into from
Apr 13, 2024
Merged
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
14 changes: 11 additions & 3 deletions TShockAPI/TextLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ namespace TShockAPI
/// </summary>
public class TextLog : ILog, IDisposable
{
private readonly StreamWriter _logWriter;
private readonly bool ClearFile;
private StreamWriter _logWriter;

/// <summary>
/// File name of the Text log
Expand All @@ -44,7 +45,7 @@ public class TextLog : ILog, IDisposable
public TextLog(string filename, bool clear)
{
FileName = filename;
_logWriter = new StreamWriter(filename, !clear);
ClearFile = clear;
}

public bool MayWriteType(TraceLevel type)
Expand Down Expand Up @@ -247,6 +248,10 @@ public void Write(string message, TraceLevel level)
{
if (!MayWriteType(level))
return;
if (_logWriter is null)
{
_logWriter = new StreamWriter(FileName, !ClearFile);
}

var caller = "TShock";

Expand Down Expand Up @@ -278,7 +283,10 @@ public void Write(string message, TraceLevel level)

public void Dispose()
{
_logWriter.Dispose();
if (_logWriter != null)
{
_logWriter.Dispose();
}
}
}
}
1 change: 1 addition & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ Use past tense when adding new entries; sign your name off when you add or chang
* Added a method `TSPlayer.UpdateSection` with arguments `rectangle` and `isLoaded`, which will load some area from the server to the player. (@AgaSpace)
* Added a method `TSPlayer.GiveItem`, which has `TShockAPI.NetItem` structure in its arguments. (@AgaSpace)
* Added a property `TSPlayer.Hostile`, which gets pvp player mode. (@AgaSpace)
* Fixed bug where when the `UseSqlLogs` config property is true, an empty log file would still get created. (@ZakFahey)
* Fixed typo in `/gbuff`. (@sgkoishi, #2955)
* Rewrote the `.dockerignore` file into a denylist. (@timschumi)

Expand Down
Loading