From 7d18937415a0ec07adedbcf7ce8eab0752be4db8 Mon Sep 17 00:00:00 2001 From: alzeih Date: Sun, 11 Jul 2010 18:42:27 +1200 Subject: [PATCH] better instance logging support --- OpenRA.FileFormats/Support/Log.cs | 22 +++++++++++++--------- OpenRA.Game/Game.cs | 6 +++++- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/OpenRA.FileFormats/Support/Log.cs b/OpenRA.FileFormats/Support/Log.cs index a09de04d41..4952af205d 100755 --- a/OpenRA.FileFormats/Support/Log.cs +++ b/OpenRA.FileFormats/Support/Log.cs @@ -55,19 +55,23 @@ namespace OpenRA { if (channels.ContainsKey(channelName)) return; - /* HACK: avoid sharing violations if running multiple instances */ - StreamWriter writer = null; - while (writer == null) - { + var i = 0; + var f = filename; + while (File.Exists(LogPathPrefix + filename)) try { - writer = File.CreateText(LogPathPrefix + filename); + StreamWriter writer = File.CreateText(LogPathPrefix + filename); + writer.AutoFlush = true; + channels.Add(channelName, new ChannelInfo() { Upload = upload, Filename = filename, Writer = writer, Diff = diff }); + return; } - catch(IOException){ filename = new Random().Next().ToString() + filename; } - } + catch (IOException) { filename = f + ".{0}".F(++i); } + + //if no logs exist, just make it + StreamWriter w = File.CreateText(LogPathPrefix + filename); + w.AutoFlush = true; + channels.Add(channelName, new ChannelInfo() { Upload = upload, Filename = filename, Writer = w, Diff = diff }); - writer.AutoFlush = true; - channels.Add(channelName, new ChannelInfo() { Upload = upload, Filename = filename, Writer = writer, Diff = diff }); } public static void Write(string channel, string format, params object[] args) diff --git a/OpenRA.Game/Game.cs b/OpenRA.Game/Game.cs index a0057f247f..57bc96df62 100644 --- a/OpenRA.Game/Game.cs +++ b/OpenRA.Game/Game.cs @@ -626,7 +626,11 @@ namespace OpenRA public static void Exit() { quit = true; } - public static void Debug(string s) { Chrome.chatWidget.AddLine(Color.White, "Debug", s); } + public static void Debug(string s) + { + if (Chrome.chatWidget != null) + Chrome.chatWidget.AddLine(Color.White, "Debug", s); + } public static void Disconnect() {