Commit a43c0496 authored by Felix Paul Kühne's avatar Felix Paul Kühne Committed by Caro

library: add API to log to a custom target using a defined protocol

(cherry picked from commit ae71c1fe)
parent 06fc2ac0
...@@ -30,6 +30,10 @@ ...@@ -30,6 +30,10 @@
@class VLCAudio; @class VLCAudio;
NS_ASSUME_NONNULL_BEGIN
@protocol VLCLibraryLogReceiverProtocol;
/** /**
* The VLCLibrary is the base library of VLCKit.framework. This object provides a shared instance that exposes the * The VLCLibrary is the base library of VLCKit.framework. This object provides a shared instance that exposes the
* internal functionalities of libvlc and libvlc-control. The VLCLibrary object is instantiated automatically when * internal functionalities of libvlc and libvlc-control. The VLCLibrary object is instantiated automatically when
...@@ -55,7 +59,7 @@ ...@@ -55,7 +59,7 @@
- (instancetype)initWithOptions:(NSArray *)options; - (instancetype)initWithOptions:(NSArray *)options;
/** /**
* Enables/disables debug logging * Enables/disables debug logging to console
* \note NSLog is used to log messages * \note NSLog is used to log messages
*/ */
@property (readwrite, nonatomic) BOOL debugLogging; @property (readwrite, nonatomic) BOOL debugLogging;
...@@ -63,7 +67,7 @@ ...@@ -63,7 +67,7 @@
/** /**
* Gets/sets the debug logging level * Gets/sets the debug logging level
* \note Logging level ranges from 0 (just error messages) to 4 (everything) * \note Logging level ranges from 0 (just error messages) to 4 (everything)
* \note values set here will be ignored when logging to a file * \note values set here will be consired only when logging to console
* \warning If an invalid level is provided, level defaults to 0 * \warning If an invalid level is provided, level defaults to 0
*/ */
@property (readwrite, nonatomic) int debugLoggingLevel; @property (readwrite, nonatomic) int debugLoggingLevel;
...@@ -73,9 +77,17 @@ ...@@ -73,9 +77,17 @@
* If the file already exists, the log will be appended by the end. If it does not exist, will be created. * If the file already exists, the log will be appended by the end. If it does not exist, will be created.
* The file will continously updated with new messages from this library instance. * The file will continously updated with new messages from this library instance.
* \note It is the client app's obligation to ensure that the target file path is writable and all subfolders exist * \note It is the client app's obligation to ensure that the target file path is writable and all subfolders exist
* \warning when enabling this feature, logging to the console will be stopped automatically * \warning when enabling this feature, logging to the console or an object target will be stopped automatically
* \return Returns NO on failure
*/
- (BOOL)setDebugLoggingToFile:(NSString *)filePath;
/**
* Activates debug logging to an object target following the VLCLibraryLogReceiverProtocol protocol
* The target will be continously called as new messages arrive from this library instance.
* \warning when enabling this feature, logging to the console or a file will be stopped automatically
*/ */
- (void)setDebugLoggingToFile:(NSString * _Nonnull)filePath; @property (readwrite, nonatomic) id<VLCLibraryLogReceiverProtocol> debugLoggingTarget;
/** /**
* Returns the library's version * Returns the library's version
...@@ -118,3 +130,16 @@ ...@@ -118,3 +130,16 @@
@property (nonatomic, assign) void *instance; @property (nonatomic, assign) void *instance;
@end @end
@protocol VLCLibraryLogReceiverProtocol <NSObject>
@required
/**
* called when VLC wants to print a debug message
* \param error the dialog title
* \param message the error message
*/
- (void)handleMessage:(NSString *)message
debugLevel:(int)level;
@end
NS_ASSUME_NONNULL_END
Version 3.2.1: Version 3.2.1:
-------------- --------------
- Add API to VLCLibrary to log debug information to a file - Add API to VLCLibrary to log debug information to a file or a custom target
Version 3.2.0: Version 3.2.0:
-------------- --------------
......
...@@ -46,6 +46,12 @@ static void HandleMessage(void *, ...@@ -46,6 +46,12 @@ static void HandleMessage(void *,
const char *, const char *,
va_list); va_list);
static void HandleMessageForCustomTarget(void *,
int,
const libvlc_log_t *,
const char *,
va_list);
static VLCLibrary * sharedLibrary = nil; static VLCLibrary * sharedLibrary = nil;
@interface VLCLibrary() @interface VLCLibrary()
...@@ -174,22 +180,54 @@ static VLCLibrary * sharedLibrary = nil; ...@@ -174,22 +180,54 @@ static VLCLibrary * sharedLibrary = nil;
} }
} }
- (void)setDebugLoggingToFile:(NSString * _Nonnull)filePath - (BOOL)setDebugLoggingToFile:(NSString * _Nonnull)filePath
{ {
if (!filePath) if (!filePath)
return; return NO;
if (!_instance) if (!_instance)
return; return NO;
if (_debugLogging) { if (_debugLogging) {
libvlc_log_unset(_instance); libvlc_log_unset(_instance);
} }
if (_logFileStream) {
fclose(_logFileStream);
}
_logFileStream = fopen([filePath UTF8String], "a"); _logFileStream = fopen([filePath UTF8String], "a");
if (_logFileStream) { if (_logFileStream) {
libvlc_log_set_file(_instance, _logFileStream); libvlc_log_set_file(_instance, _logFileStream);
_debugLogging = YES;
return YES;
}
return NO;
}
- (void)setDebugLoggingTarget:(id<VLCLibraryLogReceiverProtocol>) target
{
if (![target respondsToSelector:@selector(handleMessage:debugLevel:)]) {
VKLog(@"%s: target object does not implement required protocol", __func__);
return;
}
_debugLoggingTarget = target;
if (!_instance)
return;
if (_debugLogging) {
libvlc_log_unset(_instance);
}
if (_logFileStream)
fclose(_logFileStream);
if (target) {
libvlc_log_set(_instance, HandleMessageForCustomTarget, (__bridge void *)(self));
_debugLogging = YES;
} }
} }
...@@ -245,11 +283,10 @@ static void HandleMessage(void *data, ...@@ -245,11 +283,10 @@ static void HandleMessage(void *data,
if (level > libraryInstance.debugLoggingLevel) if (level > libraryInstance.debugLoggingLevel)
return; return;
char *str; char *str = NULL;
if (vasprintf(&str, fmt, args) == -1) { if (vasprintf(&str, fmt, args) == -1) {
if (str) if (str)
free(str); free(str);
str = NULL;
return; return;
} }
...@@ -258,5 +295,32 @@ static void HandleMessage(void *data, ...@@ -258,5 +295,32 @@ static void HandleMessage(void *data,
VKLog(@"%s", str); VKLog(@"%s", str);
free(str); free(str);
str = NULL; }
static void HandleMessageForCustomTarget(void *data,
int level,
const libvlc_log_t *ctx,
const char *fmt,
va_list args)
{
VLCLibrary *libraryInstance = (__bridge VLCLibrary *)data;
id debugLoggingTarget = libraryInstance.debugLoggingTarget;
if (!debugLoggingTarget) {
return;
}
char *str = NULL;
if (vasprintf(&str, fmt, args) == -1) {
if (str)
free(str);
return;
}
if (str == NULL)
return;
NSString *message = [[NSString alloc] initWithBytesNoCopy:str length:strlen(str) encoding:NSUTF8StringEncoding freeWhenDone:YES];
[debugLoggingTarget handleMessage:message debugLevel:level];
} }
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