Example #1
0
std::string StringFromFormat(const char* format, ...)
{
	va_list args;
	char *buf = nullptr;
#ifdef _WIN32
	int required = 0;

	va_start(args, format);
	required = _vscprintf(format, args);
	buf = new char[required + 1];
	CharArrayFromFormatV(buf, required + 1, format, args);
	va_end(args);

	std::string temp = buf;
	delete[] buf;
#else
	va_start(args, format);
	if (vasprintf(&buf, format, args) < 0)
		ERROR_LOG(COMMON, "Unable to allocate memory for string");
	va_end(args);

	std::string temp = buf;
	free(buf);
#endif
	return temp;
}
Example #2
0
std::string StringFromFormatV(const char* format, va_list args)
{
  char* buf = nullptr;
#ifdef _WIN32
  int required = _vscprintf(format, args);
  buf = new char[required + 1];
  CharArrayFromFormatV(buf, required + 1, format, args);

  std::string temp = buf;
  delete[] buf;
#else
#if !defined(ANDROID) && !defined(__HAIKU__) && !defined(__OpenBSD__)
  locale_t previousLocale = uselocale(GetCLocale());
#endif
  if (vasprintf(&buf, format, args) < 0)
  {
    ERROR_LOG(COMMON, "Unable to allocate memory for string");
    buf = nullptr;
  }

#if !defined(ANDROID) && !defined(__HAIKU__) && !defined(__OpenBSD__)
  uselocale(previousLocale);
#endif

  std::string temp = buf;
  free(buf);
#endif
  return temp;
}
Example #3
0
// This is the first stop for gui alerts where the log is updated and the
// correct window is shown
bool MsgAlert(bool yes_no, int Style, const char* format, ...)
{
	// Read message and write it to the log
	char buffer[2048];

	static const char *captions[] = {
		"Information",
		"Question",
		"Warning",
		"Critical"
	};

	const char *caption = captions[Style];

	va_list args;
	va_start(args, format);
	CharArrayFromFormatV(buffer, sizeof(buffer)-1, format, args);
	va_end(args);

	ERROR_LOG(MASTER_LOG, "%s: %s", caption, buffer);

	// Don't ignore questions, especially AskYesNo, PanicYesNo could be ignored
	if (AlertEnabled || Style == QUESTION || Style == CRITICAL)
		return MsgHandler(caption, buffer, yes_no, Style);

	return true;
}
Example #4
0
// This is the first stop for gui alerts where the log is updated and the
// correct window is shown
bool MsgAlert(bool yes_no, int Style, const char* format, ...)
{
  // Read message and write it to the log
  std::string caption;
  char buffer[2048];

  static std::string info_caption;
  static std::string warn_caption;
  static std::string ques_caption;
  static std::string crit_caption;

  if (!info_caption.length())
  {
    info_caption = str_translator(_trans("Information"));
    ques_caption = str_translator(_trans("Question"));
    warn_caption = str_translator(_trans("Warning"));
    crit_caption = str_translator(_trans("Critical"));
  }

  switch (Style)
  {
  case INFORMATION:
    caption = info_caption;
    break;
  case QUESTION:
    caption = ques_caption;
    break;
  case WARNING:
    caption = warn_caption;
    break;
  case CRITICAL:
    caption = crit_caption;
    break;
  }

  va_list args;
  va_start(args, format);
  CharArrayFromFormatV(buffer, sizeof(buffer) - 1, str_translator(format).c_str(), args);
  va_end(args);

  ERROR_LOG(MASTER_LOG, "%s: %s", caption.c_str(), buffer);

  // Don't ignore questions, especially AskYesNo, PanicYesNo could be ignored
  if (msg_handler && (AlertEnabled || Style == QUESTION || Style == CRITICAL))
    return msg_handler(caption.c_str(), buffer, yes_no, Style);

  return true;
}
Example #5
0
void LogManager::LogWithFullPath(LogTypes::LOG_LEVELS level, LogTypes::LOG_TYPE type,
                                 const char* file, int line, const char* format, va_list args)
{
  if (!IsEnabled(type, level) || !static_cast<bool>(m_listener_ids))
    return;

  char temp[MAX_MSGLEN];
  CharArrayFromFormatV(temp, MAX_MSGLEN, format, args);

  std::string msg =
      StringFromFormat("%s %s:%u %c[%s]: %s\n", Common::Timer::GetTimeFormatted().c_str(), file,
                       line, LogTypes::LOG_LEVEL_TO_CHAR[(int)level], GetShortName(type), temp);

  for (auto listener_id : m_listener_ids)
    if (m_listeners[listener_id])
      m_listeners[listener_id]->Log(level, msg.c_str());
}
Example #6
0
void LogManager::Log(LogTypes::LOG_LEVELS level, LogTypes::LOG_TYPE type,
	const char *file, int line, const char *format, va_list args)
{
	char temp[MAX_MSGLEN];
	LogContainer *log = m_Log[type];

	if (!log->IsEnabled() || level > log->GetLevel() || !log->HasListeners())
		return;

	CharArrayFromFormatV(temp, MAX_MSGLEN, format, args);

	std::string msg = StringFromFormat("%s %s:%u %c[%s]: %s\n",
	                                   Common::Timer::GetTimeFormatted().c_str(),
	                                   file, line,
	                                   LogTypes::LOG_LEVEL_TO_CHAR[(int)level],
	                                   log->GetShortName().c_str(), temp);

	for (auto listener_id : *log)
		m_listeners[listener_id]->Log(level, msg.c_str());
}
Example #7
0
void LogManager::Log(LogTypes::LOG_LEVELS level, LogTypes::LOG_TYPE type, const char *file, int line, const char *format, va_list args)
{
	std::lock_guard<std::mutex> lk(m_log_lock);

	char temp[MAX_MSGLEN];
	char msg[MAX_MSGLEN * 2];
	LogContainer *log = m_Log[type];
	if (!log || !log->IsEnabled() || level > log->GetLevel() || ! log->HasListeners())
		return;

	CharArrayFromFormatV(temp, MAX_MSGLEN, format, args);

	static const char level_to_char[7] = "-NEWID";
	char formattedTime[13];
	Common::Timer::GetTimeFormatted(formattedTime);
	sprintf(msg, "%s %s:%d %c[%s]: %s\n",
		formattedTime,
		file, line, level_to_char[(int)level],
		log->GetShortName(), temp);

	log->Trigger(level, msg);
}
Example #8
0
void LogManager::Log(LogTypes::LOG_LEVELS level, LogTypes::LOG_TYPE type, 
	const char *file, int line, const char *format, va_list args)
{
	char temp[MAX_MSGLEN];
	char msg[MAX_MSGLEN * 2];
	LogContainer *log = m_Log[type];

	if (!log->IsEnabled() || level > log->GetLevel() || ! log->HasListeners())
		return;

	CharArrayFromFormatV(temp, MAX_MSGLEN, format, args);

	static const char level_to_char[7] = "-NEWID";
	sprintf(msg, "%s %s:%u %c[%s]: %s\n",
		Common::Timer::GetTimeFormatted().c_str(),
		file, line, level_to_char[(int)level],
		log->GetShortName(), temp);
#ifdef ANDROID
	Host_SysMessage(msg);	
#endif
	log->Trigger(level, msg);
}