http://www.codeproject.com/Articles/288827/g-log-An-efficient-asynchronous-logger-using-Cplus
#include <sys/uio.h>
#include <sys/types.h>
#include <sys/file.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <ctype.h>
#include <semaphore.h>
#include <netinet/tcp.h>
#include <sys/un.h>
#include <pthread.h>
#include <sys/time.h>
#include <signal.h>
#include <chrono>
#include <string>
#include <iostream>
#include <future>
#include <time.h>
#include <unistd.h>
#include "g2logworker.h"
#include "g2log.h"
using namespace std ;
const std::string path_to_log_file = "/tmp/";
const std::string fname = "test1.exe" ;
void gexit(int gub)
{
std::cout << "done" << std::endl ;
exit( 0 ) ;
}
void *thread1(void *param)
{
pthread_detach(pthread_self());
for(int idx=0;idx< 100000;idx++){
LOG(INFO) << "The thread1:(" << idx << ")" ;
}//while
std::cout << "thread1 out" << std::endl ;
return 0 ;
}
void *thread2(void *param)
{
pthread_detach(pthread_self());
for(int idx=0;idx< 100000;idx++){
LOG(INFO) << "The thread2:(" << idx << ")" ;
}//while
std::cout << "thread2 out" << std::endl ;
//CHECK(2<1) << "FATAL ERROR" ;
return 0 ;
}
int main(int argc, char** argv)
{
g2LogWorker logger(fname, path_to_log_file);
g2::initializeLogging(&logger);
signal(SIGINT, gexit); /* interrupt */
signal(SIGTERM, gexit); /* software termination signal from kill */
std::future<std::string> log_file_name = logger.logFileName();
LOG(INFO) << "This is a info" << 123.456 << "!!!" ;
LOG(WARNING) << "This is a info" << 123.456 << "!!!" ;
LOG(DEBUG) << "This is a info" << 123.456 << "!!!" ;
std::cout << "The Log:" << log_file_name.get() << " created" << std::endl ;
pthread_t tid1,tid2 ;
pthread_create(&tid1 , NULL, thread1, (void*)(long)3);
pthread_create(&tid2 , NULL, thread2, (void*)(long)3);
while( 1 ) {
sleep( 5 ) ;
}
}
g++ --std=c++11 -O2 -Wall main1.cpp -I /home/informix/tools/KjellKod-g2log-957cf466ef70/g2log/src -L /home/informix/tools/KjellKod-g2log-957cf466ef70/g2log/build -pthread -llib_g2logger_shared -o main1.exe
留言列表