This repository was archived by the owner on Feb 15, 2018. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathMDCLogController.m
More file actions
105 lines (78 loc) · 3.11 KB
/
Copy pathMDCLogController.m
File metadata and controls
105 lines (78 loc) · 3.11 KB
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
//
// MDCLogController.m
// DeveloperMenu
//
// Created by Matthew Cheetham on 02/02/2014.
// Copyright (c) 2014 Matthew Cheetham. All rights reserved.
//
#import "MDCLogController.h"
#import "MDCDeviceInformationController.h"
#import "MDCDeviceInformationItem.h"
@interface MDCLogController ()
@end
@implementation MDCLogController
static MDCLogController *sharedController = nil;
+ (MDCLogController *)sharedController
{
@synchronized(self) {
if (sharedController == nil) {
sharedController = [[self alloc] init];
}
}
return sharedController;
}
- (id)init
{
self = [super init];
if (self) {
self.deviceLogs = [NSMutableArray array];
}
return self;
}
- (void)addLogWithLevel:(MDCLogLevel)logLevel function:(const char *)function logContent:(NSString *)logContent, ...
{
va_list args;
va_start(args, logContent);
NSString *message = [[NSString alloc] initWithFormat:logContent
arguments:args];
va_end(args);
MDCLog *log = [MDCLog logWithContent:[NSString stringWithFormat:@"%s %@", function, message] content:logLevel];
[self willChangeValueForKey:@"deviceLogs"];
[self.deviceLogs insertObject:log atIndex:0];
[self didChangeValueForKey:@"deviceLogs"];
}
- (void)generateLogFileWithCompletion:(MDCCreateLogCompletion)completion
{
//Setup file
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = paths[0];
NSURL *documentsURL = [NSURL fileURLWithPath:documentsDirectory];
NSString *fileName = [NSString stringWithFormat:@"log_%.0f", [NSDate timeIntervalSinceReferenceDate]];
NSURL *destinationURL = [documentsURL URLByAppendingPathComponent:fileName];
NSError *fileCreateError;
[[NSFileManager defaultManager] createFileAtPath:[destinationURL path] contents:nil attributes:nil];
//Setup string
NSMutableString *logContents = [NSMutableString string];
[logContents appendString:@"Device information\n"];
//Add device info to string
MDCDeviceInformationController *deviceInfoController = [MDCDeviceInformationController sharedController];
for(MDCDeviceInformationItem *deviceInfoItem in deviceInfoController.deviceInformationItems)
{
[logContents appendFormat:@"%@: %@\n", deviceInfoItem.deviceProperty, deviceInfoItem.deviceValue];
}
[logContents appendString:@"\n\nDevice Logs\n"];
//Add device logs to string
for(MDCLog *deviceLog in self.deviceLogs){
[logContents appendFormat:@"%@\n", [deviceLog logContentWithLevelPrefix]];
}
//Add plist info
[logContents appendFormat:@"\n\nInfo Plist\n%@", [[NSBundle mainBundle] infoDictionary]];
if(fileCreateError){
completion(nil, fileCreateError);
}
//Write data to file
NSError *dataWritingError;
[logContents writeToFile:[destinationURL path] atomically:YES encoding:NSUTF8StringEncoding error:&dataWritingError];
completion(destinationURL, dataWritingError);
}
@end