iPhone 응용 프로그램의 파일이나 데이터베이스에 로그 문을 쓰는 가장 좋은 방법은 무엇입니까?iPhone의 파일에 로깅
이상적으로 NSLog() 출력은 freopen()을 사용하여 파일로 리디렉션 될 수 있지만 작동하지 않는다는 여러 보고서를 보았습니다. 누구나 이미이 일을 진행하고 있거나 최선의 방법을 생각할 수 있습니까?
감사합니다.
iPhone 응용 프로그램의 파일이나 데이터베이스에 로그 문을 쓰는 가장 좋은 방법은 무엇입니까?iPhone의 파일에 로깅
이상적으로 NSLog() 출력은 freopen()을 사용하여 파일로 리디렉션 될 수 있지만 작동하지 않는다는 여러 보고서를 보았습니다. 누구나 이미이 일을 진행하고 있거나 최선의 방법을 생각할 수 있습니까?
감사합니다.
출력을 내 자신의 파일로 리디렉션하기 위해 전화기에서 freopen (...)을 성공적으로 사용했습니다.
코코아를 사용하려면 NSString과 NSData에 파일을 읽고 쓰는 메소드가 있고 NSFileManager는 파일 조작을 제공합니다. 여기에 (아이폰에서 작동합니다) 예입니다 :
NSData *dataToWrite = [[NSString stringWithString:@"String to write"] dataUsingEncoding:NSUTF8StringEncoding];
NSString *docsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *path = [docsDirectory stringByAppendingPathComponent:@"fileName.txt"];
// Write the file
[dataToWrite writeToFile:path atomically:YES];
// Read the file
NSString *stringFromFile = [[NSString alloc] initWithContentsOfFile:path];
// Check if file exists
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager fileExistsAtPath:path]; // Returns a BOOL
// Remove the file
[fileManager removeItemAtPath:path error:NULL];
// Cleanup
[stringFromFile release];
[fileManager release];
이 코드는 나를 위해 작동 :이 코드는 나를 위해 잘 작동
- (void)applicationDidFinishLaunching:(UIApplication *)application {
#if TARGET_IPHONE_SIMULATOR == 0
freopen([@"/tmp/my_logs.txt" fileSystemRepresentation], "w", stderr);
#endif
}
.. 당신은 다음을
#if TARGET_IPHONE_SIMULATOR == 0
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *logPath = [documentsDirectory stringByAppendingPathComponent:@"console.log"];
freopen([logPath cStringUsingEncoding:NSASCIIStringEncoding],"a+",stderr);
#endif
얻을 수 있습니다 여기에 설명 된 방법을 사용하여 아이폰에서 로그 파일을 엽니 다. http://blog.coriolis.ch/2009/01/09/redirect-nslog-to-a-file-on-the-iphone/#more-85
freopen을 사용하면 C 그러나 Xcode의 주최자에서 볼 수있는 콘솔은 여전히 훌륭하게 작동합니다.
저는 Xcode 문제를 해결할 수 있었지만 Xcode에서 설정 한 환경 변수를 확인하여 Xcode에서 실행하지 않을 경우에만 리디렉션했습니다. http://lists.apple.com/archives/xcode-users/2009/Aug/msg00507.html 예 :'if (getenv ("MY_ENV_VAR") == NULL) {/ * call freopen() * /}' –
Cocoa Lumberjack을 사용해보십시오. 가볍지 만 유연한 유틸리티로 NSLog 기능을 대체합니다. 필자는 Log4J와 동일한 클래스에 속하므로 사용자 지정 appenders 등을 허용합니다. 예를 들어 SQLite 로거가 있습니다.
사용한 인수가 기억 나십니까? –
모든 콘솔 출력을 logFileName으로 리디렉션 한 freopen ([newFileName UTF8String], "w +", stderr)을 사용했습니다. –
후속 조치 - 정상적으로 작동합니다. 유일한 불만은 NSLog 출력이 freopen을 호출 한 후 콘솔에 나타나지 않는다는 것입니다. 그러나 이것은 아주 사소한 문제입니다. –