summaryrefslogtreecommitdiff
path: root/zen/error_log.h
blob: 9814ec2371713811e8f4a5d0cb7a273b9854ede7 (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// **************************************************************************
// * This file is part of the FreeFileSync project. It is distributed under *
// * GNU General Public License: http://www.gnu.org/licenses/gpl-3.0        *
// * Copyright (C) Zenju (zenju AT gmx DOT de) - All Rights Reserved        *
// **************************************************************************

#ifndef ERRORLOGGING_H_INCLUDED
#define ERRORLOGGING_H_INCLUDED

#include <cassert>
#include <algorithm>
#include <vector>
#include <string>
#include "time.h"
#include "i18n.h"
#include "string_base.h"

namespace zen
{
enum MessageType
{
    TYPE_INFO        = 0x1,
    TYPE_WARNING     = 0x2,
    TYPE_ERROR       = 0x4,
    TYPE_FATAL_ERROR = 0x8,
};

typedef Zbase<wchar_t> MsgString; //std::wstring may employ small string optimization: we cannot accept bloating the "ErrorLog::entries" memory block below (think 1 million items)

struct LogEntry
{
    time_t      time;
    MessageType type;
    MsgString   message;
};

template <class String>
String formatMessage(const LogEntry& entry);


class ErrorLog
{
public:
    template <class String> //a wchar_t-based string!
    void logMsg(const String& text, MessageType type);

    int getItemCount(int typeFilter = TYPE_INFO | TYPE_WARNING | TYPE_ERROR | TYPE_FATAL_ERROR) const;

    //subset of std::vector<> interface:
    typedef std::vector<LogEntry>::const_iterator const_iterator;
    const_iterator begin() const { return entries.begin(); }
    const_iterator end  () const { return entries.end  (); }
    bool empty() const { return entries.empty(); }

private:
    std::vector<LogEntry> entries; //list of non-resolved errors and warnings
};









//######################## implementation ##########################
template <class String> inline
void ErrorLog::logMsg(const String& text, zen::MessageType type)
{
    const LogEntry newEntry = { std::time(nullptr), type, copyStringTo<MsgString>(text) };
    entries.push_back(newEntry);
}


inline
int ErrorLog::getItemCount(int typeFilter) const
{
    return static_cast<int>(std::count_if(entries.begin(), entries.end(), [&](const LogEntry& e) { return e.type & typeFilter; }));
}


namespace
{
template <class String>
String formatMessageImpl(const LogEntry& entry) //internal linkage
{
    auto getTypeName = [&]() -> std::wstring
    {
        switch (entry.type)
        {
            case TYPE_INFO:
                return _("Info");
            case TYPE_WARNING:
                return _("Warning");
            case TYPE_ERROR:
                return _("Error");
            case TYPE_FATAL_ERROR:
                return _("Serious Error");
        }
        assert(false);
        return std::wstring();
    };

    String formattedText = L"[" + formatTime<String>(FORMAT_TIME, localTime(entry.time)) + L"] " + copyStringTo<String>(getTypeName()) + L": ";
    const size_t prefixLen = formattedText.size();

    for (auto it = entry.message.begin(); it != entry.message.end(); )
        if (*it == L'\n')
        {
            formattedText += L'\n';

            String blanks;
            blanks.resize(prefixLen, L' ');
            formattedText += blanks;

            do //skip duplicate newlines
            {
                ++it;
            }
            while (it != entry.message.end() && *it == L'\n');
        }
        else
            formattedText += *it++;

    return formattedText;
}
}

template <class String> inline
String formatMessage(const LogEntry& entry) { return formatMessageImpl<String>(entry); }
}

#endif //ERRORLOGGING_H_INCLUDED
bgstack15