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
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
#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;
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 std::string &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] = msg.c_str();
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
Christoph Korn c17229
  syslog(Level2XPriority(level), "%s", msg.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 std::string &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 std::string &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 std::string &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 std::string &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 TUserLogAppend::Imp {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  Imp() : m_os(&std::cout), m_streamOwner(false) {}
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  Imp(const TFilePath &fp) : m_streamOwner(true) {
Shinya Kitaoka 120a6e
    TFileStatus fs(fp);
Shinya Kitaoka 120a6e
    if (fs.doesExist())
Shinya Kitaoka 120a6e
      m_os = new Tofstream(fp, true);
Shinya Kitaoka 120a6e
    else
Shinya Kitaoka 120a6e
      m_os = new Tofstream(fp);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  ~Imp() {
Shinya Kitaoka 120a6e
    if (m_streamOwner) delete m_os;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  void write(const std::string &msg);
Shinya Kitaoka 120a6e
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 TUserLogAppend::Imp::write(const std::string &msg) {
Shinya Kitaoka 120a6e
  QMutexLocker sl(&m_mutex);
Shinya Kitaoka 120a6e
  *m_os << msg.c_str();
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
std::string myGetCurrentTime() {
Shinya Kitaoka 120a6e
  QString tmp = QTime::currentTime().toString("hh:mm:ss");
Shinya Kitaoka 120a6e
  return tmp.toStdString();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
}  // namespace
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TUserLogAppend::TUserLogAppend() : m_imp(new Imp()) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TUserLogAppend::TUserLogAppend(const TFilePath &fp) : m_imp(new Imp(fp)) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TUserLogAppend::~TUserLogAppend() {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TUserLogAppend::warning(const std::string &msg) {
Shinya Kitaoka 120a6e
  DVGui::warning(QString::fromStdString(msg));
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  std::string 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 TUserLogAppend::error(const std::string &msg) {
Shinya Kitaoka 120a6e
  DVGui::error(QString::fromStdString(msg));
Shinya Kitaoka 120a6e
  std::string 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 TUserLogAppend::info(const std::string &msg) {
Shinya Kitaoka 120a6e
  std::string fullMsg("");
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
}