Skip to content

Commit 1a52036

Browse files
committed
-nolog startup flag
The reason for this flag is if you're running a multi instance proxy setup, in which case multiple writes to ServerLog.txt would cause an error because multiple writers are writing to the same file. While you can use Attach() to replace the default writer with something else that doesn't have this problem, there is still a small window where the writer is opened and written to, making it possible for such a startup error to happen if the timing is bad. While this sets the default writer to nothing, you can still use Attach() to attach a new writer in your own plugin.
1 parent c6b0fd8 commit 1a52036

2 files changed

Lines changed: 27 additions & 14 deletions

File tree

TerrariaServerAPI/TerrariaApi.Server/LogWriterManager.cs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,20 @@ internal ILogWriter WrappedLogWriter
1717
}
1818
public string LogWriterName
1919
{
20-
get { return this.WrappedLogWriter.Name; }
20+
get { return this.WrappedLogWriter?.Name; }
2121
}
2222

23-
internal LogWriterManager()
23+
/// <param name="enabled">
24+
/// If set to false, no default log writer will be set at first, but a new one can be added with
25+
/// <see cref="Attach(ILogWriter)"/> later.
26+
/// </param>
27+
internal LogWriterManager(bool enabled)
2428
{
25-
this.DefaultLogWriter = new ServerLogWriter();
26-
this.WrappedLogWriter = this.DefaultLogWriter;
29+
if (enabled)
30+
{
31+
this.DefaultLogWriter = new ServerLogWriter();
32+
this.WrappedLogWriter = this.DefaultLogWriter;
33+
}
2734
}
2835

2936
public void Attach(ILogWriter newLogWriter)
@@ -57,7 +64,7 @@ public void Attach(ILogWriter newLogWriter)
5764

5865
if (detachException != null)
5966
this.ServerWriteLine(
60-
string.Format("Log writer \"{0}\" had thrown an unexpected exception:\n{1}", prevLogWriter.Name, detachException),
67+
string.Format("Log writer \"{0}\" has thrown an unexpected exception:\n{1}", prevLogWriter?.Name, detachException),
6168
TraceLevel.Error);
6269
}
6370

@@ -72,7 +79,7 @@ internal void Deatch()
7279
}
7380
catch (Exception ex)
7481
{
75-
DefaultLogWriter.ServerWriteLine(
82+
DefaultLogWriter?.ServerWriteLine(
7683
string.Format("Log writer \"{0}\" has thrown an unexpected exception:\n{1}", this.LogWriterName, ex), TraceLevel.Error);
7784
}
7885

@@ -81,27 +88,33 @@ internal void Deatch()
8188

8289
internal void ServerWriteLine(string message, TraceLevel kind)
8390
{
91+
if (this.WrappedLogWriter == null)
92+
return;
93+
8494
try
8595
{
8696
this.WrappedLogWriter.ServerWriteLine(message, kind);
8797
}
8898
catch (Exception ex)
8999
{
90-
this.DefaultLogWriter.ServerWriteLine(string.Format(
100+
this.DefaultLogWriter?.ServerWriteLine(string.Format(
91101
"The attached log writer \"{0}\" has thrown an unexpected exception:\n{1}", this.LogWriterName, ex),
92102
TraceLevel.Error);
93103
}
94104
}
95105

96106
public void PluginWriteLine(TerrariaPlugin plugin, string message, TraceLevel kind)
97107
{
108+
if (this.WrappedLogWriter == null)
109+
return;
110+
98111
try
99112
{
100113
this.WrappedLogWriter.PluginWriteLine(plugin, message, kind);
101114
}
102115
catch (Exception ex)
103116
{
104-
DefaultLogWriter.ServerWriteLine(string.Format(
117+
DefaultLogWriter?.ServerWriteLine(string.Format(
105118
"The attached log writer \"{0}\" has thrown an unexpected exception:\n{1}", this.LogWriterName, ex),
106119
TraceLevel.Error);
107120
}

TerrariaServerAPI/TerrariaApi.Server/ServerApi.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using System.Collections.ObjectModel;
44
using System.Diagnostics;
55
using System.IO;
6-
using System.Net;
76
using System.Reflection;
87
using System.Linq;
98
using Terraria;
@@ -68,8 +67,9 @@ public static bool IsWorldRunning
6867

6968
static ServerApi()
7069
{
70+
Dictionary<string, string> args = Utils.ParseArguements(Environment.GetCommandLineArgs());
7171
Hooks = new HookManager();
72-
LogWriter = new LogWriterManager();
72+
LogWriter = new LogWriterManager(enabled: !args.ContainsKey("-nolog"));
7373
Profiler = new ProfilerManager();
7474

7575
UseAsyncSocketsInMono = false;
@@ -133,6 +133,8 @@ internal static void HandleCommandLine(string[] parms)
133133

134134
foreach (KeyValuePair<string, string> arg in args)
135135
{
136+
// Note that the flag -nolog also exists in the constructor, but it can't be here because
137+
// the log writer initializes before this code is run
136138
switch (arg.Key.ToLower())
137139
{
138140
case "-ignoreversion":
@@ -162,6 +164,7 @@ internal static void HandleCommandLine(string[] parms)
162164
break;
163165
}
164166
case "-players":
167+
case "-maxplayers":
165168
{
166169
int playerCount;
167170
if (!Int32.TryParse(arg.Value, out playerCount))
@@ -175,16 +178,13 @@ internal static void HandleCommandLine(string[] parms)
175178

176179
break;
177180
}
178-
case "-maxplayers":
179-
goto case "-players";
180181
case "-pass":
182+
case "-password":
181183
{
182184
Netplay.ServerPassword = arg.Value;
183185

184186
break;
185187
}
186-
case "-password":
187-
goto case "-pass";
188188
case "-worldname":
189189
{
190190
game.SetWorldName(arg.Value);

0 commit comments

Comments
 (0)