IW4M-Admin/Application/IO/GameLogReaderHttp.cs

76 lines
2.6 KiB
C#
Raw Normal View History

2018-09-06 14:25:58 -04:00
using IW4MAdmin.Application.API.GameLogServer;
using RestEase;
using SharedLibraryCore;
using SharedLibraryCore.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using ILogger = Microsoft.Extensions.Logging.ILogger;
namespace IW4MAdmin.Application.IO
{
/// <summary>
/// provides capability of reading log files over HTTP
/// </summary>
class GameLogReaderHttp : IGameLogReader
{
private readonly IEventParser _eventParser;
private readonly IGameLogServer _logServerApi;
private readonly ILogger _logger;
private readonly string _safeLogPath;
private string lastKey = "next";
public GameLogReaderHttp(Uri[] gameLogServerUris, IEventParser parser, ILogger<GameLogReaderHttp> logger)
{
_eventParser = parser;
_logServerApi = RestClient.For<IGameLogServer>(gameLogServerUris[0].ToString());
_safeLogPath = gameLogServerUris[1].LocalPath.ToBase64UrlSafeString();
_logger = logger;
}
2018-09-06 14:25:58 -04:00
public long Length => -1;
public int UpdateInterval => 500;
2022-02-13 15:53:01 -05:00
public async Task<IEnumerable<GameEvent>> ReadEventsFromLog(long fileSizeDiff, long startPosition, Server server = null)
{
2018-09-06 14:25:58 -04:00
var events = new List<GameEvent>();
var response = await _logServerApi.Log(_safeLogPath, lastKey);
lastKey = response.NextKey;
2018-09-06 14:25:58 -04:00
if (!response.Success && string.IsNullOrEmpty(lastKey))
{
_logger.LogError("Could not get log server info of {logPath}", _safeLogPath);
return events;
}
else if (!string.IsNullOrWhiteSpace(response.Data))
{
// parse each line
var lines = response.Data
.Split(Environment.NewLine)
.Where(_line => _line.Length > 0);
foreach (string eventLine in lines)
{
try
{
// this trim end should hopefully fix the nasty runaway regex
var gameEvent = _eventParser.GenerateGameEvent(eventLine.TrimEnd('\r'));
events.Add(gameEvent);
}
catch (Exception e)
{
_logger.LogError(e, "Could not properly parse event line from http {eventLine}", eventLine);
}
}
}
return events;
}
}
}