Skip to content

Commit 6dd7402

Browse files
committed
Capture trace warnings from libpalaso verse and chapter parsing
1 parent 464b715 commit 6dd7402

4 files changed

Lines changed: 99 additions & 10 deletions

File tree

src/Machine/src/Serval.Machine.Shared/Services/PreprocessBuildJob.cs

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace Serval.Machine.Shared.Services;
1+
using Trace = System.Diagnostics.Trace;
2+
3+
namespace Serval.Machine.Shared.Services;
24

35
public abstract class PreprocessBuildJob<TEngine>(
46
IPlatformService platformService,
@@ -139,21 +141,34 @@ IReadOnlyList<ParallelCorpus> corpora
139141
{
140142
List<string> warnings = [];
141143

142-
foreach (ParallelCorpus parallelCorpus in corpora)
144+
// Listen to the trace messages from libpalaso, and record them as warnings
145+
var listener = new WarningsTraceListener(warnings, "USFM Parsing error: ");
146+
try
143147
{
144-
IReadOnlyList<(string MonolingualCorpusId, IReadOnlyList<UsfmVersificationError> errors)> errorsPerCorpus =
145-
ParallelCorpusPreprocessingService.AnalyzeUsfmVersification(parallelCorpus);
146-
147-
foreach ((string monolingualCorpusId, IReadOnlyList<UsfmVersificationError> errors) in errorsPerCorpus)
148+
Trace.Listeners.Add(listener);
149+
foreach (ParallelCorpus parallelCorpus in corpora)
148150
{
149-
foreach (UsfmVersificationError error in errors)
151+
IReadOnlyList<(
152+
string MonolingualCorpusId,
153+
IReadOnlyList<UsfmVersificationError> errors
154+
)> errorsPerCorpus = ParallelCorpusPreprocessingService.AnalyzeUsfmVersification(parallelCorpus);
155+
156+
foreach ((string monolingualCorpusId, IReadOnlyList<UsfmVersificationError> errors) in errorsPerCorpus)
150157
{
151-
warnings.Add(
152-
$"USFM versification error in project {error.ProjectName}, expected verse “{error.ExpectedVerseRef}”, actual verse “{error.ActualVerseRef}”, mismatch type {error.Type} (parallel corpus {parallelCorpus.Id}, monolingual corpus {monolingualCorpusId})"
153-
);
158+
foreach (UsfmVersificationError error in errors)
159+
{
160+
warnings.Add(
161+
$"USFM versification error in project {error.ProjectName}, expected verse “{error.ExpectedVerseRef}”, actual verse “{error.ActualVerseRef}”, mismatch type {error.Type} (parallel corpus {parallelCorpus.Id}, monolingual corpus {monolingualCorpusId})"
162+
);
163+
}
154164
}
155165
}
156166
}
167+
finally
168+
{
169+
Trace.Listeners.Remove(listener);
170+
}
171+
157172
return warnings;
158173
}
159174
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace Serval.Machine.Shared.Services;
2+
3+
public class WarningsTraceListener(List<string> outputList, string prefix = "") : TraceListener
4+
{
5+
public override void Write(string? message) { }
6+
7+
public override void WriteLine(string? message)
8+
{
9+
if (!string.IsNullOrWhiteSpace(message))
10+
outputList.Add(prefix + message);
11+
}
12+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
namespace Serval.Machine.Shared.Services;
2+
3+
[TestFixture]
4+
public class WarningsTraceListenerTests
5+
{
6+
[Test]
7+
public void CapturesChapterParsingWarnings()
8+
{
9+
using var testEnvironment = new TestEnvironment();
10+
const string Chapter = "1.";
11+
12+
var verseRef = new VerseRef { Chapter = Chapter };
13+
Assert.Multiple(() =>
14+
{
15+
Assert.That(verseRef.Chapter, Is.Empty);
16+
Assert.That(testEnvironment.Warnings, Has.Count.EqualTo(1));
17+
Assert.That(
18+
testEnvironment.Warnings[0],
19+
Is.EqualTo(testEnvironment.Prefix + "Just failed to parse a chapter number: " + Chapter)
20+
);
21+
});
22+
}
23+
24+
[Test]
25+
public void CapturesVerseParsingWarnings()
26+
{
27+
using var testEnvironment = new TestEnvironment();
28+
const string Verse = "v1";
29+
30+
var verseRef = new VerseRef { Verse = Verse };
31+
Assert.Multiple(() =>
32+
{
33+
Assert.That(verseRef.Chapter, Is.EqualTo("0"));
34+
Assert.That(testEnvironment.Warnings, Has.Count.EqualTo(1));
35+
Assert.That(
36+
testEnvironment.Warnings[0],
37+
Is.EqualTo(testEnvironment.Prefix + "Just failed to parse a verse number: " + Verse)
38+
);
39+
});
40+
}
41+
42+
public class TestEnvironment : DisposableBase
43+
{
44+
private readonly WarningsTraceListener _listener;
45+
46+
public TestEnvironment()
47+
{
48+
_listener = new WarningsTraceListener(Warnings, Prefix);
49+
Trace.Listeners.Add(_listener);
50+
}
51+
52+
public string Prefix { get; } = "USFM Parsing error: ";
53+
public List<string> Warnings { get; } = [];
54+
55+
protected override void DisposeManagedResources()
56+
{
57+
Trace.Listeners.Remove(_listener);
58+
_listener.Dispose();
59+
}
60+
}
61+
}

src/Machine/test/Serval.Machine.Shared.Tests/Usings.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
global using System.Collections;
2+
global using System.Diagnostics;
23
global using System.IO.Compression;
34
global using System.Text.Json;
45
global using System.Text.Json.Nodes;

0 commit comments

Comments
 (0)