LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Implementing time based file rolling (https://www.linuxquestions.org/questions/programming-9/implementing-time-based-file-rolling-668331/)

jawaid 09-08-2008 04:07 AM

Implementing time based file rolling
 
Hi,

I have made my logging library in C/C++ and i want to implement time based file rolling functionality in this library. Initially I want to roll the file on daily basis. The problem is that i need to know when does the day changes so I can roll over the file. One way is to set the timer like calling the alarm() call and catch the SIGALRM signal, but problem with this approach is that it will not work with multiple objects and threads. If there are multiple objects or threads and each thread has its own logger then only one thread will be able to catch that signal, if each thread tries to register the signal. I don't want to check the date every time I log the message. How can I generate an event? Any suggestions?

Thanks in advance.

sundialsvcs 09-08-2008 09:16 AM

Who sez you have to 'implement' anything? :D

Most Linuxes use a logrotate program which is run every day (via cron ... see /etc/crontab) to archive older log-files.

For example, in my /var/log directory, I see not only messages but also messages.2.gz. That's the log-rotate program at work... renaming the files, compressing them, and keeping only the last so-many of them.

The cron facility is already "watching the days go by," so you can simply create a program for it to run at periodic intervals. And the program that you need might be the already-existing logrotate program.
Quote:

Dictum Ne Agas: "Do Not Do A Thing Already Done."

jawaid 09-08-2008 10:19 PM

Thanks for the reply bro!

Actually the problem is that I have written my logging library for one of my servers which will be uploaded to a server where we are not allowed to schedule anything nor install anything. weird but it is because its client's server and we have only limited access to the server. So I thought of implementing the rotation in my own library, like in log4cxx from apache. rotation is built in to log4cxx and i think without using logrotate. I wonder how they have implemented it?

chrism01 09-09-2008 08:15 PM

Use the source ....
;)

jawaid 09-11-2008 12:37 AM

well its a good idea but its a huge project with lots of functionalities that i dont want. neither i have time to study the code of it. just trying to get a quick idea about how to implement it.

jawaid 09-11-2008 10:21 PM

Well I'm doing one thing now. As time is needed in logging message without it obviously logs are useless, so at start I check the current time and calculate the time left to day end. So whenever message is being logged current time is retrieved to be placed in the message and this time is also checked if it is greater then the previously calculated day end time then roll the file otherwise just log the message. There is a slight performance drawback in it but I think its acceptable and negligible since its just one if check. Any suggestions/comments will be appreciated.


All times are GMT -5. The time now is 11:21 PM.