2009-02-10 18:37:48 +00:00
|
|
|
/*
|
|
|
|
Copyright (C) 2002 Kimmo Pekkola + few lsapi developers
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
2012-01-23 06:36:15 +00:00
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2009-02-10 18:37:48 +00:00
|
|
|
*/
|
|
|
|
|
2009-10-07 16:45:14 +00:00
|
|
|
#include "StdAfx.h"
|
2009-02-10 18:37:48 +00:00
|
|
|
#include "Litestep.h"
|
2009-07-21 12:26:50 +00:00
|
|
|
#include "Rainmeter.h"
|
2011-09-08 17:05:48 +00:00
|
|
|
#include "DialogAbout.h"
|
2010-09-11 19:39:45 +00:00
|
|
|
#include "System.h"
|
2009-02-10 18:37:48 +00:00
|
|
|
|
2009-07-21 12:26:50 +00:00
|
|
|
extern CRainmeter* Rainmeter;
|
|
|
|
|
2011-08-31 11:06:35 +00:00
|
|
|
static CRITICAL_SECTION g_CsLog = {0};
|
|
|
|
static CRITICAL_SECTION g_CsLogDelay = {0};
|
|
|
|
|
2009-02-10 18:37:48 +00:00
|
|
|
void InitalizeLitestep()
|
|
|
|
{
|
2011-08-31 11:06:35 +00:00
|
|
|
InitializeCriticalSection(&g_CsLog);
|
|
|
|
InitializeCriticalSection(&g_CsLogDelay);
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
|
|
|
|
2011-08-31 11:06:35 +00:00
|
|
|
void FinalizeLitestep()
|
|
|
|
{
|
|
|
|
DeleteCriticalSection(&g_CsLog);
|
|
|
|
DeleteCriticalSection(&g_CsLogDelay);
|
|
|
|
}
|
|
|
|
|
2012-04-09 16:45:54 +00:00
|
|
|
UINT GetUniqueID()
|
|
|
|
{
|
|
|
|
static UINT id = 0;
|
|
|
|
return id++;
|
|
|
|
}
|
|
|
|
|
2012-05-29 16:02:20 +00:00
|
|
|
void RunCommand(std::wstring command)
|
2009-07-21 12:26:50 +00:00
|
|
|
{
|
2009-02-10 18:37:48 +00:00
|
|
|
std::wstring args;
|
|
|
|
|
2011-07-18 00:32:09 +00:00
|
|
|
size_t notwhite = command.find_first_not_of(L" \t\r\n");
|
2009-02-10 18:37:48 +00:00
|
|
|
command.erase(0, notwhite);
|
|
|
|
|
2011-08-28 10:58:26 +00:00
|
|
|
size_t quotePos = command.find(L'"');
|
2009-02-10 18:37:48 +00:00
|
|
|
if (quotePos == 0)
|
|
|
|
{
|
2011-08-28 10:58:26 +00:00
|
|
|
size_t quotePos2 = command.find(L'"', quotePos + 1);
|
2009-02-10 18:37:48 +00:00
|
|
|
if (quotePos2 != std::wstring::npos)
|
|
|
|
{
|
2011-07-14 00:26:53 +00:00
|
|
|
args.assign(command, quotePos2 + 1, command.length() - (quotePos2 + 1));
|
|
|
|
command.assign(command, quotePos + 1, quotePos2 - quotePos - 1);
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-07-14 00:26:53 +00:00
|
|
|
command.erase(0, 1);
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-08-28 10:58:26 +00:00
|
|
|
size_t spacePos = command.find(L' ');
|
2009-02-10 18:37:48 +00:00
|
|
|
if (spacePos != std::wstring::npos)
|
|
|
|
{
|
2011-07-14 00:26:53 +00:00
|
|
|
args.assign(command, spacePos + 1, command.length() - (spacePos + 1));
|
|
|
|
command.erase(spacePos);
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-29 16:02:20 +00:00
|
|
|
RunFile(command.c_str(), args.c_str());
|
|
|
|
}
|
2011-09-18 10:54:16 +00:00
|
|
|
|
2012-05-29 16:02:20 +00:00
|
|
|
void RunFile(const WCHAR* file, const WCHAR* args, bool asAdmin)
|
|
|
|
{
|
|
|
|
SHELLEXECUTEINFO si = {sizeof(SHELLEXECUTEINFO)};
|
|
|
|
si.hwnd = NULL;
|
|
|
|
si.lpVerb = asAdmin ? L"runas" : L"open";
|
|
|
|
si.lpFile = file;
|
|
|
|
si.nShow = SW_SHOWNORMAL;
|
|
|
|
|
|
|
|
DWORD type = GetFileAttributes(si.lpFile);
|
|
|
|
if (type & FILE_ATTRIBUTE_DIRECTORY && type != 0xFFFFFFFF)
|
|
|
|
{
|
|
|
|
ShellExecute(si.hwnd, si.lpVerb, si.lpFile, NULL, NULL, si.nShow);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::wstring dir = CRainmeter::ExtractPath(file);
|
2011-09-18 10:54:16 +00:00
|
|
|
si.lpDirectory = dir.c_str();
|
2012-05-29 16:02:20 +00:00
|
|
|
si.lpParameters = args;
|
2011-09-18 10:54:16 +00:00
|
|
|
si.fMask = SEE_MASK_DOENVSUBST | SEE_MASK_FLAG_NO_UI;
|
|
|
|
ShellExecuteEx(&si);
|
|
|
|
}
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string ConvertToAscii(LPCTSTR str)
|
|
|
|
{
|
|
|
|
std::string szAscii;
|
|
|
|
|
2009-09-12 11:11:40 +00:00
|
|
|
if (str && *str)
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2011-08-28 16:06:23 +00:00
|
|
|
int strLen = (int)wcslen(str);
|
2009-09-12 11:11:40 +00:00
|
|
|
int bufLen = WideCharToMultiByte(CP_ACP, 0, str, strLen, NULL, 0, NULL, NULL);
|
|
|
|
if (bufLen > 0)
|
|
|
|
{
|
2011-08-28 16:06:23 +00:00
|
|
|
szAscii.resize(bufLen);
|
2011-08-28 10:58:26 +00:00
|
|
|
WideCharToMultiByte(CP_ACP, 0, str, strLen, &szAscii[0], bufLen, NULL, NULL);
|
2009-09-12 11:11:40 +00:00
|
|
|
}
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
|
|
|
return szAscii;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::wstring ConvertToWide(LPCSTR str)
|
|
|
|
{
|
|
|
|
std::wstring szWide;
|
|
|
|
|
2009-09-12 11:11:40 +00:00
|
|
|
if (str && *str)
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2011-08-28 16:06:23 +00:00
|
|
|
int strLen = (int)strlen(str);
|
2009-09-12 11:11:40 +00:00
|
|
|
int bufLen = MultiByteToWideChar(CP_ACP, 0, str, strLen, NULL, 0);
|
|
|
|
if (bufLen > 0)
|
|
|
|
{
|
2011-08-28 16:06:23 +00:00
|
|
|
szWide.resize(bufLen);
|
2011-08-28 10:58:26 +00:00
|
|
|
MultiByteToWideChar(CP_ACP, 0, str, strLen, &szWide[0], bufLen);
|
2009-09-12 11:11:40 +00:00
|
|
|
}
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
|
|
|
return szWide;
|
2011-07-29 11:49:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string ConvertToUTF8(LPCWSTR str)
|
|
|
|
{
|
|
|
|
std::string szAscii;
|
|
|
|
|
|
|
|
if (str && *str)
|
|
|
|
{
|
2011-08-28 16:06:23 +00:00
|
|
|
int strLen = (int)wcslen(str);
|
2011-07-29 11:49:46 +00:00
|
|
|
int bufLen = WideCharToMultiByte(CP_UTF8, 0, str, strLen, NULL, 0, NULL, NULL);
|
|
|
|
if (bufLen > 0)
|
|
|
|
{
|
2011-08-28 16:06:23 +00:00
|
|
|
szAscii.resize(bufLen);
|
2011-08-28 10:58:26 +00:00
|
|
|
WideCharToMultiByte(CP_UTF8, 0, str, strLen, &szAscii[0], bufLen, NULL, NULL);
|
2011-07-29 11:49:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return szAscii;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::wstring ConvertUTF8ToWide(LPCSTR str)
|
|
|
|
{
|
|
|
|
std::wstring szWide;
|
|
|
|
|
|
|
|
if (str && *str)
|
|
|
|
{
|
2011-08-28 16:06:23 +00:00
|
|
|
int strLen = (int)strlen(str);
|
2011-07-29 11:49:46 +00:00
|
|
|
int bufLen = MultiByteToWideChar(CP_UTF8, 0, str, strLen, NULL, 0);
|
|
|
|
if (bufLen > 0)
|
|
|
|
{
|
2011-08-28 16:06:23 +00:00
|
|
|
szWide.resize(bufLen);
|
2011-08-28 10:58:26 +00:00
|
|
|
MultiByteToWideChar(CP_UTF8, 0, str, strLen, &szWide[0], bufLen);
|
2011-07-29 11:49:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return szWide;
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
|
|
|
|
2012-02-14 17:58:03 +00:00
|
|
|
void LogInternal(int nLevel, ULONGLONG elapsed, LPCTSTR pszMessage)
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
|
|
|
// Add timestamp
|
2010-09-13 20:06:52 +00:00
|
|
|
WCHAR buffer[128];
|
2011-12-09 19:49:06 +00:00
|
|
|
size_t len = _snwprintf_s(buffer, _TRUNCATE, L"%02llu:%02llu:%02llu.%03llu", elapsed / (1000 * 60 * 60), (elapsed / (1000 * 60)) % 60, (elapsed / 1000) % 60, elapsed % 1000);
|
2011-08-28 10:58:26 +00:00
|
|
|
|
2011-09-28 18:28:35 +00:00
|
|
|
Rainmeter->AddAboutLogInfo(nLevel, buffer, pszMessage);
|
2009-02-10 18:37:48 +00:00
|
|
|
|
2012-02-14 17:58:03 +00:00
|
|
|
#ifndef _DEBUG
|
|
|
|
if (!Rainmeter->GetLogging()) return;
|
2009-02-10 18:37:48 +00:00
|
|
|
#endif
|
|
|
|
|
2012-02-14 17:58:03 +00:00
|
|
|
std::wstring message;
|
|
|
|
switch (nLevel)
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2012-02-14 17:58:03 +00:00
|
|
|
case LOG_ERROR:
|
|
|
|
message = L"ERRO";
|
|
|
|
break;
|
2011-08-28 10:58:26 +00:00
|
|
|
|
2012-02-14 17:58:03 +00:00
|
|
|
case LOG_WARNING:
|
|
|
|
message = L"WARN";
|
|
|
|
break;
|
2011-08-28 10:58:26 +00:00
|
|
|
|
2012-02-14 17:58:03 +00:00
|
|
|
case LOG_NOTICE:
|
|
|
|
message = L"NOTE";
|
|
|
|
break;
|
2011-08-28 10:58:26 +00:00
|
|
|
|
2012-02-14 17:58:03 +00:00
|
|
|
case LOG_DEBUG:
|
|
|
|
message = L"DBUG";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
message += L" (";
|
|
|
|
message.append(buffer, len);
|
|
|
|
message += L") ";
|
|
|
|
message += pszMessage;
|
|
|
|
message += L'\n';
|
2012-02-14 18:05:51 +00:00
|
|
|
|
|
|
|
#ifdef _DEBUG
|
2012-02-14 17:58:03 +00:00
|
|
|
_RPT0(_CRT_WARN, ConvertToAscii(message.c_str()).c_str());
|
2012-02-14 18:05:51 +00:00
|
|
|
if (!Rainmeter->GetLogging()) return;
|
|
|
|
#endif
|
2011-08-28 10:58:26 +00:00
|
|
|
|
2012-02-14 17:58:03 +00:00
|
|
|
const WCHAR* logFile = Rainmeter->GetLogFile().c_str();
|
|
|
|
if (_waccess(logFile, 0) == -1)
|
|
|
|
{
|
|
|
|
// Disable logging if the file was deleted manually
|
|
|
|
Rainmeter->StopLogging();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FILE* file = _wfopen(logFile, L"a+, ccs=UTF-8");
|
|
|
|
if (file)
|
|
|
|
{
|
|
|
|
fputws(message.c_str(), file);
|
|
|
|
fclose(file);
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-12 13:34:04 +00:00
|
|
|
void Log(int nLevel, const WCHAR* message)
|
2011-09-03 16:45:29 +00:00
|
|
|
{
|
2012-06-01 13:13:01 +00:00
|
|
|
struct DelayedLogInfo
|
2011-09-03 16:45:29 +00:00
|
|
|
{
|
|
|
|
int level;
|
|
|
|
ULONGLONG elapsed;
|
|
|
|
std::wstring message;
|
|
|
|
};
|
2012-06-01 13:13:01 +00:00
|
|
|
static std::list<DelayedLogInfo> c_LogDelay;
|
2011-08-31 11:06:35 +00:00
|
|
|
|
2011-09-03 16:45:29 +00:00
|
|
|
static ULONGLONG startTime = CSystem::GetTickCount64();
|
|
|
|
ULONGLONG elapsed = CSystem::GetTickCount64() - startTime;
|
2011-08-31 11:06:35 +00:00
|
|
|
|
2011-09-03 16:45:29 +00:00
|
|
|
if (TryEnterCriticalSection(&g_CsLog))
|
|
|
|
{
|
|
|
|
// Log the queued messages first
|
|
|
|
EnterCriticalSection(&g_CsLogDelay);
|
2011-08-31 11:06:35 +00:00
|
|
|
|
2011-09-03 16:45:29 +00:00
|
|
|
while (!c_LogDelay.empty())
|
|
|
|
{
|
2012-06-01 13:13:01 +00:00
|
|
|
DelayedLogInfo& logInfo = c_LogDelay.front();
|
2011-10-12 13:34:04 +00:00
|
|
|
LogInternal(logInfo.level, logInfo.elapsed, logInfo.message.c_str());
|
2011-08-31 11:06:35 +00:00
|
|
|
|
2011-09-03 16:45:29 +00:00
|
|
|
c_LogDelay.erase(c_LogDelay.begin());
|
2011-08-31 11:06:35 +00:00
|
|
|
}
|
|
|
|
|
2011-09-03 16:45:29 +00:00
|
|
|
LeaveCriticalSection(&g_CsLogDelay);
|
2011-08-31 11:06:35 +00:00
|
|
|
|
2011-09-03 16:45:29 +00:00
|
|
|
// Log the message
|
2011-10-12 13:34:04 +00:00
|
|
|
LogInternal(nLevel, elapsed, message);
|
2011-09-03 16:45:29 +00:00
|
|
|
|
|
|
|
LeaveCriticalSection(&g_CsLog);
|
2011-08-28 10:58:26 +00:00
|
|
|
}
|
2011-09-03 16:45:29 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// Queue the message
|
|
|
|
EnterCriticalSection(&g_CsLogDelay);
|
2011-08-28 10:58:26 +00:00
|
|
|
|
2012-06-01 13:13:01 +00:00
|
|
|
DelayedLogInfo logInfo = {nLevel, elapsed, message};
|
2011-09-03 16:45:29 +00:00
|
|
|
c_LogDelay.push_back(logInfo);
|
2010-12-15 22:03:36 +00:00
|
|
|
|
2011-09-03 16:45:29 +00:00
|
|
|
LeaveCriticalSection(&g_CsLogDelay);
|
|
|
|
}
|
2010-07-07 23:46:44 +00:00
|
|
|
}
|
2010-12-19 23:06:13 +00:00
|
|
|
|
2011-09-23 16:28:38 +00:00
|
|
|
void LogWithArgs(int nLevel, const WCHAR* format, ...)
|
2010-12-19 23:06:13 +00:00
|
|
|
{
|
2012-05-29 16:43:24 +00:00
|
|
|
WCHAR* buffer = new WCHAR[1024];
|
2010-12-19 23:06:13 +00:00
|
|
|
va_list args;
|
2012-05-29 16:43:24 +00:00
|
|
|
va_start(args, format);
|
2010-12-19 23:06:13 +00:00
|
|
|
|
|
|
|
_invalid_parameter_handler oldHandler = _set_invalid_parameter_handler(RmNullCRTInvalidParameterHandler);
|
|
|
|
_CrtSetReportMode(_CRT_ASSERT, 0);
|
|
|
|
|
|
|
|
errno = 0;
|
2012-05-29 16:43:24 +00:00
|
|
|
_vsnwprintf_s(buffer, 1024, _TRUNCATE, format, args);
|
2010-12-19 23:06:13 +00:00
|
|
|
if (errno != 0)
|
|
|
|
{
|
|
|
|
nLevel = LOG_ERROR;
|
2012-05-29 16:43:24 +00:00
|
|
|
_snwprintf_s(buffer, 1024, _TRUNCATE, L"Internal error: %s", format);
|
2010-12-19 23:06:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_set_invalid_parameter_handler(oldHandler);
|
|
|
|
|
2011-09-03 16:45:29 +00:00
|
|
|
Log(nLevel, buffer);
|
2010-12-19 23:06:13 +00:00
|
|
|
va_end(args);
|
2011-02-15 13:22:19 +00:00
|
|
|
|
|
|
|
delete [] buffer;
|
2010-12-19 23:06:13 +00:00
|
|
|
}
|
2011-08-28 10:58:26 +00:00
|
|
|
|
2011-09-08 17:05:48 +00:00
|
|
|
void LogError(CError& error)
|
|
|
|
{
|
|
|
|
Log(LOG_ERROR, error.GetString().c_str());
|
|
|
|
CDialogAbout::ShowAboutLog();
|
|
|
|
}
|
|
|
|
|
2011-09-24 09:13:13 +00:00
|
|
|
WCHAR* GetString(UINT id)
|
2011-09-23 16:28:38 +00:00
|
|
|
{
|
|
|
|
LPWSTR pData;
|
|
|
|
int len = LoadString(Rainmeter->GetResourceInstance(), id, (LPWSTR)&pData, 0);
|
2011-09-24 09:13:13 +00:00
|
|
|
return len ? pData : L"";
|
2011-09-23 16:28:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::wstring GetFormattedString(UINT id, ...)
|
|
|
|
{
|
|
|
|
LPWSTR pBuffer = NULL;
|
|
|
|
va_list args = NULL;
|
|
|
|
va_start(args, id);
|
|
|
|
|
2011-09-24 13:41:07 +00:00
|
|
|
DWORD len = FormatMessage(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ALLOCATE_BUFFER,
|
2012-02-14 17:58:03 +00:00
|
|
|
GetString(id),
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
(LPWSTR)&pBuffer,
|
|
|
|
0,
|
|
|
|
&args);
|
2011-09-23 16:28:38 +00:00
|
|
|
|
|
|
|
va_end(args);
|
|
|
|
|
2011-09-24 13:41:07 +00:00
|
|
|
std::wstring tmpSz(len ? pBuffer : L"");
|
|
|
|
if (pBuffer) LocalFree(pBuffer);
|
2011-09-23 16:28:38 +00:00
|
|
|
return tmpSz;
|
|
|
|
}
|
|
|
|
|
2012-05-05 15:53:33 +00:00
|
|
|
HICON GetIcon(UINT id, bool large)
|
|
|
|
{
|
|
|
|
typedef HRESULT (WINAPI * FPLOADICONMETRIC)(HINSTANCE hinst, PCWSTR pszName, int lims, HICON* phico);
|
|
|
|
|
|
|
|
HINSTANCE hExe = GetModuleHandle(NULL);
|
|
|
|
HINSTANCE hComctl = GetModuleHandle(L"Comctl32");
|
|
|
|
if (hComctl)
|
|
|
|
{
|
|
|
|
// Try LoadIconMetric for better quality with high DPI
|
|
|
|
FPLOADICONMETRIC loadIconMetric = (FPLOADICONMETRIC)GetProcAddress(hComctl, "LoadIconMetric");
|
|
|
|
if (loadIconMetric)
|
|
|
|
{
|
|
|
|
HICON icon;
|
|
|
|
HRESULT hr = loadIconMetric(hExe, MAKEINTRESOURCE(id), large ? LIM_LARGE : LIM_SMALL, &icon);
|
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
{
|
|
|
|
return icon;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (HICON)LoadImage(
|
|
|
|
hExe,
|
|
|
|
MAKEINTRESOURCE(id),
|
|
|
|
IMAGE_ICON,
|
|
|
|
GetSystemMetrics(large ? SM_CXICON : SM_CXSMICON),
|
|
|
|
GetSystemMetrics(large ? SM_CYICON : SM_CYSMICON),
|
|
|
|
LR_SHARED);
|
|
|
|
}
|
|
|
|
|
2012-02-14 17:58:03 +00:00
|
|
|
void RmNullCRTInvalidParameterHandler(const wchar_t* expression, const wchar_t* function, const wchar_t* file, unsigned int line, uintptr_t pReserved)
|
2011-08-28 10:58:26 +00:00
|
|
|
{
|
|
|
|
// Do nothing.
|
|
|
|
}
|