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