How to clear the content of .log file programmatically in iOS - 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+")

Related

Writing big file on IOS [duplicate]

I save some run-time generated files inside the .app bundle of my iOS app. In the simulator it works fine, in the device it crashes:
Could create output files in the given shader cache path
'/var/mobile/Applications/CB064997-B40E-4FE3-9834-B3217CE33489/SimedTest.app/Ogre3D/assets/RTShaderLib/cache/
Is there a good overview of where I should and shouldn't put files - how to use Documents, Library and tmp, etc?
To clarify, these are files created at startup which pre-calculate some data to save time. IF they are not present they get created so it's fine they are deleted, but not while the app is running.
The bundle is read-only. You don't want to mess around with it for two reasons:
Code Signing: the signature is verified by against the contents of the bundle; if you mess around with the bundle, you break the signature.
App Updates: updates work by replacing the entire app bundle with a newly downloaded one; any changes you make will get lost.
Where you should save stuff:
Documents: if you want it to persist and be backed up
Library/Caches: if you just want to cache downloaded data, like profile pics; will be auto deleted by the system if it is low on room unless you specify with a special do-not-delete flag.
tmp: temporary files, deleted when your app is not running
For a full explanation check out File System Programming Guide and QA1719.
No, every time you change your bundle you invalidate your signature.
If you want to write files you`l need to write in the best folder depending on what you want to do with that file.
Documents folder for long duration files
Cache for small operations
and so on
EDIT
To get the path you`ll need something like this:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"filename.ext"];
With this path you can write or read like this:
write:
NSString *content = #"One\nTwo\nThree\nFour\nFive";
[content writeToFile:fileName atomically:NO encoding:NSStringEncodingConversionAllowLossy error:nil];
read:
NSString *content = [[NSString alloc] initWithContentsOfFile:fileName usedEncoding:nil error:nil];

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. :(

Get iPhone Logs(not crash logs)

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

Does unique app id could change on device?

I have a coredata DB which store pdf absolute path for files.
I discovered that my pdf could not be loaded, after investigation it is because the path for Documents folder has changed becaue App's unique id has changed.
Was /var/mobile/Applications/FEF0A8B5-6FA3-47DA-98EC-732A3FFA6FD3/Documents/
And is now: /var/mobile/Applications/17525DA9-C2C5-4515-9635-0ECC76BE9131/Documents/
Could this happens?
In what situation?
Thanks a lot.
Thierry
When you delete the app, the next time that value will be different. You can use:
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

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