FLEXSystemLogTableViewController.m 9.4 KB

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