0

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

gandhigcpp
  • 587
  • 3
  • 8
  • 16
  • donot know where to start with – gandhigcpp Apr 09 '12 at 10:42
  • We don't know the architecture of your app. Your requirement is unclear. Keeping a dateTime variable up-to-date so that log entries can be timestamped can be done in diferent ways, depending on the rest of your design. Maybe you will have to wait on the logger output queue with a timeout and recalculate the timeout every time a log request is handled. On timeout, get the current time and reload the date-time string used for the timestamp. Maybe you can just use a timer on your GUI form, or System.Timer perhaps. Maybe you should write some code and try to get it to work yourself? – Martin James Apr 09 '12 at 12:20

2 Answers2

0

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
Sam Daniel
  • 51
  • 4
-1

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.

Jakub
  • 13,712
  • 17
  • 82
  • 139