From 3c0d95023287c16ea00805bba127c183701ceee4 Mon Sep 17 00:00:00 2001 From: Nazar Mandzyk Date: Tue, 9 May 2023 01:55:26 +0300 Subject: [PATCH] #736 Create log files only when receiving the first event or message --- QuickFIXn/FileLog.cs | 31 +++++++++++++++++++++++-------- UnitTests/FileLogTests.cs | 27 +++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 8 deletions(-) diff --git a/QuickFIXn/FileLog.cs b/QuickFIXn/FileLog.cs index 43401a07d..931587484 100755 --- a/QuickFIXn/FileLog.cs +++ b/QuickFIXn/FileLog.cs @@ -15,16 +15,21 @@ public class FileLog : ILog, System.IDisposable private string messageLogFileName_; private string eventLogFileName_; + private bool _isInitialized; + private readonly string _prefix; + private readonly string _fileLogPath; public FileLog(string fileLogPath) { - Init(fileLogPath, "GLOBAL"); + _fileLogPath = fileLogPath; + _prefix = "GLOBAL"; } public FileLog(string fileLogPath, SessionID sessionID) { - Init(fileLogPath, Prefix(sessionID)); + _fileLogPath = fileLogPath; + _prefix = Prefix(sessionID); } @@ -41,6 +46,7 @@ private void Init(string fileLogPath, string prefix) messageLog_.AutoFlush = true; eventLog_.AutoFlush = true; + _isInitialized = true; } public static string Prefix(SessionID sessionID) @@ -77,14 +83,17 @@ public void Clear() lock (sync_) { - messageLog_.Close(); - eventLog_.Close(); + if (_isInitialized) + { + messageLog_?.Close(); + eventLog_?.Close(); - messageLog_ = new System.IO.StreamWriter(messageLogFileName_, false); - eventLog_ = new System.IO.StreamWriter(eventLogFileName_, false); + messageLog_ = new System.IO.StreamWriter(messageLogFileName_, false); + eventLog_ = new System.IO.StreamWriter(eventLogFileName_, false); - messageLog_.AutoFlush = true; - eventLog_.AutoFlush = true; + messageLog_.AutoFlush = true; + eventLog_.AutoFlush = true; + } } } @@ -94,6 +103,8 @@ public void OnIncoming(string msg) lock (sync_) { + if(!_isInitialized) + Init(_fileLogPath, _prefix); messageLog_.WriteLine(Fields.Converters.DateTimeConverter.Convert(System.DateTime.UtcNow) + " : " + msg); } } @@ -104,6 +115,8 @@ public void OnOutgoing(string msg) lock (sync_) { + if (!_isInitialized) + Init(_fileLogPath, _prefix); messageLog_.WriteLine(Fields.Converters.DateTimeConverter.Convert(System.DateTime.UtcNow) + " : " + msg); } } @@ -114,6 +127,8 @@ public void OnEvent(string s) lock (sync_) { + if (!_isInitialized) + Init(_fileLogPath, _prefix); eventLog_.WriteLine(Fields.Converters.DateTimeConverter.Convert(System.DateTime.UtcNow) + " : "+ s); } } diff --git a/UnitTests/FileLogTests.cs b/UnitTests/FileLogTests.cs index 7fd950341..d1bcc3da7 100644 --- a/UnitTests/FileLogTests.cs +++ b/UnitTests/FileLogTests.cs @@ -74,6 +74,33 @@ public void testGeneratedFileName() Assert.That(System.IO.File.Exists(Path.Combine(logDirectory, "FIX.4.2-SENDERCOMP-TARGETCOMP.messages.current.log"))); } + [Test] + public void testDoNotCreateFileWithoutEventsOrMessages() + { + var logDirectory = Path.Combine(TestContext.CurrentContext.TestDirectory, "log"); + + if (System.IO.Directory.Exists(logDirectory)) + System.IO.Directory.Delete(logDirectory, true); + + QuickFix.SessionID sessionID = new QuickFix.SessionID("FIX.4.2", "SENDERCOMP", "TARGETCOMP"); + QuickFix.SessionSettings settings = new QuickFix.SessionSettings(); + + QuickFix.Dictionary config = new QuickFix.Dictionary(); + config.SetString(QuickFix.SessionSettings.CONNECTION_TYPE, "initiator"); + config.SetString(QuickFix.SessionSettings.FILE_LOG_PATH, logDirectory); + + settings.Set(sessionID, config); + + QuickFix.FileLogFactory factory = new QuickFix.FileLogFactory(settings); + log = (QuickFix.FileLog)factory.Create(sessionID); + + Assert.False(System.IO.File.Exists(Path.Combine(logDirectory, "FIX.4.2-SENDERCOMP-TARGETCOMP.event.current.log"))); + Assert.False(System.IO.File.Exists(Path.Combine(logDirectory, "FIX.4.2-SENDERCOMP-TARGETCOMP.messages.current.log"))); + log.Clear(); + Assert.False(System.IO.File.Exists(Path.Combine(logDirectory, "FIX.4.2-SENDERCOMP-TARGETCOMP.event.current.log"))); + Assert.False(System.IO.File.Exists(Path.Combine(logDirectory, "FIX.4.2-SENDERCOMP-TARGETCOMP.messages.current.log"))); + } + [Test] public void testThrowsIfNoConfig() {