Browse Source

Library: implement logging including support for different levels

Felix Paul Kühne 10 years ago
parent
commit
b4d87b0a29
3 changed files with 62 additions and 5 deletions
  1. 18 3
      Headers/Public/VLCLibrary.h
  2. 3 0
      NEWS
  3. 41 2
      Sources/VLCLibrary.m

+ 18 - 3
Headers/Public/VLCLibrary.h

@@ -55,25 +55,40 @@
  - (instancetype)initWithOptions:(NSArray*)options;
 
 /**
+ * enables/disables debug logging
+ * \param BOOL value to enable/disable
+ * \note we will always log using NSLog
+ * \return debug logging state
+ */
+@property (readwrite, nonatomic) BOOL debugLogging;
+
+/**
+ * gets/sets the debug logging level
+ * \param int set level from 0 (all) to 4 (just error messages)
+ * \return int debug level
+*/
+@property (readwrite, nonatomic) int debugLoggingLevel;
+
+/**
  * Returns the library's version
  * \return The library version example "0.9.0-git Grishenko".
  */
 
-@property (readonly, copy) NSString * version;
+@property (readonly, copy) NSString *version;
 
 /**
  * Returns the compiler used to build the libvlc binary
  * \return The compiler version string.
  */
 
-@property (readonly, copy) NSString * compiler;
+@property (readonly, copy) NSString *compiler;
 
 /**
  * Returns the library's changeset
  * \return The library version example "adfee99".
  */
 
-@property (readonly, copy) NSString * changeset;
+@property (readonly, copy) NSString *changeset;
 
 /**
  * sets the application name and HTTP User Agend

+ 3 - 0
NEWS

@@ -1,6 +1,9 @@
 Version 3.0.0:
 --------------
 New APIs:
+- VLCLibrary
+  - added properties: debugLogging, debugLoggingLevel
+
 - VLCMediaPlayer
   - added properties: titleDescriptions, indexOfLongestTitle
   - added selector: chaptersForTitleIndex:

+ 41 - 2
Sources/VLCLibrary.m

@@ -36,6 +36,12 @@
 #include <vlc/vlc.h>
 #include <vlc/libvlc_structures.h>
 
+static void HandleMessage(void *,
+                          int,
+                          const libvlc_log_t *,
+                          const char *,
+                          va_list);
+
 static VLCLibrary * sharedLibrary = nil;
 
 @interface VLCLibrary()
@@ -50,7 +56,7 @@ static VLCLibrary * sharedLibrary = nil;
 {
     static dispatch_once_t onceToken;
     dispatch_once(&onceToken, ^{
-        sharedLibrary = [[self alloc] init];
+        sharedLibrary = [[VLCLibrary alloc] init];
     });
     return sharedLibrary;
 }
@@ -107,7 +113,6 @@ static VLCLibrary * sharedLibrary = nil;
 #ifndef NOSCARYCODECS
                       @"--avcodec-fast",
 #endif
-                      @"--verbose=0",
                       @"--text-renderer=quartztext",
                       @"--avi-index=3",
                       @"--extraintf=ios_dialog_provider"];
@@ -134,6 +139,18 @@ static VLCLibrary * sharedLibrary = nil;
     return vlcParams;
 }
 
+- (void)setDebugLogging:(BOOL)debugLogging
+{
+    if (!_instance)
+        return;
+
+    if (debugLogging) {
+        libvlc_log_set(_instance, HandleMessage, (__bridge void *)(self));
+    } else {
+        libvlc_log_unset(_instance);
+    }
+}
+
 - (NSString *)version
 {
     return @(libvlc_get_version());
@@ -168,3 +185,25 @@ static VLCLibrary * sharedLibrary = nil;
 }
 
 @end
+
+static void HandleMessage(void *data,
+                          int level,
+                          const libvlc_log_t *ctx,
+                          const char *fmt,
+                          va_list args)
+{
+    VLCLibrary *libraryInstance = (__bridge VLCLibrary *)data;
+
+    if (level < libraryInstance.debugLoggingLevel)
+        return;
+
+    char *str;
+    if (vasprintf(&str, fmt, args) == -1) {
+        return;
+    }
+
+    if (!str)
+        return;
+
+    VKLog(@"%@", [NSString stringWithUTF8String:str]);
+}