Register a domain and help support LQ
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 09-08-2008, 04:07 AM   #1
LQ Newbie
Registered: Sep 2008
Posts: 5

Rep: Reputation: 0
Question Implementing time based file rolling


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.

Last edited by jawaid; 09-08-2008 at 04:09 AM.
Old 09-08-2008, 09:16 AM   #2
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 5,725

Rep: Reputation: 1294Reputation: 1294Reputation: 1294Reputation: 1294Reputation: 1294Reputation: 1294Reputation: 1294Reputation: 1294Reputation: 1294
Who sez you have to 'implement' anything?

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.
Dictum Ne Agas: "Do Not Do A Thing Already Done."
Old 09-08-2008, 10:19 PM   #3
LQ Newbie
Registered: Sep 2008
Posts: 5

Original Poster
Rep: Reputation: 0
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?
Old 09-09-2008, 08:15 PM   #4
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,453

Rep: Reputation: 2116Reputation: 2116Reputation: 2116Reputation: 2116Reputation: 2116Reputation: 2116Reputation: 2116Reputation: 2116Reputation: 2116Reputation: 2116Reputation: 2116
Use the source ....
Old 09-11-2008, 12:37 AM   #5
LQ Newbie
Registered: Sep 2008
Posts: 5

Original Poster
Rep: Reputation: 0
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.
Old 09-11-2008, 10:21 PM   #6
LQ Newbie
Registered: Sep 2008
Posts: 5

Original Poster
Rep: Reputation: 0
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.


event, file, handling, logging, rolling

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with a UNIX script: Need to grab information based on time from a file dba_guy Programming 13 05-28-2008 10:01 PM
Rename file based on its own date/time stamp airman99 Linux - General 19 09-05-2006 07:52 AM
Linux based gateway for time-based wireless service. TotalDefiance Linux - Software 0 10-03-2005 06:06 PM
implementing Class-based queue from a C program.. shrike_912 Programming 1 07-05-2004 01:01 PM
auto file rename based on time wx_jason Linux - Newbie 10 07-10-2003 11:14 AM

All times are GMT -5. The time now is 12:06 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration