aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/base/log_outputters.h
blob: 15f1e7a9927cedc8689520e52c32bc278198502f (plain) (blame)
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
 * barrier -- mouse and keyboard sharing utility
 * Copyright (C) 2012-2016 Symless Ltd.
 * Copyright (C) 2002 Chris Schoeneman
 * 
 * This package is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * found in the file LICENSE that should have accompanied this file.
 * 
 * This package 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 "mt/Thread.h"
#include "base/ILogOutputter.h"
#include "common/basic_types.h"
#include "common/stddeque.h"

#include <list>
#include <fstream>
#include <string>

//! Stop traversing log chain outputter
/*!
This outputter performs no output and returns false from \c write(),
causing the logger to stop traversing the outputter chain.  Insert
this to prevent already inserted outputters from writing.
*/
class StopLogOutputter : public ILogOutputter {
public:
    StopLogOutputter();
    virtual ~StopLogOutputter();

    // ILogOutputter overrides
    virtual void        open(const char* title);
    virtual void        close();
    virtual void        show(bool showIfEmpty);
    virtual bool        write(ELevel level, const char* message);
};

//! Write log to console
/*!
This outputter writes output to the console.  The level for each
message is ignored.
*/
class ConsoleLogOutputter : public ILogOutputter {
public:
    ConsoleLogOutputter();
    virtual ~ConsoleLogOutputter();

    // ILogOutputter overrides
    virtual void        open(const char* title);
    virtual void        close();
    virtual void        show(bool showIfEmpty);
    virtual bool        write(ELevel level, const char* message);
    virtual void        flush();
};

//! Write log to file
/*!
This outputter writes output to the file.  The level for each
message is ignored.
*/

class FileLogOutputter : public ILogOutputter {
public:
    FileLogOutputter(const char* logFile);
    virtual ~FileLogOutputter();

    // ILogOutputter overrides
    virtual void        open(const char* title);
    virtual void        close();
    virtual void        show(bool showIfEmpty);
    virtual bool        write(ELevel level, const char* message);

    void                setLogFilename(const char* title);

private:
    std::string            m_fileName;
};

//! Write log to system log
/*!
This outputter writes output to the system log.
*/
class SystemLogOutputter : public ILogOutputter {
public:
    SystemLogOutputter();
    virtual ~SystemLogOutputter();

    // ILogOutputter overrides
    virtual void        open(const char* title);
    virtual void        close();
    virtual void        show(bool showIfEmpty);
    virtual bool        write(ELevel level, const char* message);
};

//! Write log to system log only
/*!
Creating an object of this type inserts a StopLogOutputter followed
by a SystemLogOutputter into Log.  The destructor removes those
outputters.  Add one of these to any scope that needs to write to
the system log (only) and restore the old outputters when exiting
the scope.
*/
class SystemLogger {
public:
    SystemLogger(const char* title, bool blockConsole);
    ~SystemLogger();

private:
    ILogOutputter*        m_syslog;
    ILogOutputter*        m_stop;
};

//! Save log history
/*!
This outputter records the last N log messages.
*/
class BufferedLogOutputter : public ILogOutputter {
private:
    typedef std::deque<std::string> Buffer;

public:
    typedef Buffer::const_iterator const_iterator;

    BufferedLogOutputter(UInt32 maxBufferSize);
    virtual ~BufferedLogOutputter();

    //! @name accessors
    //@{

    //! Get start of buffer
    const_iterator        begin() const;

    //! Get end of buffer
    const_iterator        end() const;

    //@}

    // ILogOutputter overrides
    virtual void        open(const char* title);
    virtual void        close();
    virtual void        show(bool showIfEmpty);
    virtual bool        write(ELevel level, const char* message);
private:
    UInt32                m_maxBufferSize;
    Buffer                m_buffer;
};

//! Write log to message box
/*!
The level for each message is ignored.
*/
class MesssageBoxLogOutputter : public ILogOutputter {
public:
    MesssageBoxLogOutputter();
    virtual ~MesssageBoxLogOutputter();

    // ILogOutputter overrides
    virtual void        open(const char* title);
    virtual void        close();
    virtual void        show(bool showIfEmpty);
    virtual bool        write(ELevel level, const char* message);
};