- 论坛徽章:
- 0
|
#include "find/WriteLog.h"
#include <pthread.h>
extern string bugpath;
pthread_mutex_t Log_mutex=PTHREAD_MUTEX_INITIALIZER;
WriteLog::WriteLog( )
{
iLogFlag = 0;
time_t tt;
struct tm *tm;
tt = time(NULL);
tm = localtime(&tt);
strftime(cTime,sizeof(cTime),"%Y-%m-%d\0",tm);
strftime(cHour,sizeof(cHour),"%H\0",tm);
memset( fileName, 0, sizeof(fileName) );
}
WriteLog::~WriteLog()
{
if ( fout.is_open() )
fout.close();
}
void WriteLog::SetLogFlag( int value )
{
iLogFlag = value;
}
bool WriteLog::Init()
{
sprintf( fileName, "%test_%s_%d.log.%s", bugpath.data(), cTime, iLogFlag, cHour );
fout.open(fileName, ios:ut | ios::app);
if ( !fout.is_open() )
{
cerr << "Can't open" << fileName << " file for output" << endl;
return false;
}
return true;
}
oid WriteLog:rintLog( char* buf )
{
if ( !fout.is_open() )
{
fout.open(fileName, ios:ut | ios::app);
if ( !fout.is_open() )
{
cerr << "Can't open" << fileName << " file for output" << endl;
}
}
char op_hour[3], op_day[11];
time_t tt;
struct tm *tm;
tt = time(NULL);
tm = localtime(&tt);
strftime(op_day, sizeof(op_day), "%Y-%m-%d\0", tm );
strftime(op_hour, sizeof(op_hour), "%H\0", tm );
if ( strcmp(cHour, op_hour) != 0 )
{
fout.close();
strcpy( cHour, op_hour );
strcpy( cTime, op_day );
sprintf( fileName, "%test_%s_%d.log", bugpath.data(), cTime, iLogFlag, cHour );
fout.open(fileName, ios:ut | ios::app);
if ( !fout.is_open() )
{
cerr << "Can't open" << fileName << " file for output" << endl;
}
}
pthread_mutex_lock(&Log_mutex);
fout << buf;
pthread_mutex_unlock(&Log_mutex);
} |
|