-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAOPLogger.m
More file actions
95 lines (78 loc) · 2.56 KB
/
Copy pathAOPLogger.m
File metadata and controls
95 lines (78 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//
// AOPLogger.m
// AOPLogger
//
// Created by EpsilonX on 16/3/15.
// Copyright © 2016年. All rights reserved.
//
#import "AOPLogger.h"
@interface AOPLogger()
@property (nonatomic, strong) NSMutableArray *aspects;
@end
@implementation AOPLogger
typedef void (^AspectHandlerBlock)(id<AspectInfo> aspectInfo, ...);
- (instancetype)init{
self = [super init];
if (self) {
self.aspects = [NSMutableArray array];
}
return self;
}
+ (instancetype)sharedInstance
{
static AOPLogger *sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[AOPLogger alloc] init];
});
return sharedInstance;
}
+ (void)logWithDict:(NSDictionary *)dict
{
[[AOPLogger sharedInstance] logWithDict:dict];
}
+ (void)removeAOPLoggers
{
[[AOPLogger sharedInstance] removeAOPLoggers];
}
- (void)logWithDict:(NSDictionary *)dict
{
//ensure thread safe
@synchronized(self) {
for (NSString *className in dict) {
@autoreleasepool {
Class clazz = NSClassFromString(className);
NSDictionary *config = dict[className];
if (config[AOPLoggerSelectors]) {
for (NSDictionary *event in config[AOPLoggerSelectors]) {
SEL selectar = NSSelectorFromString(event[AOPLoggerSelectorName]);
AspectHandlerBlock block = event[AOPLoggerHandlerBlock];
AspectOptions option = [(NSNumber *)event[AOPLoggerPosition] unsignedIntegerValue];
NSError *error = nil;
id<AspectToken> aspect = [clazz aspect_hookSelector:selectar
withOptions:option
usingBlock:block
error:&error];
[self.aspects addObject:aspect];
if (error) {
AOPLog(@"AOPLogger error in logWithDict: %@",error);
}
}
}
}
}
}
}
- (void)removeAOPLoggers
{
//deregister all aspect logs
@synchronized(self) {
if ([self.aspects count] > 0) {
for (id<AspectToken> aspect in self.aspects) {
[aspect remove];
}
[self.aspects removeAllObjects];
}
}
}
@end