diff options
author | Fabian Guerra <fabian.guerra@mapbox.com> | 2018-08-24 16:44:00 -0700 |
---|---|---|
committer | Fabian Guerra Soto <fabian.guerra@mapbox.com> | 2018-10-26 12:45:14 -0700 |
commit | 064d8b9aa8e8d468069d5b670dde1bd873496b2e (patch) | |
tree | 4fd954a783e7ed727096612e91ed231c4076f90d /platform/darwin/src | |
parent | 885b3d33d9d2dee6447996b3d6c4d7fdea7722e4 (diff) | |
download | qtlocation-mapboxgl-064d8b9aa8e8d468069d5b670dde1bd873496b2e.tar.gz |
Add a custom logging for the Mapbox Maps SDK for iOS.
Diffstat (limited to 'platform/darwin/src')
-rw-r--r-- | platform/darwin/src/MGLLoggingConfiguration.h | 84 | ||||
-rw-r--r-- | platform/darwin/src/MGLLoggingConfiguration.m | 66 | ||||
-rw-r--r-- | platform/darwin/src/MGLLoggingConfiguration_Private.h | 19 |
3 files changed, 169 insertions, 0 deletions
diff --git a/platform/darwin/src/MGLLoggingConfiguration.h b/platform/darwin/src/MGLLoggingConfiguration.h new file mode 100644 index 0000000000..b467f80d31 --- /dev/null +++ b/platform/darwin/src/MGLLoggingConfiguration.h @@ -0,0 +1,84 @@ +#import <Foundation/Foundation.h> + +#import "MGLFoundation.h" + +NS_ASSUME_NONNULL_BEGIN + +/** + Constants indicating the message's logging level. + */ +typedef NS_ENUM(NSInteger, MGLLoggingLevel) { + /** + None-level messages are ignored. + */ + MGLLoggingLevelNone = 0, + /** + Info-level messages contain information that may be helpful for flow tracing + but is not essential. + */ + MGLLoggingLevelInfo, + /** + Debug-level messages contain information that may be helpful for troubleshooting + specific problems. + */ + MGLLoggingLevelDebug, + /** + Error-level messages contain information that is intended to aid in process-level + errors. + */ + MGLLoggingLevelError, + /** + Fault-level messages contain system-level error information. + */ + MGLLoggingLevelFault, + /** + :nodoc: Any new logging level should be included in the default logging implementation. + */ +}; + +/** + A block to be called once `loggingLevel` is set to a higher value than `MGLLoggingLevelNone`. + + @param loggingLevel The message logging level. + @param filePath The description of the file and method for the calling message. + @param line The line where the message is logged. + @param message The logging message. + */ +typedef void (^MGLLoggingBlockHandler)(MGLLoggingLevel loggingLevel, NSString *filePath, NSUInteger line, NSString *message); + +/** + The `MGLLoggingConfiguration` object provides a global way to set this SDK logging levels + and logging handler. + */ +MGL_EXPORT +@interface MGLLoggingConfiguration : NSObject + +/** + The handler this SDK uses to log messages. + + If this property is set to nil or if no custom handler is provided this property + is set to the default handler. + + The default handler uses `os_log` and `NSLog` for iOS 10+ and iOS < 10 respectively. + */ +@property (nonatomic, copy, null_resettable) MGLLoggingBlockHandler handler; + +/** + The logging level. + + The default value is `MGLLoggingLevelNone`. + + Setting this property includes logging levels less than or equal to the setted value. + */ +@property (assign, nonatomic) MGLLoggingLevel loggingLevel; + +/** + Returns the shared logging object. + */ +@property (class, nonatomic, readonly) MGLLoggingConfiguration *sharedConfiguration; + +- (MGLLoggingBlockHandler)handler UNAVAILABLE_ATTRIBUTE; + +@end + +NS_ASSUME_NONNULL_END diff --git a/platform/darwin/src/MGLLoggingConfiguration.m b/platform/darwin/src/MGLLoggingConfiguration.m new file mode 100644 index 0000000000..93f2698287 --- /dev/null +++ b/platform/darwin/src/MGLLoggingConfiguration.m @@ -0,0 +1,66 @@ +#import "MGLLoggingConfiguration_Private.h" + +#if __has_builtin(__builtin_os_log_format) +#import <os/log.h> +#endif + +@implementation MGLLoggingConfiguration + ++ (instancetype)sharedConfiguration +{ + static dispatch_once_t once; + static id sharedConfiguration; + dispatch_once(&once, ^{ + sharedConfiguration = [[self alloc] init]; + ((MGLLoggingConfiguration *)sharedConfiguration).handler = nil; + }); + return sharedConfiguration; +} + +- (void)setHandler:(void (^)(MGLLoggingLevel, NSString *, NSUInteger, NSString *))handler { + + if (!handler) { + _handler = [self defaultBlockHandler]; + } else { + _handler = handler; + } +} + +- (void)logCallingFunction:(const char *)callingFunction functionLine:(NSUInteger)functionLine messageType:(MGLLoggingLevel)type format:(id)messageFormat, ... +{ + va_list formatList; + va_start(formatList, messageFormat); + NSString *formattedMessage = [[NSString alloc] initWithFormat:messageFormat arguments:formatList]; + va_end(formatList); + + _handler(type, @(callingFunction), functionLine, formattedMessage); + +} + +- (MGLLoggingBlockHandler)defaultBlockHandler { + MGLLoggingBlockHandler mapboxHandler = ^(MGLLoggingLevel level, NSString *fileName, NSUInteger line, NSString *message) { + + if (@available(iOS 10.0, macOS 10.12.0, *)) { + static dispatch_once_t once; + static os_log_t mapbox_log; + static os_log_type_t log_types[] = { OS_LOG_TYPE_DEFAULT, + OS_LOG_TYPE_INFO, + OS_LOG_TYPE_DEBUG, + OS_LOG_TYPE_ERROR, + OS_LOG_TYPE_FAULT }; + dispatch_once(&once, ^ { + mapbox_log = os_log_create("com.mapbox.maps-ios-sdk", "SDK"); + }); + NSUInteger logTypesCount = sizeof(log_types) / sizeof(os_log_type_t); + NSAssert(level <= logTypesCount, @"There is an attempt to log a non suported logging level."); + os_log_type_t logType = log_types[level]; + os_log_with_type(mapbox_log, logType, "%@ - %d: %@", fileName, line, message); + } else { + NSLog(@"[SDK] %@ - %lu: %@", fileName, line, message); + } + }; + + return mapboxHandler; +} + +@end diff --git a/platform/darwin/src/MGLLoggingConfiguration_Private.h b/platform/darwin/src/MGLLoggingConfiguration_Private.h new file mode 100644 index 0000000000..ff7e85eed8 --- /dev/null +++ b/platform/darwin/src/MGLLoggingConfiguration_Private.h @@ -0,0 +1,19 @@ +#import "MGLLoggingConfiguration.h" + +#define MGLLogInfo(message, ...) MGLLogWithType(MGLLoggingLevelInfo, __PRETTY_FUNCTION__, __LINE__, message, ##__VA_ARGS__) +#define MGLLogDebug(message, ...) MGLLogWithType(MGLLoggingLevelDebug, __PRETTY_FUNCTION__, __LINE__, message, ##__VA_ARGS__) +#define MGLLogError(message, ...) MGLLogWithType(MGLLoggingLevelError, __PRETTY_FUNCTION__, __LINE__, message, ##__VA_ARGS__) +#define MGLLogFault(message, ...) MGLLogWithType(MGLLoggingLevelFault, __PRETTY_FUNCTION__, __LINE__, message, ##__VA_ARGS__) +#define MGLLogWithType(type, function, line, message, ...) \ +{ \ + if ([MGLLoggingConfiguration sharedConfiguration].loggingLevel != MGLLoggingLevelNone && type <= [MGLLoggingConfiguration sharedConfiguration].loggingLevel) \ + { \ + [[MGLLoggingConfiguration sharedConfiguration] logCallingFunction:function functionLine:line messageType:type format:(message), ##__VA_ARGS__]; \ + } \ +} + +@interface MGLLoggingConfiguration (Private) + +- (void)logCallingFunction:(const char *)callingFunction functionLine:(NSUInteger)functionLine messageType:(MGLLoggingLevel)type format:(id)messageFormat, ...; + +@end |