Skip to content

Commit

Permalink
Added library category. Fixed bug that no new line was written to fil…
Browse files Browse the repository at this point in the history
…e when writing log messages with max length. Introduced unit test for this scenario.
  • Loading branch information
bengeisler committed Sep 19, 2023
1 parent 7d44670 commit dff3286
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 17 deletions.
8 changes: 8 additions & 0 deletions src/TcLogProj/TcLog.libcat.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<LibraryCategories xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="LibraryCategories.xsd">
<LibraryCategory>
<Id>a008f0f9-813d-4727-ad9c-d60bc9412b1d</Id>
<Version>1.0.0.0</Version>
<DefaultName>TcLog</DefaultName>
</LibraryCategory>
</LibraryCategories>
14 changes: 13 additions & 1 deletion src/TcLogProj/TcLog/Logger/_Tests/TestWrapper_NET.TcPOU
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ VAR
Persist_long_error_message_run : BOOL;
Persist_long_error_message_data : T_MaxString;
Linebreak_is_included_when_log_message_has_maximum_length_run : BOOL;
Linebreak_is_included_when_log_message_has_maximum_length_data : T_MaxString;
Do_not_persist_logs_below_log_level_run : BOOL;
Log_message_contains_instance_path_run : BOOL;
Expand Down Expand Up @@ -95,9 +98,18 @@ END_IF
IF Persist_long_error_message_run THEN
Persist_long_error_message_run := FALSE;
_logger
.Error(Persist_long_error_message_data);
.ToCustomFormat(Persist_long_error_message_data);
END_IF
IF Linebreak_is_included_when_log_message_has_maximum_length_run THEN
Linebreak_is_included_when_log_message_has_maximum_length_run := FALSE;
_logger
.ToCustomFormat(Linebreak_is_included_when_log_message_has_maximum_length_data);
_logger
.ToCustomFormat(Linebreak_is_included_when_log_message_has_maximum_length_data);
END_IF
IF Do_not_persist_logs_below_log_level_run THEN
Do_not_persist_logs_below_log_level_run := FALSE;
_logger
Expand Down
10 changes: 10 additions & 0 deletions src/TcLogProj/TcLog/TcLog.plcproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@
<DefaultNamespace>TcLog</DefaultNamespace>
<Author>Benedikt Geisler</Author>
<Description>TcLog provides logging functionality to TwinCAT.</Description>
<LibraryCategories>
<LibraryCategory>
<Id>{a008f0f9-813d-4727-ad9c-d60bc9412b1d}</Id>
<Version>1.0.0.0</Version>
<DefaultName>TcLog</DefaultName>
</LibraryCategory>
</LibraryCategories>
<SelectedLibraryCategories>
<Id>{a008f0f9-813d-4727-ad9c-d60bc9412b1d}</Id>
</SelectedLibraryCategories>
</PropertyGroup>
<ItemGroup>
<Folder Include="Logger\_Tests" />
Expand Down
2 changes: 1 addition & 1 deletion src/TcLogProj/TcLog/Utils/DynamicStringBuffer.TcPOU
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ END_VAR
]]></Declaration>
<Implementation>
<ST><![CDATA[ResizeFifoBufferIfNecessary();
_fifo.A_AddTail(putValue := Tc2_Standard.CONCAT(text, '$N')); ]]></ST>
_fifo.A_AddTail(putValue := Tc2_Standard.CONCAT(Tc2_Standard.LEFT(text, 254), '$N')); ]]></ST>
</Implementation>
</Method>
<Method Name="AddString" Id="{6405c45a-0cb8-4431-a2bb-9cf39dc1e728}">
Expand Down
2 changes: 1 addition & 1 deletion src/TcLogProj/TcLogProj.tsproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
</Instance>
</Project>
<Project GUID="{39E514BF-8337-4E82-99CA-D7D99BEEE7FE}" Name="TcLogTEST" PrjFilePath="TcLogTEST\TcLogTEST.plcproj" TmcFilePath="TcLogTEST\TcLogTEST.tmc" ReloadTmc="true" AmsPort="851" FileArchiveSettings="#x000e" SymbolicMapping="true">
<Instance Id="#x08502000" TcSmClass="TComPlcObjDef" KeepUnrestoredLinks="2">
<Instance Id="#x08502000" TcSmClass="TComPlcObjDef" KeepUnrestoredLinks="2" TmcPath="TcLogTEST\TcLogTEST.tmc">
<Name>TcLogTEST Instance</Name>
<CLSID ClassFactory="TcPlc30">{08500001-0000-0000-F000-000000000064}</CLSID>
<Contexts>
Expand Down
18 changes: 9 additions & 9 deletions src/TcLogProj/TcLogTEST/TcLogTEST.plcproj
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@
<ProjectExtensions>
<PlcProjectOptions>
<XmlArchive>
<Data>
<o xml:space="preserve" t="OptionKey">
<Data>
<o xml:space="preserve" t="OptionKey">
<v n="Name">"&lt;ProjectRoot&gt;"</v>
<d n="SubKeys" t="Hashtable" ckt="String" cvt="OptionKey">
<v>{192FAD59-8248-4824-A8DE-9177C94C195A}</v>
Expand Down Expand Up @@ -104,13 +104,13 @@
</d>
<d n="Values" t="Hashtable" />
</o>
</Data>
<TypeList>
<Type n="Hashtable">System.Collections.Hashtable</Type>
<Type n="OptionKey">{54dd0eac-a6d8-46f2-8c27-2f43c7e49861}</Type>
<Type n="String">System.String</Type>
</TypeList>
</XmlArchive>
</Data>
<TypeList>
<Type n="Hashtable">System.Collections.Hashtable</Type>
<Type n="OptionKey">{54dd0eac-a6d8-46f2-8c27-2f43c7e49861}</Type>
<Type n="String">System.String</Type>
</TypeList>
</XmlArchive>
</PlcProjectOptions>
</ProjectExtensions>
</Project>
29 changes: 24 additions & 5 deletions src/TcLogTest.NET/PersistingToFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,20 +81,39 @@ public async void Persist_simple_error_message()
[Fact]
public async void Persist_long_error_message()
{
string message = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean aliquet orci sit amet massa placerat faucibus. Sed interdum fermentum eros. Maecenas accumsan rutrum ex, non varius orci scelerisque ac. Donec qui";
string message = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata";
uint hRun = fixture.TcClient.CreateVariableHandle(mut + ".Persist_long_error_message_run");
uint hData = fixture.TcClient.CreateVariableHandle(mut + ".Persist_long_error_message_data");

fixture.TcClient.WriteAny(hData, message);
fixture.TcClient.WriteAny(hRun, true);
await Task.Delay(1000);
var files = Directory.GetFiles(path);
var fileContent = File.ReadAllLines(files[0]);
var fileContent = File.ReadAllText(files[0]);

Assert.Contains(message, fileContent[0]);
Assert.Contains("Error", fileContent[0]);
Assert.Equal($"{message[..254]}\r\n", fileContent);

foreach (var f in files) File.Delete(f);
fixture.TcClient.DeleteVariableHandle(hRun);
fixture.TcClient.DeleteVariableHandle(hData);
}

[Fact]
public async void Linebreak_is_included_when_log_message_has_maximum_length()
{
string message = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata";
uint hRun = fixture.TcClient.CreateVariableHandle(mut + ".Linebreak_is_included_when_log_message_has_maximum_length_run");
uint hData = fixture.TcClient.CreateVariableHandle(mut + ".Linebreak_is_included_when_log_message_has_maximum_length_data");

//foreach (var f in files) File.Delete(f);
fixture.TcClient.WriteAny(hData, message);
fixture.TcClient.WriteAny(hRun, true);
await Task.Delay(1000);
var files = Directory.GetFiles(path);
var fileContent = File.ReadAllText(files[0]);

Assert.Equal($"{message[..254]}\r\n{message[..254]}\r\n", fileContent);

foreach (var f in files) File.Delete(f);
fixture.TcClient.DeleteVariableHandle(hRun);
fixture.TcClient.DeleteVariableHandle(hData);
}
Expand Down

0 comments on commit dff3286

Please sign in to comment.