Example #1
0
 bool
 Logger :: logVA(const char* filename, int line, Level level, const char *fmt, va_list ap)
 {
   bool didLog = false;
   if (mGlobalIsLogging[level] && mIsLogging[level])
   {
     char msg[cMaxLogMessageLength+1];
     formatMsg(msg, sizeof(msg), filename, line, fmt, ap);
     didLog = this->doLog(level, msg);
   }
   return didLog;
 }
Example #2
0
//------------------------------------------------------------------------------
// DESCRIPTION:
//   Log a message to the log file and to the console.
//   We used to log error and critical error msgs to error log only.
//   But changed to log to m_logFile too, because it's easy to
//   forget to look at the err log.  And seeing the error log msg
//   in the correct timeline context in the m_logFile helps to see
//   what else was going on when the problem occurred.
// PARAMETERS:
//   msg - message
//   code - return status code to include in the message
//   level - message level
// RETURN:
//    none
//------------------------------------------------------------------------------
void Log::logMsg( const char* msg,
                  int         code,
                  MsgLevel    level )
{
    std::ostringstream oss;
    formatMsg( msg, level, oss, code );

    // log error and critical msgs to syslog
    if( level == MSGLVL_ERROR || level == MSGLVL_CRITICAL ) 
    {
        { //log to log file and error log file within scope of mutex lock.
          //logSyslog uses SimpleSyslog which has it's own lock.
            boost::mutex::scoped_lock lk(m_WriteLockMutex);

            m_errLogFile << oss.str() << std::endl;
            m_logFile    << oss.str() << std::endl;

            std::cerr << oss.str() << std::endl;
        }

        logSyslog( std::string(msg), code );
    }
    else
    {
        std::ostringstream oss2;

        // Format msg again without including the status code.
        // Only log INFO2 msgs to console if m_bConsoleOutput is TRUE;
        // All other msg levels always go to console.
        if( (level != MSGLVL_INFO2) || (m_bConsoleOutput) )
            formatMsg ( msg, level, oss2 );

        boost::mutex::scoped_lock lk(m_WriteLockMutex);

        m_logFile << oss.str() << std::endl;

        if( (level != MSGLVL_INFO2) || (m_bConsoleOutput) )
            std::cout << oss2.str() << std::endl;
    }
}
Example #3
0
	/********************************************************************
	* [函数名]: insertCurrentTime
	* [描述]: 日志当前时间 (eg: YYYY-MM-DD hh:mm:ss)
	*         y: year    M: month    d: day
	*         h: hour    m: minute   s: second
	* [修改记录]:
	*   2015-05-20,littledj: create
	*   2015-05-20,littledj: 增加自定义格式
	********************************************************************/
	void GLogger::insertCurrentTime(tstring format)
	{
		if (format.empty())
			format = TEXT("** yyyy-MM-dd hh:mm:ss **\n");

		time_t tt = time(0);
		struct tm *lt = localtime(&tt);

		tchar* ct = new tchar[format.length() + 4];		
		tchar tmp[5];
		tcscpy(ct, format.c_str());
		for (size_t i = 0; i < format.size(); i++)
		{
			if (tcsncmp(ct + i, TEXT("yyyy"), 4 ) == 0)
			{
				stprintf(tmp, TEXT("%04d"), lt->tm_year + 1900);
				tcsncpy(ct + i, tmp, 4);
				i += 3;
				continue;
			}
			if (tcsncmp(ct + i, TEXT("MM"), 2) == 0)
			{
				stprintf(tmp, TEXT("%02d"), lt->tm_mon + 1);
				tcsncpy(ct + i, tmp, 2);
				i++;
				continue;
			}
			if (tcsncmp(ct + i, TEXT("dd"), 2) == 0)
			{
				stprintf(tmp, TEXT("%02d"), lt->tm_mday);
				tcsncpy(ct + i, tmp, 2);
				i++;
				continue;
			}
			if (tcsncmp(ct + i, TEXT("hh"), 2) == 0 || tcsncmp(ct + i, TEXT("HH"), 2) == 0)
			{
				stprintf(tmp, TEXT("%02d"), lt->tm_hour);
				tcsncpy(ct + i, tmp, 2);
				i++;
				continue;
			}
			if (tcsncmp(ct + i, TEXT("mm"), 2) == 0)
			{
				stprintf(tmp, TEXT("%02d"), lt->tm_min);
				tcsncpy(ct + i, tmp, 2);
				i++;
				continue;
			}
			if (tcsncmp(ct + i, TEXT("ss"), 2) == 0)
			{
				stprintf(tmp, TEXT("%02d"), lt->tm_sec);
				tcsncpy(ct + i, tmp, 2);
				i++;
				continue;
			}
		}

		tstring msg = formatMsg(PRINT_TYPE::RAW, TEXT("%s"), ct);
		delete[] ct;

		if (m_enableColor)
			output(msg, m_defaultColor);
		else
			output(msg);

		// 保存当前消息
		saveToMessagePool(PRINT_TYPE::RAW, msg);
	}