説明なし

DDMultiFormatter.m 3.5KB

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