summaryrefslogtreecommitdiff
path: root/platform/darwin/src/MGLLoggingConfiguration.m
blob: fc2d231d4a0d44f00727d42d1ec854b74c9680f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#import "MGLLoggingConfiguration_Private.h"

#ifndef MGL_DISABLE_LOGGING
#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 info_log;
#if MGL_ENABLE_DEBUG_LOGGING
            static os_log_t debug_log;
#endif
            static os_log_t error_log;
            static os_log_t fault_log;
            static os_log_type_t log_types[] = { OS_LOG_TYPE_DEFAULT,
                                                    OS_LOG_TYPE_INFO,
#if MGL_ENABLE_DEBUG_LOGGING
                                                    OS_LOG_TYPE_DEBUG,
#endif
                                                    OS_LOG_TYPE_ERROR,
                                                    OS_LOG_TYPE_FAULT };
            dispatch_once(&once, ^ {
                info_log = os_log_create("com.mapbox.maps", "INFO");
#if MGL_ENABLE_DEBUG_LOGGING
                debug_log = os_log_create("com.mapbox.maps", "DEBUG");
#endif
                error_log = os_log_create("com.mapbox.maps", "ERROR");
                fault_log = os_log_create("com.mapbox.maps", "FAULT");
            });
            
            os_log_t mapbox_log;
            switch (level) {
                case MGLLoggingLevelInfo:
                    mapbox_log = info_log;
                    break;
#if MGL_ENABLE_DEBUG_LOGGING
                case MGLLoggingLevelDebug:
                    mapbox_log = debug_log;
                    break;
#endif
                case MGLLoggingLevelError:
                    mapbox_log = error_log;
                    break;
                case MGLLoggingLevelFault:
                    mapbox_log = fault_log;
                    break;
                case MGLLoggingLevelNone:
                default:
                    break;
            }

            os_log_type_t logType = log_types[level];
            os_log_with_type(mapbox_log, logType, "%@ - %lu: %@", fileName, (unsigned long)line, message);
        } else {
            NSString *category;
            switch (level) {
                case MGLLoggingLevelInfo:
                    category = @"INFO";
                    break;
#if MGL_ENABLE_DEBUG_LOGGING
                case MGLLoggingLevelDebug:
                    category = @"DEBUG";
                    break;
#endif
                case MGLLoggingLevelError:
                    category = @"ERROR";
                    break;
                case MGLLoggingLevelFault:
                    category = @"FAULT";
                    break;
                case MGLLoggingLevelNone:
                default:
                    break;
            }
            
            NSLog(@"[%@] %@ - %lu: %@", category, fileName, (unsigned long)line, message);
        }
    };
    
    return mapboxHandler;
}

@end
#endif