Commit ae71c1fe 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

parent fdc3e809
......@@ -25,6 +25,10 @@
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@protocol VLCLibraryLogReceiverProtocol;
/**
* 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
......@@ -50,7 +54,7 @@
- (instancetype)initWithOptions:(NSArray *)options;
/**
* Enables/disables debug logging
* Enables/disables debug logging to console
* \note NSLog is used to log messages
*/
@property (readwrite, nonatomic) BOOL debugLogging;
......@@ -58,7 +62,7 @@
/**
* Gets/sets the debug logging level
* \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
*/
@property (readwrite, nonatomic) int debugLoggingLevel;
......@@ -68,9 +72,17 @@
* 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.
* \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
......@@ -113,3 +125,16 @@
@property (nonatomic, assign) void *instance;
@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
......@@ -5,7 +5,7 @@ Version 4.0.0:
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:
--------------
......
......@@ -41,6 +41,12 @@ static void HandleMessage(void *,
const char *,
va_list);
static void HandleMessageForCustomTarget(void *,
int,
const libvlc_log_t *,
const char *,
va_list);
static VLCLibrary * sharedLibrary = nil;
@interface VLCLibrary()
......@@ -169,22 +175,54 @@ static VLCLibrary * sharedLibrary = nil;
}
}
- (void)setDebugLoggingToFile:(NSString * _Nonnull)filePath
- (BOOL)setDebugLoggingToFile:(NSString * _Nonnull)filePath
{
if (!filePath)
return;
return NO;
if (!_instance)
return;
return NO;
if (_debugLogging) {
libvlc_log_unset(_instance);
}
if (_logFileStream) {
fclose(_logFileStream);
}
_logFileStream = fopen([filePath UTF8String], "a");
if (_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;
}
}
......@@ -240,11 +278,10 @@ static void HandleMessage(void *data,
if (level > libraryInstance.debugLoggingLevel)
return;
char *str;
char *str = NULL;
if (vasprintf(&str, fmt, args) == -1) {
if (str)
free(str);
str = NULL;
return;
}
......@@ -253,5 +290,32 @@ static void HandleMessage(void *data,
VKLog(@"%s", 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