Get iPhone Logs(not crash logs) - ios

I have a couple of NSLog statements in my app. In Xcode we can track NSLog() in console. When my client runs the app one of the label value is not displaying in UI. The problem is i am not able to replicate that issue at all.
Now i want the client to send log when he faces that issue. I know how to get crash logs. But i want the log which will have the result of all NSLog() statements when my client runs the app in his iPhone.
Hope my above problem is clear. Is there a way to get such logs?Any Suggestions guys?
Thanks.

Got a breakthrough!!. We need to re-direct NSLog to a file like this and then access the file.
- (void)redirectNSLogToDocumentFolder{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fileName =[NSString stringWithFormat:#"%#.log",[NSDate date]];
NSString *logFilePath = [documentsDirectory stringByAppendingPathComponent:fileName];
freopen([logFilePath cStringUsingEncoding:NSASCIIStringEncoding],"a+",stderr);
}
http://objcolumnist.com/2009/12/19/redirecting-nslog-to-a-log-file/

I've made a debug library that can be used to view your application logs from within. Also it has ability to send logs via email, so you can easily grab logs from your client. Hope this will help someone.
DVFloatingWindow

Related

How should an iOS library handle logging to a file on iPhone's file system?

I am working on an iOS library project and need to create an API that takes an NSString parameter which is a path, and the library will write some debugging messages to a file in that path.
I've done some research about logging onto a file in iPhone's file system, one approach is using
freopen([logFilePath cStringUsingEncoding:NSASCIIStringEncoding],"a+",stderr);
This will redirect any following NSLog to a file...
While this seems easy, I have a question: Will this also redirect the API consumer's application(calling application)'s NSLog to the file? I don't want this behavior because I want to be able to control what goes in there as a library..
If that is the behavior, what other approach I can use to achieve my requirement? Thanks.
If I understood correctly, the desired functionality is to pass a path and write some debugging info to a file on that path? If that is so, I don't think you should redirect all your NSLog calls to a file; just using the NSString writeToFile: would be enough:
-(void)writeToFile:(NSString*)path {
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *fileName = [documentsDirectory stringByAppendingPathComponent:path];
NSError *error=nil;
NSString *myMessage="This is the data to write to the file";
[myMessage writeToFile:fileName atomically:YES encoding:NSUTF8StringEncoding error:&error];
}
If you want to append to that file, you can use the NSFileHandle functionality - check the docs here.

asl_add_log_file() to add a log output file to "NSLog"? (iOS)

I'm willing to put a bounty on this question. I already added one, but it expired without an answer to award it to. So if you know an answer, give me a hint and I will put another bounty on this.
If I understand things correctly, asl is used behind NSLog calls. So I read the man pages and used google and eventually got the impression, that I could add an extra file to collect logging output with asl_add_log_file (man page). As with other questions found using google, my output file is empty and no log messages appear there.
The usual solution so far seems to be to freopen the stderr to a file. I ran some tests and wanted to implement a trivial log rotation. When doing another freopen to a new file, some NSLog messages are lost. So I am looking for another solution.
To test it on Mac OS X, I created a console application in Xcode and added this code to main:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSLog(#"docdir = %#", documentsDirectory);
NSString *fileName =[NSString stringWithFormat:#"asl-created-%#.log",[NSDate date]];
NSString *logFilePath = [documentsDirectory stringByAppendingPathComponent:fileName];
NSLog(#"logFilePath = %#", logFilePath);
int asl_fd = open([logFilePath cStringUsingEncoding:NSASCIIStringEncoding], O_CREAT | O_APPEND);
if (asl_fd==-1) {
NSLog(#"could not open output file");
}
asl_object_t asl;
asl = asl_open(NULL, NULL, 0);
asl_add_log_file(asl, asl_fd);
NSLog(#"test output");
/* EDIT: added this */
NSLog(#"YEAH! test output");
asl_log(asl, NULL, ASL_LEVEL_ERR, "asl_log output");
aslmsg asl_msg = asl_new(ASL_TYPE_MSG);
asl_set(asl_msg, ASL_KEY_MSG, "asl test output");
asl_set(asl_msg, ASL_KEY_HOST, "somehost");
asl_send(asl, asl_msg);
asl_free(asl_msg);
/* (EDIT: added this) */
asl_remove_log_file(asl, asl_fd);
close(asl_fd);
EDIT: However, as mentioned, the log file is empty. I would have expected, that "test output" appears in there.
I added some asl log calls to test whether or not it works when using asl directly. The two messages logged via asl_log and asl_send do appear in the created file. However, the NSLog based log messages do not appear.
Is it possible to use asl_add_log_file to adjust the NSLog "backend"?

How to clear the content of .log file programmatically in iOS

In my application, i have logged the details of the application in project.log with the help of following code.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *logPath = [documentsDirectory stringByAppendingPathComponent:#"project.log"];
freopen([logPath fileSystemRepresentation],"a+",stderr);
This code will perfectly log all the details that i defined in nslog. Now i don't want my log file to grow much further. So i want to clear the content of log files that was logged before 1 week or more. But i need the content of log for last 7 days. Is there any possible solution to accomplish the above?
There is an easy solution. It's not exactly what you have asked, but maybe it's valuable to you.
On startup, do this:
check if project.log exists and is greater than (e.g.) 2MB.
If no, just use project.log.
If yes: 1) remove project.old.log if it exists. 2) rename project.log to project.old.log 3) log to project.log (it will be created if you open with "a+")

Is there a way to create a word document or pdf in xcode 5

I would like to create a word document or a pdf of the NSLog output that I have running right now. Is there a way to do that simply or at all? I have googled the crap out of the issue and have been unable to find a result short of going into xcode and then organizer to find the output. I would prefer to not have to do that.
example:
NSLog(#"X: %.3f Y: %.3f", accelerometer.x, accelerometer.y);
//move this output straight into a word document or something of that nature that can then be pulled from the device later.
Thanks
You can write your NSLogs to a file using a method like this:
- (void)redirectNSLogToDocuments
{
NSArray *allPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [allPaths objectAtIndex:0];
NSString *pathForLog = [documentsDirectory stringByAppendingPathComponent:#"yourFile.txt"];
freopen([pathForLog cStringUsingEncoding:NSASCIIStringEncoding],"a+",stderr);
}
Then you can convert that file to whatever format you want.
How about this: https://github.com/robbiehanson/CocoaLumberjack
There's a built in file logger that outputs to txt files.
It also offers a lot more control over NSLog and you can easily stop logging without having to comment all the calls out
I actually ended up using CocoaLumberJack which will put it into a .txt document. Now to figure out a way to programmatically move the text file to my hd in linux. :(

how to filter part of a path in iOS?

we have an app released, which wasn't held for developer release. Our customer released the app instead of waiting for us changing the backend for the new version. This now leads to wrong information at a local CoreData Storage, where the problem begins.
There seems to be a field holding a local path to a file on Documents directory like:
/var/mobile/Applications/9EE88C79-F060-4EE....C0A/Documents/core23/thumb.png
The folder where the App lives in on local device changes during the update of an app (looks like!?). Now we need to filter this field to change the path before /Documents/to the actual path where the app is installed on local device.
Does somebody have a handy solution for that? Any help is appreciated.
MadMaxApp
-[NSString pathComponents] returns an array of all path components. Call that on your errant path string and build it back up from the resulting array.
-[NSString componentsSeparatedByString:(NSString*)] would also work just as well if passed #"/" for the separator string.
You can't hard code that path. To get the documents directory path use the following code:
-(NSString *)documentsDirectoryPath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
return documentsDirectoryPath;
}

Resources