0

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?

1 Answer 1

1

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+")
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. but i want to clear the log for certain time period(from beginning to a week before).
then you have to just clear it. parse the log. find the first log line that is in your time frame. write all the following log lines into the new file. delete the old file. rename the new file. there is no shortcut.
I have checked all the possibilities. No other way. Thanks for your answer. Implemented this logic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.