Skip to content

Commit

Permalink
Unit test to reproduce #2
Browse files Browse the repository at this point in the history
  • Loading branch information
bengeisler committed Aug 31, 2023
1 parent 76fdfd4 commit b2f3f46
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 34 deletions.
Binary file modified TcLog.library
Binary file not shown.
42 changes: 21 additions & 21 deletions TcLog.sln
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
# TcXaeShell Solution File, Format Version 11.00
VisualStudioVersion = 15.0.28010.2050
MinimumVisualStudioVersion = 10.0.40219.1
Project("{B1E792BE-AA5F-4E3C-8C82-674BF9C0715B}") = "TcLogProj", "TcLogProj\TcLogProj.tsproj", "{84E2C694-C6B7-48C0-A46A-93E9E79E1E4B}"
Expand Down Expand Up @@ -61,26 +61,6 @@ Global
{41F52577-96F4-46B7-BDEF-F5A9C5753EA6}.Release|TwinCAT RT (x64).Build.0 = Release|Any CPU
{41F52577-96F4-46B7-BDEF-F5A9C5753EA6}.Release|TwinCAT RT (x86).ActiveCfg = Release|Any CPU
{41F52577-96F4-46B7-BDEF-F5A9C5753EA6}.Release|TwinCAT RT (x86).Build.0 = Release|Any CPU
{534B7BB3-9B49-4F2B-86CC-2317930EA23D}.Debug|Any CPU.ActiveCfg = Debug|TwinCAT RT (x64)
{534B7BB3-9B49-4F2B-86CC-2317930EA23D}.Debug|Any CPU.Build.0 = Debug|TwinCAT RT (x64)
{534B7BB3-9B49-4F2B-86CC-2317930EA23D}.Debug|TwinCAT CE7 (ARMV7).ActiveCfg = Debug|TwinCAT CE7 (ARMV7)
{534B7BB3-9B49-4F2B-86CC-2317930EA23D}.Debug|TwinCAT CE7 (ARMV7).Build.0 = Debug|TwinCAT CE7 (ARMV7)
{534B7BB3-9B49-4F2B-86CC-2317930EA23D}.Debug|TwinCAT OS (ARMT2).ActiveCfg = Debug|TwinCAT OS (ARMT2)
{534B7BB3-9B49-4F2B-86CC-2317930EA23D}.Debug|TwinCAT OS (ARMT2).Build.0 = Debug|TwinCAT OS (ARMT2)
{534B7BB3-9B49-4F2B-86CC-2317930EA23D}.Debug|TwinCAT RT (x64).ActiveCfg = Debug|TwinCAT RT (x64)
{534B7BB3-9B49-4F2B-86CC-2317930EA23D}.Debug|TwinCAT RT (x64).Build.0 = Debug|TwinCAT RT (x64)
{534B7BB3-9B49-4F2B-86CC-2317930EA23D}.Debug|TwinCAT RT (x86).ActiveCfg = Debug|TwinCAT RT (x86)
{534B7BB3-9B49-4F2B-86CC-2317930EA23D}.Debug|TwinCAT RT (x86).Build.0 = Debug|TwinCAT RT (x86)
{534B7BB3-9B49-4F2B-86CC-2317930EA23D}.Release|Any CPU.ActiveCfg = Release|TwinCAT RT (x64)
{534B7BB3-9B49-4F2B-86CC-2317930EA23D}.Release|Any CPU.Build.0 = Release|TwinCAT RT (x64)
{534B7BB3-9B49-4F2B-86CC-2317930EA23D}.Release|TwinCAT CE7 (ARMV7).ActiveCfg = Release|TwinCAT CE7 (ARMV7)
{534B7BB3-9B49-4F2B-86CC-2317930EA23D}.Release|TwinCAT CE7 (ARMV7).Build.0 = Release|TwinCAT CE7 (ARMV7)
{534B7BB3-9B49-4F2B-86CC-2317930EA23D}.Release|TwinCAT OS (ARMT2).ActiveCfg = Release|TwinCAT OS (ARMT2)
{534B7BB3-9B49-4F2B-86CC-2317930EA23D}.Release|TwinCAT OS (ARMT2).Build.0 = Release|TwinCAT OS (ARMT2)
{534B7BB3-9B49-4F2B-86CC-2317930EA23D}.Release|TwinCAT RT (x64).ActiveCfg = Release|TwinCAT RT (x64)
{534B7BB3-9B49-4F2B-86CC-2317930EA23D}.Release|TwinCAT RT (x64).Build.0 = Release|TwinCAT RT (x64)
{534B7BB3-9B49-4F2B-86CC-2317930EA23D}.Release|TwinCAT RT (x86).ActiveCfg = Release|TwinCAT RT (x86)
{534B7BB3-9B49-4F2B-86CC-2317930EA23D}.Release|TwinCAT RT (x86).Build.0 = Release|TwinCAT RT (x86)
{67A5CD22-D1E6-4601-AC63-4CEBE4A64230}.Debug|Any CPU.ActiveCfg = Debug|TwinCAT RT (x64)
{67A5CD22-D1E6-4601-AC63-4CEBE4A64230}.Debug|Any CPU.Build.0 = Debug|TwinCAT RT (x64)
{67A5CD22-D1E6-4601-AC63-4CEBE4A64230}.Debug|TwinCAT CE7 (ARMV7).ActiveCfg = Debug|TwinCAT CE7 (ARMV7)
Expand All @@ -101,6 +81,26 @@ Global
{67A5CD22-D1E6-4601-AC63-4CEBE4A64230}.Release|TwinCAT RT (x64).Build.0 = Release|TwinCAT RT (x64)
{67A5CD22-D1E6-4601-AC63-4CEBE4A64230}.Release|TwinCAT RT (x86).ActiveCfg = Release|TwinCAT RT (x86)
{67A5CD22-D1E6-4601-AC63-4CEBE4A64230}.Release|TwinCAT RT (x86).Build.0 = Release|TwinCAT RT (x86)
{534B7BB3-9B49-4F2B-86CC-2317930EA23D}.Debug|Any CPU.ActiveCfg = Debug|TwinCAT RT (x64)
{534B7BB3-9B49-4F2B-86CC-2317930EA23D}.Debug|Any CPU.Build.0 = Debug|TwinCAT RT (x64)
{534B7BB3-9B49-4F2B-86CC-2317930EA23D}.Debug|TwinCAT CE7 (ARMV7).ActiveCfg = Debug|TwinCAT CE7 (ARMV7)
{534B7BB3-9B49-4F2B-86CC-2317930EA23D}.Debug|TwinCAT CE7 (ARMV7).Build.0 = Debug|TwinCAT CE7 (ARMV7)
{534B7BB3-9B49-4F2B-86CC-2317930EA23D}.Debug|TwinCAT OS (ARMT2).ActiveCfg = Debug|TwinCAT OS (ARMT2)
{534B7BB3-9B49-4F2B-86CC-2317930EA23D}.Debug|TwinCAT OS (ARMT2).Build.0 = Debug|TwinCAT OS (ARMT2)
{534B7BB3-9B49-4F2B-86CC-2317930EA23D}.Debug|TwinCAT RT (x64).ActiveCfg = Debug|TwinCAT RT (x64)
{534B7BB3-9B49-4F2B-86CC-2317930EA23D}.Debug|TwinCAT RT (x64).Build.0 = Debug|TwinCAT RT (x64)
{534B7BB3-9B49-4F2B-86CC-2317930EA23D}.Debug|TwinCAT RT (x86).ActiveCfg = Debug|TwinCAT RT (x86)
{534B7BB3-9B49-4F2B-86CC-2317930EA23D}.Debug|TwinCAT RT (x86).Build.0 = Debug|TwinCAT RT (x86)
{534B7BB3-9B49-4F2B-86CC-2317930EA23D}.Release|Any CPU.ActiveCfg = Release|TwinCAT RT (x64)
{534B7BB3-9B49-4F2B-86CC-2317930EA23D}.Release|Any CPU.Build.0 = Release|TwinCAT RT (x64)
{534B7BB3-9B49-4F2B-86CC-2317930EA23D}.Release|TwinCAT CE7 (ARMV7).ActiveCfg = Release|TwinCAT CE7 (ARMV7)
{534B7BB3-9B49-4F2B-86CC-2317930EA23D}.Release|TwinCAT CE7 (ARMV7).Build.0 = Release|TwinCAT CE7 (ARMV7)
{534B7BB3-9B49-4F2B-86CC-2317930EA23D}.Release|TwinCAT OS (ARMT2).ActiveCfg = Release|TwinCAT OS (ARMT2)
{534B7BB3-9B49-4F2B-86CC-2317930EA23D}.Release|TwinCAT OS (ARMT2).Build.0 = Release|TwinCAT OS (ARMT2)
{534B7BB3-9B49-4F2B-86CC-2317930EA23D}.Release|TwinCAT RT (x64).ActiveCfg = Release|TwinCAT RT (x64)
{534B7BB3-9B49-4F2B-86CC-2317930EA23D}.Release|TwinCAT RT (x64).Build.0 = Release|TwinCAT RT (x64)
{534B7BB3-9B49-4F2B-86CC-2317930EA23D}.Release|TwinCAT RT (x86).ActiveCfg = Release|TwinCAT RT (x86)
{534B7BB3-9B49-4F2B-86CC-2317930EA23D}.Release|TwinCAT RT (x86).Build.0 = Release|TwinCAT RT (x86)
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
41 changes: 39 additions & 2 deletions TcLogProj/TcLog/Helper functions/DynamicStringBuffer.TcPOU
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,19 @@ CASE state OF
END_CASE
(* Uncomment for cycle-specific usage of state machine & buffer usage
ADSLOGSTR(ADSLOG_MSGTYPE_LOG,
CONCAT('****DynamicStringBuffer.state:',
CONCAT(UINT_TO_STRING(state),
CONCAT(', DynamicStringBuffer.hFile:', UINT_TO_STRING(hFile)))), '');
ADSLOGSTR(ADSLOG_MSGTYPE_LOG,
CONCAT('****Fifo: used ',
CONCAT(UDINT_TO_STRING(Fifo.cbSize),
CONCAT(' of ', UDINT_TO_STRING(Fifo.cbBuffer)))), '');
*)
// Function blocks used in state machine
fbFileOpen(
sNetId := NET_ID, sPathName := FileName, nMode := FOPEN_MODEAPPEND OR FOPEN_MODETEXT,
Expand All @@ -164,6 +177,7 @@ fTrigFileClose(CLK := fbFileClose.bBusy);
tonTimeout(IN := state <> STATE_IDLE, PT:=TIMEOUT);
IF tonTimeout.Q THEN state := STATE_IDLE; END_IF
// Error handling
IF fbFileOpen.bError THEN
Error.Active := TRUE;
Expand All @@ -185,6 +199,12 @@ ELSE
Error.Active := FALSE;
Error.Code := E_ErrorCode.None;
Error.Info := '';
END_IF
IF Error.Active THEN
ADSLOGSTR(ADSLOG_MSGTYPE_LOG,
CONCAT('****DynamicStringBuffer.Error: ', Error.Info),
'');
END_IF]]></ST>
</Implementation>
</Method>
Expand Down Expand Up @@ -257,19 +277,36 @@ END_IF]]></ST>
<LineId Id="127" Count="0" />
<LineId Id="19" Count="1" />
<LineId Id="129" Count="0" />
<LineId Id="21" Count="25" />
<LineId Id="21" Count="8" />
<LineId Id="328" Count="0" />
<LineId Id="324" Count="0" />
<LineId Id="330" Count="0" />
<LineId Id="325" Count="2" />
<LineId Id="323" Count="0" />
<LineId Id="289" Count="3" />
<LineId Id="288" Count="0" />
<LineId Id="30" Count="0" />
<LineId Id="329" Count="0" />
<LineId Id="31" Count="15" />
<LineId Id="161" Count="0" />
<LineId Id="160" Count="0" />
<LineId Id="163" Count="0" />
<LineId Id="168" Count="0" />
<LineId Id="47" Count="1" />
<LineId Id="47" Count="0" />
<LineId Id="331" Count="0" />
<LineId Id="48" Count="0" />
<LineId Id="53" Count="3" />
<LineId Id="59" Count="3" />
<LineId Id="65" Count="3" />
<LineId Id="96" Count="3" />
<LineId Id="95" Count="0" />
<LineId Id="100" Count="2" />
<LineId Id="5" Count="0" />
<LineId Id="281" Count="0" />
<LineId Id="280" Count="0" />
<LineId Id="284" Count="1" />
<LineId Id="287" Count="0" />
<LineId Id="283" Count="0" />
</LineIds>
<LineIds Name="DynamicStringBuffer.PopHead">
<LineId Id="5" Count="0" />
Expand Down
2 changes: 1 addition & 1 deletion TcLogProj/TcLog/Logger/POU/TcLogCore.TcPOU
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ END_IF
// Delete expired log files
rTrigNewDay(CLK := timeAsSystemTime.wHour = 0);
DeleteExpiredLogFiles(
StartScan := rTrigNewDay.Q,
StartScan := rTrigNewDay.Q AND (_config.LogFileLifespan > 0),
ExpirationInDays := _config.LogFileLifespan,
FilePath := _config.FilePath,
FileName := _config.FileName,
Expand Down
107 changes: 107 additions & 0 deletions TcLogProj/TcLogTest/Helper functions/DynamicStringBuffer_TEST.TcPOU
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?xml version="1.0" encoding="utf-8"?>
<TcPlcObject Version="1.1.0.1" ProductVersion="3.1.4024.11">
<POU Name="DynamicStringBuffer_TEST" Id="{03045c1a-5281-4fe3-bc16-4d9ce12d6b50}" SpecialFunc="None">
<Declaration><![CDATA[FUNCTION_BLOCK DynamicStringBuffer_TEST EXTENDS FB_TestSuite
VAR_INPUT
END_VAR
VAR_OUTPUT
END_VAR
VAR
END_VAR
]]></Declaration>
<Implementation>
<ST><![CDATA[LogsManyMessagesForManyCycles();]]></ST>
</Implementation>
<Method Name="LogsManyMessagesForManyCycles" Id="{e452a053-adba-401f-a1d2-1bba967ad2c0}">
<Declaration><![CDATA[METHOD LogsManyMessagesForManyCycles
VAR_INPUT
END_VAR
VAR
result: STRING := '';
expected: STRING := '';
i: UDINT;
END_VAR
VAR CONSTANT
CYCLES: UDINT := 30;
MESSAGES: UDINT := 30;
END_VAR
VAR_INST
cycleCount: UDINT := 0;
LogsManyMessagesForManyCyclesLogger: TcLogCore(nBufferSize := 30*30*280); // cycles * messages * (max string length + fifo overhead (estimated))
logger: TcLog;
END_VAR
]]></Declaration>
<Implementation>
<ST><![CDATA[TEST('LogsManyMessagesForManyCycles');
LogsManyMessagesForManyCyclesLogger
.WriteToFile('c:\logs\', 'test.txt')
.RunLogger();
logger.SetLogger(LogsManyMessagesForManyCyclesLogger);
IF cycleCount < CYCLES THEN
FOR i:=0 TO MESSAGES DO
logger
.AppendString('Cycle: ')
.AppendString(UDINT_TO_STRING(cycleCount))
.AppendString(', Message: ')
.AppendString(UDINT_TO_STRING(i))
.Error('');
END_FOR
END_IF
cycleCount := cycleCount + 1;
// Length of operations (empirically determined):
// file open: 2 cycles
// pop from fifo: 1-2 cycle
// write to file: 2 cylces
// close file: 2 cylces
// => thus for x messages you need 4 * x + 4 cycles
// Catch any occuring error message
IF LEN(LogsManyMessagesForManyCyclesLogger.Error.Info) > 0 THEN
result := LogsManyMessagesForManyCyclesLogger.Error.Info;
END_IF
IF cycleCount > (CYCLES * MESSAGES * 4 + 10) THEN
AssertEquals(Expected := expected,
Actual := result,
Message := result);
TEST_FINISHED();
END_IF]]></ST>
</Implementation>
</Method>
<LineIds Name="DynamicStringBuffer_TEST">
<LineId Id="9" Count="0" />
</LineIds>
<LineIds Name="DynamicStringBuffer_TEST.LogsManyMessagesForManyCycles">
<LineId Id="6" Count="0" />
<LineId Id="79" Count="0" />
<LineId Id="76" Count="0" />
<LineId Id="80" Count="1" />
<LineId Id="83" Count="0" />
<LineId Id="74" Count="0" />
<LineId Id="20" Count="0" />
<LineId Id="7" Count="0" />
<LineId Id="31" Count="0" />
<LineId Id="36" Count="4" />
<LineId Id="30" Count="0" />
<LineId Id="33" Count="0" />
<LineId Id="45" Count="1" />
<LineId Id="43" Count="0" />
<LineId Id="47" Count="4" />
<LineId Id="88" Count="0" />
<LineId Id="85" Count="0" />
<LineId Id="84" Count="0" />
<LineId Id="86" Count="1" />
<LineId Id="52" Count="0" />
<LineId Id="42" Count="0" />
<LineId Id="10" Count="3" />
<LineId Id="5" Count="0" />
<LineId Id="44" Count="0" />
</LineIds>
</POU>
</TcPlcObject>
3 changes: 2 additions & 1 deletion TcLogProj/TcLogTest/POUs/MAIN.TcPOU
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<TcPlcObject Version="1.1.0.1" ProductVersion="3.1.4024.4">
<TcPlcObject Version="1.1.0.1" ProductVersion="3.1.4024.11">
<POU Name="MAIN" Id="{899b5391-5363-4fd8-835e-d1662fef51af}" SpecialFunc="None">
<Declaration><![CDATA[PROGRAM MAIN
VAR
fbGenerateTimeData_TEST : GenerateTimeData_TEST;
fbHelperFunctions_TEST: ConvertAnyToString_TEST;
fbDynamicStringBuffer_TEST: DynamicStringBuffer_TEST;
testWrapper : TestWrapper_NET();
END_VAR
]]></Declaration>
Expand Down
21 changes: 12 additions & 9 deletions TcLogProj/TcLogTest/TcLogTest.plcproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
<Compile Include="Helper functions\ConvertAnyToString_TEST.TcPOU">
<SubType>Code</SubType>
</Compile>
<Compile Include="Helper functions\DynamicStringBuffer_TEST.TcPOU">
<SubType>Code</SubType>
</Compile>
<Compile Include="NET\TestWrapper_NET.TcPOU">
<SubType>Code</SubType>
</Compile>
Expand Down Expand Up @@ -78,8 +81,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>{8F99A816-E488-41E4-9FA3-846536012284}</v>
Expand Down Expand Up @@ -134,13 +137,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>

0 comments on commit b2f3f46

Please sign in to comment.