FLEXSystemLogTableViewController.m 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 - Search display delegate
  120. - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
  121. {
  122. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  123. NSArray *filteredLogMessages = [self.logMessages filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(FLEXSystemLogMessage *logMessage, NSDictionary *bindings) {
  124. NSString *displayedText = [FLEXSystemLogTableViewCell displayedTextForLogMessage:logMessage];
  125. return [displayedText rangeOfString:searchString options:NSCaseInsensitiveSearch].length > 0;
  126. }]];
  127. dispatch_async(dispatch_get_main_queue(), ^{
  128. if ([self.searchDisplayController.searchBar.text isEqual:searchString]) {
  129. self.filteredLogMessages = filteredLogMessages;
  130. [self.searchDisplayController.searchResultsTableView reloadData];
  131. }
  132. });
  133. });
  134. // Reload done after the data fetches asynchronously
  135. return NO;
  136. }
  137. #pragma mark - Log Message Fetching
  138. + (NSArray *)allLogMessagesForCurrentProcess
  139. {
  140. asl_object_t query = asl_new(ASL_TYPE_QUERY);
  141. // Filter for messages from the current process. Note that this appears to happen by default on device, but is required in the simulator.
  142. NSString *pidString = [NSString stringWithFormat:@"%d", [[NSProcessInfo processInfo] processIdentifier]];
  143. asl_set_query(query, ASL_KEY_PID, [pidString UTF8String], ASL_QUERY_OP_EQUAL);
  144. aslresponse response = asl_search(NULL, query);
  145. aslmsg aslMessage = NULL;
  146. NSMutableArray *logMessages = [NSMutableArray array];
  147. // Work around deprecated API warnings while supporting multiple deployment targets.
  148. #if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_7_0
  149. while ((aslMessage = asl_next(response))) {
  150. [logMessages addObject:[FLEXSystemLogMessage logMessageFromASLMessage:aslMessage]];
  151. }
  152. asl_release(response);
  153. #else
  154. while ((aslMessage = aslresponse_next(response))) {
  155. [logMessages addObject:[FLEXSystemLogMessage logMessageFromASLMessage:aslMessage]];
  156. }
  157. aslresponse_free(response);
  158. #endif
  159. return logMessages;
  160. }
  161. @end