Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/log lost #1092

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@

+ (instancetype)sharedInstance;

@property (nonatomic, strong) NSMutableArray<DoraemonNSLogModel *> *dataArray;

- (void)startNSLogMonitor;

- (void)stopNSLogMonitor;

- (void)addNSLog:(NSString *)log;

- (NSArray<DoraemonNSLogModel *> *)readLogs;
- (void)clearLogs;

@end

Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ void myNSLog(NSString *format, ...){
old_nslog(@"%@",str);
}

@interface DoraemonNSLogManager()
@property (nonatomic, strong) NSMutableArray<DoraemonNSLogModel *> *dataArray;
@property (nonatomic, strong) dispatch_queue_t logQueue;
@end

@implementation DoraemonNSLogManager

Expand All @@ -39,6 +43,7 @@ + (instancetype)sharedInstance {
}

- (void)startNSLogMonitor{
self.logQueue = dispatch_queue_create("dokit.log.queue", DISPATCH_QUEUE_SERIAL);
doraemon_rebind_symbols((struct doraemon_rebinding[1]){"NSLog", (void *)myNSLog, (void **)&old_nslog},1);
}

Expand All @@ -47,14 +52,16 @@ - (void)stopNSLogMonitor{
}

- (void)addNSLog:(NSString *)log{
DoraemonNSLogModel *model = [[DoraemonNSLogModel alloc] init];
model.content = log;
model.timeInterval = [[NSDate date] timeIntervalSince1970];

if (!_dataArray) {
_dataArray = [[NSMutableArray alloc] init];
}
[_dataArray addObject:model];
dispatch_sync(self.logQueue, ^{
DoraemonNSLogModel *model = [[DoraemonNSLogModel alloc] init];
model.content = log;
model.timeInterval = [[NSDate date] timeIntervalSince1970];

if (!_dataArray) {
_dataArray = [[NSMutableArray alloc] init];
}
[_dataArray addObject:model];
});

// return;
// if (@available(iOS 13.0, *)) {
Expand All @@ -66,4 +73,15 @@ - (void)addNSLog:(NSString *)log{

}

- (NSArray<DoraemonNSLogModel *> *)readLogs {
NSArray *array = [_dataArray copy];
return array;
}

- (void)clearLogs {
dispatch_sync(self.logQueue, ^{
[self.dataArray removeAllObjects];
});
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ - (void)viewDidLoad {
[self setRightNavBarItems:@[model1,model2]];

//按照时间倒序排列
self.dataArray = [[[DoraemonNSLogManager sharedInstance].dataArray reverseObjectEnumerator] allObjects];

NSArray *logs = [[DoraemonNSLogManager sharedInstance] readLogs];
self.dataArray = [[logs reverseObjectEnumerator] allObjects];

_searchView = [[DoraemonNSLogSearchView alloc] initWithFrame:CGRectMake(kDoraemonSizeFrom750_Landscape(32), IPHONE_NAVIGATIONBAR_HEIGHT+kDoraemonSizeFrom750_Landscape(32), self.view.doraemon_width-2*kDoraemonSizeFrom750_Landscape(32), kDoraemonSizeFrom750_Landscape(100))];
_searchView.delegate = self;
Expand All @@ -49,13 +49,13 @@ - (void)viewDidLoad {
}

- (void)clear {
[[DoraemonNSLogManager sharedInstance].dataArray removeAllObjects];
[[DoraemonNSLogManager sharedInstance] clearLogs];
self.dataArray = [[NSArray alloc] init];
[self.tableView reloadData];
}

- (void)export {
NSArray<DoraemonNSLogModel *> *dataArray = [[DoraemonNSLogManager sharedInstance].dataArray copy];
NSArray<DoraemonNSLogModel *> *dataArray = [[DoraemonNSLogManager sharedInstance] readLogs];
NSMutableString *log = [[NSMutableString alloc] init];
for (DoraemonNSLogModel *model in dataArray) {
NSString *time = [NSString stringWithFormat:@"[%@]",[DoraemonUtil dateFormatTimeInterval:model.timeInterval]];
Expand Down Expand Up @@ -128,7 +128,7 @@ - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEd
#pragma mark - DoraemonNSLogSearchViewDelegate
- (void)searchViewInputChange:(NSString *)text{
if (text.length > 0) {
NSArray *dataArray = [[[DoraemonNSLogManager sharedInstance].dataArray reverseObjectEnumerator] allObjects];
NSArray *dataArray = [[[[DoraemonNSLogManager sharedInstance] readLogs] reverseObjectEnumerator] allObjects];
NSMutableArray *resultArray = [[NSMutableArray alloc] init];
for(DoraemonNSLogModel *model in dataArray){
NSString *content = model.content;
Expand All @@ -138,7 +138,7 @@ - (void)searchViewInputChange:(NSString *)text{
}
self.dataArray = [[NSArray alloc] initWithArray:resultArray];
}else{
self.dataArray = [[[DoraemonNSLogManager sharedInstance].dataArray reverseObjectEnumerator] allObjects];
self.dataArray = [[[[DoraemonNSLogManager sharedInstance] readLogs] reverseObjectEnumerator] allObjects];
}

[self.tableView reloadData];
Expand Down