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