Files
duplicati/Duplicati/Library/RestAPI/LogWriteHandler.cs
T

261 lines
8.7 KiB
C#
Raw Normal View History

// Copyright (C) 2025, The Duplicati Team
// https://duplicati.com, hello@duplicati.com
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
2024-06-05 11:02:56 +02:00
// DEALINGS IN THE SOFTWARE.
2014-08-03 22:25:38 +02:00
using System;
using System.Linq;
using System.Collections.Generic;
using Duplicati.Library.Main;
using Duplicati.WebserverCore.Abstractions;
using Duplicati.Library.Logging;
2014-08-03 22:25:38 +02:00
namespace Duplicati.Server
{
/// <summary>
/// Class that handles logging from the server,
/// and provides an entry point for the runner
/// to redirect log output to a file
/// </summary>
public class LogWriteHandler : ILogWriteHandler
2014-08-03 22:25:38 +02:00
{
/// <summary>
/// The number of messages to keep when inactive
/// </summary>
private const int INACTIVE_SIZE = 30;
2014-08-03 22:25:38 +02:00
/// <summary>
/// The number of messages to keep when active
/// </summary>
private const int ACTIVE_SIZE = 5000;
/// <summary>
/// Basic implementation of a ring-buffer
/// </summary>
private class RingBuffer<T> : IEnumerable<T>
{
2018-12-31 15:08:47 -08:00
private readonly T[] m_buffer;
2014-08-03 22:25:38 +02:00
private int m_head;
private int m_tail;
private int m_length;
private int m_key;
private readonly object m_lock = new object();
2014-08-03 22:25:38 +02:00
public RingBuffer(int size, IEnumerable<T> initial = null)
{
m_buffer = new T[size];
if (initial != null)
2024-06-05 11:02:56 +02:00
foreach (var t in initial)
2014-08-03 22:25:38 +02:00
this.Enqueue(t);
}
2024-06-05 11:02:56 +02:00
2014-08-03 22:25:38 +02:00
public int Length { get { return m_length; } }
public void Enqueue(T item)
{
2024-06-05 11:02:56 +02:00
lock (m_lock)
2014-08-03 22:25:38 +02:00
{
m_key++;
m_buffer[m_head] = item;
m_head = (m_head + 1) % m_buffer.Length;
if (m_length == m_buffer.Length)
m_tail = (m_tail + 1) % m_buffer.Length;
else
m_length++;
}
}
#region IEnumerable implementation
public IEnumerator<T> GetEnumerator()
{
var k = m_key;
2024-06-05 11:02:56 +02:00
for (var i = 0; i < m_length; i++)
2014-08-03 22:25:38 +02:00
if (m_key != k)
throw new InvalidOperationException("Buffer was modified while reading");
else
yield return m_buffer[(m_tail + i) % m_buffer.Length];
}
#endregion
#region IEnumerable implementation
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
#endregion
public T[] FlatArray(Func<T, bool> filter = null)
{
2024-06-05 11:02:56 +02:00
lock (m_lock)
2014-08-03 22:25:38 +02:00
if (filter == null)
return this.ToArray();
else
return this.Where(filter).ToArray();
}
public int Size { get { return m_buffer.Length; } }
}
private readonly DateTime[] m_timeouts;
private readonly object m_lock = new object();
2014-08-03 22:25:38 +02:00
private volatile bool m_anytimeouts = false;
private RingBuffer<ILogWriteHandler.LiveLogEntry> m_buffer;
2014-08-03 22:25:38 +02:00
2025-05-20 14:26:10 +02:00
private readonly ControllerMultiLogTarget m_target = new ControllerMultiLogTarget(null, LogMessageType.Warning, null, null);
private LogMessageType m_logLevel;
2014-08-03 22:25:38 +02:00
public LogWriteHandler()
{
var fields = Enum.GetValues(typeof(LogMessageType));
m_timeouts = new DateTime[fields.Length];
m_buffer = new RingBuffer<ILogWriteHandler.LiveLogEntry>(INACTIVE_SIZE);
2014-08-03 22:25:38 +02:00
}
public void RenewTimeout(LogMessageType type)
{
2024-06-05 11:02:56 +02:00
lock (m_lock)
2014-08-03 22:25:38 +02:00
{
m_timeouts[(int)type] = DateTime.Now.AddSeconds(30);
2014-08-03 22:25:38 +02:00
m_anytimeouts = true;
if (m_buffer == null || m_buffer.Size == INACTIVE_SIZE)
m_buffer = new RingBuffer<ILogWriteHandler.LiveLogEntry>(ACTIVE_SIZE, m_buffer);
2014-08-03 22:25:38 +02:00
}
}
public void SetServerFile(string path, LogMessageType level)
{
var dir = System.IO.Path.GetDirectoryName(System.IO.Path.GetFullPath(path));
if (!System.IO.Directory.Exists(dir))
System.IO.Directory.CreateDirectory(dir);
m_target.AddTarget(new StreamLogDestination(path), level, null);
UpdateLogLevel();
}
2014-08-03 22:25:38 +02:00
public void AppendLogDestination(ILogDestination destination, LogMessageType level)
{
m_target.AddTarget(destination, level, null);
2014-08-03 22:25:38 +02:00
UpdateLogLevel();
}
public ILogWriteHandler.LiveLogEntry[] AfterTime(DateTime offset, LogMessageType level)
2014-08-03 22:25:38 +02:00
{
RenewTimeout(level);
UpdateLogLevel();
offset = offset.ToUniversalTime();
2024-06-05 11:02:56 +02:00
lock (m_lock)
2014-08-03 22:25:38 +02:00
{
if (m_buffer == null)
return new ILogWriteHandler.LiveLogEntry[0];
2024-06-05 11:02:56 +02:00
return m_buffer.FlatArray((x) => x.When > offset && x.Type >= level);
2014-08-03 22:25:38 +02:00
}
}
public ILogWriteHandler.LiveLogEntry[] AfterID(long id, LogMessageType level, int pagesize)
2014-08-03 22:25:38 +02:00
{
RenewTimeout(level);
UpdateLogLevel();
2024-06-05 11:02:56 +02:00
lock (m_lock)
2014-08-03 22:25:38 +02:00
{
if (m_buffer == null)
return [];
2024-06-05 11:02:56 +02:00
var buffer = m_buffer.FlatArray((x) => x.ID > id && x.Type >= level);
// Return the <page_size> newest entries
2024-06-05 11:02:56 +02:00
if (buffer.Length > pagesize)
{
var index = buffer.Length - pagesize;
return buffer.Skip(index).Take(pagesize).ToArray();
}
2024-06-05 11:02:56 +02:00
else
{
return buffer;
}
2014-08-03 22:25:38 +02:00
}
}
private int[] GetActiveTimeouts()
{
var i = 0;
return (from n in m_timeouts
2024-06-05 11:02:56 +02:00
let ix = i++
where n > DateTime.Now
select ix).ToArray();
2014-08-03 22:25:38 +02:00
}
private void UpdateLogLevel()
2024-06-05 11:02:56 +02:00
{
m_logLevel =
(LogMessageType)GetActiveTimeouts().Append((int)m_target.MinimumLevel).Min();
2014-08-03 22:25:38 +02:00
}
#region ILog implementation
public void WriteMessage(LogEntry entry)
2014-08-03 22:25:38 +02:00
{
2024-06-05 11:02:56 +02:00
if (entry.Level < m_logLevel)
return;
2024-06-05 11:02:56 +02:00
try
{
m_target.WriteMessage(entry);
}
catch
{
}
2014-08-03 22:25:38 +02:00
2024-06-05 11:02:56 +02:00
lock (m_lock)
2014-08-03 22:25:38 +02:00
{
if (m_anytimeouts)
2014-08-03 22:25:38 +02:00
{
var q = GetActiveTimeouts();
if (q.Length == 0)
{
UpdateLogLevel();
m_anytimeouts = false;
if (m_buffer == null || m_buffer.Size != INACTIVE_SIZE)
m_buffer = new RingBuffer<ILogWriteHandler.LiveLogEntry>(INACTIVE_SIZE, m_buffer);
2014-08-03 22:25:38 +02:00
}
}
if (m_buffer != null)
m_buffer.Enqueue(new ILogWriteHandler.LiveLogEntry(entry));
2014-08-03 22:25:38 +02:00
}
2014-08-03 22:25:38 +02:00
}
#endregion
#region IDisposable implementation
public void Dispose()
{
m_target.Dispose();
2014-08-03 22:25:38 +02:00
}
#endregion
}
}