Make LogHandler customizable

This commit is contained in:
Lukas Spieß
2016-04-16 00:23:56 +02:00
parent 8d39e02106
commit d02e0f5fd6
2 changed files with 21 additions and 7 deletions

View File

@@ -3,18 +3,22 @@
#import <Foundation/Foundation.h>
#import "HockeySDKEnums.h"
#define BITHockeyLog(_level, _message, ...) [BITHockeyLogger logLevel:_level function:__PRETTY_FUNCTION__ line:__LINE__ message:_message, ##__VA_ARGS__]
#define BITHockeyLog(_level, _message, ...) [BITHockeyLogger logLevel:_level file:__FILE__ function:__PRETTY_FUNCTION__ line:__LINE__ message:_message, ##__VA_ARGS__]
#define BITHockeyLogError(format, ...) BITHockeyLog(BITLogLevelError, format, ##__VA_ARGS__)
#define BITHockeyLogWarning(format, ...) BITHockeyLog(BITLogLevelWarning, format, ##__VA_ARGS__)
#define BITHockeyLogDebug(format, ...) BITHockeyLog(BITLogLevelDebug, format, ##__VA_ARGS__)
#define BITHockeyLogVerbose(format, ...) BITHockeyLog(BITLogLevelVerbose, format, ##__VA_ARGS__)
typedef void (^BITLogHandler)(BITLogLevel, const char *, const char *, uint, NSString *);
@interface BITHockeyLogger : NSObject
+ (BITLogLevel)currentLogLevel;
+ (void)setCurrentLogLevel:(BITLogLevel)currentLogLevel;
+ (void)logLevel:(BITLogLevel)loglevel function:(const char *)function line:(int)line message:(NSString *)message, ...;
+ (void)setLogHandler:(BITLogHandler)logHandler;
+ (void)logLevel:(BITLogLevel)loglevel file:(const char *)file function:(const char *)function line:(uint)line message:(NSString *)message, ...;
@end

View File

@@ -13,14 +13,24 @@ static BITLogLevel _currentLogLevel = BITLogLevelWarning;
_currentLogLevel = currentLogLevel;
}
+ (void)logLevel:(BITLogLevel)logLevel function:(const char *)function line:(int)line message:(NSString *)message, ... {
va_list args;
static BITLogHandler currentLogHandler = ^(BITLogLevel logLevel, const char *file, const char *function, uint line, NSString *message) {
if (message) {
va_start(args, message);
if (self.currentLogLevel < logLevel) {
if (_currentLogLevel < logLevel) {
return;
}
NSLog((@"[HockeySDK] %s/%d %@"), function, line, [[NSString alloc] initWithFormat:message arguments:args]);
NSLog((@"[HockeySDK] %s/%d %@"), function, line, message);
}
};
+ (void)setLogHandler:(BITLogHandler)logHandler {
currentLogHandler = logHandler;
}
+ (void)logLevel:(BITLogLevel)loglevel file:(const char *)file function:(const char *)function line:(uint)line message:(NSString *)message, ... {
if (currentLogHandler) {
va_list args;
va_start(args, message);
currentLogHandler(loglevel, file, function, line, [[NSString alloc] initWithFormat:message arguments:args]);
va_end(args);
}
}