barrier/src/lib/base/log_outputters.h

173 lines
4.3 KiB
C
Raw Normal View History

2012-06-10 16:50:54 +00:00
/*
* synergy -- mouse and keyboard sharing utility
* Copyright (C) 2012 Synergy Si Ltd.
* Copyright (C) 2002 Chris Schoeneman
2012-06-10 16:50:54 +00:00
*
* 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.
2012-06-10 16:50:54 +00:00
*
* 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
2012-06-10 16:50:54 +00:00
#include "mt/Thread.h"
#include "base/ILogOutputter.h"
#include "base/String.h"
#include "common/basic_types.h"
#include "common/stddeque.h"
2012-06-10 16:50:54 +00:00
#include <list>
#include <fstream>
//! 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.
*/
2014-11-11 13:51:47 +00:00
class StopLogOutputter : public ILogOutputter {
2012-06-10 16:50:54 +00:00
public:
2014-11-11 13:51:47 +00:00
StopLogOutputter();
virtual ~StopLogOutputter();
2012-06-10 16:50:54 +00:00
// 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.
*/
2014-11-11 13:51:47 +00:00
class ConsoleLogOutputter : public ILogOutputter {
2012-06-10 16:50:54 +00:00
public:
2014-11-11 13:51:47 +00:00
ConsoleLogOutputter();
virtual ~ConsoleLogOutputter();
2012-06-10 16:50:54 +00:00
// 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.
*/
2014-11-11 13:51:47 +00:00
class FileLogOutputter : public ILogOutputter {
2012-06-10 16:50:54 +00:00
public:
2014-11-11 13:51:47 +00:00
FileLogOutputter(const char* logFile);
virtual ~FileLogOutputter();
2012-06-10 16:50:54 +00:00
// 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);
2012-06-10 16:50:54 +00:00
private:
std::string m_fileName;
2012-06-10 16:50:54 +00:00
};
//! Write log to system log
/*!
This outputter writes output to the system log.
*/
2014-11-11 13:51:47 +00:00
class SystemLogOutputter : public ILogOutputter {
2012-06-10 16:50:54 +00:00
public:
2014-11-11 13:51:47 +00:00
SystemLogOutputter();
virtual ~SystemLogOutputter();
2012-06-10 16:50:54 +00:00
// 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
/*!
2014-11-11 13:51:47 +00:00
Creating an object of this type inserts a StopLogOutputter followed
by a SystemLogOutputter into Log. The destructor removes those
2012-06-10 16:50:54 +00:00
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.
*/
2014-11-11 13:51:47 +00:00
class SystemLogger {
2012-06-10 16:50:54 +00:00
public:
2014-11-11 13:51:47 +00:00
SystemLogger(const char* title, bool blockConsole);
~SystemLogger();
2012-06-10 16:50:54 +00:00
private:
ILogOutputter* m_syslog;
ILogOutputter* m_stop;
};
//! Save log history
/*!
This outputter records the last N log messages.
*/
2014-11-11 13:51:47 +00:00
class BufferedLogOutputter : public ILogOutputter {
2012-06-10 16:50:54 +00:00
private:
2014-11-11 13:51:47 +00:00
typedef std::deque<String> Buffer;
2012-06-10 16:50:54 +00:00
public:
2014-11-11 13:51:47 +00:00
typedef Buffer::const_iterator const_iterator;
2012-06-10 16:50:54 +00:00
2014-11-11 13:51:47 +00:00
BufferedLogOutputter(UInt32 maxBufferSize);
virtual ~BufferedLogOutputter();
2012-06-10 16:50:54 +00:00
//! @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;
2014-11-11 13:51:47 +00:00
Buffer m_buffer;
2012-06-10 16:50:54 +00:00
};
//! Write log to message box
/*!
The level for each message is ignored.
*/
2014-11-11 13:51:47 +00:00
class MesssageBoxLogOutputter : public ILogOutputter {
public:
2014-11-11 13:51:47 +00:00
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);
};