Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Logger.h « mpc-hc « src - github.com/mpc-hc/mpc-hc.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9903b2bf021a339c8d81af8522e13173e6f67c3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
 * (C) 2015-2016 see Authors.txt
 *
 * This file is part of MPC-HC.
 *
 * MPC-HC 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 3 of the License, or
 * (at your option) any later version.
 *
 * MPC-HC 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, see <http://www.gnu.org/licenses/>.
 *
 */

#pragma once

#include "PathUtils.h"
#include "mplayerc.h"

enum class LogTargets {
    BDA,
    SUBTITLES
};

namespace
{
    template<LogTargets TARGET>
    constexpr LPCTSTR GetFileName();

    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>
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);
        WriteToFile(logger.m_file, function, file, line, fmt, args);
        va_end(args);
    }

private:
    Logger() {
        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);
    }

    ~Logger() {
        if (m_file) {
            fclose(m_file);
        }
    }

    FILE* m_file;
};


#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__)