No Description

AWSDDMultiFormatter.m 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Software License Agreement (BSD License)
  2. //
  3. // Copyright (c) 2010-2016, 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 "AWSDDMultiFormatter.h"
  16. #if TARGET_OS_IOS
  17. // Compiling for iOS
  18. #if __IPHONE_OS_VERSION_MIN_REQUIRED >= 60000 // iOS 6.0 or later
  19. #define NEEDS_DISPATCH_RETAIN_RELEASE 0
  20. #else // iOS 5.X or earlier
  21. #define NEEDS_DISPATCH_RETAIN_RELEASE 1
  22. #endif
  23. #elif TARGET_OS_WATCH || TARGET_OS_TV
  24. // Compiling for watchOS, tvOS
  25. #define NEEDS_DISPATCH_RETAIN_RELEASE 0
  26. #else
  27. // Compiling for Mac OS X
  28. #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1080 // Mac OS X 10.8 or later
  29. #define NEEDS_DISPATCH_RETAIN_RELEASE 0
  30. #else // Mac OS X 10.7 or earlier
  31. #define NEEDS_DISPATCH_RETAIN_RELEASE 1
  32. #endif
  33. #endif
  34. #if !__has_feature(objc_arc)
  35. #error This file must be compiled with ARC. Use -fobjc-arc flag (or convert project to ARC).
  36. #endif
  37. @interface AWSDDMultiFormatter () {
  38. dispatch_queue_t _queue;
  39. NSMutableArray *_formatters;
  40. }
  41. - (AWSDDLogMessage *)logMessageForLine:(NSString *)line originalMessage:(AWSDDLogMessage *)message;
  42. @end
  43. @implementation AWSDDMultiFormatter
  44. - (instancetype)init {
  45. self = [super init];
  46. if (self) {
  47. #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
  48. _queue = dispatch_queue_create("cocoa.lumberjack.multiformatter", DISPATCH_QUEUE_CONCURRENT);
  49. #else
  50. _queue = dispatch_queue_create("cocoa.lumberjack.multiformatter", NULL);
  51. #endif
  52. _formatters = [NSMutableArray new];
  53. }
  54. return self;
  55. }
  56. #if NEEDS_DISPATCH_RETAIN_RELEASE
  57. - (void)dealloc {
  58. dispatch_release(_queue);
  59. }
  60. #endif
  61. #pragma mark Processing
  62. - (NSString *)formatLogMessage:(AWSDDLogMessage *)logMessage {
  63. __block NSString *line = logMessage->_message;
  64. dispatch_sync(_queue, ^{
  65. for (id<AWSDDLogFormatter> formatter in self->_formatters) {
  66. AWSDDLogMessage *message = [self logMessageForLine:line originalMessage:logMessage];
  67. line = [formatter formatLogMessage:message];
  68. if (!line) {
  69. break;
  70. }
  71. }
  72. });
  73. return line;
  74. }
  75. - (AWSDDLogMessage *)logMessageForLine:(NSString *)line originalMessage:(AWSDDLogMessage *)message {
  76. AWSDDLogMessage *newMessage = [message copy];
  77. newMessage->_message = line;
  78. return newMessage;
  79. }
  80. #pragma mark Formatters
  81. - (NSArray *)formatters {
  82. __block NSArray *formatters;
  83. dispatch_sync(_queue, ^{
  84. formatters = [self->_formatters copy];
  85. });
  86. return formatters;
  87. }
  88. - (void)addFormatter:(id<AWSDDLogFormatter>)formatter {
  89. dispatch_barrier_async(_queue, ^{
  90. [self->_formatters addObject:formatter];
  91. });
  92. }
  93. - (void)removeFormatter:(id<AWSDDLogFormatter>)formatter {
  94. dispatch_barrier_async(_queue, ^{
  95. [self->_formatters removeObject:formatter];
  96. });
  97. }
  98. - (void)removeAllFormatters {
  99. dispatch_barrier_async(_queue, ^{
  100. [self->_formatters removeAllObjects];
  101. });
  102. }
  103. - (BOOL)isFormattingWithFormatter:(id<AWSDDLogFormatter>)formatter {
  104. __block BOOL hasFormatter;
  105. dispatch_sync(_queue, ^{
  106. hasFormatter = [self->_formatters containsObject:formatter];
  107. });
  108. return hasFormatter;
  109. }
  110. @end