FLEXSystemLogTableViewController.m 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. //
  2. // FLEXSystemLogTableViewController.m
  3. // UICatalog
  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 "FLEXSystemLogMessage.h"
  11. #import "FLEXSystemLogTableViewCell.h"
  12. #import <asl.h>
  13. @interface FLEXSystemLogTableViewController () <UISearchDisplayDelegate>
  14. @property (nonatomic, strong) UISearchDisplayController *searchController;
  15. @property (nonatomic, copy) NSArray *logMessages;
  16. @property (nonatomic, copy) NSArray *filteredLogMessages;
  17. @property (nonatomic, strong) NSTimer *logUpdateTimer;
  18. @end
  19. @implementation FLEXSystemLogTableViewController
  20. - (void)viewDidLoad
  21. {
  22. [super viewDidLoad];
  23. [self.tableView registerClass:[FLEXSystemLogTableViewCell class] forCellReuseIdentifier:kFLEXSystemLogTableViewCellIdentifier];
  24. self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  25. self.title = @"Loading...";
  26. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@" ⬇︎ " style:UIBarButtonItemStylePlain target:self action:@selector(scrollToLastRow)];
  27. UISearchBar *searchBar = [[UISearchBar alloc] init];
  28. [searchBar sizeToFit];
  29. self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
  30. self.searchController.delegate = self;
  31. self.searchController.searchResultsDataSource = self;
  32. self.searchController.searchResultsDelegate = self;
  33. [self.searchController.searchResultsTableView registerClass:[FLEXSystemLogTableViewCell class] forCellReuseIdentifier:kFLEXSystemLogTableViewCellIdentifier];
  34. self.searchController.searchResultsTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  35. self.tableView.tableHeaderView = self.searchController.searchBar;
  36. [self updateLogMessages];
  37. }
  38. - (void)viewWillAppear:(BOOL)animated
  39. {
  40. [super viewWillAppear:animated];
  41. NSTimeInterval updateInterval = 1.0;
  42. #if TARGET_IPHONE_SIMULATOR
  43. // Querrying the ASL is much slower in the simulator. We need a longer polling interval to keep things repsonsive.
  44. updateInterval = 5.0;
  45. #endif
  46. self.logUpdateTimer = [NSTimer scheduledTimerWithTimeInterval:updateInterval target:self selector:@selector(updateLogMessages) userInfo:nil repeats:YES];
  47. }
  48. - (void)viewWillDisappear:(BOOL)animated
  49. {
  50. [super viewWillDisappear:animated];
  51. [self.logUpdateTimer invalidate];
  52. }
  53. - (void)updateLogMessages
  54. {
  55. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  56. NSArray *logMessages = [[self class] allLogMessagesForCurrentProcess];
  57. dispatch_async(dispatch_get_main_queue(), ^{
  58. self.title = @"System Log";
  59. self.logMessages = logMessages;
  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. });
  68. }
  69. - (void)scrollToLastRow
  70. {
  71. NSInteger numberOfRows = [self.tableView numberOfRowsInSection:0];
  72. if (numberOfRows > 0) {
  73. NSIndexPath *lastIndexPath = [NSIndexPath indexPathForRow:numberOfRows - 1 inSection:0];
  74. [self.tableView scrollToRowAtIndexPath:lastIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
  75. }
  76. }
  77. #pragma mark - Table view data source
  78. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  79. {
  80. return 1;
  81. }
  82. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  83. {
  84. NSInteger numberOfRows = 0;
  85. if (tableView == self.tableView) {
  86. numberOfRows = [self.logMessages count];
  87. } else if (tableView == self.searchDisplayController.searchResultsTableView) {
  88. numberOfRows = [self.filteredLogMessages count];
  89. }
  90. return numberOfRows;
  91. }
  92. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  93. {
  94. FLEXSystemLogTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kFLEXSystemLogTableViewCellIdentifier forIndexPath:indexPath];
  95. if (tableView == self.tableView) {
  96. cell.logMessage = [self.logMessages objectAtIndex:indexPath.row];
  97. cell.highlightedText = nil;
  98. } else if (tableView == self.searchDisplayController.searchResultsTableView) {
  99. cell.logMessage = [self.filteredLogMessages objectAtIndex:indexPath.row];
  100. cell.highlightedText = self.searchDisplayController.searchBar.text;
  101. }
  102. if (indexPath.row % 2 == 0) {
  103. cell.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1.0];
  104. } else {
  105. cell.backgroundColor = [UIColor whiteColor];
  106. }
  107. return cell;
  108. }
  109. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  110. {
  111. FLEXSystemLogMessage *logMessage = nil;
  112. if (tableView == self.tableView) {
  113. logMessage = [self.logMessages objectAtIndex:indexPath.row];
  114. } else if (tableView == self.searchDisplayController.searchResultsTableView) {
  115. logMessage = [self.filteredLogMessages objectAtIndex:indexPath.row];
  116. }
  117. return [FLEXSystemLogTableViewCell preferredHeightForLogMessage:logMessage inWidth:self.tableView.bounds.size.width];
  118. }
  119. #pragma mark - Copy on long press
  120. - (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath
  121. {
  122. return YES;
  123. }
  124. - (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
  125. {
  126. return action == @selector(copy:);
  127. }
  128. - (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
  129. {
  130. if (action == @selector(copy:)) {
  131. FLEXSystemLogMessage *logMessage = nil;
  132. if (tableView == self.tableView) {
  133. logMessage = [self.logMessages objectAtIndex:indexPath.row];
  134. } else if (tableView == self.searchDisplayController.searchResultsTableView) {
  135. logMessage = [self.filteredLogMessages objectAtIndex:indexPath.row];
  136. }
  137. NSString *stringToCopy = [FLEXSystemLogTableViewCell displayedTextForLogMessage:logMessage] ?: @"";
  138. [[UIPasteboard generalPasteboard] setString:stringToCopy];
  139. }
  140. }
  141. #pragma mark - Search display delegate
  142. - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
  143. {
  144. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  145. NSArray *filteredLogMessages = [self.logMessages filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(FLEXSystemLogMessage *logMessage, NSDictionary *bindings) {
  146. NSString *displayedText = [FLEXSystemLogTableViewCell displayedTextForLogMessage:logMessage];
  147. return [displayedText rangeOfString:searchString options:NSCaseInsensitiveSearch].length > 0;
  148. }]];
  149. dispatch_async(dispatch_get_main_queue(), ^{
  150. if ([self.searchDisplayController.searchBar.text isEqual:searchString]) {
  151. self.filteredLogMessages = filteredLogMessages;
  152. [self.searchDisplayController.searchResultsTableView reloadData];
  153. }
  154. });
  155. });
  156. // Reload done after the data fetches asynchronously
  157. return NO;
  158. }
  159. #pragma mark - Log Message Fetching
  160. + (NSArray *)allLogMessagesForCurrentProcess
  161. {
  162. asl_object_t query = asl_new(ASL_TYPE_QUERY);
  163. // Filter for messages from the current process. Note that this appears to happen by default on device, but is required in the simulator.
  164. NSString *pidString = [NSString stringWithFormat:@"%d", [[NSProcessInfo processInfo] processIdentifier]];
  165. asl_set_query(query, ASL_KEY_PID, [pidString UTF8String], ASL_QUERY_OP_EQUAL);
  166. aslresponse response = asl_search(NULL, query);
  167. aslmsg aslMessage = NULL;
  168. NSMutableArray *logMessages = [NSMutableArray array];
  169. // Work around deprecated API warnings while supporting multiple deployment targets.
  170. #if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_7_0
  171. while ((aslMessage = asl_next(response))) {
  172. [logMessages addObject:[FLEXSystemLogMessage logMessageFromASLMessage:aslMessage]];
  173. }
  174. asl_release(response);
  175. #else
  176. while ((aslMessage = aslresponse_next(response))) {
  177. [logMessages addObject:[FLEXSystemLogMessage logMessageFromASLMessage:aslMessage]];
  178. }
  179. aslresponse_free(response);
  180. #endif
  181. return logMessages;
  182. }
  183. @end