Пример #1
0
// Debug output - Same usage as MFC TRACE
void pws_os::Trace(LPCTSTR lpszFormat, ...)
{
  openlog("pwsafe:", LOG_PID, LOG_USER);
  va_list args;
  va_start(args, lpszFormat);

  TCHAR* buf = 0;
  int nwritten, len = STARTING_LOG_STATEMENT;
  do {
    len *= 2;
    delete [] buf;
    buf = new TCHAR[len+1];
    memset(buf, 0, sizeof(TCHAR)*(len+1));
    nwritten = vstprintf(buf, len, lpszFormat, args);
    //apple's documentation doesn't say if nwritten is +ve, -ve, 0 or if errno is set in case of overflow
  }
  while(!(nwritten > 0 && nwritten < len) && len <= MAX_LOG_STATEMENT);

#ifdef UNICODE
  size_t N = wcstombs(NULL, buf, 0) + 1;
  char *message = new char[N];
  wcstombs(message, buf, N);
  delete[] buf;
#else
  char* message = buf;
#endif
  
  syslog(LOG_DEBUG, "%s", message);

  delete[] message;
  closelog();

  va_end(args);
}
Пример #2
0
void pws_os::Logit(LPCTSTR lpszFormat, ...)
{
  va_list args;
  va_start(args, lpszFormat);

  TCHAR *szbuffer = 0;
  int nwritten, len = STARTING_LOG_STATEMENT;
  do {
    len *= 2;
    delete [] szbuffer;
    szbuffer = new TCHAR[len + 1];
    memset(szbuffer, 0, sizeof(TCHAR) * (len + 1));
    nwritten = vstprintf(szbuffer, len, lpszFormat, args);
    //apple's documentation doesn't say if nwritten is +ve, -ve, 0 or if errno is set in case of overflow
  }
  while(!(nwritten > 0 && nwritten < len) && len <= MAX_LOG_STATEMENT);

  PWSLog::GetLog()->Add(stringT(szbuffer));
  delete[] szbuffer;
  va_end(args);
}
Пример #3
0
	/********************************************************************
	* [函数名]: formatMsg
	* [描述]: 格式化当前消息m_msg
	* [输入]:
	*   type:信息类型(INFO、WARNING、...)
	*   format: 输出信息格式化字符串
	*   ap: 参数列表
	* [修改记录]:
	*   2015-05-20,littledj: create
	********************************************************************/
	tstring GLogger::formatMsg_v(const PRINT_TYPE type, const tstring& format, va_list ap)
	{
		tstring msg;
		if (format.find('%') != tstring::npos)
		{
			tchar* msg_tmp = new tchar[format.length() + 1024];
			vstprintf(msg_tmp, format.c_str(), ap);
			msg.assign(msg_tmp);
			delete[] msg_tmp;
		}
		else
		{
			msg = format;
		}

		if (type != PRINT_TYPE::RAW)
		{
			msg = PRINT_TYPE_ICON(type) + TEXT("[") + m_header + TEXT("] ") + msg + TEXT("\n");
		}

		// 为了防止溢出,将信息截断为MAX_MSG_LEN
		if (msg.length() > 1000)
		{			
			if (*(--msg.cend()) == '\n')
			{
				msg.erase(msg.begin() + 1000 - 3, msg.end());
				msg.append(TEXT("..\n"));
			}
			else
			{
				msg.erase(msg.begin() + 1000, msg.end());
				msg.append(TEXT("..."));
			}
		}			
		return msg;
	}