Checkpoint
This commit is contained in:
@@ -1,15 +1,19 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Foodsoft.Alpm.Interop;
|
||||
|
||||
namespace Foodsoft.Alpm
|
||||
{
|
||||
public sealed class Handle : IDisposable
|
||||
{
|
||||
private readonly object _eventLock = new object();
|
||||
private readonly SafeAlpmHandle _handle;
|
||||
|
||||
public Handle(string root, string dbpath)
|
||||
private EventHandler<LogEventArgs> _logEvent;
|
||||
|
||||
public Handle(string root, string dbPath)
|
||||
{
|
||||
_handle = alpm.alpm_initialize(root, dbpath, out var err);
|
||||
_handle = alpm.alpm_initialize(root, dbPath, out var err);
|
||||
if (_handle.IsInvalid) throw new AlpmException(err);
|
||||
}
|
||||
|
||||
@@ -22,6 +26,8 @@ namespace Foodsoft.Alpm
|
||||
set => Detail.SetStringCollection(value, _handle, s => alpm.alpm_option_add_cachedir(_handle, s));
|
||||
}
|
||||
|
||||
public LogLevel LogLevel { get; set; }
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_handle.Dispose();
|
||||
@@ -55,5 +61,42 @@ namespace Foodsoft.Alpm
|
||||
{
|
||||
return alpm.alpm_pkg_should_ignore(_handle, pkg.Handle) == 0;
|
||||
}
|
||||
|
||||
public event EventHandler<LogEventArgs> Log
|
||||
{
|
||||
add
|
||||
{
|
||||
void LogCallback(LogLevel level, IntPtr format, IntPtr args)
|
||||
{
|
||||
if (level > LogLevel) return;
|
||||
_logEvent?.Invoke(this,
|
||||
new LogEventArgs {Level = level, Message = CFormatter.Format(format, args)});
|
||||
}
|
||||
|
||||
lock (_eventLock)
|
||||
{
|
||||
var wasEmpty = _logEvent == null;
|
||||
_logEvent += value;
|
||||
if (wasEmpty)
|
||||
alpm.alpm_option_set_logcb(_handle, LogCallback);
|
||||
}
|
||||
}
|
||||
remove
|
||||
{
|
||||
lock (_eventLock)
|
||||
{
|
||||
// ReSharper disable once DelegateSubtraction
|
||||
_logEvent -= value;
|
||||
if (_logEvent == null)
|
||||
alpm.alpm_option_set_logcb(_handle, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public struct LogEventArgs
|
||||
{
|
||||
public LogLevel Level;
|
||||
public string Message;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user