FLEXSystemLogTableViewController.m 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. //
  2. // FLEXSystemLogTableViewController.m
  3. // FLEX
  4. //
  5. // Created by Ryan Olson on 1/19/15.
  6. // Copyright (c) 2015 f. All rights reserved.
  7. //
  8. #import "FLEXSystemLogTableViewController.h"
  9. #import "FLEXUtility.h"
  10. #import "FLEXColor.h"
  11. #import "FLEXASLLogController.h"
  12. #import "FLEXOSLogController.h"
  13. #import "FLEXSystemLogTableViewCell.h"
  14. @interface FLEXSystemLogTableViewController () <UISearchResultsUpdating, UISearchControllerDelegate>
  15. @property (nonatomic, strong) UISearchController *searchController;
  16. @property (nonatomic, readonly) id<FLEXLogController> logController;
  17. @property (nonatomic, readonly) NSMutableArray<FLEXSystemLogMessage *> *logMessages;
  18. @property (nonatomic, copy) NSArray<FLEXSystemLogMessage *> *filteredLogMessages;
  19. @end
  20. @implementation FLEXSystemLogTableViewController
  21. - (void)viewDidLoad
  22. {
  23. [super viewDidLoad];
  24. __weak __typeof(self) weakSelf = self;
  25. id logHandler = ^(NSArray<FLEXSystemLogMessage *> *newMessages) {
  26. __strong __typeof(weakSelf) self = weakSelf;
  27. [self handleUpdateWithNewMessages:newMessages];
  28. };
  29. _logMessages = [NSMutableArray array];
  30. if ([NSProcessInfo processInfo].operatingSystemVersion.majorVersion <= 9) {
  31. _logController = [FLEXASLLogController withUpdateHandler:logHandler];
  32. } else {
  33. _logController = [FLEXOSLogController withUpdateHandler:logHandler];
  34. }
  35. [self.tableView registerClass:[FLEXSystemLogTableViewCell class] forCellReuseIdentifier:kFLEXSystemLogTableViewCellIdentifier];
  36. self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  37. self.title = @"Loading...";
  38. UIBarButtonItem *scrollDown = [[UIBarButtonItem alloc] initWithTitle:@" ⬇︎ "
  39. style:UIBarButtonItemStylePlain
  40. target:self
  41. action:@selector(scrollToLastRow)];
  42. UIBarButtonItem *settings = [[UIBarButtonItem alloc] initWithTitle:@"Settings"
  43. style:UIBarButtonItemStylePlain
  44. target:self
  45. action:@selector(showLogSettings)];
  46. if (FLEXOSLogAvailable()) {
  47. self.navigationItem.rightBarButtonItems = @[scrollDown, settings];
  48. } else {
  49. self.navigationItem.rightBarButtonItem = scrollDown;
  50. }
  51. self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
  52. self.searchController.delegate = self;
  53. self.searchController.searchResultsUpdater = self;
  54. self.searchController.dimsBackgroundDuringPresentation = NO;
  55. self.tableView.tableHeaderView = self.searchController.searchBar;
  56. }
  57. - (void)handleUpdateWithNewMessages:(NSArray<FLEXSystemLogMessage *> *)newMessages
  58. {
  59. self.title = @"System Log";
  60. [self.logMessages addObjectsFromArray:newMessages];
  61. // "Follow" the log as new messages stream in if we were previously near the bottom.
  62. BOOL wasNearBottom = self.tableView.contentOffset.y >= self.tableView.contentSize.height - self.tableView.frame.size.height - 100.0;
  63. [self.tableView reloadData];
  64. if (wasNearBottom) {
  65. [self scrollToLastRow];
  66. }
  67. }
  68. - (void)viewWillAppear:(BOOL)animated
  69. {
  70. [super viewWillAppear:animated];
  71. [self.logController startMonitoring];
  72. }
  73. - (void)scrollToLastRow
  74. {
  75. NSInteger numberOfRows = [self.tableView numberOfRowsInSection:0];
  76. if (numberOfRows > 0) {
  77. NSIndexPath *lastIndexPath = [NSIndexPath indexPathForRow:numberOfRows - 1 inSection:0];
  78. [self.tableView scrollToRowAtIndexPath:lastIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
  79. }
  80. }
  81. - (void)showLogSettings
  82. {
  83. FLEXOSLogController *logController = (FLEXOSLogController *)self.logController;
  84. BOOL persistent = [[NSUserDefaults standardUserDefaults] boolForKey:kFLEXiOSPersistentOSLogKey];
  85. NSString *toggle = persistent ? @"Disable" : @"Enable";
  86. NSString *title = [@"Persistent logging: " stringByAppendingString:persistent ? @"ON" : @"OFF"];
  87. NSString *body = @"In iOS 10 and up, ASL is gone. The OS Log API is much more limited. "
  88. "To get as close to the old behavior as possible, logs must be collected manually at launch and stored.\n\n"
  89. "Turn this feature on only when you need it.";
  90. UIAlertController *settings = [UIAlertController alertControllerWithTitle:title message:body preferredStyle:UIAlertControllerStyleAlert];
  91. [settings addAction:[UIAlertAction actionWithTitle:toggle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  92. [[NSUserDefaults standardUserDefaults] setBool:!persistent forKey:kFLEXiOSPersistentOSLogKey];
  93. logController.persistent = !persistent;
  94. [logController.messages addObjectsFromArray:self.logMessages];
  95. }]];
  96. [settings addAction:[UIAlertAction actionWithTitle:@"Dismiss" style:UIAlertActionStyleCancel handler:nil]];
  97. [self presentViewController:settings animated:YES completion:nil];
  98. }
  99. #pragma mark - Table view data source
  100. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  101. {
  102. return 1;
  103. }
  104. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  105. {
  106. return self.searchController.isActive ? self.filteredLogMessages.count : self.logMessages.count;
  107. }
  108. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  109. {
  110. FLEXSystemLogTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kFLEXSystemLogTableViewCellIdentifier forIndexPath:indexPath];
  111. cell.logMessage = [self logMessageAtIndexPath:indexPath];
  112. cell.highlightedText = self.searchController.searchBar.text;
  113. if (indexPath.row % 2 == 0) {
  114. cell.backgroundColor = [FLEXColor primaryBackgroundColor];
  115. } else {
  116. cell.backgroundColor = [FLEXColor secondaryBackgroundColor];
  117. }
  118. return cell;
  119. }
  120. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  121. {
  122. FLEXSystemLogMessage *logMessage = [self logMessageAtIndexPath:indexPath];
  123. return [FLEXSystemLogTableViewCell preferredHeightForLogMessage:logMessage inWidth:self.tableView.bounds.size.width];
  124. }
  125. #pragma mark - Copy on long press
  126. - (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath
  127. {
  128. return YES;
  129. }
  130. - (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
  131. {
  132. return action == @selector(copy:);
  133. }
  134. - (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
  135. {
  136. if (action == @selector(copy:)) {
  137. // We usually only want to copy the log message itself, not any metadata associated with it.
  138. [UIPasteboard generalPasteboard].string = [self logMessageAtIndexPath:indexPath].messageText;
  139. }
  140. }
  141. - (FLEXSystemLogMessage *)logMessageAtIndexPath:(NSIndexPath *)indexPath
  142. {
  143. return self.searchController.isActive ? self.filteredLogMessages[indexPath.row] : self.logMessages[indexPath.row];
  144. }
  145. #pragma mark - UISearchResultsUpdating
  146. - (void)updateSearchResultsForSearchController:(UISearchController *)searchController
  147. {
  148. NSString *searchString = searchController.searchBar.text;
  149. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  150. NSArray<FLEXSystemLogMessage *> *filteredLogMessages = [self.logMessages filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(FLEXSystemLogMessage *logMessage, NSDictionary<NSString *, id> *bindings) {
  151. NSString *displayedText = [FLEXSystemLogTableViewCell displayedTextForLogMessage:logMessage];
  152. return [displayedText rangeOfString:searchString options:NSCaseInsensitiveSearch].length > 0;
  153. }]];
  154. dispatch_async(dispatch_get_main_queue(), ^{
  155. if ([searchController.searchBar.text isEqual:searchString]) {
  156. self.filteredLogMessages = filteredLogMessages;
  157. [self.tableView reloadData];
  158. }
  159. });
  160. });
  161. }
  162. @end