[vlc-commits] cppcx: Expose logSet
Hugo Beauzée-Luyssen
git at videolan.org
Mon Sep 28 18:47:54 CEST 2015
libvlcpp | branch: master | Hugo Beauzée-Luyssen <hugo at beauzee.fr> | Mon Sep 28 18:31:23 2015 +0200| [109ff137444ccd1d6f39bdc871e9adde24414a60] | committer: Hugo Beauzée-Luyssen
cppcx: Expose logSet
> http://git.videolan.org/gitweb.cgi/libvlcpp.git/?a=commit;h=109ff137444ccd1d6f39bdc871e9adde24414a60
---
cppcx/InstanceCX.cpp | 19 +++++++++++++++++++
cppcx/InstanceCX.hpp | 30 ++++++++++++++++++++++++++++++
vlcpp/Instance.hpp | 21 ++++++++++++++-------
3 files changed, 63 insertions(+), 7 deletions(-)
diff --git a/cppcx/InstanceCX.cpp b/cppcx/InstanceCX.cpp
index b756202..91af4d8 100644
--- a/cppcx/InstanceCX.cpp
+++ b/cppcx/InstanceCX.cpp
@@ -118,6 +118,25 @@ namespace libVLCX
m_instance.setAppId(VLCString(id), VLCString(version), VLCString(icon));
}
+ void Instance::logUnset()
+ {
+ m_instance.logUnset();
+ }
+
+ void Instance::logSet(LogCallback^ logCb)
+ {
+ m_instance.logSet([logCb](int logLevel, const libvlc_log_t* Log, std::string msgStr)
+ {
+ size_t len = MultiByteToWideChar(CP_UTF8, 0, msgStr.c_str(), -1, nullptr, 0);
+ if (len == 0)
+ return;
+ wchar_t *out = new wchar_t[len];
+ std::unique_ptr<wchar_t[]> out_u(out);
+ MultiByteToWideChar(CP_UTF8, 0, msgStr.c_str(), -1, out, len);
+ logCb(logLevel, ref new Platform::String(out));
+ });
+ }
+
Windows::Foundation::Collections::IVector<ModuleDescription^>^ Instance::audioFilterList()
{
return MarshallVector<ModuleDescription, VLC::ModuleDescription>(m_instance.audioFilterList());
diff --git a/cppcx/InstanceCX.hpp b/cppcx/InstanceCX.hpp
index 9b0754d..46853f9 100644
--- a/cppcx/InstanceCX.hpp
+++ b/cppcx/InstanceCX.hpp
@@ -32,6 +32,7 @@
namespace libVLCX
{
+ public delegate void LogCallback(int, Platform::String^);
public ref class Instance sealed
{
public:
@@ -101,6 +102,35 @@ namespace libVLCX
void setAppId(Platform::String^ id, Platform::String^ version, Platform::String^ icon);
/**
+ * Unsets the logging callback for a LibVLC instance. This is rarely
+ * needed: the callback is implicitly unset when the instance is
+ * destroyed. This function will wait for any pending callbacks
+ * invocation to complete (causing a deadlock if called from within the
+ * callback).
+ *
+ * \version LibVLC 2.1.0 or later
+ */
+ void logUnset();
+
+ /**
+ * Sets the logging callback for a LibVLC instance. This function is
+ * thread-safe: it will wait for any pending callbacks invocation to
+ * complete.
+ *
+ * \note Some log messages (especially debug) are emitted by LibVLC while
+ * is being initialized. These messages cannot be captured with this
+ * interface.
+ *
+ * \param logCb A delegate of the form void(int logLevel, String message)
+ *
+ * \warning A deadlock may occur if this function is called from the
+ * callback.
+ *
+ * \version LibVLC 2.1.0 or later
+ */
+ void logSet(LogCallback^ logCb);
+
+ /**
* Returns a list of audio filters that are available.
*
* \return a list of module descriptions. It should be freed with
diff --git a/vlcpp/Instance.hpp b/vlcpp/Instance.hpp
index 0ba78e5..d4a61e5 100644
--- a/vlcpp/Instance.hpp
+++ b/vlcpp/Instance.hpp
@@ -192,17 +192,24 @@ public:
static_assert(signature_match<LogCb, void(int, const libvlc_log_t*, std::string)>::value,
"Mismatched log callback" );
auto wrapper = [logCb](int level, const libvlc_log_t* ctx, const char* format, va_list va) {
- VaCopy vaCopy( va );
- int len = vsnprintf( nullptr, 0, format, vaCopy.va );
- if ( len > 0 )
+#ifndef _MSC_VER
+ VaCopy vaCopy(va);
+ int len = vsnprintf(nullptr, 0, format, vaCopy.va);
+ if (len > 0)
{
std::unique_ptr<char[]> message{ new char[len] };
- if ( vsnprintf( message.get(), len, format, va ) != -1 )
- logCb( level, ctx, std::string{ message.get() } );
+ if (vsnprintf(message.get(), len, format, va) != -1)
+ logCb(level, ctx, std::string{ message.get() });
}
+#else
+ //MSVC treats passing nullptr as 1st vsnprintf(_s) as an error
+ char buff[512];
+ vsnprintf(buff, sizeof(buff) - 1, format, va);
+ logCb(level, ctx, std::string{ buff });
+#endif
};
- libvlc_log_set( *this, CallbackWrapper<(unsigned int)CallbackIdx::Log, libvlc_log_cb>::wrap( this, std::move( wrapper ) ),
- static_cast<CallbackOwner<2>*>( this ) );
+ libvlc_log_set(*this, CallbackWrapper<(unsigned int)CallbackIdx::Log, libvlc_log_cb>::wrap(this, std::move(wrapper)),
+ static_cast<CallbackOwner<2>*>(this));
}
/**
More information about the vlc-commits
mailing list