FLEXSystemLogTableViewController.m 8.1 KB

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