2013-02-21 01:33:05 +00:00
|
|
|
|
using MediaBrowser.Common.IO;
|
2013-03-07 05:34:00 +00:00
|
|
|
|
using MediaBrowser.Common.Net;
|
2013-02-22 15:16:48 +00:00
|
|
|
|
using MediaBrowser.Model.Logging;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2013-02-22 04:23:06 +00:00
|
|
|
|
namespace MediaBrowser.Api.WebSocket
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Class ScheduledTasksWebSocketListener
|
|
|
|
|
/// </summary>
|
2013-02-22 15:16:48 +00:00
|
|
|
|
public class LogFileWebSocketListener : BasePeriodicWebSocketListener<IEnumerable<string>, LogFileWebSocketState>
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the name.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The name.</value>
|
|
|
|
|
protected override string Name
|
|
|
|
|
{
|
|
|
|
|
get { return "LogFile"; }
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-22 15:16:48 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _kernel
|
|
|
|
|
/// </summary>
|
2013-02-26 17:21:18 +00:00
|
|
|
|
private readonly ILogManager _logManager;
|
|
|
|
|
|
2013-02-21 20:26:35 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new instance of the <see cref="LogFileWebSocketListener" /> class.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="logger">The logger.</param>
|
2013-02-26 17:21:18 +00:00
|
|
|
|
/// <param name="logManager">The log manager.</param>
|
|
|
|
|
public LogFileWebSocketListener(ILogger logger, ILogManager logManager)
|
2013-02-21 20:26:35 +00:00
|
|
|
|
: base(logger)
|
|
|
|
|
{
|
2013-02-26 17:21:18 +00:00
|
|
|
|
_logManager = logManager;
|
|
|
|
|
_logManager.LoggerLoaded += kernel_LoggerLoaded;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the data to send.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="state">The state.</param>
|
|
|
|
|
/// <returns>IEnumerable{System.String}.</returns>
|
|
|
|
|
protected override async Task<IEnumerable<string>> GetDataToSend(LogFileWebSocketState state)
|
|
|
|
|
{
|
2013-02-26 17:21:18 +00:00
|
|
|
|
if (!string.Equals(_logManager.LogFilePath, state.LastLogFilePath))
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-02-26 17:21:18 +00:00
|
|
|
|
state.LastLogFilePath = _logManager.LogFilePath;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
state.StartLine = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var lines = await GetLogLines(state.LastLogFilePath, state.StartLine).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
state.StartLine += lines.Count;
|
|
|
|
|
|
|
|
|
|
return lines;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Releases unmanaged and - optionally - managed resources.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
|
|
|
|
|
protected override void Dispose(bool dispose)
|
|
|
|
|
{
|
|
|
|
|
if (dispose)
|
|
|
|
|
{
|
2013-02-26 17:21:18 +00:00
|
|
|
|
_logManager.LoggerLoaded -= kernel_LoggerLoaded;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
base.Dispose(dispose);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Handles the LoggerLoaded event of the kernel control.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender">The source of the event.</param>
|
|
|
|
|
/// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
|
|
|
|
|
void kernel_LoggerLoaded(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
// Reset the startline for each connection whenever the logger reloads
|
|
|
|
|
lock (ActiveConnections)
|
|
|
|
|
{
|
|
|
|
|
foreach (var connection in ActiveConnections)
|
|
|
|
|
{
|
|
|
|
|
connection.Item4.StartLine = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the log lines.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="logFilePath">The log file path.</param>
|
|
|
|
|
/// <param name="startLine">The start line.</param>
|
|
|
|
|
/// <returns>Task{IEnumerable{System.String}}.</returns>
|
|
|
|
|
internal static async Task<List<string>> GetLogLines(string logFilePath, int startLine)
|
|
|
|
|
{
|
|
|
|
|
var lines = new List<string>();
|
|
|
|
|
|
|
|
|
|
using (var fs = new FileStream(logFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, StreamDefaults.DefaultFileStreamBufferSize, true))
|
|
|
|
|
{
|
|
|
|
|
using (var reader = new StreamReader(fs))
|
|
|
|
|
{
|
|
|
|
|
while (!reader.EndOfStream)
|
|
|
|
|
{
|
|
|
|
|
lines.Add(await reader.ReadLineAsync().ConfigureAwait(false));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (startLine > 0)
|
|
|
|
|
{
|
|
|
|
|
lines = lines.Skip(startLine).ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return lines;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Class LogFileWebSocketState
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class LogFileWebSocketState
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the last log file path.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The last log file path.</value>
|
|
|
|
|
public string LastLogFilePath { get; set; }
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the start line.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The start line.</value>
|
|
|
|
|
public int StartLine { get; set; }
|
|
|
|
|
}
|
|
|
|
|
}
|