base
This commit is contained in:
@@ -0,0 +1,152 @@
|
||||
#include "ReportLog.h"
|
||||
|
||||
#include <sys/stat.h>
|
||||
#include <errno.h>
|
||||
#include <stdarg.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
|
||||
|
||||
#define REPORT_LOG_NAME "report" // Report Log 폴더 및 파일 명칭 지정자..
|
||||
#define MAX_BUFFER_SIZE 2048 // 임시 버퍼 최대 크기
|
||||
|
||||
|
||||
// 생성자..
|
||||
CReportLog::CReportLog()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
// 소멸자..
|
||||
CReportLog::~CReportLog()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
bool CReportLog::Init( const char * szPath )
|
||||
{
|
||||
// 멤버 변수 초기화.
|
||||
m_strFullPath.clear();
|
||||
m_strLastError.clear();
|
||||
|
||||
// 변수 유효성 검사.
|
||||
if( szPath == NULL || strlen( szPath ) == 0 )
|
||||
{
|
||||
m_strLastError = "Log path empty.";
|
||||
return false;
|
||||
}
|
||||
|
||||
// Log Path check...
|
||||
struct stat dirStat;
|
||||
if( lstat ( szPath, &dirStat ) != 0)
|
||||
{
|
||||
//m_strLastError = "Log path not valid. Check path [" + szPath + "][" + strerror(errno) + "]" ;
|
||||
m_strLastError = "Log path not valid. Check path [";
|
||||
m_strLastError.append( szPath );
|
||||
m_strLastError.append( "][" );
|
||||
m_strLastError.append( strerror(errno) );
|
||||
m_strLastError.append( "]" );
|
||||
return false;
|
||||
}
|
||||
|
||||
// 해당 정보가 Directory 가 아닌 경우
|
||||
if( S_ISDIR( dirStat.st_mode ) == 0 )
|
||||
{
|
||||
if( S_ISLNK( dirStat.st_mode ) == 0 )
|
||||
{
|
||||
//m_strLastError = "Log path not directory or link. Check path [" + szPath + "]";
|
||||
m_strLastError = "Log path not directory or link. Check path [" ;
|
||||
m_strLastError.append( szPath );
|
||||
m_strLastError.append ( "]" );
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
//m_strLastError = "Log path is symbolic link. path [" + szPath + "]";
|
||||
m_strLastError = "Log path is symbolic link. path [";
|
||||
m_strLastError.append( szPath );
|
||||
m_strLastError.append( "]");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// 멤버 변수 정보 설정.
|
||||
m_strFullPath = szPath;
|
||||
m_strFullPath.append( "/" );
|
||||
m_strFullPath.append( REPORT_LOG_NAME );
|
||||
|
||||
// 디렉토리 정보 검사
|
||||
if( lstat ( m_strFullPath.c_str(), &dirStat ) == 0)
|
||||
{
|
||||
// 해당 이름을 가진 파일 또는 디렉토리가 존재하고
|
||||
if( S_ISDIR( dirStat.st_mode ) != 0 )
|
||||
{
|
||||
// 해당 이름이 디렉토리인 경우
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// 최종 경로에 대한 Directory 가 존재하지 않는 경우 Directory 생성 시도
|
||||
// 2013-11-27 report 로그는 fluentd 에 의해 감시, 분석되어야 하는데.. root 계정이 아닌 다른 td-agent 계정을 사용함.
|
||||
// 이로 인해 fluentd 에서 report 폴더에 접근할 수 있도록 폴더 권한 설정을 777 로 해야 함.
|
||||
if( mkdir( m_strFullPath.c_str(), 0777 ) != 0 )
|
||||
{
|
||||
m_strLastError = "Log directory create failed. Check path [" + m_strFullPath + "][" + strerror(errno) + "]";
|
||||
return false;
|
||||
}
|
||||
else
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CReportLog::Write( const char * szLevel, const char * fmt, ...)
|
||||
{
|
||||
// Get Current Data & Time
|
||||
char szTimeString[256];
|
||||
time_t now = time( NULL );
|
||||
struct tm timeNow;
|
||||
localtime_r( &now, &timeNow );
|
||||
|
||||
// Make File Name
|
||||
std::string fileName = m_strFullPath + "/" + REPORT_LOG_NAME + "_";
|
||||
snprintf( szTimeString, (size_t)256, "%04d%02d%02d.log", timeNow.tm_year+1900, timeNow.tm_mon+1, timeNow.tm_mday);
|
||||
fileName.append( szTimeString );
|
||||
|
||||
|
||||
// Log file open
|
||||
FILE * pFile = NULL;
|
||||
pFile = fopen( fileName.c_str(), "a+" );
|
||||
if( pFile == NULL )
|
||||
{
|
||||
m_strLastError = "Log file open fail.[" + fileName + "][" + strerror(errno) + "]";
|
||||
return false;
|
||||
}
|
||||
|
||||
// 시간 정보처리
|
||||
snprintf( szTimeString, (size_t)256, "[%04d-%02d-%02d %02d:%02d:%02d]"
|
||||
, timeNow.tm_year+1900, timeNow.tm_mon+1, timeNow.tm_mday
|
||||
, timeNow.tm_hour, timeNow.tm_min, timeNow.tm_sec );
|
||||
|
||||
// 가변 인자 처리
|
||||
va_list args;
|
||||
char szBuffer[MAX_BUFFER_SIZE];
|
||||
va_start( args, fmt );
|
||||
if( vsnprintf( szBuffer, MAX_BUFFER_SIZE, fmt, args) < 0 )
|
||||
{
|
||||
va_end( args );
|
||||
fclose( pFile );
|
||||
return false;
|
||||
}
|
||||
va_end(args);
|
||||
|
||||
|
||||
// Write to log file
|
||||
fprintf( pFile, "%s [%s] %s\n", szTimeString, szLevel, szBuffer );
|
||||
fflush( pFile );
|
||||
|
||||
// 종료 처리.
|
||||
fclose( pFile );
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user