static STLW::string EscapeString(const STLW::string  & sSource)
{
	if (sSource.empty()) { return "\"\""; }
	STLW::string::const_iterator itsSource = sSource.begin();
	while (itsSource != sSource.end())
	{
		const UCHAR_8 ucTMP = *itsSource;
		if (!((ucTMP >= 'a' && ucTMP <= 'z') ||
		      (ucTMP >= 'A' && ucTMP <= 'Z') ||
		      (ucTMP >= '0' && ucTMP <= '9') ||
		       ucTMP == '.' || ucTMP == '_' || ucTMP == ':' || ucTMP == '*' ||
		       ucTMP == '[' || ucTMP == ']' || ucTMP == '\\' || ucTMP == '/' || ucTMP == '-'))
		{
			return '"' + sSource + '"';
		}
		++itsSource;
	}
return sSource;
}
示例#2
0
//
// Write message to log file
//
IRIS::Logger::State ParametrizedLogger::ParametrizedLogger::WriteLog(const IRIS::LogPriority::LogPriorities  ePriority,
                                                                     CCHAR_P                                 szString,
                                                                     const UINT_32                           iStringLen)
{
	using namespace IRIS;
	MutexLocker oMutexLocker(oMutex);
/*
	# $timegm.unix      - current GMT unixtime, seconds
	# $timegm.ascii     - current GMT time, ascii representation [Wkd Mmm DD HH:MM:SS YYYY]
	# $time.unix        - current LOCAL unixtime, seconds
	# $time.ascii       - current LOCAL time, ascii representation
*/
	SystemVars::Time oTimeGM = SystemVars::GetTime();

	IRIS::DataBuffer & oDataBuffer = oLogWriter;

	struct tm oLocalTime;
	time_t iTime = oTimeGM.sec;
	localtime_r(&iTime, &oLocalTime);

	STLW::string::const_iterator itsLogFormat = sLogFormat.begin();
	STLW::stringstream sStream;
	for (;;)
	{
		STLW::string::const_iterator itsTMP = itsLogFormat;
		while(itsLogFormat != sLogFormat.end() && *itsLogFormat != '$') { ++itsLogFormat; }
		sStream << STLW::string(itsTMP, itsLogFormat);

		if (itsLogFormat == sLogFormat.end()) { break; }

		itsTMP = itsLogFormat++;
		while(itsLogFormat != sLogFormat.end())
		{
			const CHAR_8 chTMP = *itsLogFormat;
			if (!((chTMP >= 'a' && chTMP <= 'z') ||
			      (chTMP >= 'A' && chTMP <= 'Z') ||
			      (chTMP >= '0' && chTMP <= '9') ||
			       chTMP == '.' || chTMP == '_')) { break; }
			++itsLogFormat;
		}

		const STLW::string sParam(++itsTMP, itsLogFormat);
		if      (sParam == "message.str")  { sStream << STLW::string(szString, iStringLen); }
		else if (sParam == "timegm.unix")  { sStream << oTimeGM.sec << '.' << oTimeGM.nsec; }
		else if (sParam == "time.unix")
		{
#if defined(sun) || defined(_AIX)
			sStream << oTimeGM.sec + GetGMTOff(iTime, oLocalTime) << '.' << oTimeGM.nsec;
#else
			sStream << oTimeGM.sec + oLocalTime.tm_gmtoff << '.' << oTimeGM.nsec;
#endif
		}
		else if (sParam == "timegm.ascii") { sStream << FormatDate(oTimeGM.sec); }
		else if (sParam == "time.ascii")
		{
#if defined(sun) || defined(_AIX)
			sStream << FormatDate(oTimeGM.sec + GetGMTOff(iTime, oLocalTime));
#else
			sStream << FormatDate(oTimeGM.sec + oLocalTime.tm_gmtoff);
#endif
		}
		else if (sParam == "priority")     { sStream << LogPriority::GetPrintableState(ePriority); }
		else
		{
			if (pLoggerContext == NULL)
			{
				sStream << "-";
			}
			else
			{
				STLW::string sValue;
				pLoggerContext -> GetParam(sParam, sValue);
				sStream << sValue;
			}
		}
	}

	const STLW::string sResult = sStream.str();
	STLW::string::const_iterator itsResult = sResult.begin();
	for (;;)
	{
		STLW::string::const_iterator itsStart = itsResult;
		while (itsResult != sResult.end() && *itsResult >= ' ') { ++itsResult; }
		oDataBuffer.Append(itsStart, itsResult);
		if (itsResult == sResult.end()) { break; }

		const UCHAR_8 ucTMP = *itsResult;
		switch (ucTMP)
		{
			case '\n':
				oDataBuffer.Append("\\n");
				break;
			case '\r':
				oDataBuffer.Append("\\r");
				break;
			case '\t':
				oDataBuffer.Append("\\t");
				break;
			default:
				{
					CHAR_8 szBuf[6];
					INT_32 iBufLen = snprintf(szBuf, 6, "\\x%02X", ucTMP);
					oDataBuffer.Append(szBuf, iBufLen);
				}
		}

		++itsResult;
		if (itsResult == sResult.end()) { break; }
	}
	oDataBuffer.Append("\n");
	oLogWriter.Flush();

return IRIS::Logger::OK;
}