Add timestamps to server log files

Servers are now writing timestamps to the log files using the the ISO 8601 timestamp format defined in the game server settings.
This commit is contained in:
4mfie
2019-06-27 16:37:28 -04:00
committed by Paul Chote
parent ebc533ed53
commit ff02b8ba06
5 changed files with 31 additions and 15 deletions

View File

@@ -295,7 +295,7 @@ namespace OpenRA
Log.AddChannel("perf", "perf.log");
Log.AddChannel("debug", "debug.log");
Log.AddChannel("server", "server.log");
Log.AddChannel("server", "server.log", true);
Log.AddChannel("sound", "sound.log");
Log.AddChannel("graphics", "graphics.log");
Log.AddChannel("geoip", "geoip.log");

View File

@@ -124,7 +124,7 @@ namespace OpenRA.Server
public Server(IPEndPoint endpoint, ServerSettings settings, ModData modData, bool dedicated)
{
Log.AddChannel("server", "server.log");
Log.AddChannel("server", "server.log", true);
listener = new TcpListener(endpoint);
listener.Start();

View File

@@ -77,7 +77,8 @@ namespace OpenRA
[Desc("Enable client-side report generation to help debug desync errors.")]
public bool EnableSyncReports = false;
public string TimestampFormat = "s";
[Desc("Sets the timestamp format. Defaults to the ISO 8601 standard.")]
public string TimestampFormat = "yyyy-MM-ddTHH:mm:ss";
public ServerSettings Clone()
{

View File

@@ -18,6 +18,7 @@ namespace OpenRA
public struct ChannelInfo
{
public string Filename;
public bool IsTimestamped;
public TextWriter Writer;
}
@@ -44,7 +45,7 @@ namespace OpenRA
return info;
}
public static void AddChannel(string channelName, string baseFilename)
public static void AddChannel(string channelName, string baseFilename, bool isTimestamped = false)
{
lock (Channels)
{
@@ -66,6 +67,7 @@ namespace OpenRA
new ChannelInfo
{
Filename = filename,
IsTimestamped = isTimestamped,
Writer = TextWriter.Synchronized(writer)
});
@@ -75,22 +77,35 @@ namespace OpenRA
}
}
public static void Write(string channel, string value)
public static void Write(string channelName, string value)
{
var writer = Channel(channel).Writer;
var channel = Channel(channelName);
var writer = channel.Writer;
if (writer == null)
return;
writer.WriteLine(value);
if (!channel.IsTimestamped)
writer.WriteLine(value);
else
{
var timestamp = DateTime.Now.ToString(Game.Settings.Server.TimestampFormat);
writer.WriteLine("[{0}] {1}", timestamp, value);
}
}
public static void Write(string channel, string format, params object[] args)
public static void Write(string channelName, string format, params object[] args)
{
var writer = Channel(channel).Writer;
if (writer == null)
var channel = Channel(channelName);
if (channel.Writer == null)
return;
writer.WriteLine(format, args);
if (!channel.IsTimestamped)
channel.Writer.WriteLine(format, args);
else
{
var timestamp = DateTime.Now.ToString(Game.Settings.Server.TimestampFormat);
channel.Writer.WriteLine("[{0}] {1}", timestamp, format.F(args));
}
}
}
}