0

私はロギングプロジェクトを持っています。プログラムの実行中にローテーターが毎分チェックし、一日のタイムスタンプをログファイルに保存するためだけに毎分タイムスタンプを作成する機能を構築する方法を知る必要があります。助けてください。

ありがとう

4

2 に答える 2

0

要件に合うサンプルの非同期タイマークラス...

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();
   }
}

タイムアウトを処理するためのインターフェースを定義する

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

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;
}

出力:

>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
于 2012-04-10T11:58:14.117 に答える
-1

チェックにはmutexを使用することをお勧めします:http://msdn.microsoft.com/en-us/library/ms686927%28VS.85%29.aspx タイムスタンプとログファイルはそれとは関係ありません。MSDNを使用してください-そこに例があります。

于 2012-04-09T11:03:17.907 に答える