2016-02-29 20:00:38 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <boost/log/common.hpp>
|
|
|
|
#include <boost/log/sinks/basic_sink_backend.hpp>
|
|
|
|
#include <boost/log/sinks/frontend_requirements.hpp>
|
|
|
|
#include <boost/log/sinks/text_ostream_backend.hpp>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <mutex>
|
|
|
|
#include <tuple>
|
2016-03-01 20:57:05 +00:00
|
|
|
#include "centiseconds.h"
|
2016-03-08 21:59:44 +00:00
|
|
|
#include <boost/filesystem.hpp>
|
2016-04-09 20:07:25 +00:00
|
|
|
#include "tools.h"
|
2016-03-08 20:44:57 +00:00
|
|
|
#include "enumTools.h"
|
2016-04-09 20:07:25 +00:00
|
|
|
#include "Timed.h"
|
2016-02-29 20:00:38 +00:00
|
|
|
|
|
|
|
enum class LogLevel {
|
|
|
|
Trace,
|
|
|
|
Debug,
|
|
|
|
Info,
|
|
|
|
Warning,
|
|
|
|
Error,
|
|
|
|
Fatal,
|
|
|
|
EndSentinel
|
|
|
|
};
|
|
|
|
|
2016-03-08 20:44:57 +00:00
|
|
|
template<>
|
|
|
|
const std::string& getEnumTypeName<LogLevel>();
|
2016-02-29 20:00:38 +00:00
|
|
|
|
2016-03-08 20:44:57 +00:00
|
|
|
template<>
|
|
|
|
const std::vector<std::tuple<LogLevel, std::string>>& getEnumMembers<LogLevel>();
|
|
|
|
|
|
|
|
std::ostream& operator<<(std::ostream& stream, LogLevel value);
|
|
|
|
|
|
|
|
std::istream& operator>>(std::istream& stream, LogLevel& value);
|
2016-02-29 20:00:38 +00:00
|
|
|
|
|
|
|
using LoggerType = boost::log::sources::severity_logger_mt<LogLevel>;
|
|
|
|
|
2016-03-08 21:59:44 +00:00
|
|
|
BOOST_LOG_GLOBAL_LOGGER(globalLogger, LoggerType)
|
2016-02-29 20:00:38 +00:00
|
|
|
|
|
|
|
#define LOG(level) \
|
|
|
|
BOOST_LOG_STREAM_WITH_PARAMS(globalLogger::get(), (::boost::log::keywords::severity = level))
|
|
|
|
|
|
|
|
#define LOG_TRACE LOG(LogLevel::Trace)
|
|
|
|
#define LOG_DEBUG LOG(LogLevel::Debug)
|
|
|
|
#define LOG_INFO LOG(LogLevel::Info)
|
|
|
|
#define LOG_WARNING LOG(LogLevel::Warning)
|
|
|
|
#define LOG_ERROR LOG(LogLevel::Error)
|
|
|
|
#define LOG_FATAL LOG(LogLevel::Fatal)
|
|
|
|
|
|
|
|
class PausableBackendAdapter :
|
|
|
|
public boost::log::sinks::basic_formatted_sink_backend<char, boost::log::sinks::concurrent_feeding>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
PausableBackendAdapter(boost::shared_ptr<boost::log::sinks::text_ostream_backend> backend);
|
2016-03-06 19:40:31 +00:00
|
|
|
~PausableBackendAdapter();
|
2016-02-29 20:00:38 +00:00
|
|
|
void consume(const boost::log::record_view& recordView, const std::string message);
|
|
|
|
void pause();
|
|
|
|
void resume();
|
|
|
|
private:
|
|
|
|
boost::shared_ptr<boost::log::sinks::text_ostream_backend> backend;
|
|
|
|
std::vector<std::tuple<boost::log::record_view, std::string>> buffer;
|
|
|
|
std::mutex mutex;
|
|
|
|
bool isPaused = false;
|
|
|
|
};
|
|
|
|
|
2016-03-08 21:59:44 +00:00
|
|
|
boost::shared_ptr<PausableBackendAdapter> addPausableStderrSink(LogLevel minLogLevel);
|
|
|
|
|
|
|
|
void addFileSink(const boost::filesystem::path& logFilePath, LogLevel minLogLevel);
|
2016-03-01 20:57:05 +00:00
|
|
|
|
2016-04-09 20:07:25 +00:00
|
|
|
template<typename TValue>
|
|
|
|
void logTimedEvent(const std::string& eventName, const Timed<TValue> timedValue) {
|
|
|
|
LOG_DEBUG
|
|
|
|
<< "##" << eventName << "[" << formatDuration(timedValue.getStart()) << "-" << formatDuration(timedValue.getEnd()) << "]: "
|
|
|
|
<< timedValue.getValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename TValue>
|
|
|
|
void logTimedEvent(const std::string& eventName, const TimeRange& timeRange, const TValue& value) {
|
|
|
|
logTimedEvent(eventName, Timed<TValue>(timeRange, value));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename TValue>
|
|
|
|
void logTimedEvent(const std::string& eventName, centiseconds start, centiseconds end, const TValue& value) {
|
|
|
|
logTimedEvent(eventName, Timed<TValue>(start, end, value));
|
|
|
|
}
|