summaryrefslogtreecommitdiff
path: root/lib/error_log.cpp
blob: fe4234798ec45f498259a604200fb3f3d8dac2d1 (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
99
// **************************************************************************
// * This file is part of the FreeFileSync project. It is distributed under *
// * GNU General Public License: http://www.gnu.org/licenses/gpl.html       *
// * Copyright (C) 2008-2011 ZenJu (zhnmju123 AT gmx.de)                    *
// **************************************************************************

#include "error_log.h"
#include <wx/datetime.h>
#include <zen/i18n.h>
#include <algorithm>

using namespace zen;


void ErrorLogging::logMsg(const wxString& message, zen::MessageType type)
{
    Entry newEntry;
    newEntry.type    = type;
    newEntry.time    = wxDateTime::GetTimeNow();
    newEntry.message = message;

    messages.push_back(newEntry);

    ++statistics[type];
}


int ErrorLogging::typeCount(int typeFilter) const
{
    int count = 0;

    if (typeFilter & TYPE_INFO)
        count += statistics[TYPE_INFO];
    if (typeFilter & TYPE_WARNING)
        count += statistics[TYPE_WARNING];
    if (typeFilter & TYPE_ERROR)
        count += statistics[TYPE_ERROR];
    if (typeFilter & TYPE_FATAL_ERROR)
        count += statistics[TYPE_FATAL_ERROR];

    return count;
}


std::vector<wxString> ErrorLogging::getFormattedMessages(int typeFilter) const
{
    std::vector<wxString> output;

    std::for_each(messages.begin(), messages.end(),
                  [&](const Entry& entry)
    {
        if (entry.type & typeFilter)
            output.push_back(formatMessage(entry));
    });

    return output;
}


wxString ErrorLogging::formatMessage(const Entry& msg)
{
    wxString typeName;
    switch (msg.type)
    {
        case TYPE_INFO:
            typeName = _("Info");
            break;
        case TYPE_WARNING:
            typeName = _("Warning");
            break;
        case TYPE_ERROR:
            typeName = _("Error");
            break;
        case TYPE_FATAL_ERROR:
            typeName = _("Fatal Error");
            break;
    }

    const wxString prefix = wxString(L"[") + wxDateTime(msg.time).FormatTime() + L"] " + typeName + L": ";

    wxString formattedText = prefix;
    for (auto i = msg.message.begin(); i != msg.message.end(); ++i)
        if (*i == wxChar('\n'))
        {
            formattedText += L'\n';

            wxString blanks;
            blanks.resize(prefix.size(), L' ');
            formattedText += blanks;

            while (*++i == L'\n') //remove duplicate newlines
                ;
            --i;
        }
        else
            formattedText += *i;

    return formattedText;
}
bgstack15