You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

189 lines
7.9 KiB

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