FLEXSystemLogTableViewController.m 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 () <UISearchResultsUpdating, UISearchControllerDelegate>
  14. @property (nonatomic, strong) UISearchController *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. self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
  28. self.searchController.delegate = self;
  29. self.searchController.searchResultsUpdater = self;
  30. self.searchController.dimsBackgroundDuringPresentation = NO;
  31. self.tableView.tableHeaderView = self.searchController.searchBar;
  32. [self updateLogMessages];
  33. }
  34. - (void)viewWillAppear:(BOOL)animated
  35. {
  36. [super viewWillAppear:animated];
  37. NSTimeInterval updateInterval = 1.0;
  38. #if TARGET_IPHONE_SIMULATOR
  39. // Querrying the ASL is much slower in the simulator. We need a longer polling interval to keep things repsonsive.
  40. updateInterval = 5.0;
  41. #endif
  42. self.logUpdateTimer = [NSTimer scheduledTimerWithTimeInterval:updateInterval target:self selector:@selector(updateLogMessages) userInfo:nil repeats:YES];
  43. }
  44. - (void)viewWillDisappear:(BOOL)animated
  45. {
  46. [super viewWillDisappear:animated];
  47. [self.logUpdateTimer invalidate];
  48. }
  49. - (void)updateLogMessages
  50. {
  51. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  52. NSArray *logMessages = [[self class] allLogMessagesForCurrentProcess];
  53. dispatch_async(dispatch_get_main_queue(), ^{
  54. self.title = @"System Log";
  55. self.logMessages = logMessages;
  56. // "Follow" the log as new messages stream in if we were previously near the bottom.
  57. BOOL wasNearBottom = self.tableView.contentOffset.y >= self.tableView.contentSize.height - self.tableView.frame.size.height - 100.0;
  58. [self.tableView reloadData];
  59. if (wasNearBottom) {
  60. [self scrollToLastRow];
  61. }
  62. });
  63. });
  64. }
  65. - (void)scrollToLastRow
  66. {
  67. NSInteger numberOfRows = [self.tableView numberOfRowsInSection:0];
  68. if (numberOfRows > 0) {
  69. NSIndexPath *lastIndexPath = [NSIndexPath indexPathForRow:numberOfRows - 1 inSection:0];
  70. [self.tableView scrollToRowAtIndexPath:lastIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
  71. }
  72. }
  73. #pragma mark - Table view data source
  74. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  75. {
  76. return 1;
  77. }
  78. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  79. {
  80. return self.searchController.isActive ? [self.filteredLogMessages count] : [self.logMessages count];
  81. }
  82. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  83. {
  84. FLEXSystemLogTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kFLEXSystemLogTableViewCellIdentifier forIndexPath:indexPath];
  85. cell.logMessage = [self logMessageAtIndexPath:indexPath];
  86. cell.highlightedText = self.searchController.searchBar.text;
  87. if (indexPath.row % 2 == 0) {
  88. cell.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1.0];
  89. } else {
  90. cell.backgroundColor = [UIColor whiteColor];
  91. }
  92. return cell;
  93. }
  94. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  95. {
  96. FLEXSystemLogMessage *logMessage = [self logMessageAtIndexPath:indexPath];
  97. return [FLEXSystemLogTableViewCell preferredHeightForLogMessage:logMessage inWidth:self.tableView.bounds.size.width];
  98. }
  99. #pragma mark - Copy on long press
  100. - (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath
  101. {
  102. return YES;
  103. }
  104. - (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
  105. {
  106. return action == @selector(copy:);
  107. }
  108. - (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
  109. {
  110. if (action == @selector(copy:)) {
  111. FLEXSystemLogMessage *logMessage = [self logMessageAtIndexPath:indexPath];
  112. NSString *stringToCopy = [FLEXSystemLogTableViewCell displayedTextForLogMessage:logMessage] ?: @"";
  113. [[UIPasteboard generalPasteboard] setString:stringToCopy];
  114. }
  115. }
  116. - (FLEXSystemLogMessage *)logMessageAtIndexPath:(NSIndexPath *)indexPath
  117. {
  118. return self.searchController.isActive ? self.filteredLogMessages[indexPath.row] : self.logMessages[indexPath.row];
  119. }
  120. #pragma mark - UISearchResultsUpdating
  121. - (void)updateSearchResultsForSearchController:(UISearchController *)searchController
  122. {
  123. NSString *searchString = searchController.searchBar.text;
  124. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  125. NSArray *filteredLogMessages = [self.logMessages filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(FLEXSystemLogMessage *logMessage, NSDictionary *bindings) {
  126. NSString *displayedText = [FLEXSystemLogTableViewCell displayedTextForLogMessage:logMessage];
  127. return [displayedText rangeOfString:searchString options:NSCaseInsensitiveSearch].length > 0;
  128. }]];
  129. dispatch_async(dispatch_get_main_queue(), ^{
  130. if ([searchController.searchBar.text isEqual:searchString]) {
  131. self.filteredLogMessages = filteredLogMessages;
  132. [self.tableView reloadData];
  133. }
  134. });
  135. });
  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. while ((aslMessage = asl_next(response))) {
  148. [logMessages addObject:[FLEXSystemLogMessage logMessageFromASLMessage:aslMessage]];
  149. }
  150. asl_release(response);
  151. return logMessages;
  152. }
  153. @end