Files
thehub/libs/utils/LogChannels.cpp
T

239 lines
6.3 KiB
C++
Raw Permalink Normal View History

/*
2017-11-09 19:34:51 +01:00
* This file is part of the Flowee project
2022-11-24 17:50:08 +01:00
* Copyright (C) 2017-2022 Tom Zander <tom@flowee.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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/>.
*/
2022-11-24 17:50:08 +01:00
#include "Logger.h"
#include "LogChannels_p.h"
#include <boost/filesystem.hpp>
#include <iostream>
2017-05-15 14:39:52 +02:00
namespace {
std::string shortenMethod(const char *methodName) {
assert(methodName);
const char *start = strchr(methodName, ' ');
const char *end = strchr(methodName, '(');
if (end) {
if (!start || start > end)
start = methodName;
else
++start;
2017-05-15 14:39:52 +02:00
++end;
std::string copy(start, end - start);
2020-10-27 14:28:49 +01:00
return copy;
2017-05-15 14:39:52 +02:00
}
return std::string();
}
}
2022-11-24 17:50:08 +01:00
2017-05-15 14:39:52 +02:00
Log::Channel::Channel(TimeStampFormat f)
: m_timeStampFormat(f),
2022-11-24 17:50:08 +01:00
m_logSubSecondPrecision(true)
{
}
2017-05-15 14:39:52 +02:00
Log::Channel::TimeStampFormat Log::Channel::timeStampFormat() const
{
return m_timeStampFormat;
}
void Log::Channel::setTimeStampFormat(const TimeStampFormat &timeStampFormat)
{
m_timeStampFormat = timeStampFormat;
}
2022-11-24 17:50:08 +01:00
bool Log::Channel::logSubSecondPrecision() const
{
2022-11-24 17:50:08 +01:00
return m_logSubSecondPrecision;
}
2022-11-24 17:50:08 +01:00
void Log::Channel::setlogSubSecondPrecision(bool showSubSecondPrecision)
{
2022-11-24 17:50:08 +01:00
m_logSubSecondPrecision = showSubSecondPrecision;
}
// ------------------------------------------------------
ConfigurableChannel::ConfigurableChannel(TimeStampFormat f)
: Channel(f),
m_timeStampFormat(f),
m_logSection(true),
m_logLineNumber(false),
m_logMethodName(true),
m_logFilename(false)
{
}
bool ConfigurableChannel::logSection() const
{
return m_logSection;
}
void ConfigurableChannel::setLogSection(bool printSection)
{
m_logSection = printSection;
}
bool ConfigurableChannel::logLineNumber() const
{
return m_logLineNumber;
}
void ConfigurableChannel::setLogLineNumber(bool printLineNumber)
{
m_logLineNumber = printLineNumber;
}
bool ConfigurableChannel::logMethodName() const
{
return m_logMethodName;
}
void ConfigurableChannel::setLogMethodName(bool printMethodName)
{
m_logMethodName = printMethodName;
}
bool ConfigurableChannel::logFilename() const
{
return m_logFilename;
}
void ConfigurableChannel::setLogFilename(bool printFilename)
{
m_logFilename = printFilename;
}
2017-05-15 14:39:52 +02:00
// ------------------------------------------------------
ConsoleLogChannel::ConsoleLogChannel()
2022-11-24 17:50:08 +01:00
: ConfigurableChannel(TimeOnly)
2017-05-15 14:39:52 +02:00
{
}
2018-08-14 09:27:07 +02:00
void ConsoleLogChannel::setPrefix(const char *prefix)
2018-05-06 22:00:16 +02:00
{
m_prefix = prefix;
}
2017-05-15 14:39:52 +02:00
void ConsoleLogChannel::pushLog(int64_t, std::string *timestamp, const std::string &line, const char *filename, int lineNumber, const char *methodName, short logSection, short logLevel)
{
2018-05-06 22:00:16 +02:00
std::ostream &out = (logLevel == Log::WarningLevel || logLevel == Log::FatalLevel) ? std::clog : std::cout;
2017-05-15 14:39:52 +02:00
if (timestamp)
out << *timestamp << ' ';
2022-11-24 17:50:08 +01:00
if (m_logSection && logSection) {
2017-05-15 14:39:52 +02:00
out << '[';
const std::string section = Log::Manager::sectionString(logSection);
if (!section.empty())
out << section;
else
out << logSection;
out << "] ";
}
2018-08-14 09:27:07 +02:00
if (m_prefix)
2018-05-06 22:00:16 +02:00
out << m_prefix << ' ';
2022-11-24 17:50:08 +01:00
if (m_logFilename && filename)
out << filename << (m_logLineNumber ? ':' : ' ');
if (m_logLineNumber && lineNumber)
2017-05-15 14:39:52 +02:00
out << lineNumber << ';';
2022-11-24 17:50:08 +01:00
if (m_logMethodName && methodName) {
2017-05-15 14:39:52 +02:00
std::string m(shortenMethod(methodName));
if (!m.empty())
out << m << ") ";
}
out << line;
if (line.empty() || line.back() != '\n')
out << std::endl;
out.flush();
}
FileLogChannel::FileLogChannel(const boost::filesystem::path &logFilename)
2022-11-24 17:50:08 +01:00
: ConfigurableChannel(DateTime),
m_fileout(0),
m_logFilename(logFilename)
{
}
FileLogChannel::~FileLogChannel()
{
if (m_fileout)
fclose(m_fileout);
}
static void FileWriteStr(const std::string &str, FILE *fp) {
fwrite(str.data(), 1, str.size(), fp);
}
2017-05-15 14:39:52 +02:00
void FileLogChannel::pushLog(int64_t, std::string *timestamp, const std::string &line, const char*, int, const char *methodName, short logSection, short)
{
if (m_fileout) {
2017-05-15 14:39:52 +02:00
if (timestamp) {
FileWriteStr(*timestamp, m_fileout);
FileWriteStr(" ", m_fileout);
}
2022-11-24 17:50:08 +01:00
if (m_logSection && logSection) {
2017-05-15 14:39:52 +02:00
FileWriteStr("[", m_fileout);
const std::string section = Log::Manager::sectionString(logSection);
if (section.empty()) {
std::ostringstream num;
num << logSection;
FileWriteStr(num.str() , m_fileout);
} else {
FileWriteStr(section , m_fileout);
}
FileWriteStr("] ", m_fileout);
}
2022-11-24 17:50:08 +01:00
if (m_logMethodName && methodName) {
2017-05-15 14:39:52 +02:00
std::string m(shortenMethod(methodName));
if (!m.empty()) {
FileWriteStr(m , m_fileout);
FileWriteStr(") " , m_fileout);
}
}
FileWriteStr(line, m_fileout);
2017-05-12 15:06:08 +02:00
if (line.empty() || line.back() != '\n')
FileWriteStr("\n", m_fileout);
}
}
void FileLogChannel::reopenLogFiles()
{
if (m_fileout)
fclose(m_fileout);
if (m_logFilename.empty())
return;
boost::system::error_code error;
boost::filesystem::create_directories(m_logFilename.parent_path(), error);
m_fileout = fopen(m_logFilename.string().c_str(), "a");
if (!m_fileout)
throw std::runtime_error(std::string("Failed to open(append) file: ") + m_logFilename.string());
setbuf(m_fileout, NULL); // unbuffered
}
2018-03-08 19:32:14 +01:00
void FileLogChannel::setPath(const std::string &path)
{
2019-05-08 17:04:09 +02:00
if (!m_logFilename.empty() && boost::filesystem::is_directory(path))
// interpret as dir, append previous filename.
m_logFilename = boost::filesystem::path(path) / m_logFilename.filename();
else
m_logFilename = path;
2018-03-08 19:32:14 +01:00
}