Modified the behavior when the log file was deleted manually.

This commit is contained in:
spx 2010-07-08 10:59:06 +00:00
parent 05c14bfa26
commit d07e8665b0
4 changed files with 114 additions and 73 deletions

View File

@ -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);
}
}
}

View File

@ -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;

View File

@ -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);

View File

@ -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)
{