Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "toonz/tlog.h"
Toshihiro Shimizu 890ddd
//#include "tfilepath.h"
Toshihiro Shimizu 890ddd
#include "tfilepath_io.h"
Toshihiro Shimizu 890ddd
Campbell Barton d0e335
#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
#include "tmsgcore.h"
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
Shinya Kitaoka 3bfa54
void notify(LEVEL level, const std::string &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] = msg.c_str();
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
	syslog(Level2XPriority(level), msg.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
Shinya Kitaoka 3bfa54
void TSysLog::success(const std::string &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
Shinya Kitaoka 3bfa54
void TSysLog::warning(const std::string &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
Shinya Kitaoka 3bfa54
void TSysLog::error(const std::string &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
Shinya Kitaoka 3bfa54
void TSysLog::info(const std::string &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 TUserLogAppend::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_streamOwner(true)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		TFileStatus fs(fp);
Toshihiro Shimizu 890ddd
		if (fs.doesExist())
Toshihiro Shimizu 890ddd
			m_os = new Tofstream(fp, true);
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			m_os = new Tofstream(fp);
Toshihiro Shimizu 890ddd
	}
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
Shinya Kitaoka 3bfa54
	void write(const std::string &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
Shinya Kitaoka 3bfa54
void TUserLogAppend::Imp::write(const std::string &msg)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QMutexLocker sl(&m_mutex);
Toshihiro Shimizu 890ddd
	*m_os << msg.c_str();
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
Shinya Kitaoka 3bfa54
std::string myGetCurrentTime()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QString tmp = QTime::currentTime().toString("hh:mm:ss");
Toshihiro Shimizu 890ddd
	return tmp.toStdString();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
} // namespace
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TUserLogAppend::TUserLogAppend() : m_imp(new Imp())
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TUserLogAppend::TUserLogAppend(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
TUserLogAppend::~TUserLogAppend()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
void TUserLogAppend::warning(const std::string &msg)
Toshihiro Shimizu 890ddd
{
Shinya Kitaoka eabf18
	DVGui::warning(QString::fromStdString(msg));
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
	std::string 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
Shinya Kitaoka 3bfa54
void TUserLogAppend::error(const std::string &msg)
Toshihiro Shimizu 890ddd
{
Shinya Kitaoka eabf18
	DVGui::error(QString::fromStdString(msg));
Shinya Kitaoka 3bfa54
	std::string 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
Shinya Kitaoka 3bfa54
void TUserLogAppend::info(const std::string &msg)
Toshihiro Shimizu 890ddd
{
Shinya Kitaoka 3bfa54
	std::string fullMsg("");
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
}