Exemplo n.º 1
0
std::string Syslog_print::build_message_prefix(const std::string& binary_name) {
  /* PRI FAC_NAME PRI_NAME TIMESTAMP APP-NAME IDENT PROCID */

  // First: Priority- and facility-value (PRIVAL)
  std::string message = "<" + std::to_string(calculate_pri()) + "> ";

  // Second : Facility and priority/severity in plain text with colors
  message += pri_colors.at(priority()) + "<" + facility_name() + "." +
    priority_name() + "> " + COLOR_END;

  // Third: Timestamp
  char timebuf[TIMELEN];
  time_t now;
  time(&now);
  strftime(timebuf, TIMELEN, "%FT%T.000Z", localtime(&now));
  message += std::string{timebuf} + " ";

  // Fourth: App-name
  message += std::string(binary_name);

  // Fifth: Add ident if is set (through openlog)
  if (ident_is_set())
    message += " " + std::string{ident()};

  // Sixth: Add PID (PROCID) if LOG_PID is specified (through openlog)
  if (logopt() & LOG_PID)
    message += "[" + std::to_string(getpid()) + "]";

  message += ": ";

  // NEW: Was only in plugin
  // Add ident before message (buf) if is set (through openlog)
  // if (ident_is_set())
  //    message += std::string{ident()} + " ";

  return message;
}
Exemplo n.º 2
0
int CLogFile::WriteLog(const char *sFile, const int iLine, const char *sFunc, int i32Level, const char * pszFormat, va_list ap)
{
	FILE * m_pLogFile;

	if (m_sLogFilePath.empty()) return -2; //not initialize
	m_pLogFile = fopen(m_sLogFilePath.c_str(), "a+");
	if (m_pLogFile == NULL)
	{
		return -1;
	}

	struct tm _tm;
	time_t nowtime = time(NULL);
	localtime_r(&nowtime, &_tm);

	char _pstr[20];
	snprintf(_pstr, sizeof(_pstr), "%04d-%02d-%02d %02d:%02d:%02d",
					_tm.tm_year+1900, _tm.tm_mon+1, _tm.tm_mday,
					_tm.tm_hour, _tm.tm_min, _tm.tm_sec);
	_pstr[20] = '\0';

	fprintf(m_pLogFile, "[%d,%d--%s:%d:%s] <%s> [%s]", getppid(), getpid(), sFile, iLine, sFunc, priority_name(i32Level), _pstr);
	vfprintf(m_pLogFile, pszFormat, ap);
	fprintf(m_pLogFile, "\n");
	fclose(m_pLogFile);

	ShiftFiles(10000000, 5);
	return 0;
}