#include "ReportLog.h" #include #include #include #include #include #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; }