diff options
Diffstat (limited to 'src/mpc-hc/Logger.h')
-rw-r--r-- | src/mpc-hc/Logger.h | 103 |
1 files changed, 55 insertions, 48 deletions
diff --git a/src/mpc-hc/Logger.h b/src/mpc-hc/Logger.h index 3eb09c823..9903b2bf0 100644 --- a/src/mpc-hc/Logger.h +++ b/src/mpc-hc/Logger.h @@ -1,5 +1,5 @@ /* - * (C) 2015 see Authors.txt + * (C) 2015-2016 see Authors.txt * * This file is part of MPC-HC. * @@ -20,72 +20,79 @@ #pragma once -#include <mutex> -#include <sys/timeb.h> -#include "AppSettings.h" +#include "PathUtils.h" #include "mplayerc.h" enum class LogTargets { - BDA + BDA, + SUBTITLES }; -template <LogTargets> -struct LoggerFile; +namespace +{ + template<LogTargets TARGET> + constexpr LPCTSTR GetFileName(); -template<> -struct LoggerFile<LogTargets::BDA> { -protected: - const LPCTSTR filename = _T("bda.log"); - virtual ~LoggerFile() = default; -}; + template<> + constexpr LPCTSTR GetFileName<LogTargets::BDA>() + { + return _T("bda.log"); + } + + template<> + constexpr LPCTSTR GetFileName<LogTargets::SUBTITLES>() + { + return _T("subtitles.log"); + } + + void WriteToFile(FILE* f, LPCSTR function, LPCSTR file, int line, _In_z_ _Printf_format_string_ LPCTSTR fmt, va_list& args) + { + SYSTEMTIME local_time; + GetLocalTime(&local_time); + + _ftprintf_s(f, _T("%.2hu:%.2hu:%.2hu.%.3hu - %S: "), local_time.wHour, local_time.wMinute, + local_time.wSecond, local_time.wMilliseconds, function); + _vftprintf_s(f, fmt, args); + _ftprintf_s(f, _T(" (%S:%d)\n"), file, line); + } +} template<LogTargets TARGET> -class Logger final : public LoggerFile<TARGET> -{ -public: - static void LOG(LPCTSTR fmt...) { +struct Logger final { + static void Log(LPCSTR function, LPCSTR file, int line, LPCTSTR fmt...) { static Logger logger; + if (!logger.m_file) { + return; + } + va_list args; va_start(args, fmt); - logger.WriteToFile(fmt, args); + WriteToFile(logger.m_file, function, file, line, fmt, args); va_end(args); - }; + } private: - const bool m_bLogging = AfxGetAppSettings().bEnableLogging; - std::mutex m_mutex; - Logger() { - // Check if logging is enabled only during initialization - // to avoid incomplete logs - ASSERT(AfxGetAppSettings().IsInitialized()); + const auto& s = AfxGetAppSettings(); + // Check if logging is enabled only during initialization to avoid incomplete logs + ASSERT(s.IsInitialized()); + CString savePath; + VERIFY(AfxGetMyApp()->GetAppSavePath(savePath)); + m_file = s.bEnableLogging ? _tfsopen(PathUtils::CombinePaths(savePath, GetFileName<TARGET>()), _T("at"), SH_DENYWR) : nullptr; + ASSERT(!s.bEnableLogging || m_file); } - void WriteToFile(LPCTSTR fmt, va_list& args) { - if (!m_bLogging) { - return; + ~Logger() { + if (m_file) { + fclose(m_file); } + } - TCHAR buff[3000]; - FILE* f; - _timeb timebuffer; - TCHAR time1[8]; - TCHAR wbuf[26]; - - _ftime_s(&timebuffer); - _tctime_s(wbuf, _countof(wbuf), &timebuffer.time); - - for (size_t i = 0; i < _countof(time1); i++) { - time1[i] = wbuf[i + 11]; - } + FILE* m_file; +}; - _vstprintf_s(buff, _countof(buff), fmt, args); - std::lock_guard<std::mutex> lock(m_mutex); - if (_tfopen_s(&f, filename, _T("at")) == 0) { - _ftprintf_s(f, _T("%.8s.%03hu - %s\n"), time1, timebuffer.millitm, buff); - fclose(f); - } - } -}; +#define MPCHC_LOG(TARGET, fmt, ...) Logger<LogTargets::TARGET>::Log(__FUNCTION__, __FILE__, __LINE__, fmt, __VA_ARGS__) +#define BDA_LOG(...) MPCHC_LOG(BDA, __VA_ARGS__) +#define SUBTITLES_LOG(...) MPCHC_LOG(SUBTITLES, __VA_ARGS__) |