diff --git a/telldus-core/service/CMakeLists.txt b/telldus-core/service/CMakeLists.txt index a3109036..8fc6125c 100644 --- a/telldus-core/service/CMakeLists.txt +++ b/telldus-core/service/CMakeLists.txt @@ -137,10 +137,21 @@ ELSEIF (WIN32) #### Windows #### main_win.cpp SettingsWinRegistry.cpp TelldusWinService_win.cpp + Messages.mc + ${CMAKE_CURRENT_BINARY_DIR}/Messages.rc + ${CMAKE_CURRENT_BINARY_DIR}/Messages.h ) LIST(APPEND telldus-service_HDRS TelldusWinService_win.h ) + ADD_CUSTOM_COMMAND( + OUTPUT Messages.rc Messages.h + COMMAND mc.exe -u -r ${CMAKE_CURRENT_BINARY_DIR} -h ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/Messages.mc + DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/Messages.mc + DEPENDS Messages.rc + COMMENT "Compiling Messages Resource" + ) + INCLUDE_DIRECTORIES( ${CMAKE_CURRENT_BINARY_DIR} ) ELSE (APPLE) #### Linux #### SET(DEFAULT_FTDI_ENGINE "libftdi") diff --git a/telldus-core/service/Log.cpp b/telldus-core/service/Log.cpp index 318bcb16..0282f9de 100644 --- a/telldus-core/service/Log.cpp +++ b/telldus-core/service/Log.cpp @@ -1,8 +1,12 @@ #include "Log.h" #include -#ifdef _LINUX +#if defined(_LINUX) #include +#elif defined(_WINDOWS) +#include +#include "Strings.h" +#include "Messages.h" #endif class Log::PrivateData { @@ -12,6 +16,9 @@ public: Log::LogOutput logOutput; static Log *instance; +#ifdef _WINDOWS + HANDLE eventSource; +#endif }; Log *Log::PrivateData::instance = 0; @@ -19,17 +26,50 @@ Log *Log::PrivateData::instance = 0; Log::Log() :d(new PrivateData) { -#ifdef _LINUX +#if defined(_LINUX) setlogmask(LOG_UPTO(LOG_INFO)); openlog("telldusd", LOG_CONS, LOG_USER); +#elif defined(_WINDOWS) + //Add ourselves to the registy + HKEY hRegKey = NULL; + DWORD dwError = 0; + TCHAR filePath[MAX_PATH]; + + std::wstring path(L"SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\TelldusService"); + dwError = RegCreateKey( HKEY_LOCAL_MACHINE, path.c_str(), &hRegKey ); + + GetModuleFileName( NULL, filePath, MAX_PATH ); + dwError = RegSetValueEx( hRegKey, L"EventMessageFile", 0, + REG_EXPAND_SZ, (PBYTE) filePath, + (DWORD)(wcslen(filePath) + 1) * sizeof TCHAR ); + + DWORD dwTypes = LOG_DEBUG | LOG_NOTICE | LOG_WARNING | LOG_ERR; + dwError = RegSetValueEx( hRegKey, L"TypesSupported", + 0, REG_DWORD, (LPBYTE) &dwTypes, sizeof dwTypes ); + + RegCloseKey(hRegKey); + + d->eventSource = RegisterEventSource(NULL, L"TelldusService"); #endif } Log::~Log() { - delete d; -#ifdef _LINUX +#if defined(_LINUX) closelog(); +#elif defined(_WINDOWS) + if (d->eventSource != NULL) { + DeregisterEventSource(d->eventSource); + } #endif + delete d; +} + +void Log::destroy() { + if (PrivateData::instance == 0) { + return; + } + delete PrivateData::instance; + PrivateData::instance = 0; } void Log::debug(const char *fmt, ...) { @@ -79,7 +119,7 @@ void Log::message(Log::LogLevel logLevel, const char *format, va_list ap) const fprintf(stream, "\n"); fflush(stream); } else { -#ifdef _LINUX +#if defined(_LINUX) switch (logLevel) { case Debug: vsyslog(LOG_DEBUG, format, ap); @@ -94,6 +134,25 @@ void Log::message(Log::LogLevel logLevel, const char *format, va_list ap) const vsyslog(LOG_ERR, format, ap); break; } +#elif defined(_WINDOWS) + LPWSTR pInsertStrings[2] = {NULL, NULL}; + std::wstring str = TelldusCore::charToWstring(format); + pInsertStrings[0] = (LPWSTR)str.c_str(); + + switch (logLevel) { + case Debug: + ReportEvent(d->eventSource, EVENTLOG_SUCCESS, NULL, LOG_DEBUG, NULL, 1, 0, (LPCWSTR*)pInsertStrings, NULL); + break; + case Notice: + ReportEvent(d->eventSource, EVENTLOG_INFORMATION_TYPE, NULL, LOG_NOTICE, NULL, 1, 0, (LPCWSTR*)pInsertStrings, NULL); + break; + case Warning: + ReportEvent(d->eventSource, EVENTLOG_WARNING_TYPE, NULL, LOG_WARNING, NULL, 1, 0, (LPCWSTR*)pInsertStrings, NULL); + break; + case Error: + ReportEvent(d->eventSource, EVENTLOG_ERROR_TYPE, NULL, LOG_ERR, NULL, 1, 0, (LPCWSTR*)pInsertStrings, NULL); + break; + } #endif } } diff --git a/telldus-core/service/Log.h b/telldus-core/service/Log.h index 3fa199f5..4b10728d 100644 --- a/telldus-core/service/Log.h +++ b/telldus-core/service/Log.h @@ -9,6 +9,8 @@ public: enum LogOutput { StdOut, System }; virtual ~Log(); + static void destroy(); + static void debug(const char *fmt, ...); static void notice(const char *fmt, ...); static void warning(const char *fmt, ...); diff --git a/telldus-core/service/Messages.mc b/telldus-core/service/Messages.mc new file mode 100644 index 00000000..b7f81b41 Binary files /dev/null and b/telldus-core/service/Messages.mc differ diff --git a/telldus-core/service/TelldusWinService_win.cpp b/telldus-core/service/TelldusWinService_win.cpp index a7c48322..3deeba9a 100644 --- a/telldus-core/service/TelldusWinService_win.cpp +++ b/telldus-core/service/TelldusWinService_win.cpp @@ -1,5 +1,6 @@ #include "TelldusWinService_win.h" #include "TelldusMain.h" +#include "Log.h" #include #include @@ -140,8 +141,13 @@ void WINAPI TelldusWinService::serviceMain( DWORD argc, TCHAR* argv[] ) { devInterface.dbcc_classguid = GUID_DEVINTERFACE_USBRAW; HDEVNOTIFY deviceNotificationHandle = RegisterDeviceNotificationW(instance.serviceStatusHandle, &devInterface, DEVICE_NOTIFY_SERVICE_HANDLE); + Log::notice("TelldusService started"); + //Start our main-loop instance.tm->start(); + + Log::notice("TelldusService stopping"); + Log::destroy(); // service was stopped instance.serviceStatus.dwCurrentState = SERVICE_STOP_PENDING;