Commit 2e63ab47 authored by Martin Finkel's avatar Martin Finkel

LibVLC: Remove Log API

Remove the current Log API which only (kinda) works on Windows and not other platforms, in preparation of 3.0 stable release. Proper logging mechanism will come back soon with https://github.com/videolan/libvlcsharp/pull/15
parent 623cf712
......@@ -105,28 +105,12 @@ namespace LibVLCSharp.Tests
Assert.IsTrue(called);
}
[Test]
public async Task SetLogCallback()
{
var logCallbackCalled = false;
void LogCallback(object sender, LogEventArgs args) => logCallbackCalled = true;
_libVLC.Log += LogCallback;
await Task.Delay(1000);
_libVLC.Log -= LogCallback;
Assert.IsTrue(logCallbackCalled);
}
[Test]
public void SetLogFile()
{
var path = Path.GetTempFileName();
_libVLC.SetLogFile(path);
_libVLC.UnsetLog();
_libVLC.CloseLogFile();
var logs = File.ReadAllText(path);
Assert.True(logs.StartsWith("VLC media player"));
}
......@@ -134,7 +118,6 @@ namespace LibVLCSharp.Tests
[Test]
public void DisposeLibVLC()
{
_libVLC.SetLog((data, logLevel, logContext, format, args) => { });
_libVLC.SetDialogHandlers((title, text) => Task.CompletedTask,
(dialog, title, text, defaultUsername, askStore, token) => Task.CompletedTask,
(dialog, title, text, type, cancelText, firstActionText, secondActonText, token) => Task.CompletedTask,
......
......@@ -103,7 +103,6 @@ namespace LibVLCSharp.Tests
[Test]
public async Task CreateRealMediaSpecialCharacters()
{
_libVLC.Log += LibVLC_Log;
using (var media = new Media(_libVLC, RealMp3PathSpecialCharacter, FromType.FromPath))
{
Assert.False(media.IsParsed);
......@@ -119,7 +118,6 @@ namespace LibVLCSharp.Tests
mp.Stop();
}
}
_libVLC.Log -= LibVLC_Log;
}
[Test]
......
......@@ -18,31 +18,6 @@ namespace LibVLCSharp.Shared.Helpers
[DllImport(Constants.Msvcrt, CallingConvention = CallingConvention.Cdecl, EntryPoint = "fclose", SetLastError = true)]
public static extern int fcloseWindows(IntPtr stream);
/// <summary>
/// Compute the size required by vsprintf to print the parameters.
/// </summary>
/// <param name="format"></param>
/// <param name="ptr"></param>
/// <returns></returns>
[DllImport(Constants.Msvcrt, CallingConvention = CallingConvention.Cdecl)]
public static extern int _vscprintf(string format, IntPtr ptr);
/// <summary>
/// Format a string using printf style markers
/// </summary>
/// <remarks>
/// See https://stackoverflow.com/a/37629480/2663813
/// </remarks>
/// <param name="buffer">The output buffer (should be large enough, use _vscprintf)</param>
/// <param name="format">The message format</param>
/// <param name="args">The variable arguments list pointer. We do not know what it is, but the pointer must be given as-is from C back to sprintf.</param>
/// <returns>A negative value on failure, the number of characters written otherwise.</returns>
[DllImport(Constants.Msvcrt, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern int vsprintf(
IntPtr buffer,
string format,
IntPtr args);
#endregion
#region Linux
......@@ -430,9 +405,6 @@ namespace LibVLCSharp.Shared.Helpers
#endif
}
internal static int Vscprintf(string format, IntPtr ptr) => Native._vscprintf(format, ptr);
internal static int Vsprintf(IntPtr buffer, string format, IntPtr args) => Native.vsprintf(buffer, format, args);
#endif
/// <summary>
/// Frees an heap allocation returned by a LibVLC function.
......
......@@ -3,7 +3,6 @@ using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using LibVLCSharp.Shared.Helpers;
using LibVLCSharp.Shared.Structures;
......@@ -27,18 +26,7 @@ namespace LibVLCSharp.Shared
if (obj.GetType() != this.GetType()) return false;
return Equals((LibVLC) obj);
}
LogCallback _logCallback;
readonly object _logLock = new object();
#if NET || NETSTANDARD
/// <summary>
/// The real log event handlers.
/// </summary>
EventHandler<LogEventArgs> _log;
/// <summary>
/// A boolean to make sure that we are calling SetLog only once
/// </summary>
bool _logAttached;
IntPtr _logFileHandle;
#endif
IntPtr _dialogCbsPtr;
......@@ -76,10 +64,6 @@ namespace LibVLCSharp.Shared
EntryPoint = "libvlc_set_app_id")]
internal static extern void LibVLCSetAppId(IntPtr libVLC, IntPtr id, IntPtr version, IntPtr icon);
[DllImport(Constants.LibraryName, CallingConvention = CallingConvention.Cdecl,
EntryPoint = "libvlc_log_unset")]
internal static extern void LibVLCLogUnset(IntPtr libVLC);
[DllImport(Constants.LibraryName, CallingConvention = CallingConvention.Cdecl,
EntryPoint = "libvlc_log_set_file")]
internal static extern void LibVLCLogSetFile(IntPtr libVLC, IntPtr stream);
......@@ -88,10 +72,6 @@ namespace LibVLCSharp.Shared
CharSet = CharSet.Ansi, EntryPoint = "libvlc_log_get_context")]
internal static extern void LibVLCLogGetContext(IntPtr ctx, out IntPtr module, out IntPtr file, out UIntPtr line);
[DllImport(Constants.LibraryName, CallingConvention = CallingConvention.Cdecl,
EntryPoint = "libvlc_log_set")]
internal static extern void LibVLCLogSet(IntPtr libVLC, LogCallback cb, IntPtr data);
[DllImport(Constants.LibraryName, CallingConvention = CallingConvention.Cdecl,
EntryPoint = "libvlc_module_description_list_release")]
internal static extern void LibVLCModuleDescriptionListRelease(IntPtr moduleDescriptionList);
......@@ -236,7 +216,6 @@ namespace LibVLCSharp.Shared
if (disposing)
{
UnsetLog();
UnsetDialogHandlers();
}
......@@ -322,26 +301,6 @@ namespace LibVLCSharp.Shared
idUtf8, versionUtf8, iconUtf8);
}
/// <summary>Unsets the logging callback.</summary>
/// <remarks>
/// <para>This function deregisters the logging callback for a LibVLC instance.</para>
/// <para>This is rarely needed as the callback is implicitly unset when the instance</para>
/// <para>is destroyed.</para>
/// <para>This function will wait for any pending callbacks invocation to</para>
/// <para>complete (causing a deadlock if called from within the callback).</para>
/// <para>LibVLC 2.1.0 or later</para>
/// </remarks>
public void UnsetLog()
{
if (_logCallback != null)
Native.LibVLCLogUnset(NativeReference);
#if NET || NETSTANDARD
if (!CloseLogFile())
throw new VLCException("Could not close log file");
#endif
_logCallback = null;
}
/// <summary>
/// Unset dialog callbacks if previously set
/// </summary>
......@@ -355,54 +314,17 @@ namespace LibVLCSharp.Shared
}
}
public void SetLog(LogCallback cb)
{
if (cb == null) throw new ArgumentException(nameof(cb));
_logCallback = cb;
Native.LibVLCLogSet(NativeReference, cb, IntPtr.Zero);
}
#if NET || NETSTANDARD
/// <summary>
/// Native close log file handle
/// Close log file handle
/// </summary>
/// <returns>true if no file to close or close operation successful, false otherwise</returns>
bool CloseLogFile()
public bool CloseLogFile()
{
if (_logFileHandle == IntPtr.Zero) return true;
return MarshalUtils.Close(_logFileHandle);
}
/// <summary>
/// The event that is triggered when a log is emitted from libVLC.
/// Listening to this event will discard the default logger in libvlc.
/// </summary>
public event EventHandler<LogEventArgs> Log
{
add
{
lock (_logLock)
{
_log += value;
if (!_logAttached)
{
SetLog(OnLogInternal);
_logAttached = true;
}
}
}
remove
{
lock (_logLock)
{
_log -= value;
}
}
}
/// <summary>Sets up logging to a file.
/// Watch out: Overwrite contents if file exists!
......@@ -594,69 +516,6 @@ namespace LibVLCSharp.Shared
m => m.Build(),
Native.LibVLCRendererDiscovererReleaseList);
#if NET || NETSTANDARD
/// <summary>
/// Code taken from Vlc.DotNet
/// </summary>
/// <param name="data"></param>
/// <param name="level"></param>
/// <param name="ctx"></param>
/// <param name="format"></param>
/// <param name="args"></param>
void OnLogInternal(IntPtr data, LogLevel level, IntPtr ctx, string format, IntPtr args)
{
if (_log == null) return;
// Original source for va_list handling: https://stackoverflow.com/a/37629480/2663813
var byteLength = MarshalUtils.Vscprintf(format, args) + 1;
var utf8Buffer = Marshal.AllocHGlobal(byteLength);
string formattedDecodedMessage;
try
{
MarshalUtils.Vsprintf(utf8Buffer, format, args);
formattedDecodedMessage = utf8Buffer.FromUtf8();
}
finally
{
Marshal.FreeHGlobal(utf8Buffer);
}
GetLogContext(ctx, out var module, out var file, out var line);
// Do the notification on another thread, so that VLC is not interrupted by the logging
#if NET40
Task.Factory.StartNew(() => _log?.Invoke(NativeReference, new LogEventArgs(level, formattedDecodedMessage, module, file, line)));
#else
Task.Run(() => _log?.Invoke(NativeReference, new LogEventArgs(level, formattedDecodedMessage, module, file, line)));
#endif
}
#endif
/// <summary>
/// Gets log message debug infos.
///
/// This function retrieves self-debug information about a log message:
/// - the name of the VLC module emitting the message,
/// - the name of the source code module (i.e.file) and
/// - the line number within the source code module.
///
/// The returned module name and file name will be NULL if unknown.
/// The returned line number will similarly be zero if unknown.
/// </summary>
/// <param name="logContext">The log message context (as passed to the <see cref="LogCallback"/>)</param>
/// <param name="module">The module name storage.</param>
/// <param name="file">The source code file name storage.</param>
/// <param name="line">The source code file line number storage.</param>
void GetLogContext(IntPtr logContext, out string module, out string file, out uint? line)
{
Native.LibVLCLogGetContext(logContext, out var modulePtr, out var filePtr, out var linePtr);
line = linePtr == UIntPtr.Zero ? null : (uint?)linePtr.ToUInt32();
module = modulePtr.FromUtf8();
file = filePtr.FromUtf8();
}
/// <summary>Increments the native reference counter for this libvlc instance</summary>
internal void Retain() => Native.LibVLCRetain(NativeReference);
......@@ -711,10 +570,5 @@ namespace LibVLCSharp.Shared
/// </summary>
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void ExitCallback();
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void LogCallback(IntPtr data, LogLevel logLevel, IntPtr logContext,
[MarshalAs(UnmanagedType.LPStr)] string format, IntPtr args);
#endregion
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment