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

@@ -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));
}
}
}
}