DDMultiFormatter.m 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Software License Agreement (BSD License)
  2. //
  3. // Copyright (c) 2010-2019, Deusty, LLC
  4. // All rights reserved.
  5. //
  6. // Redistribution and use of this software in source and binary forms,
  7. // with or without modification, are permitted provided that the following conditions are met:
  8. //
  9. // * Redistributions of source code must retain the above copyright notice,
  10. // this list of conditions and the following disclaimer.
  11. //
  12. // * Neither the name of Deusty nor the names of its contributors may be used
  13. // to endorse or promote products derived from this software without specific
  14. // prior written permission of Deusty, LLC.
  15. #import "DDMultiFormatter.h"
  16. #if !__has_feature(objc_arc)
  17. #error This file must be compiled with ARC. Use -fobjc-arc flag (or convert project to ARC).
  18. #endif
  19. @interface DDMultiFormatter () {
  20. dispatch_queue_t _queue;
  21. NSMutableArray *_formatters;
  22. }
  23. - (DDLogMessage *)logMessageForLine:(NSString *)line originalMessage:(DDLogMessage *)message;
  24. @end
  25. @implementation DDMultiFormatter
  26. - (instancetype)init {
  27. self = [super init];
  28. if (self) {
  29. _queue = dispatch_queue_create("cocoa.lumberjack.multiformatter", DISPATCH_QUEUE_CONCURRENT);
  30. _formatters = [NSMutableArray new];
  31. }
  32. return self;
  33. }
  34. #pragma mark Processing
  35. - (NSString *)formatLogMessage:(DDLogMessage *)logMessage {
  36. __block NSString *line = logMessage->_message;
  37. dispatch_sync(_queue, ^{
  38. for (id<DDLogFormatter> formatter in self->_formatters) {
  39. DDLogMessage *message = [self logMessageForLine:line originalMessage:logMessage];
  40. line = [formatter formatLogMessage:message];
  41. if (!line) {
  42. break;
  43. }
  44. }
  45. });
  46. return line;
  47. }
  48. - (DDLogMessage *)logMessageForLine:(NSString *)line originalMessage:(DDLogMessage *)message {
  49. DDLogMessage *newMessage = [message copy];
  50. newMessage->_message = line;
  51. return newMessage;
  52. }
  53. #pragma mark Formatters
  54. - (NSArray *)formatters {
  55. __block NSArray *formatters;
  56. dispatch_sync(_queue, ^{
  57. formatters = [self->_formatters copy];
  58. });
  59. return formatters;
  60. }
  61. - (void)addFormatter:(id<DDLogFormatter>)formatter {
  62. dispatch_barrier_async(_queue, ^{
  63. [self->_formatters addObject:formatter];
  64. });
  65. }
  66. - (void)removeFormatter:(id<DDLogFormatter>)formatter {
  67. dispatch_barrier_async(_queue, ^{
  68. [self->_formatters removeObject:formatter];
  69. });
  70. }
  71. - (void)removeAllFormatters {
  72. dispatch_barrier_async(_queue, ^{
  73. [self->_formatters removeAllObjects];
  74. });
  75. }
  76. - (BOOL)isFormattingWithFormatter:(id<DDLogFormatter>)formatter {
  77. __block BOOL hasFormatter;
  78. dispatch_sync(_queue, ^{
  79. hasFormatter = [self->_formatters containsObject:formatter];
  80. });
  81. return hasFormatter;
  82. }
  83. @end