#ifdef TEST_MODE #define NSLog(...) iydLog(__FILE__, __FUNCTION__, __LINE__, __VA_ARGS__) #endif // // iydLogWithFile.m // ebook // // Created by xiejinniu on 12-7-3. // Copyright (c) 2012年 __MyCompanyName__. All rights reserved. // #import "iydLogWithFile.h" #include <sys/stat.h> #import "systemhelper.h" #import "ebookAppDelegate.h" #define KLOG_FILE_MAX_SIZE (5*1024*1024) static void checkLogFileSizeAndDelete() { struct stat fileStatus = {0}; const char* logPath = [[SystemHelper GetIYDFileLogPath_str] cStringUsingEncoding:NSUTF8StringEncoding]; if(stat(logPath, &fileStatus) == -1) { return; } if (fileStatus.st_size > KLOG_FILE_MAX_SIZE) { [[NSFileManager defaultManager] removeItemAtPath:[SystemHelper GetIYDFileLogPath_str] error:nil]; } } void iydLog(const char* file, const char* function, int line, NSString* format, ...) { [[ebookAppDelegate getInstance].logLock lock]; { checkLogFileSizeAndDelete(); va_list args; va_start(args, format); NSMutableString* strContent = [[NSMutableString alloc] initWithFormat:format arguments:args]; [strContent appendFormat:@"\r\n[file:%s], [function:%s], [line:%d]\r\n\r\n", file, function, line]; NSLogv(format, args); const char* cLog = [strContent cStringUsingEncoding:NSUTF8StringEncoding]; const char* logPath = [[SystemHelper GetIYDFileLogPath_str] cStringUsingEncoding:NSUTF8StringEncoding]; FILE* filehandle = fopen(logPath, "ab+"); if (filehandle != NULL) { int realsize = fwrite(cLog, 1, strlen(cLog), filehandle); if (realsize != strlen(cLog)) { NSLogv(@"write log file error!", nil); } } fclose(filehandle); [strContent release]; va_end(args); } [[ebookAppDelegate getInstance].logLock unlock]; }
时间: 2024-11-06 07:49:19