FLEXSystemLogViewController.m 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. //
  2. // FLEXSystemLogViewController.m
  3. // FLEX
  4. //
  5. // Created by Ryan Olson on 1/19/15.
  6. // Copyright (c) 2015 f. All rights reserved.
  7. //
  8. #import "FLEXSystemLogViewController.h"
  9. #import "FLEXUtility.h"
  10. #import "FLEXColor.h"
  11. #import "FLEXASLLogController.h"
  12. #import "FLEXOSLogController.h"
  13. #import "FLEXSystemLogCell.h"
  14. #import "fishhook.h"
  15. #import <dlfcn.h>
  16. @interface FLEXSystemLogViewController ()
  17. @property (nonatomic, readonly) id<FLEXLogController> logController;
  18. @property (nonatomic, readonly) NSMutableArray<FLEXSystemLogMessage *> *logMessages;
  19. @property (nonatomic, copy) NSArray<FLEXSystemLogMessage *> *filteredLogMessages;
  20. @end
  21. static void (*MSHookFunction)(void *symbol, void *replace, void **result);
  22. static BOOL FLEXDidHookNSLog = NO;
  23. static BOOL FLEXNSLogHookWorks = NO;
  24. BOOL (*orig_os_log_shim_enabled)() = nil;
  25. BOOL my_os_log_shim_enabled() {
  26. return NO;
  27. }
  28. extern BOOL os_log_shim_enabled();
  29. @implementation FLEXSystemLogViewController
  30. + (void)load {
  31. // Thanks to @Ram4096 on GitHub for telling me that
  32. // os_log is conditionally enabled by the SDK version
  33. FLEXDidHookNSLog = rebind_symbols((struct rebinding[1]) {
  34. "os_log_shim_enabled",
  35. (void *)my_os_log_shim_enabled,
  36. (void **)&orig_os_log_shim_enabled
  37. }, 1) == 0;
  38. if (FLEXDidHookNSLog && orig_os_log_shim_enabled != nil) {
  39. // Check if our rebinding worked
  40. FLEXNSLogHookWorks = os_log_shim_enabled() == NO;
  41. }
  42. // So, just because we rebind the lazily loaded symbol for
  43. // this function doesn't mean it's even going to be used.
  44. // While it seems to be sufficient for the simulator, for
  45. // whatever reason it is not sufficient on-device. We need
  46. // to actually hook the function with something like Substrate.
  47. // Check if we have substrate, and if so use that instead
  48. void *handle = dlopen("/usr/lib/libsubstrate.dylib", RTLD_LAZY);
  49. if (handle) {
  50. MSHookFunction = dlsym(handle, "MSHookFunction");
  51. if (MSHookFunction) {
  52. // Set the hook and check if it worked
  53. //
  54. // Very important that we use orig_os_log_shim_enabled
  55. // here as opposed to os_log_shim_enabled.
  56. MSHookFunction(orig_os_log_shim_enabled, my_os_log_shim_enabled, nil);
  57. FLEXNSLogHookWorks = orig_os_log_shim_enabled() == NO;
  58. if (FLEXNSLogHookWorks) {
  59. return;
  60. }
  61. }
  62. }
  63. }
  64. - (id)init {
  65. return [super initWithStyle:UITableViewStylePlain];
  66. }
  67. - (void)viewDidLoad {
  68. [super viewDidLoad];
  69. self.showsSearchBar = YES;
  70. __weak __typeof(self) weakSelf = self;
  71. id logHandler = ^(NSArray<FLEXSystemLogMessage *> *newMessages) {
  72. __strong __typeof(weakSelf) self = weakSelf;
  73. [self handleUpdateWithNewMessages:newMessages];
  74. };
  75. _logMessages = [NSMutableArray array];
  76. if (FLEXOSLogAvailable() && !FLEXNSLogHookWorks) {
  77. _logController = [FLEXOSLogController withUpdateHandler:logHandler];
  78. } else {
  79. _logController = [FLEXASLLogController withUpdateHandler:logHandler];
  80. }
  81. [self.tableView registerClass:[FLEXSystemLogCell class] forCellReuseIdentifier:kFLEXSystemLogCellIdentifier];
  82. self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  83. self.title = @"Loading...";
  84. UIBarButtonItem *scrollDown = [[UIBarButtonItem alloc] initWithTitle:@" ⬇︎ "
  85. style:UIBarButtonItemStylePlain
  86. target:self
  87. action:@selector(scrollToLastRow)];
  88. UIBarButtonItem *settings = [[UIBarButtonItem alloc] initWithTitle:@"Settings"
  89. style:UIBarButtonItemStylePlain
  90. target:self
  91. action:@selector(showLogSettings)];
  92. if (FLEXOSLogAvailable()) {
  93. self.navigationItem.rightBarButtonItems = @[scrollDown, settings];
  94. } else {
  95. self.navigationItem.rightBarButtonItem = scrollDown;
  96. }
  97. }
  98. - (void)handleUpdateWithNewMessages:(NSArray<FLEXSystemLogMessage *> *)newMessages {
  99. self.title = @"System Log";
  100. [self.logMessages addObjectsFromArray:newMessages];
  101. // "Follow" the log as new messages stream in if we were previously near the bottom.
  102. BOOL wasNearBottom = self.tableView.contentOffset.y >= self.tableView.contentSize.height - self.tableView.frame.size.height - 100.0;
  103. [self.tableView reloadData];
  104. if (wasNearBottom) {
  105. [self scrollToLastRow];
  106. }
  107. }
  108. - (void)viewWillAppear:(BOOL)animated {
  109. [super viewWillAppear:animated];
  110. [self.logController startMonitoring];
  111. }
  112. - (void)scrollToLastRow {
  113. NSInteger numberOfRows = [self.tableView numberOfRowsInSection:0];
  114. if (numberOfRows > 0) {
  115. NSIndexPath *lastIndexPath = [NSIndexPath indexPathForRow:numberOfRows - 1 inSection:0];
  116. [self.tableView scrollToRowAtIndexPath:lastIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
  117. }
  118. }
  119. - (void)showLogSettings {
  120. FLEXOSLogController *logController = (FLEXOSLogController *)self.logController;
  121. BOOL persistent = [[NSUserDefaults standardUserDefaults] boolForKey:kFLEXiOSPersistentOSLogKey];
  122. NSString *toggle = persistent ? @"Disable" : @"Enable";
  123. NSString *title = [@"Persistent logging: " stringByAppendingString:persistent ? @"ON" : @"OFF"];
  124. NSString *body = @"In iOS 10 and up, ASL is gone. The OS Log API is much more limited. "
  125. "To get as close to the old behavior as possible, logs must be collected manually at launch and stored.\n\n"
  126. "Turn this feature on only when you need it.";
  127. [FLEXAlert makeAlert:^(FLEXAlert *make) {
  128. make.title(title).message(body).button(toggle).handler(^(NSArray<NSString *> *strings) {
  129. [[NSUserDefaults standardUserDefaults] setBool:!persistent forKey:kFLEXiOSPersistentOSLogKey];
  130. logController.persistent = !persistent;
  131. [logController.messages addObjectsFromArray:self.logMessages];
  132. });
  133. make.button(@"Dismiss").cancelStyle();
  134. } showFrom:self];
  135. }
  136. #pragma mark - FLEXGlobalsEntry
  137. + (NSString *)globalsEntryTitle:(FLEXGlobalsRow)row {
  138. return @"⚠️ System Log";
  139. }
  140. + (UIViewController *)globalsEntryViewController:(FLEXGlobalsRow)row {
  141. return [self new];
  142. }
  143. #pragma mark - Table view data source
  144. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  145. return self.searchController.isActive ? self.filteredLogMessages.count : self.logMessages.count;
  146. }
  147. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  148. FLEXSystemLogCell *cell = [tableView dequeueReusableCellWithIdentifier:kFLEXSystemLogCellIdentifier forIndexPath:indexPath];
  149. cell.logMessage = [self logMessageAtIndexPath:indexPath];
  150. cell.highlightedText = self.searchText;
  151. if (indexPath.row % 2 == 0) {
  152. cell.backgroundColor = [FLEXColor primaryBackgroundColor];
  153. } else {
  154. cell.backgroundColor = [FLEXColor secondaryBackgroundColor];
  155. }
  156. return cell;
  157. }
  158. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  159. FLEXSystemLogMessage *logMessage = [self logMessageAtIndexPath:indexPath];
  160. return [FLEXSystemLogCell preferredHeightForLogMessage:logMessage inWidth:self.tableView.bounds.size.width];
  161. }
  162. #pragma mark - Copy on long press
  163. - (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath {
  164. return YES;
  165. }
  166. - (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
  167. return action == @selector(copy:);
  168. }
  169. - (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
  170. if (action == @selector(copy:)) {
  171. // We usually only want to copy the log message itself, not any metadata associated with it.
  172. UIPasteboard.generalPasteboard.string = [self logMessageAtIndexPath:indexPath].messageText;
  173. }
  174. }
  175. - (FLEXSystemLogMessage *)logMessageAtIndexPath:(NSIndexPath *)indexPath {
  176. return self.searchController.isActive ? self.filteredLogMessages[indexPath.row] : self.logMessages[indexPath.row];
  177. }
  178. #pragma mark - Search bar
  179. - (void)updateSearchResults:(NSString *)searchString {
  180. [self onBackgroundQueue:^NSArray *{
  181. return [self.logMessages filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(FLEXSystemLogMessage *logMessage, NSDictionary<NSString *, id> *bindings) {
  182. NSString *displayedText = [FLEXSystemLogCell displayedTextForLogMessage:logMessage];
  183. return [displayedText rangeOfString:searchString options:NSCaseInsensitiveSearch].length > 0;
  184. }]];
  185. } thenOnMainQueue:^(NSArray *filteredLogMessages) {
  186. if ([self.searchText isEqual:searchString]) {
  187. self.filteredLogMessages = filteredLogMessages;
  188. [self.tableView reloadData];
  189. }
  190. }];
  191. }
  192. @end