아이폰어플개발정보2010. 10. 21. 18:52
[아이폰 앱 개발] 파일로 log남기기

  Logging to a file on the iPhone
 

What would be the best way to write log statements to a file or database in an iPhone application?

Ideally, NSLog() output could be redirected to a file using freopen(), but I've seen several reports that it doesn't work. Does anyone have this going already or have any ideas how this might best be done?

Thanks!

    202299        Mike McMaster        iphone  iphone-sdk  nslog  logging  


 

If you want to use Cocoa, NSString and NSData have methods for reading/writing to file and NSFileManager gives you file operations. Here's an example (should work on iPhone):

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];
    203561        Martin Gordon        iphone  iphone-sdk  nslog  logging  


  Thanks. It looks like: [fileManager removeItemAtIndexPath:path error:NULL]; should be: [fileManager removeItemAtPath:path error:NULL]; Works fine otherwise. I'll leave the question open for now to see what else people suggest. This is helpful though. #78012    Mike McMaster
  Oops. Just fixed that. I was doing some table view programming while answering your question and this slipped through. #79308    Martin Gordon


 

I've successfully used freopen(...) on the phone to re-direct output to my own file.

    202358        Ben Gottlieb        iphone  iphone-sdk  nslog  logging  


  Do you remember which arguments you used? #77434    Mike McMaster
  I used freopen([newFileName UTF8String], "w+", stderr), which redirected all console output to logFileName. #78393    Ben Gottlieb
  Just to follow up - this worked fine. The only bummer is that NSLog output stops appearing on the console after calling freopen, but it's a pretty minor issue. #327171    Mike McMaster


 

This code works for me:

- (void)applicationDidFinishLaunching:(UIApplication *)application {
#if TARGET_IPHONE_SIMULATOR == 0
freopen
([@"/tmp/my_logs.txt" fileSystemRepresentation], "w", stderr);
#endif
}
    479556        nst        iphone  iphone-sdk  nslog  logging  


 

how to limit the log size?

    1193034        Hikaru        iphone  iphone-sdk  nslog  logging  


 

Hi Mike

Had any luck? I tried what ben wrote but nothing was written to the file. If you have any conclusions please post them.

Thanks

Gidon

Posted by 오늘마감

댓글을 달아 주세요