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
|
|
|
|
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
static int logFound = 0;
|
|
|
|
|
|
|
|
void ResetLoggingFlag()
|
|
|
|
{
|
|
|
|
logFound = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2011-10-22 20:28:15 +00:00
|
|
|
HRGN BitmapToRegion(HBITMAP hbm, COLORREF clrTransp, COLORREF clrTolerance)
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2011-10-22 20:28:15 +00:00
|
|
|
HRGN hRgn = NULL;
|
2009-02-10 18:37:48 +00:00
|
|
|
|
|
|
|
if (hbm)
|
|
|
|
{
|
|
|
|
// create a dc for the 32 bit dib
|
|
|
|
HDC hdcMem = CreateCompatibleDC(NULL);
|
|
|
|
if (hdcMem)
|
|
|
|
{
|
|
|
|
// get the size
|
2011-10-22 20:28:15 +00:00
|
|
|
BITMAP bm;
|
2009-02-10 18:37:48 +00:00
|
|
|
GetObject(hbm, sizeof(BITMAP), &bm);
|
|
|
|
|
|
|
|
BITMAPINFOHEADER bmpInfo32;
|
2011-10-22 20:28:15 +00:00
|
|
|
bmpInfo32.biSize = sizeof(BITMAPINFOHEADER);
|
|
|
|
bmpInfo32.biWidth = bm.bmWidth;
|
|
|
|
bmpInfo32.biHeight = bm.bmHeight;
|
|
|
|
bmpInfo32.biPlanes = 1;
|
|
|
|
bmpInfo32.biBitCount = 32;
|
|
|
|
bmpInfo32.biCompression = BI_RGB;
|
|
|
|
bmpInfo32.biSizeImage = 0;
|
|
|
|
bmpInfo32.biXPelsPerMeter = 0;
|
|
|
|
bmpInfo32.biYPelsPerMeter = 0;
|
|
|
|
bmpInfo32.biClrUsed = 0;
|
|
|
|
bmpInfo32.biClrImportant = 0;
|
|
|
|
|
|
|
|
VOID* pbits32;
|
|
|
|
HBITMAP hbm32 = CreateDIBSection(hdcMem, (BITMAPINFO *) & bmpInfo32, DIB_RGB_COLORS, &pbits32, NULL, 0);
|
2009-02-10 18:37:48 +00:00
|
|
|
if (hbm32)
|
|
|
|
{
|
|
|
|
HBITMAP hbmOld32 = (HBITMAP)SelectObject(hdcMem, hbm32);
|
|
|
|
|
|
|
|
// Create a DC just to copy the bitmap into the memory D
|
|
|
|
HDC hdcTmp = CreateCompatibleDC(hdcMem);
|
|
|
|
if (hdcTmp)
|
|
|
|
{
|
|
|
|
// Get how many bytes per row we have for the bitmap bits (rounded up to 32 bits
|
|
|
|
BITMAP bm32;
|
|
|
|
GetObject(hbm32, sizeof(bm32), &bm32);
|
|
|
|
while (bm32.bmWidthBytes % 4)
|
2011-10-22 20:28:15 +00:00
|
|
|
++bm32.bmWidthBytes;
|
|
|
|
|
|
|
|
// Copy the bitmap into the memory D
|
|
|
|
HBITMAP hbmOld = (HBITMAP)SelectObject(hdcTmp, hbm);
|
|
|
|
BitBlt(hdcMem, 0, 0, bm.bmWidth, bm.bmHeight, hdcTmp, 0, 0, SRCCOPY);
|
2009-02-10 18:37:48 +00:00
|
|
|
|
|
|
|
// get the limits for the colors
|
|
|
|
BYTE clrHiR = ( 0xff - GetRValue( clrTolerance ) > GetRValue( clrTransp ) ) ? GetRValue( clrTransp ) + GetRValue( clrTolerance ) : 0xff;
|
|
|
|
BYTE clrHiG = ( 0xff - GetGValue( clrTolerance ) > GetGValue( clrTransp ) ) ? GetGValue( clrTransp ) + GetGValue( clrTolerance ) : 0xff;
|
|
|
|
BYTE clrHiB = ( 0xff - GetBValue( clrTolerance ) > GetBValue( clrTransp ) ) ? GetBValue( clrTransp ) + GetBValue( clrTolerance ) : 0xff;
|
|
|
|
BYTE clrLoR = ( GetRValue( clrTolerance ) < GetRValue( clrTransp ) ) ? GetRValue( clrTransp ) - GetRValue( clrTolerance ) : 0x00;
|
|
|
|
BYTE clrLoG = ( GetGValue( clrTolerance ) < GetGValue( clrTransp ) ) ? GetGValue( clrTransp ) - GetGValue( clrTolerance ) : 0x00;
|
|
|
|
BYTE clrLoB = ( GetBValue( clrTolerance ) < GetBValue( clrTransp ) ) ? GetBValue( clrTransp ) - GetBValue( clrTolerance ) : 0x00;
|
|
|
|
|
2011-10-22 20:28:15 +00:00
|
|
|
// Allocate initial RGNDATA buffer
|
|
|
|
#define ALLOC_UNIT 100
|
|
|
|
DWORD maxRects = ALLOC_UNIT;
|
|
|
|
HANDLE hRgnData = GlobalAlloc(GMEM_MOVEABLE, sizeof(RGNDATAHEADER) + (sizeof(RECT) * maxRects));
|
|
|
|
RGNDATA* pRgnData = (RGNDATA*)GlobalLock(hRgnData);
|
|
|
|
pRgnData->rdh.dwSize = sizeof(RGNDATAHEADER);
|
|
|
|
pRgnData->rdh.iType = RDH_RECTANGLES;
|
|
|
|
pRgnData->rdh.nCount = pRgnData->rdh.nRgnSize = 0;
|
|
|
|
SetRect(&pRgnData->rdh.rcBound, 0, 0, bm.bmWidth, bm.bmHeight);
|
2009-02-10 18:37:48 +00:00
|
|
|
|
|
|
|
// Scan each bitmap row from bottom to top (the bitmap is inverted vertically
|
2011-10-22 20:28:15 +00:00
|
|
|
BYTE* p32 = (BYTE*)bm32.bmBits + (bm32.bmHeight - 1) * bm32.bmWidthBytes;
|
|
|
|
for (int y = 0; y < bm.bmHeight; ++y)
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2011-10-22 20:28:15 +00:00
|
|
|
for (int x = 0; x < bm.bmWidth; ++x)
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2011-10-22 20:28:15 +00:00
|
|
|
int x0 = x;
|
|
|
|
|
2009-02-10 18:37:48 +00:00
|
|
|
// loop through all non transparent pixels
|
2011-10-22 20:28:15 +00:00
|
|
|
while (x < bm.bmWidth)
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2011-10-22 20:28:15 +00:00
|
|
|
BYTE* p = p32 + 4 * x;
|
2009-02-10 18:37:48 +00:00
|
|
|
// if the pixel is transparent, then break
|
|
|
|
if (*p >= clrLoB && *p <= clrHiB)
|
|
|
|
{
|
2011-10-22 20:28:15 +00:00
|
|
|
++p;
|
2009-02-10 18:37:48 +00:00
|
|
|
if (*p >= clrLoG && *p <= clrHiG)
|
|
|
|
{
|
2011-10-22 20:28:15 +00:00
|
|
|
++p;
|
2009-02-10 18:37:48 +00:00
|
|
|
if (*p >= clrLoR && *p <= clrHiR)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-10-22 20:28:15 +00:00
|
|
|
++x;
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
2011-10-22 20:28:15 +00:00
|
|
|
|
2009-02-10 18:37:48 +00:00
|
|
|
// if found one or more non-transparent pixels in a row, add them to the rgn...
|
2011-10-22 20:28:15 +00:00
|
|
|
if (x > x0)
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2011-10-22 20:28:15 +00:00
|
|
|
if (pRgnData->rdh.nCount >= maxRects)
|
|
|
|
{
|
|
|
|
// Reallocate RGNDATA buffer
|
|
|
|
GlobalUnlock(hRgnData);
|
|
|
|
maxRects += ALLOC_UNIT;
|
|
|
|
hRgnData = GlobalReAlloc(hRgnData, sizeof(RGNDATAHEADER) + (sizeof(RECT) * maxRects), GMEM_MOVEABLE);
|
|
|
|
pRgnData = (RGNDATA*)GlobalLock(hRgnData);
|
|
|
|
}
|
|
|
|
|
|
|
|
SetRect(((RECT*)pRgnData->Buffer) + pRgnData->rdh.nCount, x0, y, x, y + 1);
|
|
|
|
++pRgnData->rdh.nCount;
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
p32 -= bm32.bmWidthBytes;
|
|
|
|
}
|
2011-10-22 20:28:15 +00:00
|
|
|
|
|
|
|
// Create the region with the collected rectangles
|
|
|
|
hRgn = ExtCreateRegion(NULL, sizeof(RGNDATAHEADER) + (sizeof(RECT) * maxRects), pRgnData);
|
|
|
|
|
2009-02-10 18:37:48 +00:00
|
|
|
// Clean up
|
2011-10-22 20:28:15 +00:00
|
|
|
GlobalUnlock(hRgnData);
|
|
|
|
GlobalFree(hRgnData);
|
2009-02-10 18:37:48 +00:00
|
|
|
SelectObject(hdcTmp, hbmOld);
|
|
|
|
DeleteDC(hdcTmp);
|
|
|
|
}
|
|
|
|
SelectObject(hdcMem, hbmOld32);
|
|
|
|
DeleteObject(hbm32);
|
|
|
|
}
|
|
|
|
DeleteDC(hdcMem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return hRgn;
|
|
|
|
}
|
|
|
|
|
2011-09-28 18:28:35 +00:00
|
|
|
void RunCommand(HWND Owner, LPCTSTR szCommand, int nShowCmd, bool asAdmin)
|
2009-07-21 12:26:50 +00:00
|
|
|
{
|
2009-02-10 18:37:48 +00:00
|
|
|
// The stub implementation (some of this code is taken from lsapi.cpp)
|
2011-09-28 18:28:35 +00:00
|
|
|
if (szCommand == NULL || *szCommand == 0) return;
|
2009-02-10 18:37:48 +00:00
|
|
|
|
|
|
|
std::wstring args;
|
|
|
|
std::wstring command = szCommand;
|
|
|
|
|
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-09-28 18:28:35 +00:00
|
|
|
if (command.empty()) return;
|
2009-02-10 18:37:48 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-18 10:54:16 +00:00
|
|
|
if (!command.empty())
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2011-09-28 18:28:35 +00:00
|
|
|
LPCWSTR szVerb = asAdmin ? L"runas" : L"open";
|
2011-09-18 10:54:16 +00:00
|
|
|
DWORD type = GetFileAttributes(command.c_str());
|
|
|
|
if (type & FILE_ATTRIBUTE_DIRECTORY && type != 0xFFFFFFFF)
|
|
|
|
{
|
2011-09-28 18:28:35 +00:00
|
|
|
ShellExecute(Owner, szVerb, command.c_str(), NULL, NULL, nShowCmd ? nShowCmd : SW_SHOWNORMAL);
|
|
|
|
return;
|
2011-09-18 10:54:16 +00:00
|
|
|
}
|
2009-02-10 18:37:48 +00:00
|
|
|
|
2011-09-18 10:54:16 +00:00
|
|
|
std::wstring dir = CRainmeter::ExtractPath(command);
|
|
|
|
|
|
|
|
SHELLEXECUTEINFO si = {sizeof(SHELLEXECUTEINFO)};
|
|
|
|
si.hwnd = Owner;
|
|
|
|
si.lpVerb = szVerb;
|
|
|
|
si.lpFile = command.c_str();
|
|
|
|
si.lpParameters = args.c_str();
|
|
|
|
si.lpDirectory = dir.c_str();
|
|
|
|
si.nShow = nShowCmd ? nShowCmd : SW_SHOWNORMAL;
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2011-10-12 13:34:04 +00:00
|
|
|
BOOL 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-08-31 11:06:35 +00:00
|
|
|
_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
|
|
|
|
2011-08-28 10:58:26 +00:00
|
|
|
std::wstring message = L"(";
|
|
|
|
message += buffer;
|
|
|
|
message += L") ";
|
2009-02-10 18:37:48 +00:00
|
|
|
message += pszMessage;
|
|
|
|
|
|
|
|
#ifdef _DEBUG
|
|
|
|
_RPT0(_CRT_WARN, ConvertToAscii(message.c_str()).c_str());
|
|
|
|
_RPT0(_CRT_WARN, "\n");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// The stub implementation
|
2011-09-28 18:28:35 +00:00
|
|
|
if (Rainmeter->GetLogging())
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2011-11-14 14:32:11 +00:00
|
|
|
const std::wstring& logfile = Rainmeter->GetLogFile();
|
2009-07-21 12:26:50 +00:00
|
|
|
if (logFound == 0)
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2009-07-21 12:26:50 +00:00
|
|
|
// Check if the file exists
|
2010-07-08 10:59:06 +00:00
|
|
|
if (_waccess(logfile.c_str(), 0) != -1)
|
2009-07-21 12:26:50 +00:00
|
|
|
{
|
|
|
|
logFound = 1;
|
2009-02-10 18:37:48 +00:00
|
|
|
|
2009-07-21 12:26:50 +00:00
|
|
|
// Clear the file
|
2010-07-08 10:59:06 +00:00
|
|
|
FILE* logFile = _wfopen(logfile.c_str(), L"w");
|
2009-07-21 12:26:50 +00:00
|
|
|
fclose(logFile);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-07-08 10:59:06 +00:00
|
|
|
logFound = 2; // not found
|
2009-07-21 12:26:50 +00:00
|
|
|
}
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
|
|
|
|
2009-07-21 12:26:50 +00:00
|
|
|
if (logFound == 1)
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2010-07-08 10:59:06 +00:00
|
|
|
if (_waccess(logfile.c_str(), 0) == -1)
|
|
|
|
{
|
|
|
|
// Disable logging if the file was deleted manually
|
|
|
|
Rainmeter->StopLogging();
|
|
|
|
}
|
|
|
|
else
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2010-07-08 10:59:06 +00:00
|
|
|
FILE* logFile = _wfopen(logfile.c_str(), L"a+, ccs=UTF-8");
|
|
|
|
if (logFile)
|
2009-07-21 12:26:50 +00:00
|
|
|
{
|
2011-08-28 10:58:26 +00:00
|
|
|
message.insert(0, L": ");
|
|
|
|
|
|
|
|
switch (nLevel)
|
|
|
|
{
|
|
|
|
case LOG_ERROR:
|
|
|
|
message.insert(0, L"ERROR");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LOG_WARNING:
|
|
|
|
message.insert(0, L"WARNING");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LOG_NOTICE:
|
|
|
|
message.insert(0, L"NOTICE");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LOG_DEBUG:
|
|
|
|
message.insert(0, L"DEBUG");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
message += L"\n";
|
2010-07-08 10:59:06 +00:00
|
|
|
fputws(message.c_str(), logFile);
|
|
|
|
fclose(logFile);
|
2009-07-21 12:26:50 +00:00
|
|
|
}
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-08-31 11:06:35 +00:00
|
|
|
|
2009-02-10 18:37:48 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2011-08-28 10:58:26 +00:00
|
|
|
BOOL LSLog(int nLevel, LPCTSTR pszModule, LPCTSTR pszMessage)
|
2010-12-15 22:03:36 +00:00
|
|
|
{
|
2011-09-03 16:45:29 +00:00
|
|
|
// Ignore LOG_DEBUG messages from plugins unless in debug mode
|
2011-08-28 10:58:26 +00:00
|
|
|
if (nLevel != LOG_DEBUG || Rainmeter->GetDebug())
|
|
|
|
{
|
2011-10-12 13:34:04 +00:00
|
|
|
Log(nLevel, pszMessage);
|
2011-09-03 16:45:29 +00:00
|
|
|
}
|
2011-08-31 11:06:35 +00:00
|
|
|
|
2011-09-03 16:45:29 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
2011-08-31 11:06:35 +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
|
|
|
{
|
|
|
|
struct DELAYED_LOG_INFO
|
|
|
|
{
|
|
|
|
int level;
|
|
|
|
ULONGLONG elapsed;
|
|
|
|
std::wstring message;
|
|
|
|
};
|
|
|
|
static std::list<DELAYED_LOG_INFO> 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())
|
|
|
|
{
|
|
|
|
DELAYED_LOG_INFO& 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
|
|
|
|
2011-10-12 13:34:04 +00:00
|
|
|
DELAYED_LOG_INFO 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
|
|
|
{
|
2011-02-15 13:22:19 +00:00
|
|
|
WCHAR* buffer = new WCHAR[4096];
|
2010-12-19 23:06:13 +00:00
|
|
|
va_list args;
|
2011-03-29 19:21:57 +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;
|
2011-02-15 13:22:19 +00:00
|
|
|
_vsnwprintf_s( buffer, 4096, _TRUNCATE, format, args );
|
2010-12-19 23:06:13 +00:00
|
|
|
if (errno != 0)
|
|
|
|
{
|
|
|
|
nLevel = LOG_ERROR;
|
2011-09-09 16:31:55 +00:00
|
|
|
_snwprintf_s(buffer, 4096, _TRUNCATE, L"LogWithArgs 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,
|
2011-09-24 09:13:13 +00:00
|
|
|
GetString(id),
|
2011-09-23 16:28:38 +00:00
|
|
|
0,
|
|
|
|
0,
|
|
|
|
(LPWSTR)&pBuffer,
|
|
|
|
0,
|
|
|
|
&args);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-08-28 10:58:26 +00:00
|
|
|
void RmNullCRTInvalidParameterHandler(const wchar_t* expression, const wchar_t* function, const wchar_t* file, unsigned int line, uintptr_t pReserved)
|
|
|
|
{
|
|
|
|
// Do nothing.
|
|
|
|
}
|