Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tlog.h"
Toshihiro Shimizu 890ddd
//#include "tfilepath.h"
Toshihiro Shimizu 890ddd
#include "tfilepath_io.h"
Toshihiro Shimizu 890ddd
#include <qdatetime></qdatetime>
Toshihiro Shimizu 890ddd
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Toshihiro Shimizu 890ddd
#pragma warning(disable : 4996)
Toshihiro Shimizu 890ddd
#include <windows.h></windows.h>
Toshihiro Shimizu 890ddd
#include <stdio.h></stdio.h>
Toshihiro Shimizu 890ddd
#include <stdlib.h></stdlib.h>
Toshihiro Shimizu 890ddd
#include <tchar.h></tchar.h>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
#include <syslog.h></syslog.h>
Toshihiro Shimizu 890ddd
#include <unistd.h></unistd.h>
Toshihiro Shimizu 890ddd
#include <sys timeb.h=""></sys>
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tthreadmessage.h"
Toshihiro Shimizu 890ddd
#include "tsystem.h"
Toshihiro Shimizu 890ddd
#include <fstream></fstream>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
using namespace TSysLog;
Toshihiro Shimizu 890ddd
namespace
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
enum LEVEL {
Toshihiro Shimizu 890ddd
	LEVEL_SUCCESS,
Toshihiro Shimizu 890ddd
	LEVEL_ERROR,
Toshihiro Shimizu 890ddd
	LEVEL_WARNING,
Toshihiro Shimizu 890ddd
	LEVEL_INFO
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Toshihiro Shimizu 890ddd
WORD Level2WinEventType(LEVEL level)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	switch (level) {
Toshihiro Shimizu 890ddd
	case LEVEL_SUCCESS:
Toshihiro Shimizu 890ddd
		return EVENTLOG_SUCCESS; // Success event
Toshihiro Shimizu 890ddd
	case LEVEL_ERROR:
Toshihiro Shimizu 890ddd
		return EVENTLOG_ERROR_TYPE; // Error event
Toshihiro Shimizu 890ddd
	case LEVEL_WARNING:
Toshihiro Shimizu 890ddd
		return EVENTLOG_WARNING_TYPE; //Warning event
Toshihiro Shimizu 890ddd
	case LEVEL_INFO:
Toshihiro Shimizu 890ddd
		return EVENTLOG_INFORMATION_TYPE; // Information event
Toshihiro Shimizu 890ddd
										  //      case : return EVENTLOG_AUDIT_SUCCESS Success audit event
Toshihiro Shimizu 890ddd
										  //      case : return EVENTLOG_AUDIT_FAILURE Failure audit event
Toshihiro Shimizu 890ddd
	default:
Toshihiro Shimizu 890ddd
		return LEVEL_WARNING;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
int Level2XPriority(LEVEL level)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	switch (level) {
Toshihiro Shimizu 890ddd
	case LEVEL_SUCCESS:
Toshihiro Shimizu 890ddd
		return LOG_INFO;
Toshihiro Shimizu 890ddd
	case LEVEL_ERROR:
Toshihiro Shimizu 890ddd
		return LOG_ERR; // Errors.
Toshihiro Shimizu 890ddd
	case LEVEL_WARNING:
Toshihiro Shimizu 890ddd
		return LOG_WARNING; // Warning messages.
Toshihiro Shimizu 890ddd
	case LEVEL_INFO:
Toshihiro Shimizu 890ddd
		return LOG_INFO; // Informational messages.
Toshihiro Shimizu 890ddd
	default:
Toshihiro Shimizu 890ddd
		return LEVEL_WARNING;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void notify(LEVEL level, const QString &msg)
Toshihiro Shimizu 890ddd
{
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Toshihiro Shimizu 890ddd
	TCHAR buf[_MAX_PATH + 1];
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	GetModuleFileName(0, buf, _MAX_PATH);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	HANDLE handle = RegisterEventSource(NULL,							   // uses local computer
Toshihiro Shimizu 890ddd
										TFilePath(buf).getName().c_str()); // source name
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	LPCTSTR lpszStrings[2];
Toshihiro Shimizu 890ddd
	TCHAR szMsg[256];
Toshihiro Shimizu 890ddd
	DWORD dwErr = 1;
Toshihiro Shimizu 890ddd
	_stprintf(szMsg, TEXT("%s error: %d"), "appname", dwErr);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	lpszStrings[0] =
Toshihiro Shimizu 890ddd
		lpszStrings[1] = (LPCTSTR)msg.data();
Toshihiro Shimizu 890ddd
	ReportEvent(handle,					   // event log handle
Toshihiro Shimizu 890ddd
				Level2WinEventType(level), // event type
Toshihiro Shimizu 890ddd
				0,						   // category zero
Toshihiro Shimizu 890ddd
				dwErr,					   // event identifier
Toshihiro Shimizu 890ddd
				NULL,					   // no user security identifier
Toshihiro Shimizu 890ddd
				1,						   // one substitution string
Toshihiro Shimizu 890ddd
				0,						   // no data
Toshihiro Shimizu 890ddd
				lpszStrings,			   // pointer to string array
Toshihiro Shimizu 890ddd
				NULL);					   // pointer to data
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	DeregisterEventSource(handle);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
	std::string str = msg.toStdString();
Toshihiro Shimizu 890ddd
	syslog(Level2XPriority(level), str.c_str());
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
static TThread::Mutex MyMutex;
Toshihiro Shimizu 890ddd
} //namespace
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TSysLog::success(const QString &msg)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QMutexLocker sl(&MyMutex);
Toshihiro Shimizu 890ddd
	notify(LEVEL_SUCCESS, msg);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TSysLog::warning(const QString &msg)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QMutexLocker sl(&MyMutex);
Toshihiro Shimizu 890ddd
	notify(LEVEL_WARNING, msg);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TSysLog::error(const QString &msg)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QMutexLocker sl(&MyMutex);
Toshihiro Shimizu 890ddd
	notify(LEVEL_ERROR, msg);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TSysLog::info(const QString &msg)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QMutexLocker sl(&MyMutex);
Toshihiro Shimizu 890ddd
	notify(LEVEL_INFO, msg);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//==============================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class TUserLog::Imp
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	Imp() : m_os(&std::cout), m_streamOwner(false) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	Imp(const TFilePath &fp)
Toshihiro Shimizu 890ddd
		: m_os(new Tofstream(fp)), m_streamOwner(true) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	~Imp()
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		if (m_streamOwner)
Toshihiro Shimizu 890ddd
			delete m_os;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void write(const QString &msg);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TThread::Mutex m_mutex;
Toshihiro Shimizu 890ddd
	std::ostream *m_os;
Toshihiro Shimizu 890ddd
	bool m_streamOwner;
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TUserLog::Imp::write(const QString &msg)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QMutexLocker sl(&MyMutex);
Toshihiro Shimizu 890ddd
	*m_os << msg.toStdString();
Toshihiro Shimizu 890ddd
	m_os->flush();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
namespace
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QString myGetCurrentTime()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return QDateTime::currentDateTime().toString();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
} // namespace
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TUserLog::TUserLog() : m_imp(new Imp())
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TUserLog::TUserLog(const TFilePath &fp) : m_imp(new Imp(fp))
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TUserLog::~TUserLog()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TUserLog::warning(const QString &msg)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QString fullMsg(myGetCurrentTime());
Toshihiro Shimizu 890ddd
	fullMsg += " WRN:";
Toshihiro Shimizu 890ddd
	fullMsg += "\n";
Toshihiro Shimizu 890ddd
	fullMsg += msg;
Toshihiro Shimizu 890ddd
	fullMsg += "\n";
Toshihiro Shimizu 890ddd
	m_imp->write(fullMsg);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TUserLog::error(const QString &msg)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QString fullMsg(myGetCurrentTime());
Toshihiro Shimizu 890ddd
	fullMsg += " ERR:";
Toshihiro Shimizu 890ddd
	fullMsg += "\n";
Toshihiro Shimizu 890ddd
	fullMsg += msg;
Toshihiro Shimizu 890ddd
	fullMsg += "\n";
Toshihiro Shimizu 890ddd
	m_imp->write(fullMsg);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TUserLog::info(const QString &msg)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QString fullMsg(myGetCurrentTime());
Toshihiro Shimizu 890ddd
	fullMsg += " INF:";
Toshihiro Shimizu 890ddd
	fullMsg += "\n";
Toshihiro Shimizu 890ddd
	fullMsg += msg;
Toshihiro Shimizu 890ddd
	fullMsg += "\n";
Toshihiro Shimizu 890ddd
	m_imp->write(fullMsg);
Toshihiro Shimizu 890ddd
}