Skip to content
This repository has been archived by the owner on Jan 21, 2022. It is now read-only.

Commit

Permalink
Merge pull request #278 from jeremyVignelles/Logging
Browse files Browse the repository at this point in the history
Added the ability to get VLC log messages from the application
  • Loading branch information
ZeBobo5 authored Aug 4, 2017
2 parents 5fc79b9 + 7c895f5 commit 57ac881
Show file tree
Hide file tree
Showing 22 changed files with 426 additions and 14 deletions.
15 changes: 9 additions & 6 deletions src/Samples/Vlc.DotNet.Forms.Samples/Sample.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/Samples/Vlc.DotNet.Forms.Samples/Sample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

namespace Vlc.DotNet.Forms.Samples
{
using System.Diagnostics;

public partial class Sample : Form
{
public Sample()
Expand Down Expand Up @@ -202,5 +204,9 @@ void ResizeVlcControl()
}
}

private void OnVlcMediaPlayerLog(object sender, Core.VlcMediaPlayerLogEventArgs e)
{
System.Diagnostics.Debug.WriteLine(string.Format("libVlc : {0} {1} @ {2}", e.Level, e.Message, e.Module));
}
}
}
6 changes: 6 additions & 0 deletions src/Samples/Vlc.DotNet.Wpf.Samples/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ public MainWindow()
InitializeComponent();
myControl.MediaPlayer.VlcLibDirectoryNeeded += OnVlcControlNeedsLibDirectory;
myControl.MediaPlayer.EndInit();

// This can also be called before EndInit
this.myControl.MediaPlayer.Log += (sender, args) =>
{
System.Diagnostics.Debug.WriteLine(string.Format("libVlc : {0} {1} @ {2}", args.Level, args.Message, args.Module));
};
}

private void OnVlcControlNeedsLibDirectory(object sender, Forms.VlcLibDirectoryNeededEventArgs e)
Expand Down
17 changes: 17 additions & 0 deletions src/Vlc.DotNet.Core.Interops/Signatures/libvlc.h/libvlc_log_cb.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace Vlc.DotNet.Core.Interops.Signatures
{
using System;
using System.Runtime.InteropServices;

/// <summary>
/// The delegate type that represent logging functions
/// </summary>
/// <param name="data">The data given to libvlc_log_set. In our case, this value will always be <see langword="null" /></param>
/// <param name="level">The log level</param>
/// <param name="logContext">The address of the structure that contains information about the log event. <see cref="GetLogContext"/></param>
/// <param name="format">The format of the log messages</param>
/// <param name="args">The va_list of printf arguments.</param>
[LibVlcFunction("libvlc_log_cb")]
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void LogCallback(IntPtr data, VlcLogLevel level, IntPtr logContext, [MarshalAs(UnmanagedType.LPStr)] string format, IntPtr args);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace Vlc.DotNet.Core.Interops.Signatures
{
using System;
using System.Runtime.InteropServices;

/// <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>
[LibVlcFunction("libvlc_log_get_context")]
[UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public delegate void GetLogContext(IntPtr logContext, out IntPtr module, out IntPtr file, out UIntPtr line);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Vlc.DotNet.Core.Interops.Signatures
{
public enum VlcLogLevel
: int
{
Debug = 0,
Notice = 2,
Warning = 3,
Error = 4
}
}
15 changes: 15 additions & 0 deletions src/Vlc.DotNet.Core.Interops/Signatures/libvlc.h/libvlc_log_set.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Runtime.InteropServices;

namespace Vlc.DotNet.Core.Interops.Signatures
{
/// <summary>
/// Registers a log callback
/// </summary>
/// <param name="libVlcInstance">The libvlc instance.</param>
/// <param name="callback">The method that will be called whenever a log is available.</param>
/// <param name="userData">User provided data to carry with the event.</param>
[LibVlcFunction("libvlc_log_set")]
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void SetLog(IntPtr libVlcInstance, LogCallback callback, IntPtr userData);
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,15 @@
<Compile Include="LibVlcFunctionAttribute.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Signatures\InteropObjectInstance.cs" />
<Compile Include="Signatures\libvlc.h\libvlc_log_level_e.cs" />
<Compile Include="Signatures\libvlc.h\libvlc_log_set.cs" />
<Compile Include="Signatures\libvlc.h\libvlc_video_filter_list_get.cs" />
<Compile Include="Signatures\libvlc.h\libvlc_audio_filter_list_get.cs" />
<Compile Include="Signatures\libvlc.h\libvlc_log_cb.cs" />
<Compile Include="Signatures\libvlc.h\libvlc_module_description_list_release.cs" />
<Compile Include="Signatures\libvlc.h\libvlc_module_description_t.cs" />
<Compile Include="Signatures\libvlc.h\libvlc_get_changeset.cs" />
<Compile Include="Signatures\libvlc.h\libvlc_log_get_context.cs" />
<Compile Include="Signatures\libvlc_media.h\libvlc_media_get_tracks_info.cs" />
<Compile Include="Signatures\libvlc_media.h\libvlc_media_get_stats.cs" />
<Compile Include="Signatures\libvlc_media.h\libvlc_media_track_info_t.cs" />
Expand Down Expand Up @@ -158,6 +162,7 @@
<Compile Include="VlcManager.GetAudioTracksDescriptions.cs" />
<Compile Include="VlcManager.GetAudioDelay.cs" />
<Compile Include="VlcManager.GetLength.cs" />
<Compile Include="VlcManager.GetLogContext.cs" />
<Compile Include="VlcManager.GetMediaPlayerVideoHostHandle.cs" />
<Compile Include="VlcManager.GetTime.cs" />
<Compile Include="VlcManager.GetVideoAspectRatio.cs" />
Expand Down Expand Up @@ -210,6 +215,7 @@
<Compile Include="VlcManager.NextFrame.cs" />
<Compile Include="VlcManager.SetRate.cs" />
<Compile Include="VlcManager.GetRate.cs" />
<Compile Include="VlcManager.SetLog.cs" />
<Compile Include="VlcManager.GetMediaPosition.cs" />
<Compile Include="VlcManager.GetMediaChapter.cs" />
<Compile Include="VlcManager.GetMediaChapterCount.cs" />
Expand Down Expand Up @@ -306,7 +312,7 @@
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@
<Compile Include="Signatures\libvlc.h\libvlc_get_changeset.cs" />
<Compile Include="Signatures\libvlc.h\libvlc_get_compiler.cs" />
<Compile Include="Signatures\libvlc.h\libvlc_get_version.cs" />
<Compile Include="Signatures\libvlc.h\libvlc_log_cb.cs" />
<Compile Include="Signatures\libvlc.h\libvlc_log_get_context.cs" />
<Compile Include="Signatures\libvlc.h\libvlc_log_level_e.cs" />
<Compile Include="Signatures\libvlc.h\libvlc_log_set.cs" />
<Compile Include="Signatures\libvlc.h\libvlc_module_description_list_release.cs" />
<Compile Include="Signatures\libvlc.h\libvlc_module_description_t.cs" />
<Compile Include="Signatures\libvlc.h\libvlc_new.cs" />
Expand Down Expand Up @@ -214,6 +218,7 @@
<Compile Include="VlcManager.GetCompiler.cs" />
<Compile Include="VlcManager.GetEventTypeName.cs" />
<Compile Include="VlcManager.GetLength.cs" />
<Compile Include="VlcManager.GetLogContext.cs" />
<Compile Include="VlcManager.GetMediaPlayerVideoHostHandle.cs" />
<Compile Include="VlcManager.GetTime.cs" />
<Compile Include="VlcManager.GetVideoAdjust.cs" />
Expand Down Expand Up @@ -270,6 +275,7 @@
<Compile Include="VlcManager.SetAudioOutput.cs" />
<Compile Include="VlcManager.SetAudioOutputDevice.cs" />
<Compile Include="VlcManager.SetAudioTrack.cs" />
<Compile Include="VlcManager.SetLog.cs" />
<Compile Include="VlcManager.SetMediaChapter.cs" />
<Compile Include="VlcManager.SetMediaMeta.cs" />
<Compile Include="VlcManager.SetMediaPlayerVideoHostHandle.cs" />
Expand Down Expand Up @@ -307,7 +313,7 @@
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@
<Compile Include="Signatures\libvlc.h\libvlc_get_changeset.cs" />
<Compile Include="Signatures\libvlc.h\libvlc_get_compiler.cs" />
<Compile Include="Signatures\libvlc.h\libvlc_get_version.cs" />
<Compile Include="Signatures\libvlc.h\libvlc_log_cb.cs" />
<Compile Include="Signatures\libvlc.h\libvlc_log_get_context.cs" />
<Compile Include="Signatures\libvlc.h\libvlc_log_level_e.cs" />
<Compile Include="Signatures\libvlc.h\libvlc_log_set.cs" />
<Compile Include="Signatures\libvlc.h\libvlc_module_description_list_release.cs" />
<Compile Include="Signatures\libvlc.h\libvlc_module_description_t.cs" />
<Compile Include="Signatures\libvlc.h\libvlc_new.cs" />
Expand Down Expand Up @@ -186,6 +190,7 @@
<Compile Include="VlcEventManagerInstance.cs" />
<Compile Include="VlcInstance.cs" />
<Compile Include="VlcManager.GetLength.cs" />
<Compile Include="VlcManager.GetLogContext.cs" />
<Compile Include="VlcManager.GetMediaPlayerVideoHostHandle.cs" />
<Compile Include="VlcManager.GetTime.cs" />
<Compile Include="VlcManager.GetVideoAspectRatio.cs" />
Expand Down Expand Up @@ -277,6 +282,7 @@
<Compile Include="VlcManager.SetAudioOutput.cs" />
<Compile Include="VlcManager.SetAudioOutputDevice.cs" />
<Compile Include="VlcManager.SetAudioTrack.cs" />
<Compile Include="VlcManager.SetLog.cs" />
<Compile Include="VlcManager.SetMediaChapter.cs" />
<Compile Include="VlcManager.SetMediaMeta.cs" />
<Compile Include="VlcManager.SetMediaPlayerVideoHostHandle.cs" />
Expand Down Expand Up @@ -307,7 +313,7 @@
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Signatures\InteropObjectInstance.cs" />
<Compile Include="Signatures\libvlc.h\libvlc_audio_filter_list_get.cs" />
<Compile Include="Signatures\libvlc.h\libvlc_log_cb.cs" />
<Compile Include="Signatures\libvlc.h\libvlc_callback_t.cs" />
<Compile Include="Signatures\libvlc.h\libvlc_clearerr.cs" />
<Compile Include="Signatures\libvlc.h\libvlc_errmsg.cs" />
Expand All @@ -68,6 +69,9 @@
<Compile Include="Signatures\libvlc.h\libvlc_get_changeset.cs" />
<Compile Include="Signatures\libvlc.h\libvlc_get_compiler.cs" />
<Compile Include="Signatures\libvlc.h\libvlc_get_version.cs" />
<Compile Include="Signatures\libvlc.h\libvlc_log_get_context.cs" />
<Compile Include="Signatures\libvlc.h\libvlc_log_level_e.cs" />
<Compile Include="Signatures\libvlc.h\libvlc_log_set.cs" />
<Compile Include="Signatures\libvlc.h\libvlc_module_description_list_release.cs" />
<Compile Include="Signatures\libvlc.h\libvlc_module_description_t.cs" />
<Compile Include="Signatures\libvlc.h\libvlc_new.cs" />
Expand Down Expand Up @@ -221,6 +225,7 @@
<Compile Include="VlcManager.GetFramesPerSecond.cs" />
<Compile Include="VlcManager.GetLastErrorMessage.cs" />
<Compile Include="VlcManager.GetLength.cs" />
<Compile Include="VlcManager.GetLogContext.cs" />
<Compile Include="VlcManager.GetMediaChapter.cs" />
<Compile Include="VlcManager.GetMediaChapterCount.cs" />
<Compile Include="VlcManager.GetMediaDuration.cs" />
Expand Down Expand Up @@ -275,6 +280,7 @@
<Compile Include="VlcManager.SetAudioOutput.cs" />
<Compile Include="VlcManager.SetAudioOutputDevice.cs" />
<Compile Include="VlcManager.SetAudioTrack.cs" />
<Compile Include="VlcManager.SetLog.cs" />
<Compile Include="VlcManager.SetMediaChapter.cs" />
<Compile Include="VlcManager.SetMediaMeta.cs" />
<Compile Include="VlcManager.SetMediaPlayerVideoHostHandle.cs" />
Expand Down Expand Up @@ -307,7 +313,7 @@
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
Expand Down
44 changes: 44 additions & 0 deletions src/Vlc.DotNet.Core.Interops/VlcManager.GetLogContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using Vlc.DotNet.Core.Interops.Signatures;

namespace Vlc.DotNet.Core.Interops
{
using System.Runtime.InteropServices;

public sealed partial class VlcManager
{
/// <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>
public void GetLogContext(IntPtr logContext, out string module, out string file, out uint? line)
{
UIntPtr _line;
IntPtr _module;
IntPtr _file;
GetInteropDelegate<GetLogContext>().Invoke(logContext, out _module, out _file, out _line);
if (_line == UIntPtr.Zero)
{
line = null;
}
else
{
line = _line.ToUInt32();
}

module = Marshal.PtrToStringAnsi(_module);
file = Marshal.PtrToStringAnsi(_file);
}
}
}
24 changes: 24 additions & 0 deletions src/Vlc.DotNet.Core.Interops/VlcManager.SetLog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using Vlc.DotNet.Core.Interops.Signatures;

namespace Vlc.DotNet.Core.Interops
{
public sealed partial class VlcManager
{
/// <summary>
/// Keeps a reference to the last callback that was given to the <see cref="SetLog"/> method.
/// This is to avoid garbage collection of the delegate before the function is called.
/// </summary>
private LogCallback _logCallbackReference;

public void SetLog(LogCallback callback)
{
if (callback == null)
{
throw new ArgumentException("Callback for log is not initialized.");
}
this._logCallbackReference = callback;
GetInteropDelegate<SetLog>().Invoke(this.myVlcInstance, this._logCallbackReference, IntPtr.Zero);
}
}
}
Loading

0 comments on commit 57ac881

Please sign in to comment.