En attendant des solution pour tracer et debugger correctement une appli Iphone, voici un moyen simple qui permet de récupérer les résultats des NSLog dans une application lancée par le Springboard : réorienter les NSLog vers un fichier.

Une macro remplace le NSLog.
Les données sont écrites dans /var/root/VSLog.txt

/*

* VSLog.h

* Created by Jérémie Engel 12/12/2007.

* Copyright 2007 VisuaWeb. All rights reserved.

*

*/

#include <Foundation/Foundation.h>

#include <CoreFoundation/CoreFoundation.h>

#define VSLog(s,...) [VSLog LogToDefaultFile:(s),##__VA_ARGS__]

#define NSLog(s,...) [VSLog LogToDefaultFile:(s),##__VA_ARGS__]

@interface VSLog : NSObject {

va_list ap;

NSFileHandle *fileOutHandle;

NSString *strToWrite, *fileOut;

}

+ (void)LogToDefaultFile: (NSString*)format, ...;

@end

/*

* VSLog.m

* Created by Jérémie Engel 12/12/2007.

* Copyright 2007 VisuaWeb. All rights reserved.

*

*/

#include "VSLog.h"

@implementation VSLog

+ (void)LogToDefaultFile:(NSString*)format, ...; {

va_listap;

NSFileHandle *fileOutHandle;

NSString *strToWrite, *fileOut;

va_start(ap,format);

strToWrite=[[[NSStringalloc] initWithFormat:format arguments:ap] stringByAppendingString:@"\n"];

va_end(ap);

if (![[NSFileManagerdefaultManager] fileExistsAtPath:@"/var/root/VSLog.txt"]) [[NSFileManagerdefaultManager] createFileAtPath:@"/var/root/VSLog.txt"contents:nilattributes:nil];

fileOut = [[NSStringstringWithString:@"/var/root/VSLog.txt"] stringByExpandingTildeInPath];

fileOutHandle = [NSFileHandlefileHandleForWritingAtPath:fileOut];

[fileOutHandletruncateFileAtOffset:[fileOutHandleseekToEndOfFile]];

[fileOutHandlewriteData:[strToWritedataUsingEncoding:NSUTF8StringEncoding]];

[fileOutHandlecloseFile];

[fileOutHandlerelease];

[fileOutrelease];

[strToWriterelease];

return;

}

@end