DDContextFilterLogFormatter.m 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 "DDContextFilterLogFormatter.h"
  16. #import <pthread/pthread.h>
  17. #if !__has_feature(objc_arc)
  18. #error This file must be compiled with ARC. Use -fobjc-arc flag (or convert project to ARC).
  19. #endif
  20. @interface DDLoggingContextSet : NSObject
  21. - (void)addToSet:(NSUInteger)loggingContext;
  22. - (void)removeFromSet:(NSUInteger)loggingContext;
  23. @property (readonly, copy) NSArray *currentSet;
  24. - (BOOL)isInSet:(NSUInteger)loggingContext;
  25. @end
  26. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  27. #pragma mark -
  28. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  29. @interface DDContextWhitelistFilterLogFormatter () {
  30. DDLoggingContextSet *_contextSet;
  31. }
  32. @end
  33. @implementation DDContextWhitelistFilterLogFormatter
  34. - (instancetype)init {
  35. if ((self = [super init])) {
  36. _contextSet = [[DDLoggingContextSet alloc] init];
  37. }
  38. return self;
  39. }
  40. - (void)addToWhitelist:(NSUInteger)loggingContext {
  41. [_contextSet addToSet:loggingContext];
  42. }
  43. - (void)removeFromWhitelist:(NSUInteger)loggingContext {
  44. [_contextSet removeFromSet:loggingContext];
  45. }
  46. - (NSArray *)whitelist {
  47. return [_contextSet currentSet];
  48. }
  49. - (BOOL)isOnWhitelist:(NSUInteger)loggingContext {
  50. return [_contextSet isInSet:loggingContext];
  51. }
  52. - (NSString *)formatLogMessage:(DDLogMessage *)logMessage {
  53. if ([self isOnWhitelist:logMessage->_context]) {
  54. return logMessage->_message;
  55. } else {
  56. return nil;
  57. }
  58. }
  59. @end
  60. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  61. #pragma mark -
  62. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  63. @interface DDContextBlacklistFilterLogFormatter () {
  64. DDLoggingContextSet *_contextSet;
  65. }
  66. @end
  67. @implementation DDContextBlacklistFilterLogFormatter
  68. - (instancetype)init {
  69. if ((self = [super init])) {
  70. _contextSet = [[DDLoggingContextSet alloc] init];
  71. }
  72. return self;
  73. }
  74. - (void)addToBlacklist:(NSUInteger)loggingContext {
  75. [_contextSet addToSet:loggingContext];
  76. }
  77. - (void)removeFromBlacklist:(NSUInteger)loggingContext {
  78. [_contextSet removeFromSet:loggingContext];
  79. }
  80. - (NSArray *)blacklist {
  81. return [_contextSet currentSet];
  82. }
  83. - (BOOL)isOnBlacklist:(NSUInteger)loggingContext {
  84. return [_contextSet isInSet:loggingContext];
  85. }
  86. - (NSString *)formatLogMessage:(DDLogMessage *)logMessage {
  87. if ([self isOnBlacklist:logMessage->_context]) {
  88. return nil;
  89. } else {
  90. return logMessage->_message;
  91. }
  92. }
  93. @end
  94. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  95. #pragma mark -
  96. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  97. @interface DDLoggingContextSet () {
  98. pthread_mutex_t _mutex;
  99. NSMutableSet *_set;
  100. }
  101. @end
  102. @implementation DDLoggingContextSet
  103. - (instancetype)init {
  104. if ((self = [super init])) {
  105. _set = [[NSMutableSet alloc] init];
  106. pthread_mutex_init(&_mutex, NULL);
  107. }
  108. return self;
  109. }
  110. - (void)dealloc {
  111. pthread_mutex_destroy(&_mutex);
  112. }
  113. - (void)addToSet:(NSUInteger)loggingContext {
  114. pthread_mutex_lock(&_mutex);
  115. {
  116. [_set addObject:@(loggingContext)];
  117. }
  118. pthread_mutex_unlock(&_mutex);
  119. }
  120. - (void)removeFromSet:(NSUInteger)loggingContext {
  121. pthread_mutex_lock(&_mutex);
  122. {
  123. [_set removeObject:@(loggingContext)];
  124. }
  125. pthread_mutex_unlock(&_mutex);
  126. }
  127. - (NSArray *)currentSet {
  128. NSArray *result = nil;
  129. pthread_mutex_lock(&_mutex);
  130. {
  131. result = [_set allObjects];
  132. }
  133. pthread_mutex_unlock(&_mutex);
  134. return result;
  135. }
  136. - (BOOL)isInSet:(NSUInteger)loggingContext {
  137. BOOL result = NO;
  138. pthread_mutex_lock(&_mutex);
  139. {
  140. result = [_set containsObject:@(loggingContext)];
  141. }
  142. pthread_mutex_unlock(&_mutex);
  143. return result;
  144. }
  145. @end