From ea67809d9b2882d258b99144557b7227a3907d3d Mon Sep 17 00:00:00 2001 From: Birunthan Mohanathas Date: Fri, 6 Apr 2012 19:54:20 +0300 Subject: [PATCH] Replaced throw with Log where possible --- Library/Measure.cpp | 8 +--- Library/MeasureCalc.cpp | 15 ++------ Library/MeasurePlugin.cpp | 5 +-- Library/MeasureRegistry.cpp | 9 +---- Library/Meter.cpp | 8 +--- Library/MeterBar.cpp | 7 +--- Library/MeterBitmap.cpp | 7 +--- Library/MeterString.cpp | 31 +++------------ Library/MeterWindow.cpp | 75 +++++++++++++------------------------ Library/TintedImage.cpp | 17 ++------- 10 files changed, 46 insertions(+), 136 deletions(-) diff --git a/Library/Measure.cpp b/Library/Measure.cpp index dc99bfc0..bd70e17d 100644 --- a/Library/Measure.cpp +++ b/Library/Measure.cpp @@ -796,13 +796,7 @@ CMeasure* CMeasure::Create(const WCHAR* measure, CMeterWindow* meterWindow, cons return new CMeasureScript(meterWindow, name); } - // Error - std::wstring error = L"Measure="; - error += measure; - error += L" is not valid in ["; - error += name; - error += L']'; - throw CError(error); + LogWithArgs(LOG_ERROR, L"Measure=%s is not valid in [%s]", measure, name); return NULL; } diff --git a/Library/MeasureCalc.cpp b/Library/MeasureCalc.cpp index ed59dd3e..092f6d44 100644 --- a/Library/MeasureCalc.cpp +++ b/Library/MeasureCalc.cpp @@ -60,12 +60,7 @@ bool CMeasureCalc::Update() const WCHAR* errMsg = MathParser::Parse(m_Formula.c_str(), this, &m_Value); if (errMsg != NULL) { - std::wstring error = L"Calc: "; - error += errMsg; - error += L" in ["; - error += m_Name; - error += L']'; - Log(LOG_ERROR, error.c_str()); + LogWithArgs(LOG_ERROR, L"Calc: %s in [%s]", errMsg, m_Name.c_str()); } return PostUpdate(); @@ -105,12 +100,8 @@ void CMeasureCalc::ReadConfig(CConfigParser& parser, const WCHAR* section) const WCHAR* errMsg = MathParser::Check(m_Formula.c_str()); if (errMsg != NULL) { - std::wstring error = L"Calc: "; - error += errMsg; - error += L" in ["; - error += m_Name; - error += L']'; - throw CError(error); + LogWithArgs(LOG_ERROR, L"Calc: %s in [%s]", errMsg, m_Name.c_str()); + m_Formula.clear(); } } } diff --git a/Library/MeasurePlugin.cpp b/Library/MeasurePlugin.cpp index 62465ca5..402a9e1b 100644 --- a/Library/MeasurePlugin.cpp +++ b/Library/MeasurePlugin.cpp @@ -169,9 +169,8 @@ void CMeasurePlugin::ReadConfig(CConfigParser& parser, const WCHAR* section) if (m_Plugin == NULL) { - std::wstring error = L"Plugin: \"" + pluginName; - error += L"\" not found"; - throw CError(error); + LogWithArgs(LOG_ERROR, L"Plugin: \"%s\" not found", pluginName.c_str()); + return; } } diff --git a/Library/MeasureRegistry.cpp b/Library/MeasureRegistry.cpp index c5049b4f..b4366263 100644 --- a/Library/MeasureRegistry.cpp +++ b/Library/MeasureRegistry.cpp @@ -27,7 +27,7 @@ */ CMeasureRegistry::CMeasureRegistry(CMeterWindow* meterWindow, const WCHAR* name) : CMeasure(meterWindow, name), m_RegKey(), - m_HKey() + m_HKey(HKEY_CURRENT_USER) { m_MaxValue = 0.0; } @@ -138,12 +138,7 @@ void CMeasureRegistry::ReadConfig(CConfigParser& parser, const WCHAR* section) } else { - std::wstring error = L"RegHKey="; - error += keyname; - error += L" is not valid in ["; - error += m_Name; - error += L']'; - throw CError(error); + LogWithArgs(LOG_ERROR, L"RegHKey=%s is not valid in [%s]", keyname, m_Name.c_str()); } m_RegKeyName = parser.ReadString(section, L"RegKey", L""); diff --git a/Library/Meter.cpp b/Library/Meter.cpp index 38345428..dcd9c745 100644 --- a/Library/Meter.cpp +++ b/Library/Meter.cpp @@ -488,13 +488,7 @@ CMeter* CMeter::Create(const WCHAR* meter, CMeterWindow* meterWindow, const WCHA return new CMeterButton(meterWindow, name); } - // Error - std::wstring error = L"Meter="; - error += meter; - error += L" is not valid in ["; - error += name; - error += L']'; - throw CError(error); + LogWithArgs(LOG_ERROR, L"Meter=%s is not valid in [%s]", meter, name); return NULL; } diff --git a/Library/MeterBar.cpp b/Library/MeterBar.cpp index e36c253e..7085c1e6 100644 --- a/Library/MeterBar.cpp +++ b/Library/MeterBar.cpp @@ -122,12 +122,7 @@ void CMeterBar::ReadConfig(CConfigParser& parser, const WCHAR* section) } else { - std::wstring error = L"BarOrientation="; - error += orientation; - error += L" is not valid in ["; - error += m_Name; - error += L']'; - throw CError(error); + LogWithArgs(LOG_ERROR, L"BarOrientation=%s is not valid in [%s]", orientation, m_Name.c_str()); } if (m_Initialized) diff --git a/Library/MeterBitmap.cpp b/Library/MeterBitmap.cpp index 0e07b24f..01c616e1 100644 --- a/Library/MeterBitmap.cpp +++ b/Library/MeterBitmap.cpp @@ -201,12 +201,7 @@ void CMeterBitmap::ReadConfig(CConfigParser& parser, const WCHAR* section) } else { - std::wstring error = L"BitmapAlign="; - error += align; - error += L" is not valid in ["; - error += m_Name; - error += L']'; - throw CError(error); + LogWithArgs(LOG_ERROR, L"BitmapAlign=%s is not valid in [%s]", align, m_Name.c_str()); } if (m_Initialized) diff --git a/Library/MeterString.cpp b/Library/MeterString.cpp index ca7edaed..69a34377 100644 --- a/Library/MeterString.cpp +++ b/Library/MeterString.cpp @@ -191,8 +191,7 @@ void CMeterString::Initialize() // It couldn't find the font family: Log it. if (Ok != status) { - std::wstring error = L"String: Unable to load font: " + m_FontFace; - Log(LOG_ERROR, error.c_str()); + LogWithArgs(LOG_ERROR, L"String: Unable to load font: %s", m_FontFace.c_str()); delete m_FontFamily; m_FontFamily = NULL; @@ -357,12 +356,7 @@ void CMeterString::ReadConfig(CConfigParser& parser, const WCHAR* section) } else { - std::wstring error = L"StringAlign="; - error += align; - error += L" is not valid in ["; - error += m_Name; - error += L']'; - throw CError(error); + LogWithArgs(LOG_ERROR, L"StringAlign=%s is not valid in [%s]", align, m_Name.c_str()); } const WCHAR* stringCase = parser.ReadString(section, L"StringCase", L"NONE").c_str(); @@ -384,12 +378,7 @@ void CMeterString::ReadConfig(CConfigParser& parser, const WCHAR* section) } else { - std::wstring error = L"StringCase="; - error += stringCase; - error += L" is not valid in ["; - error += m_Name; - error += L']'; - throw CError(error); + LogWithArgs(LOG_ERROR, L"StringCase=%s is not valid in [%s]", stringCase, m_Name.c_str()); } const WCHAR* style = parser.ReadString(section, L"StringStyle", L"NORMAL").c_str(); @@ -411,12 +400,7 @@ void CMeterString::ReadConfig(CConfigParser& parser, const WCHAR* section) } else { - std::wstring error = L"StringStyle="; - error += style; - error += L" is not valid in ["; - error += m_Name; - error += L']'; - throw CError(error); + LogWithArgs(LOG_ERROR, L"StringStyle=%s is not valid in [%s]", style, m_Name.c_str()); } const WCHAR* effect = parser.ReadString(section, L"StringEffect", L"NONE").c_str(); @@ -434,12 +418,7 @@ void CMeterString::ReadConfig(CConfigParser& parser, const WCHAR* section) } else { - std::wstring error = L"StringEffect="; - error += effect; - error += L" is not valid in ["; - error += m_Name; - error += L']'; - throw CError(error); + LogWithArgs(LOG_ERROR, L"StringEffect=%s is not valid in [%s]", effect, m_Name.c_str()); } if (m_Initialized && diff --git a/Library/MeterWindow.cpp b/Library/MeterWindow.cpp index a28e378e..8585eaa3 100644 --- a/Library/MeterWindow.cpp +++ b/Library/MeterWindow.cpp @@ -157,6 +157,17 @@ CMeterWindow::CMeterWindow(const std::wstring& config, const std::wstring& iniFi } } + if (c_InstanceCount == 0) + { + WNDCLASSEX wc = {sizeof(WNDCLASSEX)}; + wc.style = CS_NOCLOSE | CS_DBLCLKS; + wc.lpfnWndProc = InitialWndProc; + wc.hInstance = m_Rainmeter->GetInstance(); + wc.hCursor = NULL; // The cursor should be controlled by using SetCursor() when needed. + wc.lpszClassName = METERWINDOW_CLASS_NAME; + RegisterClassEx(&wc); + } + ++c_InstanceCount; } @@ -211,16 +222,7 @@ CMeterWindow::~CMeterWindow() if (c_InstanceCount == 0) { - BOOL Result; - int counter = 0; - do - { - // Wait for the window to die - Result = UnregisterClass(METERWINDOW_CLASS_NAME, m_Rainmeter->GetInstance()); - Sleep(100); - ++counter; - } - while(!Result && counter < 10); + UnregisterClass(METERWINDOW_CLASS_NAME, m_Rainmeter->GetInstance()); if (c_DwmInstance) { @@ -243,36 +245,19 @@ int CMeterWindow::Initialize(CRainmeter& Rainmeter) { m_Rainmeter = &Rainmeter; - // Register the windowclass - WNDCLASSEX wc = {sizeof(WNDCLASSEX)}; - wc.style = CS_NOCLOSE | CS_DBLCLKS; - wc.lpfnWndProc = InitialWndProc; - wc.hInstance = m_Rainmeter->GetInstance(); - wc.hCursor = NULL; // The cursor should be controlled by using SetCursor() when needed. - wc.lpszClassName = METERWINDOW_CLASS_NAME; - - if (!RegisterClassEx(&wc)) - { - DWORD err = GetLastError(); - - if (err != 0 && ERROR_CLASS_ALREADY_EXISTS != err) - { - throw CError(L"Unable to register class"); - } - } - - m_Window = CreateWindowEx(WS_EX_TOOLWINDOW, - METERWINDOW_CLASS_NAME, - NULL, - WS_POPUP, - CW_USEDEFAULT, - CW_USEDEFAULT, - CW_USEDEFAULT, - CW_USEDEFAULT, - NULL, - NULL, - m_Rainmeter->GetInstance(), - this); + m_Window = CreateWindowEx( + WS_EX_TOOLWINDOW, + METERWINDOW_CLASS_NAME, + NULL, + WS_POPUP, + CW_USEDEFAULT, + CW_USEDEFAULT, + CW_USEDEFAULT, + CW_USEDEFAULT, + NULL, + NULL, + m_Rainmeter->GetInstance(), + this); if (m_Window == NULL) { @@ -462,16 +447,10 @@ void CMeterWindow::Refresh(bool init, bool all) // Start the timers if (m_WindowUpdate >= 0) { - if (0 == SetTimer(m_Window, TIMER_METER, m_WindowUpdate, NULL)) - { - throw CError(L"Unable to set timer"); - } + SetTimer(m_Window, TIMER_METER, m_WindowUpdate, NULL); } - if (0 == SetTimer(m_Window, TIMER_MOUSE, INTERVAL_MOUSE, NULL)) // Mouse position is checked twice per sec - { - throw CError(L"Unable to set timer"); - } + SetTimer(m_Window, TIMER_MOUSE, INTERVAL_MOUSE, NULL); m_Rainmeter->SetCurrentParser(NULL); diff --git a/Library/TintedImage.cpp b/Library/TintedImage.cpp index f4b8b42d..9aaf52ec 100644 --- a/Library/TintedImage.cpp +++ b/Library/TintedImage.cpp @@ -623,13 +623,8 @@ void CTintedImage::ReadConfig(CConfigParser& parser, const WCHAR* section) if (m_CropMode < CROPMODE_TL || m_CropMode > CROPMODE_C) { - std::wstring error = m_ConfigArray[ConfigIndexImageCrop]; - error += L'='; - error += crop; - error += L" (origin) is not valid in ["; - error += section; - error += L']'; - throw CError(error); + m_CropMode = CROPMODE_TL; + LogWithArgs(LOG_ERROR, L"%s=%s (origin) is not valid in [%s]", m_ConfigArray[ConfigIndexImageCrop], crop, section); } } } @@ -731,13 +726,7 @@ void CTintedImage::ReadConfig(CConfigParser& parser, const WCHAR* section) } else { - std::wstring error = m_ConfigArray[ConfigIndexImageFlip]; - error += L'='; - error += flip; - error += L" is not valid in ["; - error += section; - error += L']'; - throw CError(error); + LogWithArgs(LOG_ERROR, L"%s=%s (origin) is not valid in [%s]", m_ConfigArray[ConfigIndexImageFlip], flip, section); } if (!m_DisableTransform)