using System;
using System.Collections.Generic;
using System.Globalization;
namespace TensorStack.Python
{
internal record LogEntry(DateTime Timestamp, string Message);
internal static class LogParser
{
///
/// Parses the python logs.
///
/// The log entries.
/// IEnumerable<PipelineProgress>.
internal static IEnumerable ParseLogs(IReadOnlyList logEntries)
{
foreach (var logEntry in logEntries)
{
var progress = ParsePythonLog(logEntry);
if (progress == null)
continue;
yield return progress;
}
}
///
/// Parses the python log.
///
/// The log entry.
/// PythonProgress.
private static LogEntry ParsePythonLog(string logEntry)
{
try
{
var messageSections = logEntry.Split('|', 3, StringSplitOptions.TrimEntries).AsSpan();
if (messageSections.Length < 2)
return default;
var message = messageSections[1].Trim([' ', '\n', '\r', '\t']);
if (string.IsNullOrWhiteSpace(message) || message.Length < 5 || !message.StartsWith('['))
return default;
return new LogEntry(DateTime.Parse(messageSections[0], CultureInfo.InvariantCulture), message);
}
catch (Exception)
{
return default;
}
}
}
}