2013-03-27 2 views
0

코드를 사용하여 모든 NSLog를 텍스트 파일에 작성합니다. 어떻게하면 파일에 쓸 필요가있는 NSlogs를 선택할 수 있습니까?특정 NSLog 출력을 파일로 리디렉션하는 방법

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
                NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *logPath = [documentsDirectory stringByAppendingPathComponent:@"MOVbandlog.txt"]; 
//freopen([logPath cStringUsingEncoding:NSASCIIStringEncoding],"a+",stderr); 
freopen([logPath fileSystemRepresentation],"a+",stderr); 

답변

1

NSLog 만 사용하는 MYLog는 콘솔에 표시되므로 NSLog의 두 가지 버전을 만들 수 있습니다. 두 번째 MYLogFile은 NSLog를 호출하고 파일에도 기록합니다.

그래서 기본적으로 두 가지 매크로 또는 메서드를 만들어야합니다.

+0

내가 예를받을 수 : 링크 아래

소스 코드를 다운로드 할 수

? 다음은 NSLogs NSLog 중 하나입니다 (@ "\ n ***************** \ n % @", [data description]). – TWcode

+0

약간의 조정을하십시오 [here] (http://stackoverflow.com/questions/9619708/nslog-to-both-console-and-file) –

+0

이 줄이 작동하지 않습니다 : fprintf (myFileDescriptor, "% s \ n", [ formattedMessage UTF8String]); myfileDescriptor를 사용하여 무언가 – TWcode

2

이 도움이 될 수 좀 제발 : 장치가 시스템에 연결되어 있지 않을 때 파일에 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/

+0

이 링크가 질문에 대답 할 수 있지만 여기에 대답의 핵심 부분을 포함시키고 참조 용 링크를 제공하는 것이 좋습니다. 링크 된 페이지가 변경되면 링크 전용 답변이 유효하지 않게 될 수 있습니다. –

+0

Jon Stirling에게 감사드립니다. 같은 내용으로 편집했습니다. –

관련 문제