이 도움이 될 수 좀 제발 : 장치가 시스템에 연결되어 있지 않을 때 파일에 nslog 출력 scenerios에 유용하고 우리가 할 몇 가지 issues.In 순서를 추적하기 위해 콘솔 로그를 필요로 리디렉션
을 NSObject의이 우리를 추가 한 singelton 클래스 USTLogger 서브 클래스 : USTLogger.h 파일
소스 코드는 다음과 같습니다 USTLogger.m 파일
#import <Foundation/Foundation.h>
@interface USTLogger : NSObject
{
BOOL stdErrRedirected;
}
@property (nonatomic, assign) BOOL stdErrRedirected;
-(void) writeNSLogToFile;
+ (USTLogger *) sharedInstance;
소스 코드는 다음과 같습니다
#import "USTLogger.h"
#import <unistd.h>
@implementation USTLogger
@synthesize stdErrRedirected;
static int savedStdErr = 0;
static USTLogger *sharedInstance;
+ (USTLogger *) sharedInstance {
static dispatch_once_t once;
static id sharedInstance;
dispatch_once(&once, ^{
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}
-(id)init {
return self;
}
- (void) writeNSLogToFile
{
if (!stdErrRedirected)
{
stdErrRedirected = YES;
savedStdErr = dup(STDERR_FILENO);
NSString *cachesDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *logPath = [cachesDirectory stringByAppendingPathComponent:@"nslog.log"];
freopen([logPath cStringUsingEncoding:NSASCIIStringEncoding], "a+", stderr);
}
}
- (void)restoreStdErr
{
if (stdErrRedirected)
{
stdErrRedirected = NO;
fflush(stderr);
dup2(savedStdErr, STDERR_FILENO);
close(savedStdErr);
savedStdErr = 0;
}
}
USTLogger 클래스의 구현은 우리가 단지 우리가 파일
#import "AppDelegate.h"
#import "USTLogger.h"
@interface AppDelegate()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[[USTLogger sharedInstance] writeNSLogToFile];
NSLog(@"didFinishLaunchingWithOptions");
return YES;
}
@end
이 파일에 전체 nslog 출력을 작성하고이 응용 프로그램 문서 디렉토리에 저장됩니다에 nslog 필요 이러한 메서드를 호출 할 필요 후, 응용 프로그램에서 파일 공유를 활성화하여 해당 nslog 파일을 가져올 수 있습니다.
https://shankertiwari3.wordpress.com/2015/06/09/redirect-the-nslog-output-to-file-instead-of-console/
내가 예를받을 수 : 링크 아래
소스 코드를 다운로드 할 수
? 다음은 NSLogs NSLog 중 하나입니다 (@ "\ n ***************** \ n % @", [data description]). – TWcode약간의 조정을하십시오 [here] (http://stackoverflow.com/questions/9619708/nslog-to-both-console-and-file) –
이 줄이 작동하지 않습니다 : fprintf (myFileDescriptor, "% s \ n", [ formattedMessage UTF8String]); myfileDescriptor를 사용하여 무언가 – TWcode