| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- // Software License Agreement (BSD License)
- //
- // Copyright (c) 2010-2016, Deusty, LLC
- // All rights reserved.
- //
- // Redistribution and use of this software in source and binary forms,
- // with or without modification, are permitted provided that the following conditions are met:
- //
- // * Redistributions of source code must retain the above copyright notice,
- // this list of conditions and the following disclaimer.
- //
- // * Neither the name of Deusty nor the names of its contributors may be used
- // to endorse or promote products derived from this software without specific
- // prior written permission of Deusty, LLC.
- #import "DDContextFilterLogFormatter.h"
- #import <pthread/pthread.h>
- #if !__has_feature(objc_arc)
- #error This file must be compiled with ARC. Use -fobjc-arc flag (or convert project to ARC).
- #endif
- @interface DDLoggingContextSet : NSObject
- - (void)addToSet:(NSUInteger)loggingContext;
- - (void)removeFromSet:(NSUInteger)loggingContext;
- @property (readonly, copy) NSArray *currentSet;
- - (BOOL)isInSet:(NSUInteger)loggingContext;
- @end
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- #pragma mark -
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- @interface DDContextWhitelistFilterLogFormatter () {
- DDLoggingContextSet *_contextSet;
- }
- @end
- @implementation DDContextWhitelistFilterLogFormatter
- - (instancetype)init {
- if ((self = [super init])) {
- _contextSet = [[DDLoggingContextSet alloc] init];
- }
- return self;
- }
- - (void)addToWhitelist:(NSUInteger)loggingContext {
- [_contextSet addToSet:loggingContext];
- }
- - (void)removeFromWhitelist:(NSUInteger)loggingContext {
- [_contextSet removeFromSet:loggingContext];
- }
- - (NSArray *)whitelist {
- return [_contextSet currentSet];
- }
- - (BOOL)isOnWhitelist:(NSUInteger)loggingContext {
- return [_contextSet isInSet:loggingContext];
- }
- - (NSString *)formatLogMessage:(DDLogMessage *)logMessage {
- if ([self isOnWhitelist:logMessage->_context]) {
- return logMessage->_message;
- } else {
- return nil;
- }
- }
- @end
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- #pragma mark -
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- @interface DDContextBlacklistFilterLogFormatter () {
- DDLoggingContextSet *_contextSet;
- }
- @end
- @implementation DDContextBlacklistFilterLogFormatter
- - (instancetype)init {
- if ((self = [super init])) {
- _contextSet = [[DDLoggingContextSet alloc] init];
- }
- return self;
- }
- - (void)addToBlacklist:(NSUInteger)loggingContext {
- [_contextSet addToSet:loggingContext];
- }
- - (void)removeFromBlacklist:(NSUInteger)loggingContext {
- [_contextSet removeFromSet:loggingContext];
- }
- - (NSArray *)blacklist {
- return [_contextSet currentSet];
- }
- - (BOOL)isOnBlacklist:(NSUInteger)loggingContext {
- return [_contextSet isInSet:loggingContext];
- }
- - (NSString *)formatLogMessage:(DDLogMessage *)logMessage {
- if ([self isOnBlacklist:logMessage->_context]) {
- return nil;
- } else {
- return logMessage->_message;
- }
- }
- @end
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- #pragma mark -
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- @interface DDLoggingContextSet () {
- pthread_mutex_t _mutex;
- NSMutableSet *_set;
- }
- @end
- @implementation DDLoggingContextSet
- - (instancetype)init {
- if ((self = [super init])) {
- _set = [[NSMutableSet alloc] init];
- pthread_mutex_init(&_mutex, NULL);
- }
- return self;
- }
- - (void)dealloc {
- pthread_mutex_destroy(&_mutex);
- }
- - (void)addToSet:(NSUInteger)loggingContext {
- pthread_mutex_lock(&_mutex);
- {
- [_set addObject:@(loggingContext)];
- }
- pthread_mutex_unlock(&_mutex);
- }
- - (void)removeFromSet:(NSUInteger)loggingContext {
- pthread_mutex_lock(&_mutex);
- {
- [_set removeObject:@(loggingContext)];
- }
- pthread_mutex_unlock(&_mutex);
- }
- - (NSArray *)currentSet {
- NSArray *result = nil;
- pthread_mutex_lock(&_mutex);
- {
- result = [_set allObjects];
- }
- pthread_mutex_unlock(&_mutex);
- return result;
- }
- - (BOOL)isInSet:(NSUInteger)loggingContext {
- BOOL result = NO;
- pthread_mutex_lock(&_mutex);
- {
- result = [_set containsObject:@(loggingContext)];
- }
- pthread_mutex_unlock(&_mutex);
- return result;
- }
- @end
|