2012-06-10 16:50:54 +00:00
|
|
|
/*
|
2024-09-17 19:00:25 +00:00
|
|
|
* Deskflow -- mouse and keyboard sharing utility
|
2016-09-07 14:24:00 +00:00
|
|
|
* Copyright (C) 2012-2016 Symless Ltd.
|
2012-09-04 02:09:56 +00:00
|
|
|
* Copyright (C) 2002 Chris Schoeneman
|
2024-07-02 19:07:06 +00:00
|
|
|
*
|
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
|
2015-05-03 02:33:52 +00:00
|
|
|
* found in the file LICENSE that should have accompanied this file.
|
2024-07-02 19:07:06 +00:00
|
|
|
*
|
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/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2014-02-28 12:36:45 +00:00
|
|
|
#include "base/log_outputters.h"
|
|
|
|
|
#include "arch/Arch.h"
|
2024-07-02 19:07:06 +00:00
|
|
|
#include "base/Path.h"
|
|
|
|
|
#include "base/TMethodJob.h"
|
2012-06-10 16:50:54 +00:00
|
|
|
|
|
|
|
|
#include <fstream>
|
2014-02-28 12:36:45 +00:00
|
|
|
|
2015-05-19 13:04:02 +00:00
|
|
|
enum EFileLogOutputter {
|
2024-07-02 19:07:06 +00:00
|
|
|
kFileSizeLimit = 1024 // kb
|
2015-05-19 13:04:02 +00:00
|
|
|
};
|
|
|
|
|
|
2012-06-10 16:50:54 +00:00
|
|
|
//
|
2014-11-11 13:51:47 +00:00
|
|
|
// StopLogOutputter
|
2012-06-10 16:50:54 +00:00
|
|
|
//
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
StopLogOutputter::StopLogOutputter() {
|
|
|
|
|
// do nothing
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
StopLogOutputter::~StopLogOutputter() {
|
|
|
|
|
// do nothing
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
void StopLogOutputter::open(const char *) {
|
|
|
|
|
// do nothing
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
void StopLogOutputter::close() {
|
|
|
|
|
// do nothing
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
void StopLogOutputter::show(bool) {
|
|
|
|
|
// do nothing
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
bool StopLogOutputter::write(ELevel, const char *) { return false; }
|
2012-06-10 16:50:54 +00:00
|
|
|
|
|
|
|
|
//
|
2014-11-11 13:51:47 +00:00
|
|
|
// ConsoleLogOutputter
|
2012-06-10 16:50:54 +00:00
|
|
|
//
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
ConsoleLogOutputter::ConsoleLogOutputter() {}
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
ConsoleLogOutputter::~ConsoleLogOutputter() {}
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
void ConsoleLogOutputter::open(const char *title) { ARCH->openConsole(title); }
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
void ConsoleLogOutputter::close() { ARCH->closeConsole(); }
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
void ConsoleLogOutputter::show(bool showIfEmpty) {
|
|
|
|
|
ARCH->showConsole(showIfEmpty);
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
bool ConsoleLogOutputter::write(ELevel level, const char *msg) {
|
|
|
|
|
ARCH->writeConsole(level, msg);
|
|
|
|
|
return true;
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
void ConsoleLogOutputter::flush() {}
|
2012-06-10 16:50:54 +00:00
|
|
|
|
|
|
|
|
//
|
2014-11-11 13:51:47 +00:00
|
|
|
// SystemLogOutputter
|
2012-06-10 16:50:54 +00:00
|
|
|
//
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
SystemLogOutputter::SystemLogOutputter() {
|
|
|
|
|
// do nothing
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
SystemLogOutputter::~SystemLogOutputter() {
|
|
|
|
|
// do nothing
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
void SystemLogOutputter::open(const char *title) { ARCH->openLog(title); }
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
void SystemLogOutputter::close() { ARCH->closeLog(); }
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
void SystemLogOutputter::show(bool showIfEmpty) { ARCH->showLog(showIfEmpty); }
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
bool SystemLogOutputter::write(ELevel level, const char *msg) {
|
|
|
|
|
ARCH->writeLog(level, msg);
|
|
|
|
|
return true;
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//
|
2014-11-11 13:51:47 +00:00
|
|
|
// SystemLogger
|
2012-06-10 16:50:54 +00:00
|
|
|
//
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
SystemLogger::SystemLogger(const char *title, bool blockConsole)
|
|
|
|
|
: m_stop(NULL) {
|
|
|
|
|
// redirect log messages
|
|
|
|
|
if (blockConsole) {
|
|
|
|
|
m_stop = new StopLogOutputter;
|
|
|
|
|
CLOG->insert(m_stop);
|
|
|
|
|
}
|
|
|
|
|
m_syslog = new SystemLogOutputter;
|
|
|
|
|
m_syslog->open(title);
|
|
|
|
|
CLOG->insert(m_syslog);
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
SystemLogger::~SystemLogger() {
|
|
|
|
|
CLOG->remove(m_syslog);
|
|
|
|
|
delete m_syslog;
|
|
|
|
|
if (m_stop != NULL) {
|
|
|
|
|
CLOG->remove(m_stop);
|
|
|
|
|
delete m_stop;
|
|
|
|
|
}
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//
|
2014-11-11 13:51:47 +00:00
|
|
|
// BufferedLogOutputter
|
2012-06-10 16:50:54 +00:00
|
|
|
//
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
BufferedLogOutputter::BufferedLogOutputter(UInt32 maxBufferSize)
|
|
|
|
|
: m_maxBufferSize(maxBufferSize) {
|
|
|
|
|
// do nothing
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
BufferedLogOutputter::~BufferedLogOutputter() {
|
|
|
|
|
// do nothing
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
BufferedLogOutputter::const_iterator BufferedLogOutputter::begin() const {
|
|
|
|
|
return m_buffer.begin();
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
BufferedLogOutputter::const_iterator BufferedLogOutputter::end() const {
|
|
|
|
|
return m_buffer.end();
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
void BufferedLogOutputter::open(const char *) {
|
|
|
|
|
// do nothing
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
void BufferedLogOutputter::close() {
|
|
|
|
|
// remove all elements from the buffer
|
|
|
|
|
m_buffer.clear();
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
void BufferedLogOutputter::show(bool) {
|
|
|
|
|
// do nothing
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
bool BufferedLogOutputter::write(ELevel, const char *message) {
|
|
|
|
|
while (m_buffer.size() >= m_maxBufferSize) {
|
|
|
|
|
m_buffer.pop_front();
|
|
|
|
|
}
|
|
|
|
|
m_buffer.push_back(String(message));
|
|
|
|
|
return true;
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//
|
2014-11-11 13:51:47 +00:00
|
|
|
// FileLogOutputter
|
2012-06-10 16:50:54 +00:00
|
|
|
//
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
FileLogOutputter::FileLogOutputter(const char *logFile) {
|
|
|
|
|
setLogFilename(logFile);
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
FileLogOutputter::~FileLogOutputter() {}
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
void FileLogOutputter::setLogFilename(const char *logFile) {
|
|
|
|
|
assert(logFile != NULL);
|
|
|
|
|
m_fileName = logFile;
|
2014-10-27 14:11:43 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
bool FileLogOutputter::write(ELevel level, const char *message) {
|
|
|
|
|
bool moveFile = false;
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
std::ofstream m_handle;
|
2024-09-17 19:00:25 +00:00
|
|
|
m_handle.open(deskflow::filesystem::path(m_fileName), std::fstream::app);
|
2024-07-02 19:07:06 +00:00
|
|
|
if (m_handle.is_open() && m_handle.fail() != true) {
|
|
|
|
|
m_handle << message << std::endl;
|
|
|
|
|
|
|
|
|
|
// when file size exceeds limits, move to 'old log' filename.
|
|
|
|
|
size_t p = m_handle.tellp();
|
|
|
|
|
if (p > (kFileSizeLimit * 1024)) {
|
|
|
|
|
moveFile = true;
|
2016-12-28 11:50:32 +00:00
|
|
|
}
|
2024-07-02 19:07:06 +00:00
|
|
|
}
|
|
|
|
|
m_handle.close();
|
2015-05-19 13:04:02 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
if (moveFile) {
|
|
|
|
|
String oldLogFilename =
|
2024-09-17 19:00:25 +00:00
|
|
|
deskflow::string::sprintf("%s.1", m_fileName.c_str());
|
2024-07-02 19:07:06 +00:00
|
|
|
remove(oldLogFilename.c_str());
|
|
|
|
|
rename(m_fileName.c_str(), oldLogFilename.c_str());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
void FileLogOutputter::open(const char *title) {}
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
void FileLogOutputter::close() {}
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
void FileLogOutputter::show(bool showIfEmpty) {}
|