Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#736 Don't create file logs until firm event or message #772

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 23 additions & 8 deletions QuickFIXn/FileLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}


Expand All @@ -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)
Expand Down Expand Up @@ -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;
}
}
}

Expand All @@ -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);
}
}
Expand All @@ -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);
}
}
Expand All @@ -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);
}
}
Expand Down
27 changes: 27 additions & 0 deletions UnitTests/FileLogTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down