fix logfile opening to be sensible

This commit is contained in:
Chris Forbes
2011-04-09 12:52:30 +12:00
parent 19ad0bf0cc
commit abb895d8be

View File

@@ -34,33 +34,38 @@ namespace OpenRA
set set
{ {
LogPathPrefix = value; LogPathPrefix = value;
if (!Directory.Exists(LogPathPrefix)) Directory.CreateDirectory(LogPathPrefix);
Directory.CreateDirectory(LogPathPrefix);
} }
} }
public static void AddChannel(string channelName, string filename) static IEnumerable<string> FilenamesForChannel(string channelName, string baseFilename)
{ {
if (channels.ContainsKey(channelName)) return; for(var i = 0;; i++ )
yield return Path.Combine(LogPathPrefix,
var i = 0; i > 0 ? "{0}.{1}".F(baseFilename, i) : baseFilename);
var f = filename; }
while (File.Exists(LogPathPrefix + filename))
try public static void AddChannel(string channelName, string baseFilename)
{ {
StreamWriter writer = File.CreateText(LogPathPrefix + filename); if (channels.ContainsKey(channelName)) return;
writer.AutoFlush = true;
channels.Add(channelName, new ChannelInfo() { Filename = filename, Writer = writer }); foreach (var filename in FilenamesForChannel(channelName, baseFilename))
return; try
} {
catch (IOException) { filename = f + ".{0}".F(++i); } var writer = File.CreateText(filename);
writer.AutoFlush = true;
//if no logs exist, just make it
StreamWriter w = File.CreateText(LogPathPrefix + filename); channels.Add(channelName,
w.AutoFlush = true; new ChannelInfo()
channels.Add(channelName, new ChannelInfo() { Filename = filename, Writer = w }); {
Filename = filename,
} Writer = writer
});
return;
}
catch (IOException) { }
}
public static void Write(string channel, string format, params object[] args) public static void Write(string channel, string format, params object[] args)
{ {