Skip to content

Commit fa5dec4

Browse files
authored
Merge pull request #206 from sonic-ios-dev/dev
Write the log into event notification form
2 parents 43c4ffa + 69c4e34 commit fa5dec4

12 files changed

+58
-35
lines changed

sonic-iOS/Sonic/Cache/SonicCache.m

+7-7
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ - (SonicCacheItem *)updateWithJsonData:(NSData *)jsonData
382382
#if DEBUG
383383
NSAssert(sha1.length>0, errMsg);
384384
#endif
385-
NSLog(@"%@",errMsg);
385+
SonicLogEvent(@"%@",errMsg);
386386
}
387387
[config setObject:sha1 forKey:kSonicSha1];
388388
cacheItem.config = config;
@@ -799,10 +799,10 @@ - (void)checkAndTrimRootCache
799799
unsigned long long lastTrimStamp = [[[NSUserDefaults standardUserDefaults] objectForKey:kSonicRootCacheTrimTimestampUDF] longLongValue];
800800
unsigned long long duration = currentTimeStamp() - lastTrimStamp;
801801
if (duration < [SonicConfiguration defaultConfiguration].rootCacheSizeCheckDuration) {
802-
NSLog(@"Trim root cache in duration!");
802+
SonicLogEvent(@"Trim root cache in duration!");
803803
return;
804804
}
805-
NSLog(@"Trim root cache start !");
805+
SonicLogEvent(@"Trim root cache start !");
806806
[self checkAndTrimCacheAtDirPath:_rootCachePath unIncludeSubDir:SonicRootCacheConfigDirName withMaxDirectorySize:[SonicConfiguration defaultConfiguration].cacheMaxDirectorySize withWarningPercent:[SonicConfiguration defaultConfiguration].cacheDirectorySizeWarningPercent];
807807
[[NSUserDefaults standardUserDefaults] setObject:[@(currentTimeStamp()) stringValue] forKey:kSonicRootCacheTrimTimestampUDF];
808808
}
@@ -876,7 +876,7 @@ - (void)checkAndTrimCacheAtDirPath:(NSString *)dirPath unIncludeSubDir:(NSString
876876

877877
[SonicFileManager removeItemAtPath:subDir error:nil];
878878

879-
NSLog(@"trim clear cache at subDir :%@",subDir);
879+
SonicLogEvent(@"trim clear cache at subDir :%@",subDir);
880880
}
881881
}
882882

@@ -927,7 +927,7 @@ - (BOOL)setupSubResourceCacheDirectory
927927

928928
_rootResourceCachePath = [[self createDirectoryIfNotExist:[paths objectAtIndex:0] withSubPath:SonicResourceCacheDirName] copy];
929929

930-
NSLog(@"resource cache path:%@",_rootResourceCachePath);
930+
SonicLogEvent(@"resource cache path:%@",_rootResourceCachePath);
931931

932932
[self setupSubResourceConfigDirectory];
933933

@@ -971,7 +971,7 @@ - (BOOL)saveSubResourceData:(NSData *)data withConfig:(NSDictionary *)config wit
971971
[SonicFileManager removeItemAtPath:cacheFilePath error:nil];
972972
}
973973

974-
NSLog(@"resource save state:%d sessionID:%@",isSuccess,sessionID);
974+
SonicLogEvent(@"resource save state:%d sessionID:%@",isSuccess,sessionID);
975975

976976
return isSuccess;
977977
}
@@ -1010,7 +1010,7 @@ - (void)checkAndTrimResourceCache
10101010
unsigned long long lastTrimStamp = [[[NSUserDefaults standardUserDefaults] objectForKey:kSonicResourceCacheTrimTimestampUDF] longLongValue];
10111011
unsigned long long duration = currentTimeStamp() - lastTrimStamp;
10121012
if (duration < [SonicConfiguration defaultConfiguration].resourceCacheSizeCheckDuration) {
1013-
NSLog(@"Trim resource cache in duration!");
1013+
SonicLogEvent(@"Trim resource cache in duration!");
10141014
return;
10151015
}
10161016
//event

sonic-iOS/Sonic/Cache/SonicDatabase.m

+3-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#import "SonicDatabase.h"
2121
#import "sqlite3.h"
22+
#import "SonicEventStatistics.h"
2223

2324
#if __has_feature(objc_arc)
2425
#error This file must be compiled without ARC. Use -fno-objc-arc flag.
@@ -47,7 +48,7 @@ - (instancetype)initWithPath:(NSString *)dbPath
4748

4849
if (ret != SQLITE_OK) {
4950

50-
NSLog(@"database open db faild :%@ code:%d",dbPath,ret);
51+
SonicLogEvent(@"database open db faild :%@ code:%d",dbPath,ret);
5152
}
5253

5354
[self createConfigTableIfNotExist];
@@ -80,7 +81,7 @@ - (BOOL)execSql:(NSString *)sql
8081

8182
int ret = sqlite3_exec(_db, sql.UTF8String, NULL, NULL, NULL);
8283
if (ret != SQLITE_OK) {
83-
NSLog(@"sql error:%@",sql);
84+
SonicLogEvent(@"sql error:%@",sql);
8485
}
8586
return ret == SQLITE_OK;
8687
}

sonic-iOS/Sonic/Network/SonicServer.m

+2-2
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ - (NSDictionary *)sonicItemForCache
299299
}
300300
return nil;
301301
}
302-
NSLog(@"sonicItemForCache warning:Should never call this function before connection did completion!");
302+
SonicLogEvent(@"sonicItemForCache warning:Should never call this function before connection did completion!");
303303
return nil;
304304
}
305305

@@ -407,7 +407,7 @@ - (void)connectionDidCompleteWithoutError:(SonicConnection *)connection
407407

408408
//if http status is 304, there is nothing changed
409409
if (self.response.statusCode == 304) {
410-
NSLog(@"response status 304!");
410+
SonicLogEvent(@"Response status 304!");
411411
break;
412412
}
413413

sonic-iOS/Sonic/Network/SonicURLProtocol.m

+5-5
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@ + (BOOL)canInitWithRequest:(NSURLRequest *)request
4040
if (session && [sessionID isEqualToString:session.sessionID]) {
4141
return YES;
4242
}
43-
NSLog(@"SonicURLProtocol.canInitWithRequest error:Cannot find sonic session!");
43+
SonicLogEvent(@"SonicURLProtocol.canInitWithRequest error:Cannot find sonic session!");
4444
}
4545
}
4646

4747
//Sub resource intercept
4848
NSString * sessionID = sonicSessionID(request.mainDocumentURL.absoluteString);
4949
SonicSession *session = [[SonicEngine sharedEngine] sessionById:sessionID];
5050
if (session.resourceLoader && [session.resourceLoader canInterceptResourceWithUrl:request.URL.absoluteString]) {
51-
NSLog(@"SonicURLProtocol resource should intercept:%@",request.debugDescription);
51+
SonicLogEvent(@"SonicURLProtocol resource should intercept:%@",request.debugDescription);
5252
return YES;
5353
}
5454

@@ -71,7 +71,7 @@ - (void)startLoading
7171

7272
if ([session.resourceLoader canInterceptResourceWithUrl:self.request.URL.absoluteString]) {
7373

74-
NSLog(@"protocol resource did start loading :%@",self.request.debugDescription);
74+
SonicLogEvent(@"protocol resource did start loading :%@",self.request.debugDescription);
7575

7676
SonicSession *session = [[SonicEngine sharedEngine] sessionById:sessionID];
7777

@@ -118,14 +118,14 @@ - (void)callClientActionWithParams:(NSDictionary *)params
118118
NSData *recvData = params[kSonicProtocolData];
119119
if (recvData.length > 0) {
120120
[self.client URLProtocol:self didLoadData:recvData];
121-
NSLog(@"protocol did load data length:%ld",recvData.length);
121+
SonicLogEvent(@"protocol did load data length:%ld",recvData.length);
122122
}
123123
}
124124
break;
125125
case SonicURLProtocolActionDidSuccess:
126126
{
127127
[self.client URLProtocolDidFinishLoading:self];
128-
NSLog(@"protocol did finish loading request:%@",self.request.debugDescription);
128+
SonicLogEvent(@"protocol did finish loading request:%@",self.request.debugDescription);
129129
}
130130
break;
131131
case SonicURLProtocolActionDidFaild:

sonic-iOS/Sonic/ResourceLoader/SonicResourceLoadOperation.m

+7-7
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ - (instancetype)initWithUrl:(NSString *)aUrl
7272
}
7373
if (isCacheExpire) {
7474
self.config = nil;
75-
NSLog(@"resource expire:%@",self.url);
75+
SonicLogEvent(@"resource expire:%@",self.url);
7676
}else{
7777
self.cacheFileData = [[SonicCache shareCache] resourceCacheWithSessionID:self.sessionID];
7878
NSString *cacheFileSha1 = getDataSha1(self.cacheFileData);
@@ -87,7 +87,7 @@ - (instancetype)initWithUrl:(NSString *)aUrl
8787
}else{
8888
self.cacheFileData = nil;
8989
self.config = nil;
90-
NSLog(@"resource sha1 wrong:%@",self.url);
90+
SonicLogEvent(@"resource sha1 wrong:%@",self.url);
9191
}
9292
}
9393
}
@@ -126,7 +126,7 @@ - (void)preloadDataWithProtocolCallBack:(SonicURLProtocolCallBack)callBack
126126
dataItem = [SonicUtil protocolActionItem:SonicURLProtocolActionLoadData param:self.cacheFileData];
127127
[self.lock unlock];
128128
finishItem = [SonicUtil protocolActionItem:SonicURLProtocolActionDidSuccess param:nil];
129-
NSLog(@"resource read from cache:%@",self.url);
129+
SonicLogEvent(@"resource read from cache:%@",self.url);
130130
[actions addObjectsFromArray:@[rspItem,dataItem,finishItem]];
131131
}else{
132132
if (self.isComplete) {
@@ -135,7 +135,7 @@ - (void)preloadDataWithProtocolCallBack:(SonicURLProtocolCallBack)callBack
135135
dataItem = [SonicUtil protocolActionItem:SonicURLProtocolActionLoadData param:self.responseData];
136136
[self.lock unlock];
137137
finishItem = [SonicUtil protocolActionItem:SonicURLProtocolActionDidSuccess param:nil];
138-
NSLog(@"resource read from network:%@",self.url);
138+
SonicLogEvent(@"resource read from network:%@",self.url);
139139
[actions addObjectsFromArray:@[rspItem,dataItem,finishItem]];
140140
}else{
141141
[self.lock lock];
@@ -148,7 +148,7 @@ - (void)preloadDataWithProtocolCallBack:(SonicURLProtocolCallBack)callBack
148148
[actions addObject:dataItem];
149149
}
150150
[self.lock unlock];
151-
NSLog(@"resource read from network preload:%@",self.url);
151+
SonicLogEvent(@"resource read from network preload:%@",self.url);
152152
}
153153
}
154154
self.protocolCallBack = callBack;
@@ -233,7 +233,7 @@ - (void)connection:(SonicConnection *)connection didReceiveData:(NSData *)data
233233

234234
- (void)connection:(SonicConnection *)connection didCompleteWithError:(NSError *)error
235235
{
236-
NSLog(@"resource recieve error:%@",error.debugDescription);
236+
SonicLogEvent(@"resource recieve error:%@",error.debugDescription);
237237

238238
[self disaptchProtocolAction:SonicURLProtocolActionDidFaild withParam:error];
239239
self.isComplete = YES;
@@ -263,7 +263,7 @@ - (void)connectionDidCompleteWithoutError:(SonicConnection *)connection
263263
@"reseponse":headersLog
264264
}];
265265
}else{
266-
NSLog(@"config create fail to save resource:%@",self.url);
266+
SonicLogEvent(@"config create fail to save resource:%@",self.url);
267267
}
268268
}];
269269
[[SonicCache subResourceQueue] addOperation:block];

sonic-iOS/Sonic/Session/SonicSession.m

+9-9
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ - (void)start
184184
if (self.configuration.supportCacheControl) {
185185
SonicCacheItem *cacheItem = [[SonicCache shareCache] cacheForSession:self.sessionID];
186186
if (![cacheItem isCacheExpired]) {
187-
NSLog(@"SonicSession.start finish:session(%@) is under cache expired.", self.sessionID);
187+
SonicLogEvent(@"SonicSession.start finish:session(%@) is under cache expired.", self.sessionID);
188188
return;
189189
}
190190
}
@@ -435,7 +435,7 @@ - (void)firstLoadDidSuccess
435435
if (200 == self.sonicServer.response.statusCode) {
436436
[self dealWithFirstLoad];
437437
} else {
438-
NSLog(@"firstLoadDidSuccess warning:statusCode[%ld] miss!", (long)self.sonicServer.response.statusCode);
438+
SonicLogEvent(@"firstLoadDidSuccess warning:statusCode[%ld] miss!", (long)self.sonicServer.response.statusCode);
439439
}
440440

441441
[self checkAutoCompletionAction];
@@ -464,7 +464,7 @@ - (void)dealWithFirstLoad
464464
if (self.configuration.supportCacheControl) {
465465
NSString *cacheControl = [self.sonicServer responseHeaderForKey:SonicHeaderValueCacheControl];
466466
if ([cacheControl isEqualToString:@"no-cache"] || [cacheControl isEqualToString:@"no-store"] || [cacheControl isEqualToString:@"must-revalidate"]) {
467-
NSLog(@"cache control need't cache!");
467+
SonicLogEvent(@"cache control need't cache!");
468468
return;
469469
}
470470
}
@@ -473,7 +473,7 @@ - (void)dealWithFirstLoad
473473

474474
NSDictionary *serverResult = [self.sonicServer sonicItemForCache];
475475
if (!serverResult) {
476-
NSLog(@"Sonic first load item for cache nil");
476+
SonicLogEvent(@"Sonic first load item for cache nil");
477477
return;
478478
}
479479

@@ -668,7 +668,7 @@ - (NSDictionary *)sonicDiffResult
668668
NSDictionary *extra = @{@"template-tag":templateTag,@"eTag":etag,@"isReload":isRefresh,@"cache-offline":cacheOffline};
669669
[resultDict setObject:extra forKey:@"extra"];
670670

671-
NSLog(@"sonic diff result :%@",resultDict);
671+
SonicLogEvent(@"sonic diff result :%@",resultDict);
672672

673673
return resultDict;
674674
}
@@ -692,7 +692,7 @@ - (void)updateDidSuccess
692692
{
693693
if (![self.sonicServer isSonicResponse]) {
694694
[[SonicCache shareCache] removeCacheBySessionID:self.sessionID];
695-
NSLog(@"Clear cache because while not sonic repsonse!");
695+
SonicLogEvent(@"Clear cache because while not sonic repsonse!");
696696
break;
697697
}
698698

@@ -742,7 +742,7 @@ - (void)updateDidSuccess
742742
self.webviewCallBack(resultDict);
743743
}
744744
}else{
745-
NSLog(@"There is no webViewCallBack!");
745+
SonicLogEvent(@"There is no webViewCallBack!");
746746
}
747747
}
748748

@@ -758,7 +758,7 @@ - (void)dispatchUpdateCallBack
758758
self.updateCallBack(resultDict);
759759
}
760760
}else{
761-
NSLog(@"There is no updateCallBack!");
761+
SonicLogEvent(@"There is no updateCallBack!");
762762
}
763763
}
764764
}
@@ -845,7 +845,7 @@ - (void)preloadSubResourceWithResponseHeaders:(NSDictionary *)responseHeaders
845845
{
846846
NSString *linkValue = responseHeaders[SonicHeaderKeyLink];
847847
if (linkValue.length == 0) {
848-
NSLog(@"no preload link exist!");
848+
SonicLogEvent(@"no preload link exist!");
849849
return;
850850
}
851851
NSArray *linkArray = [linkValue componentsSeparatedByString:@";"];

sonic-iOS/Sonic/Statistics/SonicEventConstants.h

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
* Sonic Event Type
2525
*/
2626
typedef NS_ENUM(NSUInteger, SonicStatisticsEvent) {
27+
28+
//Throw all step log
29+
SonicStatisticsEvent_EventLog,
2730

2831
//Session create faild
2932
SonicStatisticsEvent_SessionCreateFaild,

sonic-iOS/Sonic/Statistics/SonicEventStatistics.h

+4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#import <Foundation/Foundation.h>
2121
#import "SonicEventConstants.h"
2222

23+
#define SonicLogEvent(format,...) [[SonicEventStatistics shareStatistics]addLog:format,##__VA_ARGS__]
24+
2325
@protocol SonicEventStatisticsObserver <NSObject>
2426

2527
- (void)handleEvent:(SonicStatisticsEvent)event withEventInfo:(NSDictionary *)info;
@@ -30,6 +32,8 @@
3032

3133
+ (SonicEventStatistics *)shareStatistics;
3234

35+
- (void)addLog:(NSString *)format, ...;
36+
3337
/**
3438
* Add an observer to handle the events
3539
*/

sonic-iOS/Sonic/Statistics/SonicEventStatistics.m

+10
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,16 @@ - (void)dealloc
6363
[super dealloc];
6464
}
6565

66+
- (void)addLog:(NSString *)format, ...
67+
{
68+
va_list args;
69+
va_start(args,format);
70+
NSString *logString = [[[NSString alloc]initWithFormat:format arguments:args]autorelease];
71+
va_end(args);
72+
logString = [NSString stringWithFormat:@"#SonicEventLog# %@",logString];
73+
[[SonicEventStatistics shareStatistics] addEvent:SonicStatisticsEvent_EventLog withEventInfo:@{@"logMsg":logString}];
74+
}
75+
6676
- (void)addEventObserver:(id<SonicEventStatisticsObserver>)eventObserver
6777
{
6878
if (![eventObserver conformsToProtocol:@protocol(SonicEventStatisticsObserver)]) {

sonic-iOS/Sonic/Util/SonicUtil.h

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#import "SonicConnection.h"
2222
#import "SonicConstants.h"
2323
#import "SonicSession.h"
24+
#import "SonicEventStatistics.h"
2425

2526
@interface SonicUtil : NSObject
2627

sonic-iOS/SonicSample/SonicSample/SonicEventObserver.m

+6-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,12 @@ + (NSString *)eventTypeToString:(SonicStatisticsEvent)event
3333

3434
- (void)handleEvent:(SonicStatisticsEvent)event withEventInfo:(NSDictionary *)info
3535
{
36-
NSString *eventString = [SonicEventObserver eventTypeToString:event];
37-
NSLog(@"event :%@ info:%@",eventString,info.debugDescription);
36+
if (event == SonicStatisticsEvent_EventLog) {
37+
NSLog(@"%@",info[@"logMsg"]);
38+
}else{
39+
NSString *eventString = [SonicEventObserver eventTypeToString:event];
40+
SonicLogEvent(@"event :%@ info:%@",eventString,info.debugDescription);
41+
}
3842
}
3943

4044
@end

sonic-iOS/SonicSample/SonicSample/SonicWebViewController.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ - (instancetype)initWithUrl:(NSString *)aUrl useSonicMode:(BOOL)isSonic unStrict
4343
SonicSessionConfiguration *configuration = [SonicSessionConfiguration new];
4444
NSString *linkValue = @"http://assets.kgc.cn/ff7f069b/css/common-min.www.kgc.css?v=e4ecfe82;http://assets.kgc.cn/ff7f069b/css/themes.www.kgc.css?v=612eb426;http://assets.kgc.cn/ff7f069b/css/style.www.kgc.css?v=05d94f84";
4545
configuration.customResponseHeaders = @{
46-
SonicHeaderKeyCacheOffline:SonicHeaderValueCacheOfflineStoreRefresh,
46+
SonicHeaderKeyCacheOffline:SonicHeaderValueCacheOfflineStore,
4747
SonicHeaderKeyLink:linkValue
4848
};
4949
configuration.enableLocalServer = YES;

0 commit comments

Comments
 (0)