Question

I have a logging project , i need to know how to build a functionality so that rotator checks for every minute while the program is running and create timestamps for every minute just to store all day's timestamps to a log file ,please help.

thanks

Was it helpful?

Solution

I suggest to use mutex for checking: http://msdn.microsoft.com/en-us/library/ms686927%28VS.85%29.aspx Timestamp and log file is nothing to do with it. Use a MSDN - you'll find there examples.

OTHER TIPS

A sample Async Timer class to fit your req...

MyPeriodicTimer.hh

#include "MyTimeOutHandler.hh"
#include <pthread.h>
#include <iostream>

using namespace std;

void* func(void* ptr);


class MyPeriodicTimer
{
    public:
       MyPeriodicTimer(MyTimeOutHandler* handler,int ms){
            milliSecondsM = ms;
            handlerM = handler;
            createClock();
       }
       void act(){
            time_t rawtime;
            time ( &rawtime );
            handlerM->handleTimeOut(&rawtime);
            sleep(milliSecondsM);
       }
       pthread_t getThread(){
           return clockThreadM;
       }
private:

   void createClock(){
        int retVal = pthread_create( &clockThreadM,NULL,&func,(void*)this);
        pthread_join(clockThreadM,NULL);
    }
    int milliSecondsM;
    MyTimeOutHandler* handlerM;
    pthread_t clockThreadM;
};


void* func(void* obj){
    while(1){
      (reinterpret_cast<MyPeriodicTimer*>(obj))->act();
   }
}

Define a Interface for handling timeout

MyTimeOutHandler.hh

#ifndef MY_TIMEOUT_HANDLER_HH
#define MY_TIMEOUT_HANDLER_HH

#include <time.h>

class MyTimeOutHandler{
  public:
    virtual void handleTimeOut(time_t*) = 0;
};


#endif

Create your LogHandler

LogHandler.cc

#include "MyTimeOutHandler.hh"
#include "MyPeriodicTimer.hh"
#include <iostream>
#include <time.h>

using namespace std;

class LogHandler : public MyTimeOutHandler{

  public:

    void start(int ms){
    MyPeriodicTimer timer(this,ms);
    }

    /* CallBack when timer is fired */
    void handleTimeOut(time_t* time){
        // Implement your Logging functionality Here
    cout<<"Time : "<<ctime(time)<<endl;
    }

};

int main(){
    LogHandler l;
    l.start(60);
     return 0;
}

Output:

>g++ -pthread LogHandler.cc

>./a.out
Time : Tue Apr 10 17:24:17 2012
Time : Tue Apr 10 17:25:17 2012
Time : Tue Apr 10 17:26:17 2012
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top