123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515 |
- #import "CompressingLogFileManager.h"
- #import <zlib.h>
- #define LOG_LEVEL 4
- #define NSLogError(frmt, ...) do{ if(LOG_LEVEL >= 1) NSLog(frmt, ##__VA_ARGS__); } while(0)
- #define NSLogWarn(frmt, ...) do{ if(LOG_LEVEL >= 2) NSLog(frmt, ##__VA_ARGS__); } while(0)
- #define NSLogInfo(frmt, ...) do{ if(LOG_LEVEL >= 3) NSLog(frmt, ##__VA_ARGS__); } while(0)
- #define NSLogVerbose(frmt, ...) do{ if(LOG_LEVEL >= 4) NSLog(frmt, ##__VA_ARGS__); } while(0)
- @interface CompressingLogFileManager (/* Must be nameless for properties */)
- @property (readwrite) BOOL isCompressing;
- @end
- @interface DDLogFileInfo (Compressor)
- @property (nonatomic, readonly) BOOL isCompressed;
- - (NSString *)tempFilePathByAppendingPathExtension:(NSString *)newExt;
- - (NSString *)fileNameByAppendingPathExtension:(NSString *)newExt;
- @end
- #pragma mark -
- @implementation CompressingLogFileManager
- @synthesize isCompressing;
- - (id)init
- {
- return [self initWithLogsDirectory:nil];
- }
- - (id)initWithLogsDirectory:(NSString *)aLogsDirectory
- {
- if ((self = [super initWithLogsDirectory:aLogsDirectory]))
- {
- upToDate = NO;
-
-
-
-
-
- [self performSelector:@selector(compressNextLogFile) withObject:nil afterDelay:5.0];
- }
- return self;
- }
- - (void)dealloc
- {
- [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(compressNextLogFile) object:nil];
- }
- - (void)compressLogFile:(DDLogFileInfo *)logFile
- {
- self.isCompressing = YES;
- CompressingLogFileManager* __weak weakSelf = self;
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
- [weakSelf backgroundThread_CompressLogFile:logFile];
- });
- }
- - (void)compressNextLogFile
- {
- if (self.isCompressing)
- {
-
-
- return;
- }
-
- NSLogVerbose(@"CompressingLogFileManager: compressNextLogFile");
-
- upToDate = NO;
-
- NSArray *sortedLogFileInfos = [self sortedLogFileInfos];
-
- NSUInteger count = [sortedLogFileInfos count];
- if (count == 0)
- {
-
- upToDate = YES;
- return;
- }
-
- NSUInteger i = count;
- while (i > 0)
- {
- DDLogFileInfo *logFileInfo = [sortedLogFileInfos objectAtIndex:(i - 1)];
-
- if (logFileInfo.isArchived && !logFileInfo.isCompressed)
- {
- [self compressLogFile:logFileInfo];
-
- break;
- }
-
- i--;
- }
-
- upToDate = YES;
- }
- - (void)compressionDidSucceed:(DDLogFileInfo *)logFile
- {
- NSLogVerbose(@"CompressingLogFileManager: compressionDidSucceed: %@", logFile.fileName);
-
- self.isCompressing = NO;
-
- [self compressNextLogFile];
- }
- - (void)compressionDidFail:(DDLogFileInfo *)logFile
- {
- NSLogWarn(@"CompressingLogFileManager: compressionDidFail: %@", logFile.fileName);
-
- self.isCompressing = NO;
-
-
-
-
-
-
- NSTimeInterval delay = (60 * 15);
-
- [self performSelector:@selector(compressNextLogFile) withObject:nil afterDelay:delay];
- }
- - (void)didArchiveLogFile:(NSString *)logFilePath
- {
- NSLogVerbose(@"CompressingLogFileManager: didArchiveLogFile: %@", [logFilePath lastPathComponent]);
-
-
-
-
-
- if (upToDate)
- {
- [self compressLogFile:[DDLogFileInfo logFileWithPath:logFilePath]];
- }
- }
- - (void)didRollAndArchiveLogFile:(NSString *)logFilePath
- {
- NSLogVerbose(@"CompressingLogFileManager: didRollAndArchiveLogFile: %@", [logFilePath lastPathComponent]);
-
-
-
-
-
- if (upToDate)
- {
- [self compressLogFile:[DDLogFileInfo logFileWithPath:logFilePath]];
- }
- }
- - (void)backgroundThread_CompressLogFile:(DDLogFileInfo *)logFile
- {
- @autoreleasepool {
-
- NSLogInfo(@"CompressingLogFileManager: Compressing log file: %@", logFile.fileName);
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- NSString *inputFilePath = logFile.filePath;
-
- NSString *tempOutputFilePath = [logFile tempFilePathByAppendingPathExtension:@"gz"];
-
- #if TARGET_OS_IPHONE
-
-
-
-
- NSString* protection = logFile.fileAttributes[NSFileProtectionKey];
- NSDictionary* attributes = protection == nil ? nil : @{NSFileProtectionKey: protection};
- [[NSFileManager defaultManager] createFileAtPath:tempOutputFilePath contents:nil attributes:attributes];
- #endif
-
-
-
- NSInputStream *inputStream = [NSInputStream inputStreamWithFileAtPath:inputFilePath];
- NSOutputStream *outputStream = [NSOutputStream outputStreamToFileAtPath:tempOutputFilePath append:NO];
-
- [inputStream open];
- [outputStream open];
-
-
-
- z_stream strm;
-
-
- bzero(&strm, sizeof(strm));
-
- strm.zalloc = Z_NULL;
- strm.zfree = Z_NULL;
- strm.opaque = Z_NULL;
- strm.total_out = 0;
-
-
-
-
-
-
-
- deflateInit2(&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, (15+16), 8, Z_DEFAULT_STRATEGY);
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- NSUInteger inputDataLength = (1024 * 2);
- NSUInteger outputDataLength = (1024 * 1);
-
- NSMutableData *inputData = [NSMutableData dataWithLength:inputDataLength];
- NSMutableData *outputData = [NSMutableData dataWithLength:outputDataLength];
-
- NSUInteger inputDataSize = 0;
-
- BOOL done = YES;
- NSError* error = nil;
- do
- {
- @autoreleasepool {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- const void *inputBuffer = [inputData mutableBytes] + inputDataSize;
- NSUInteger inputBufferLength = inputDataLength - inputDataSize;
-
- NSInteger readLength = [inputStream read:(uint8_t *)inputBuffer maxLength:inputBufferLength];
- if (readLength < 0) {
- error = [inputStream streamError];
- break;
- }
-
- NSLogVerbose(@"CompressingLogFileManager: Read %li bytes from file", (long)readLength);
-
- inputDataSize += readLength;
-
-
-
-
-
- strm.next_in = (Bytef *)[inputData mutableBytes];
- strm.avail_in = (uInt)inputDataSize;
-
- strm.next_out = (Bytef *)[outputData mutableBytes];
- strm.avail_out = (uInt)outputDataLength;
-
-
-
-
-
-
-
-
-
- NSInteger prevTotalIn = strm.total_in;
- NSInteger prevTotalOut = strm.total_out;
-
- int flush = [inputStream hasBytesAvailable] ? Z_SYNC_FLUSH : Z_FINISH;
- deflate(&strm, flush);
-
- NSInteger inputProcessed = strm.total_in - prevTotalIn;
- NSInteger outputProcessed = strm.total_out - prevTotalOut;
-
- NSLogVerbose(@"CompressingLogFileManager: Total bytes uncompressed: %lu", (unsigned long)strm.total_in);
- NSLogVerbose(@"CompressingLogFileManager: Total bytes compressed: %lu", (unsigned long)strm.total_out);
- NSLogVerbose(@"CompressingLogFileManager: Compression ratio: %.1f%%",
- (1.0F - (float)(strm.total_out) / (float)(strm.total_in)) * 100);
-
-
-
-
-
-
-
-
- NSUInteger totalWriteLength = 0;
- NSInteger writeLength = 0;
-
- do
- {
- const void *outputBuffer = [outputData mutableBytes] + totalWriteLength;
- NSUInteger outputBufferLength = outputProcessed - totalWriteLength;
-
- writeLength = [outputStream write:(const uint8_t *)outputBuffer maxLength:outputBufferLength];
-
- if (writeLength < 0)
- {
- error = [outputStream streamError];
- }
- else
- {
- totalWriteLength += writeLength;
- }
-
- } while((totalWriteLength < outputProcessed) && !error);
-
-
-
-
-
-
-
-
-
- NSUInteger inputRemaining = inputDataSize - inputProcessed;
- if (inputRemaining > 0)
- {
- void *inputDst = [inputData mutableBytes];
- void *inputSrc = [inputData mutableBytes] + inputProcessed;
-
- memmove(inputDst, inputSrc, inputRemaining);
- }
-
- inputDataSize = inputRemaining;
-
-
-
- done = ((flush == Z_FINISH) && (inputDataSize == 0));
-
-
-
-
- }
-
- } while (!done && error == nil);
-
-
-
- [inputStream close];
- [outputStream close];
-
-
-
- deflateEnd(&strm);
-
-
-
-
- if (error)
- {
-
-
- NSLogError(@"Compression of %@ failed: %@", inputFilePath, error);
- error = nil;
- BOOL ok = [[NSFileManager defaultManager] removeItemAtPath:tempOutputFilePath error:&error];
- if (!ok)
- NSLogError(@"Failed to clean up %@ after failed compression: %@", tempOutputFilePath, error);
-
-
-
- dispatch_async([DDLog loggingQueue], ^{ @autoreleasepool {
-
- [self compressionDidFail:logFile];
- }});
- }
- else
- {
-
-
- error = nil;
- BOOL ok = [[NSFileManager defaultManager] removeItemAtPath:inputFilePath error:&error];
- if (!ok)
- NSLogWarn(@"Warning: failed to remove original file %@ after compression: %@", inputFilePath, error);
-
-
-
-
-
-
-
-
-
-
- DDLogFileInfo *compressedLogFile = [DDLogFileInfo logFileWithPath:tempOutputFilePath];
- compressedLogFile.isArchived = YES;
-
- NSString *outputFileName = [logFile fileNameByAppendingPathExtension:@"gz"];
- [compressedLogFile renameFile:outputFileName];
-
-
-
- dispatch_async([DDLog loggingQueue], ^{ @autoreleasepool {
-
- [self compressionDidSucceed:compressedLogFile];
- }});
- }
-
- }
- }
-
- @end
- #pragma mark -
- @implementation DDLogFileInfo (Compressor)
- @dynamic isCompressed;
- - (BOOL)isCompressed
- {
- return [[[self fileName] pathExtension] isEqualToString:@"gz"];
- }
- - (NSString *)tempFilePathByAppendingPathExtension:(NSString *)newExt
- {
-
-
-
-
-
-
-
- NSString *tempFileName = [NSString stringWithFormat:@"temp-%@", [self fileName]];
-
- NSString *newFileName = [tempFileName stringByAppendingPathExtension:newExt];
-
- NSString *fileDir = [[self filePath] stringByDeletingLastPathComponent];
-
- NSString *newFilePath = [fileDir stringByAppendingPathComponent:newFileName];
-
- return newFilePath;
- }
- - (NSString *)fileNameByAppendingPathExtension:(NSString *)newExt
- {
-
-
-
-
-
-
-
- NSString *fileNameExtension = [[self fileName] pathExtension];
-
- if ([fileNameExtension isEqualToString:newExt])
- {
- return [self fileName];
- }
-
- return [[self fileName] stringByAppendingPathExtension:newExt];
- }
- @end
|