2013-05-29 19:05:41 +00:00
|
|
|
/*
|
|
|
|
Copyright (C) 2013 Birunthan Mohanathas
|
|
|
|
|
|
|
|
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "StdAfx.h"
|
|
|
|
#include "Logger.h"
|
|
|
|
#include "DialogAbout.h"
|
2013-05-30 16:35:52 +00:00
|
|
|
#include "Litestep.h"
|
|
|
|
#include "Rainmeter.h"
|
2013-08-02 12:00:36 +00:00
|
|
|
#include "Section.h"
|
|
|
|
#include "MeterWindow.h"
|
2013-05-29 19:05:41 +00:00
|
|
|
#include "System.h"
|
|
|
|
#include "resource.h"
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
const size_t MAX_LOG_ENTIRES = 20;
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2013-05-31 14:18:52 +00:00
|
|
|
Logger::Logger() :
|
2013-05-29 19:05:41 +00:00
|
|
|
m_LogToFile(false)
|
|
|
|
{
|
2013-05-31 14:18:52 +00:00
|
|
|
System::InitializeCriticalSection(&m_CsLog);
|
|
|
|
System::InitializeCriticalSection(&m_CsLogDelay);
|
2013-05-29 19:05:41 +00:00
|
|
|
}
|
|
|
|
|
2013-05-31 14:18:52 +00:00
|
|
|
Logger::~Logger()
|
2013-05-29 19:05:41 +00:00
|
|
|
{
|
|
|
|
DeleteCriticalSection(&m_CsLog);
|
|
|
|
DeleteCriticalSection(&m_CsLogDelay);
|
|
|
|
}
|
|
|
|
|
2013-05-31 14:18:52 +00:00
|
|
|
Logger& Logger::GetInstance()
|
2013-05-29 19:05:41 +00:00
|
|
|
{
|
2013-05-31 14:18:52 +00:00
|
|
|
static Logger s_Logger;
|
|
|
|
return s_Logger;
|
2013-05-29 19:05:41 +00:00
|
|
|
}
|
|
|
|
|
2013-05-31 14:18:52 +00:00
|
|
|
void Logger::StartLogFile()
|
2013-05-29 19:05:41 +00:00
|
|
|
{
|
|
|
|
const WCHAR* filePath = m_LogFilePath.c_str();
|
|
|
|
if (_waccess(filePath, 0) == -1)
|
|
|
|
{
|
|
|
|
// Create empty log file.
|
2013-05-31 14:28:39 +00:00
|
|
|
HANDLE file = CreateFile(filePath, GENERIC_WRITE, 0, nullptr, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, nullptr);
|
2013-05-29 19:05:41 +00:00
|
|
|
if (file != INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
CloseHandle(file);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const std::wstring text = GetFormattedString(ID_STR_LOGFILECREATEFAIL, filePath);
|
2013-06-13 14:20:27 +00:00
|
|
|
GetRainmeter().ShowMessage(nullptr, text.c_str(), MB_OK | MB_ICONERROR);
|
2013-05-29 19:05:41 +00:00
|
|
|
SetLogToFile(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SetLogToFile(true);
|
|
|
|
}
|
|
|
|
|
2013-05-31 14:18:52 +00:00
|
|
|
void Logger::StopLogFile()
|
2013-05-29 19:05:41 +00:00
|
|
|
{
|
|
|
|
SetLogToFile(false);
|
|
|
|
}
|
|
|
|
|
2013-05-31 14:18:52 +00:00
|
|
|
void Logger::DeleteLogFile()
|
2013-05-29 19:05:41 +00:00
|
|
|
{
|
|
|
|
const WCHAR* filePath = m_LogFilePath.c_str();
|
|
|
|
if (_waccess(filePath, 0) != -1)
|
|
|
|
{
|
|
|
|
const std::wstring text = GetFormattedString(ID_STR_LOGFILEDELETE, filePath);
|
2013-06-13 14:20:27 +00:00
|
|
|
const int res = GetRainmeter().ShowMessage(nullptr, text.c_str(), MB_YESNO | MB_ICONQUESTION);
|
2013-05-29 19:05:41 +00:00
|
|
|
if (res == IDYES)
|
|
|
|
{
|
|
|
|
SetLogToFile(false);
|
2013-05-31 14:18:52 +00:00
|
|
|
System::RemoveFile(m_LogFilePath);
|
2013-05-29 19:05:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-31 14:18:52 +00:00
|
|
|
void Logger::SetLogToFile(bool logToFile)
|
2013-05-29 19:05:41 +00:00
|
|
|
{
|
|
|
|
m_LogToFile = logToFile;
|
|
|
|
WritePrivateProfileString(
|
2013-06-13 14:20:27 +00:00
|
|
|
L"Rainmeter", L"Logging", logToFile ? L"1" : L"0", GetRainmeter().GetIniFile().c_str());
|
2013-05-29 19:05:41 +00:00
|
|
|
}
|
|
|
|
|
2013-06-29 16:59:18 +00:00
|
|
|
void Logger::LogInternal(Level level, ULONGLONG timestamp, const WCHAR* source, const WCHAR* msg)
|
2013-05-29 19:05:41 +00:00
|
|
|
{
|
|
|
|
WCHAR timestampSz[128];
|
|
|
|
size_t len = _snwprintf_s(
|
|
|
|
timestampSz,
|
|
|
|
_TRUNCATE,
|
|
|
|
L"%02llu:%02llu:%02llu.%03llu",
|
|
|
|
timestamp / (1000 * 60 * 60),
|
|
|
|
(timestamp / (1000 * 60)) % 60,
|
|
|
|
(timestamp / 1000) % 60,
|
|
|
|
timestamp % 1000);
|
|
|
|
|
|
|
|
// Store up to MAX_LOG_ENTIRES entries.
|
2013-06-29 16:59:18 +00:00
|
|
|
Entry entry = {level, std::wstring(timestampSz, len), source, msg};
|
2013-05-29 19:05:41 +00:00
|
|
|
m_Entries.push_back(entry);
|
|
|
|
if (m_Entries.size() > MAX_LOG_ENTIRES)
|
|
|
|
{
|
|
|
|
m_Entries.pop_front();
|
|
|
|
}
|
|
|
|
|
2013-06-29 16:59:18 +00:00
|
|
|
DialogAbout::AddLogItem(level, timestampSz, source, msg);
|
2013-05-30 16:35:52 +00:00
|
|
|
WriteToLogFile(entry);
|
2013-05-29 19:05:41 +00:00
|
|
|
}
|
|
|
|
|
2013-05-31 14:18:52 +00:00
|
|
|
void Logger::WriteToLogFile(Entry& entry)
|
2013-05-29 19:05:41 +00:00
|
|
|
{
|
|
|
|
#ifndef _DEBUG
|
|
|
|
if (!m_LogToFile) return;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
const WCHAR* levelSz =
|
|
|
|
(entry.level == Level::Error) ? L"ERRO" :
|
|
|
|
(entry.level == Level::Warning) ? L"WARN" :
|
|
|
|
(entry.level == Level::Notice) ? L"NOTE" :
|
|
|
|
L"DBUG";
|
|
|
|
|
|
|
|
std::wstring message = levelSz;
|
|
|
|
message += L" (";
|
|
|
|
message.append(entry.timestamp);
|
|
|
|
message += L") ";
|
2013-06-29 16:59:18 +00:00
|
|
|
message += entry.source;
|
|
|
|
message += L": ";
|
2013-05-29 19:05:41 +00:00
|
|
|
message += entry.message;
|
|
|
|
message += L'\n';
|
|
|
|
|
|
|
|
#ifdef _DEBUG
|
|
|
|
_RPT0(_CRT_WARN, StringUtil::Narrow(message).c_str());
|
|
|
|
if (!m_LogToFile) return;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
const WCHAR* filePath = m_LogFilePath.c_str();
|
|
|
|
if (_waccess(filePath, 0) == -1)
|
|
|
|
{
|
|
|
|
// The file has been deleted manually.
|
|
|
|
StopLogFile();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FILE* file = _wfopen(filePath, L"a+, ccs=UTF-8");
|
|
|
|
if (file)
|
|
|
|
{
|
|
|
|
fputws(message.c_str(), file);
|
|
|
|
fclose(file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-29 16:59:18 +00:00
|
|
|
void Logger::Log(Level level, const WCHAR* source, const WCHAR* msg)
|
2013-05-29 19:05:41 +00:00
|
|
|
{
|
|
|
|
struct DelayedEntry
|
|
|
|
{
|
|
|
|
Level level;
|
|
|
|
ULONGLONG elapsed;
|
|
|
|
std::wstring message;
|
|
|
|
};
|
|
|
|
static std::list<DelayedEntry> s_DelayedEntries;
|
|
|
|
|
2013-05-31 14:18:52 +00:00
|
|
|
static ULONGLONG s_StartTime = System::GetTickCount64();
|
|
|
|
ULONGLONG elapsed = System::GetTickCount64() - s_StartTime;
|
2013-05-29 19:05:41 +00:00
|
|
|
|
|
|
|
if (TryEnterCriticalSection(&m_CsLog))
|
|
|
|
{
|
|
|
|
// Log queued messages first.
|
|
|
|
EnterCriticalSection(&m_CsLogDelay);
|
|
|
|
|
|
|
|
while (!s_DelayedEntries.empty())
|
|
|
|
{
|
|
|
|
DelayedEntry& entry = s_DelayedEntries.front();
|
2013-06-29 16:59:18 +00:00
|
|
|
LogInternal(entry.level, entry.elapsed, source, entry.message.c_str());
|
2013-05-29 19:05:41 +00:00
|
|
|
|
|
|
|
s_DelayedEntries.erase(s_DelayedEntries.begin());
|
|
|
|
}
|
|
|
|
|
|
|
|
LeaveCriticalSection(&m_CsLogDelay);
|
|
|
|
|
|
|
|
// Log the actual message.
|
2013-06-29 16:59:18 +00:00
|
|
|
LogInternal(level, elapsed, source, msg);
|
2013-05-29 19:05:41 +00:00
|
|
|
|
|
|
|
LeaveCriticalSection(&m_CsLog);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Queue message.
|
|
|
|
EnterCriticalSection(&m_CsLogDelay);
|
|
|
|
|
|
|
|
DelayedEntry entry = {level, elapsed, msg};
|
|
|
|
s_DelayedEntries.push_back(entry);
|
|
|
|
|
|
|
|
LeaveCriticalSection(&m_CsLogDelay);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-02 12:00:36 +00:00
|
|
|
void Logger::LogVF(Level level, const WCHAR* source, const WCHAR* format, va_list args)
|
2013-05-29 19:05:41 +00:00
|
|
|
{
|
|
|
|
WCHAR* buffer = new WCHAR[1024];
|
|
|
|
|
|
|
|
_invalid_parameter_handler oldHandler = _set_invalid_parameter_handler(RmNullCRTInvalidParameterHandler);
|
|
|
|
_CrtSetReportMode(_CRT_ASSERT, 0);
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
_vsnwprintf_s(buffer, 1024, _TRUNCATE, format, args);
|
|
|
|
if (errno != 0)
|
|
|
|
{
|
|
|
|
level = Level::Error;
|
|
|
|
_snwprintf_s(buffer, 1024, _TRUNCATE, L"Internal error: %s", format);
|
|
|
|
}
|
|
|
|
|
|
|
|
_set_invalid_parameter_handler(oldHandler);
|
|
|
|
|
2013-06-29 16:59:18 +00:00
|
|
|
Log(level, source, buffer);
|
|
|
|
delete [] buffer;
|
|
|
|
}
|
|
|
|
|
2013-08-02 12:00:36 +00:00
|
|
|
void Logger::LogSectionVF(Logger::Level level, Section* section, const WCHAR* format, va_list args)
|
2013-06-29 16:59:18 +00:00
|
|
|
{
|
|
|
|
std::wstring source;
|
|
|
|
if (section)
|
|
|
|
{
|
|
|
|
MeterWindow* meterWindow = section->GetMeterWindow();
|
|
|
|
if (meterWindow)
|
|
|
|
{
|
|
|
|
source = meterWindow->GetSkinPath();
|
|
|
|
source += L" - ";
|
|
|
|
}
|
|
|
|
|
2013-08-02 12:00:36 +00:00
|
|
|
source += L'[';
|
2013-06-29 16:59:18 +00:00
|
|
|
source += section->GetOriginalName();
|
2013-08-02 12:00:36 +00:00
|
|
|
source += L']';
|
2013-06-29 16:59:18 +00:00
|
|
|
}
|
|
|
|
|
2013-08-02 12:00:36 +00:00
|
|
|
GetLogger().LogVF(level, source.c_str(), format, args);
|
2013-06-29 16:59:18 +00:00
|
|
|
}
|
|
|
|
|
2013-08-02 12:00:36 +00:00
|
|
|
void Logger::LogMeterWindowVF(Logger::Level level, MeterWindow* meterWindow, const WCHAR* format, va_list args)
|
2013-06-29 16:59:18 +00:00
|
|
|
{
|
2013-08-04 18:24:06 +00:00
|
|
|
std::wstring source;
|
|
|
|
if (meterWindow)
|
|
|
|
{
|
2013-08-04 18:25:43 +00:00
|
|
|
source = meterWindow->GetSkinPath();
|
2013-08-04 18:24:06 +00:00
|
|
|
}
|
|
|
|
GetLogger().LogVF(level, source.c_str(), format, args);
|
2013-05-29 19:05:41 +00:00
|
|
|
}
|