123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- //
- // FKLogUtil.m
- // FirstLink
- //
- // Created by ascii on 15/8/6.
- // Copyright (c) 2015年 FirstLink. All rights reserved.
- //
- #import "FKLogUtil.h"
- #import <libkern/OSAtomic.h>
- #import "FKReachabilityUtil.h"
- #import "SSZipArchive.h"
- #import "UploadFileModel.h"
- @implementation FKLogUtil
- + (void)configCocoaLumberjack {
- FKLogFormatter *formatter = [[FKLogFormatter alloc] init];
-
- DDFileLogger *fileLogger = [[DDFileLogger alloc] init];
- fileLogger.rollingFrequency = 60 * 60 * 24 * 7; // 7 day rolling
- fileLogger.logFileManager.maximumNumberOfLogFiles = 7;
- [fileLogger setLogFormatter:formatter];
- [DDLog addLogger:fileLogger];
-
- DDTTYLogger *logger = [DDTTYLogger sharedInstance];
- [logger setLogFormatter:formatter];
- //#ifdef DEBUG
- // [DDLog addLogger:logger withLevel:DDLogLevelDebug];
- //#else
- [DDLog addLogger:logger withLevel:DDLogLevelInfo];
- //#endif
-
- // DDASLLogger *consoleLogger = [DDASLLogger sharedInstance];
- // [consoleLogger setLogFormatter:formatter];
- //
- // [DDLog addLogger:consoleLogger withLevel:DDLogLevelDebug];
- }
- + (void)logTransformEntity:(FKLogTransformEntity *)entity {
- if (entity && entity.code) {
- DDLogInfo(@"%@", [entity description]);
- }
- }
- + (void)asyncUploadRollingFiles {
- if (![FKLogUtil isUploadLogOpen]) {
- return;
- }
-
- #ifndef DEBUG
- NSString *net = [FKReachabilityUtil sharedInstance].networkType;
- if ([net isEqualToString:@"wifi"]) {
- DDFileLogger *fileLogger = [[DDFileLogger alloc] init];
-
- // check and remove zip file
- NSError *error;
- NSString *logsDirectoryPath = [fileLogger.logFileManager logsDirectory];
- NSArray *allFilePaths = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:logsDirectoryPath error:&error];
- if (!error) {
- for (NSString *file in allFilePaths) {
- if ([file containsString:@".zip"]) {
- [FKLogUtil removeFiltAtPath:file];
- }
- }
- }
-
- NSArray *filePaths = [fileLogger.logFileManager sortedLogFilePaths];
- if (filePaths.count > 1) {
- NSString *filePath = filePaths.lastObject;
- NSString *zipFilePath = [NSString stringWithFormat:@"%@.zip", filePath];
- BOOL ret = [SSZipArchive createZipFileAtPath:zipFilePath withFilesAtPaths:@[filePath]];
- if (ret) {
- // upload file
- [FKLogUtil uploadRollingZipFile:zipFilePath source:filePath];
- } else {
- [FKLogUtil removeFiltAtPath:zipFilePath];
- }
- }
- }
- #endif
- }
- + (BOOL)isUploadLogOpen {
- return YES;
- }
- + (void)removeFiltAtPath:(NSString *)filePath {
- if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
- [[NSFileManager defaultManager] removeItemAtPath:filePath
- error:nil];
- }
- }
- + (void)uploadRollingZipFile:(NSString *)zipFilePath source:(NSString *)sourceFilePath {
- NSData *data = [[NSData alloc] initWithContentsOfFile:zipFilePath];
- [UploadFileModel uploadLogFile:data
- fileType:@"log"
- contentType:@"text/plain"
- parameters:nil
- success:^(MSGHeader *header, id responseObject)
- {
- if (header.code.integerValue == RESPONSE_MSG_NORMAL) {
- [FKLogUtil removeFiltAtPath:sourceFilePath];
- }
- [FKLogUtil removeFiltAtPath:zipFilePath];
- } failure:^(MSGHeader *header, NSError *error) {
- [FKLogUtil removeFiltAtPath:zipFilePath];
- }];
- }
- @end
- @implementation FKLogFormatter
- static NSString *dateFormatString = @"yyyy-MM-dd HH:mm:ss.SSS";
- - (NSString *)stringFromDate:(NSDate *)date {
- int32_t loggerCount = OSAtomicAdd32(0, &atomicLoggerCount);
-
- if (loggerCount <= 1) {
- // Single-threaded mode.
-
- if (threadUnsafeDateFormatter == nil) {
- threadUnsafeDateFormatter = [[NSDateFormatter alloc] init];
- [threadUnsafeDateFormatter setDateFormat:dateFormatString];
- }
-
- return [threadUnsafeDateFormatter stringFromDate:date];
- } else {
- // Multi-threaded mode.
- // NSDateFormatter is NOT thread-safe.
-
- NSString *key = @"MyCustomFormatter_NSDateFormatter";
-
- NSMutableDictionary *threadDictionary = [[NSThread currentThread] threadDictionary];
- NSDateFormatter *dateFormatter = [threadDictionary objectForKey:key];
-
- if (dateFormatter == nil) {
- dateFormatter = [[NSDateFormatter alloc] init];
- [dateFormatter setDateFormat:dateFormatString];
-
- [threadDictionary setObject:dateFormatter forKey:key];
- }
-
- return [dateFormatter stringFromDate:date];
- }
- }
- - (NSString *)formatLogMessage:(DDLogMessage *)logMessage {
- // NSString *logLevel;
- // switch (logMessage->_flag) {
- // case DDLogFlagError : logLevel = @"error"; break;
- // case DDLogFlagWarning : logLevel = @"warning"; break;
- // case DDLogFlagInfo : logLevel = @"info"; break;
- // case DDLogFlagDebug : logLevel = @"debug"; break;
- // default : logLevel = @"verbose"; break;
- // }
-
- NSString *dateAndTime = [self stringFromDate:(logMessage.timestamp)];
- NSString *logMsg = logMessage->_message;
- return [NSString stringWithFormat:@"%@;%@;%@;%@;%@"
- , dateAndTime
- , PlatformIdentify
- , [[UIDevice currentDevice] systemVersion]
- , [SystemUtil applicationVersion]
- , logMsg
- ];
- }
- - (void)didAddToLogger:(id <DDLogger>)logger {
- OSAtomicIncrement32(&atomicLoggerCount);
- }
- - (void)willRemoveFromLogger:(id <DDLogger>)logger {
- OSAtomicDecrement32(&atomicLoggerCount);
- }
- @end
|