diff --git a/Library/Litestep.cpp b/Library/Litestep.cpp index 9e68ce98..40c6d8a5 100644 --- a/Library/Litestep.cpp +++ b/Library/Litestep.cpp @@ -538,50 +538,55 @@ BOOL LSLog(int nLevel, LPCTSTR pszModule, LPCTSTR pszMessage) // The stub implementation if (Rainmeter && Rainmeter->GetLogging()) { - FILE* logFile; std::wstring logfile = Rainmeter->GetLogFile(); if (logFound == 0) { // Check if the file exists - logFile = _wfopen(logfile.c_str(), L"r"); - if (logFile) + if (_waccess(logfile.c_str(), 0) != -1) { logFound = 1; - fclose(logFile); // Clear the file - logFile = _wfopen(logfile.c_str(), L"w"); + FILE* logFile = _wfopen(logfile.c_str(), L"w"); fclose(logFile); } else { - logFound = 2; + logFound = 2; // not found } } if (logFound == 1) { - logFile = _wfopen(logfile.c_str(), L"a+, ccs=UTF-8"); - if (logFile) + if (_waccess(logfile.c_str(), 0) == -1) { - switch(nLevel) + // Disable logging if the file was deleted manually + Rainmeter->StopLogging(); + } + else + { + FILE* logFile = _wfopen(logfile.c_str(), L"a+, ccs=UTF-8"); + if (logFile) { - case 1: - fputws(L"ERROR: ", logFile); - break; - case 2: - fputws(L"WARNING: ", logFile); - break; - case 3: - fputws(L"NOTICE: ", logFile); - break; - case 4: - fputws(L"DEBUG: ", logFile); - break; + switch(nLevel) + { + case 1: + fputws(L"ERROR: ", logFile); + break; + case 2: + fputws(L"WARNING: ", logFile); + break; + case 3: + fputws(L"NOTICE: ", logFile); + break; + case 4: + fputws(L"DEBUG: ", logFile); + break; + } + fputws(message.c_str(), logFile); + fputws(L"\n", logFile); + fclose(logFile); } - fputws(message.c_str(), logFile); - fputws(L"\n", logFile); - fclose(logFile); } } } diff --git a/Library/Rainmeter.cpp b/Library/Rainmeter.cpp index 023c30ba..3556055f 100644 --- a/Library/Rainmeter.cpp +++ b/Library/Rainmeter.cpp @@ -913,6 +913,11 @@ int CRainmeter::Initialize(HWND Parent, HINSTANCE Instance, LPCSTR szPath) m_Logging = 0!=GetPrivateProfileInt(L"Rainmeter", L"Logging", 0, m_IniFile.c_str()); c_Debug = 0!=GetPrivateProfileInt(L"Rainmeter", L"Debug", 0, m_IniFile.c_str()); + if (m_Logging) + { + StartLogging(); + } + m_PluginPath = tmpName; m_PluginPath += L"Plugins\\"; m_SkinPath = m_Path + L"Skins\\"; @@ -2086,6 +2091,11 @@ void CRainmeter::ReadGeneralSettings(std::wstring& iniFile) m_Logging = 0!=parser.ReadInt(L"Rainmeter", L"Logging", 0); c_Debug = 0!=parser.ReadInt(L"Rainmeter", L"Debug", 0); + if (m_Logging) + { + StartLogging(); + } + if (m_TrayWindow) { m_TrayWindow->ReadConfig(parser); @@ -2404,15 +2414,18 @@ void CRainmeter::ShowContextMenu(POINT pos, CMeterWindow* meterWindow) { EnableMenuItem(subMenu, ID_CONTEXT_SHOWLOGFILE, MF_BYCOMMAND | MF_GRAYED); EnableMenuItem(subMenu, ID_CONTEXT_DELETELOGFILE, MF_BYCOMMAND | MF_GRAYED); - } - - if (m_Logging) - { - EnableMenuItem(subMenu, ID_CONTEXT_STARTLOG, MF_BYCOMMAND | MF_GRAYED); + EnableMenuItem(subMenu, ID_CONTEXT_STOPLOG, MF_BYCOMMAND | MF_GRAYED); } else { - EnableMenuItem(subMenu, ID_CONTEXT_STOPLOG, MF_BYCOMMAND | MF_GRAYED); + if (m_Logging) + { + EnableMenuItem(subMenu, ID_CONTEXT_STARTLOG, MF_BYCOMMAND | MF_GRAYED); + } + else + { + EnableMenuItem(subMenu, ID_CONTEXT_STOPLOG, MF_BYCOMMAND | MF_GRAYED); + } } if (c_Debug) @@ -2788,6 +2801,64 @@ void CRainmeter::ChangeSkinIndex(HMENU menu, int index) } } +void CRainmeter::StartLogging() +{ + // Check if the file exists + if (_waccess(m_LogFile.c_str(), 0) == -1) + { + // Create log file + HANDLE file = CreateFile(m_LogFile.c_str(), GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL); + if (file != INVALID_HANDLE_VALUE) + { + CloseHandle(file); + ResetLoggingFlag(); // Re-enable logging + SetLogging(true); + + std::wstring message = L"Log file created at: "; + message += m_LogFile; + MessageBox(NULL, message.c_str(), L"Rainmeter", MB_OK | MB_ICONINFORMATION); + } + else + { + // Disable logging + SetLogging(false); + ResetLoggingFlag(); + + std::wstring message = L"Unable to create log file: "; + message += m_LogFile; + MessageBox(NULL, message.c_str(), L"Rainmeter", MB_OK | MB_ICONERROR); + } + } + else + { + SetLogging(true); + } +} + +void CRainmeter::StopLogging() +{ + SetLogging(false); +} + +void CRainmeter::DeleteLogFile() +{ + // Check if the file exists + if (_waccess(m_LogFile.c_str(), 0) != -1) + { + std::wstring message = L"Do you want to delete the following log file?\n"; + message += m_LogFile; + int res = MessageBox(NULL, message.c_str(), L"Rainmeter", MB_YESNO | MB_ICONQUESTION); + if (res == IDYES) + { + // Disable logging + SetLogging(false); + ResetLoggingFlag(); + + DeleteFile(m_LogFile.c_str()); + } + } +} + void CRainmeter::SetLogging(bool logging) { m_Logging = logging; diff --git a/Library/Rainmeter.h b/Library/Rainmeter.h index 2eea8c24..c5b434cf 100644 --- a/Library/Rainmeter.h +++ b/Library/Rainmeter.h @@ -163,8 +163,11 @@ public: void SetDisableVersionCheck(BOOL check) { m_DisableVersionCheck = check; }; void SetNewVersion(BOOL NewVer) { m_NewVersion = NewVer; }; - void SetLogging(bool logging); bool GetLogging() { return m_Logging; } + void StartLogging(); + void StopLogging(); + void DeleteLogFile(); + void SetDebug(bool debug); void ShowContextMenu(POINT pos, CMeterWindow* meterWindow); @@ -205,6 +208,7 @@ private: HMENU CreateThemeMenu(); void CreateMonitorMenu(HMENU monitorMenu, CMeterWindow* meterWindow); void CreateDefaultConfigFile(std::wstring strFile); + void SetLogging(bool logging); void TestSettingsFile(bool bDefaultIniLocation); void CheckSkinVersions(); int CompareVersions(std::wstring strA, std::wstring strB); diff --git a/Library/TrayWindow.cpp b/Library/TrayWindow.cpp index 26f25bd3..199c8839 100644 --- a/Library/TrayWindow.cpp +++ b/Library/TrayWindow.cpp @@ -430,54 +430,15 @@ LRESULT CALLBACK CTrayWindow::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARA } else if(wParam == ID_CONTEXT_STARTLOG) { - // Check if the file exists - std::wstring log = Rainmeter->GetLogFile(); - if (_waccess(log.c_str(), 0) == -1) - { - // Create log file - HANDLE file = CreateFile(log.c_str(), GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL); - if (file != INVALID_HANDLE_VALUE) - { - CloseHandle(file); - ResetLoggingFlag(); // Re-enable logging - Rainmeter->SetLogging(true); - - std::wstring message; - message = L"Log file created at: "; - message += log; - MessageBox(tray->GetWindow(), message.c_str(), L"Rainmeter", MB_OK | MB_ICONINFORMATION); - } - else - { - std::wstring message; - message = L"Unable to create log file: "; - message += log; - MessageBox(tray->GetWindow(), message.c_str(), L"Rainmeter", MB_OK | MB_ICONERROR); - } - } - else - { - Rainmeter->SetLogging(true); - } + Rainmeter->StartLogging(); } else if(wParam == ID_CONTEXT_STOPLOG) { - Rainmeter->SetLogging(false); + Rainmeter->StopLogging(); } else if(wParam == ID_CONTEXT_DELETELOGFILE) { - // Check if the file exists - std::wstring log = Rainmeter->GetLogFile(); - if (_waccess(log.c_str(), 0) != -1) - { - int res = MessageBox(tray->GetWindow(), L"Do you want to delete log file?", L"Rainmeter", MB_YESNO | MB_ICONQUESTION); - if (res == IDYES) - { - Rainmeter->SetLogging(false); - ResetLoggingFlag(); - DeleteFile(log.c_str()); - } - } + Rainmeter->DeleteLogFile(); } else if(wParam == ID_CONTEXT_DEBUGLOG) {