FLEXSystemLogMessage.m 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. //
  2. // FLEXSystemLogMessage.m
  3. // FLEX
  4. //
  5. // Created by Ryan Olson on 1/25/15.
  6. // Copyright (c) 2020 FLEX Team. All rights reserved.
  7. //
  8. #import "FLEXSystemLogMessage.h"
  9. @implementation FLEXSystemLogMessage
  10. + (instancetype)logMessageFromASLMessage:(aslmsg)aslMessage {
  11. NSDate *date = nil;
  12. NSString *sender = nil, *text = nil;
  13. long long identifier = 0;
  14. const char *timestamp = asl_get(aslMessage, ASL_KEY_TIME);
  15. if (timestamp) {
  16. NSTimeInterval timeInterval = [@(timestamp) integerValue];
  17. const char *nanoseconds = asl_get(aslMessage, ASL_KEY_TIME_NSEC);
  18. if (nanoseconds) {
  19. timeInterval += [@(nanoseconds) doubleValue] / NSEC_PER_SEC;
  20. }
  21. date = [NSDate dateWithTimeIntervalSince1970:timeInterval];
  22. }
  23. const char *s = asl_get(aslMessage, ASL_KEY_SENDER);
  24. if (s) {
  25. sender = @(s);
  26. }
  27. const char *messageText = asl_get(aslMessage, ASL_KEY_MSG);
  28. if (messageText) {
  29. text = @(messageText);
  30. }
  31. const char *messageID = asl_get(aslMessage, ASL_KEY_MSG_ID);
  32. if (messageID) {
  33. identifier = [@(messageID) longLongValue];
  34. }
  35. FLEXSystemLogMessage *message = [[self alloc] initWithDate:date sender:sender text:text messageID:identifier];
  36. message->_aslMessage = aslMessage;
  37. return message;
  38. }
  39. + (instancetype)logMessageFromDate:(NSDate *)date text:(NSString *)text {
  40. return [[self alloc] initWithDate:date sender:nil text:text messageID:0];
  41. }
  42. - (id)initWithDate:(NSDate *)date sender:(NSString *)sender text:(NSString *)text messageID:(long long)identifier {
  43. self = [super init];
  44. if (self) {
  45. _date = date;
  46. _sender = sender;
  47. _messageText = text;
  48. _messageID = identifier;
  49. }
  50. return self;
  51. }
  52. - (BOOL)isEqual:(id)object {
  53. if ([object isKindOfClass:[self class]]) {
  54. if (self.messageID) {
  55. // Only ASL uses messageID, otherwise it is 0
  56. return self.messageID == [object messageID];
  57. } else {
  58. // Test message texts and dates for OS Log
  59. return [self.messageText isEqual:[object messageText]] &&
  60. [self.date isEqualToDate:[object date]];
  61. }
  62. }
  63. return NO;
  64. }
  65. - (NSUInteger)hash {
  66. return (NSUInteger)self.messageID;
  67. }
  68. - (NSString *)description {
  69. NSString *escaped = [self.messageText stringByReplacingOccurrencesOfString:@"\n" withString:@"\\n"];
  70. return [NSString stringWithFormat:@"(%@) %@", @(self.messageText.length), escaped];
  71. }
  72. @end