WebSocketLogger.m 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //
  2. // WebSocketLogger.m
  3. // WebServerIPhone
  4. //
  5. // CocoaLumberjack Demos
  6. //
  7. #import "WebSocketLogger.h"
  8. #import "HTTPLogging.h"
  9. @implementation WebSocketLogger
  10. - (id)initWithWebSocket:(WebSocket *)ws
  11. {
  12. if ((self = [super init]))
  13. {
  14. websocket = ws;
  15. websocket.delegate = self;
  16. self.logFormatter = [[WebSocketFormatter alloc] init];
  17. }
  18. return self;
  19. }
  20. - (void)dealloc
  21. {
  22. [websocket setDelegate:nil];
  23. }
  24. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  25. #pragma mark WebSocket delegate
  26. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  27. - (void)webSocketDidOpen:(WebSocket *)ws
  28. {
  29. // This method is invoked on the websocketQueue
  30. isWebSocketOpen = YES;
  31. // Add our logger
  32. [DDLog addLogger:self];
  33. }
  34. - (void)webSocketDidClose:(WebSocket *)ws
  35. {
  36. // This method is invoked on the websocketQueue
  37. isWebSocketOpen = NO;
  38. // Remove our logger
  39. [DDLog removeLogger:self];
  40. // Post notification
  41. [[NSNotificationCenter defaultCenter] postNotificationName:WebSocketLoggerDidDieNotification object:self];
  42. }
  43. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  44. #pragma mark DDLogger Protocol
  45. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  46. - (void)logMessage:(DDLogMessage *)logMessage
  47. {
  48. if (logMessage->_context == HTTP_LOG_CONTEXT)
  49. {
  50. // Don't relay HTTP log messages.
  51. // Doing so could essentially cause an endless loop of log messages.
  52. return;
  53. }
  54. NSString *logMsg = logMessage->_message;
  55. if (_logFormatter)
  56. {
  57. logMsg = [_logFormatter formatLogMessage:logMessage];
  58. }
  59. if (logMsg)
  60. {
  61. dispatch_async(websocket.websocketQueue, ^{ @autoreleasepool {
  62. if (isWebSocketOpen)
  63. {
  64. [websocket sendMessage:logMsg];
  65. }
  66. }});
  67. }
  68. }
  69. @end
  70. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  71. #pragma mark -
  72. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  73. @implementation WebSocketFormatter
  74. - (id)init
  75. {
  76. if((self = [super init]))
  77. {
  78. dateFormatter = [[NSDateFormatter alloc] init];
  79. [dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
  80. [dateFormatter setDateFormat:@"yyyy/MM/dd HH:mm:ss:SSS"];
  81. }
  82. return self;
  83. }
  84. - (NSString *)formatLogMessage:(DDLogMessage *)logMessage
  85. {
  86. NSString *dateAndTime = [dateFormatter stringFromDate:(logMessage->_timestamp)];
  87. NSMutableString *webMsg = [logMessage->_message mutableCopy];
  88. [webMsg replaceOccurrencesOfString:@"<" withString:@"&lt;" options:0 range:NSMakeRange(0, [webMsg length])];
  89. [webMsg replaceOccurrencesOfString:@">" withString:@"&gt;" options:0 range:NSMakeRange(0, [webMsg length])];
  90. [webMsg replaceOccurrencesOfString:@"\n" withString:@"<br/>" options:0 range:NSMakeRange(0, [webMsg length])];
  91. return [NSString stringWithFormat:@"%@ &nbsp;%@", dateAndTime, webMsg];
  92. }
  93. @end