2016-04-14 19:12:43 -05:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
|
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef INCLUDED_LOG_HPP
|
|
|
|
#define INCLUDED_LOG_HPP
|
|
|
|
|
|
|
|
#include <functional>
|
2016-10-29 20:15:00 -05:00
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
2016-04-14 19:12:43 -05:00
|
|
|
|
|
|
|
#include <Poco/Logger.h>
|
|
|
|
|
|
|
|
namespace Log
|
|
|
|
{
|
2016-08-07 22:32:39 -05:00
|
|
|
void initialize(const std::string& name,
|
|
|
|
const std::string& logLevel = "trace",
|
|
|
|
const bool withColor = true,
|
|
|
|
const bool logToFile = false,
|
|
|
|
std::map<std::string, std::string> config = {});
|
2016-04-14 19:12:43 -05:00
|
|
|
Poco::Logger& logger();
|
2016-10-30 18:16:57 -05:00
|
|
|
std::string prefix(const char* level);
|
2016-04-14 19:12:43 -05:00
|
|
|
|
|
|
|
void trace(const std::string& msg);
|
|
|
|
void debug(const std::string& msg);
|
|
|
|
void info(const std::string& msg);
|
|
|
|
void warn(const std::string& msg);
|
|
|
|
void error(const std::string& msg);
|
|
|
|
void syserror(const std::string& msg);
|
2016-10-14 04:47:48 -05:00
|
|
|
void fatal(const std::string& msg);
|
|
|
|
void sysfatal(const std::string& msg);
|
2016-04-14 19:12:43 -05:00
|
|
|
|
2016-10-30 18:16:57 -05:00
|
|
|
inline bool traceEnabled() { return logger().getLevel() >= Poco::Message::PRIO_TRACE; }
|
|
|
|
inline bool debugEnabled() { return logger().getLevel() >= Poco::Message::PRIO_DEBUG; }
|
|
|
|
inline bool infoEnabled() { return logger().getLevel() >= Poco::Message::PRIO_INFORMATION; }
|
|
|
|
inline bool warnEnabled() { return logger().getLevel() >= Poco::Message::PRIO_WARNING; }
|
|
|
|
inline bool errorEnabled() { return logger().getLevel() >= Poco::Message::PRIO_ERROR; }
|
|
|
|
inline bool fatalEnabled() { return logger().getLevel() >= Poco::Message::PRIO_FATAL; }
|
2016-10-30 18:07:41 -05:00
|
|
|
|
2016-04-15 14:46:44 -05:00
|
|
|
/// Signal safe prefix logging
|
|
|
|
void signalLogPrefix();
|
|
|
|
/// Signal safe logging
|
2016-10-29 20:15:00 -05:00
|
|
|
void signalLog(const char* message);
|
2016-04-15 14:46:44 -05:00
|
|
|
|
2016-04-14 19:12:43 -05:00
|
|
|
/// The following is to write streaming logs.
|
|
|
|
/// Log::info() << "Value: 0x" << std::hex << value
|
|
|
|
/// << ", pointer: " << this << Log::end;
|
|
|
|
static const struct _end_marker
|
|
|
|
{
|
|
|
|
_end_marker()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
} end;
|
|
|
|
|
2016-08-13 23:01:13 -05:00
|
|
|
/// Helper class to support implementing streaming
|
|
|
|
/// operator for logging.
|
2016-04-14 19:12:43 -05:00
|
|
|
class StreamLogger
|
|
|
|
{
|
2016-10-29 20:15:00 -05:00
|
|
|
public:
|
2016-10-30 18:07:41 -05:00
|
|
|
/// No-op instance.
|
|
|
|
StreamLogger()
|
|
|
|
: _enabled(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-10-30 18:16:57 -05:00
|
|
|
StreamLogger(std::function<void(const std::string&)> func, const char* level)
|
2016-10-30 18:07:41 -05:00
|
|
|
: _func(std::move(func)),
|
|
|
|
_enabled(true)
|
2016-10-29 20:15:00 -05:00
|
|
|
{
|
2016-10-30 18:16:57 -05:00
|
|
|
_stream << prefix(level);
|
2016-10-29 20:15:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
StreamLogger(StreamLogger&& sl) noexcept
|
2016-10-30 18:07:41 -05:00
|
|
|
: _stream(sl._stream.str()),
|
|
|
|
_func(std::move(sl._func)),
|
|
|
|
_enabled(sl._enabled)
|
2016-10-29 20:15:00 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-10-30 18:07:41 -05:00
|
|
|
bool enabled() const { return _enabled; }
|
|
|
|
|
2016-10-29 20:15:00 -05:00
|
|
|
void flush() const
|
|
|
|
{
|
2016-10-30 18:07:41 -05:00
|
|
|
if (_enabled)
|
|
|
|
{
|
|
|
|
_func(_stream.str());
|
|
|
|
}
|
2016-10-29 20:15:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
std::ostringstream _stream;
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::function<void(const std::string&)> _func;
|
2016-10-30 18:07:41 -05:00
|
|
|
const bool _enabled;
|
2016-04-14 19:12:43 -05:00
|
|
|
};
|
|
|
|
|
2016-10-29 20:15:00 -05:00
|
|
|
inline StreamLogger trace()
|
2016-04-14 19:12:43 -05:00
|
|
|
{
|
2016-10-30 18:07:41 -05:00
|
|
|
return traceEnabled()
|
2016-10-30 18:16:57 -05:00
|
|
|
? StreamLogger([](const std::string& msg) { logger().trace(msg); }, "TRC")
|
2016-10-30 18:07:41 -05:00
|
|
|
: StreamLogger();
|
2016-04-14 19:12:43 -05:00
|
|
|
}
|
|
|
|
|
2016-10-29 20:15:00 -05:00
|
|
|
inline StreamLogger debug()
|
2016-04-14 19:12:43 -05:00
|
|
|
{
|
2016-10-30 18:16:57 -05:00
|
|
|
return debugEnabled()
|
|
|
|
? StreamLogger([](const std::string& msg) { logger().debug(msg); }, "DBG")
|
|
|
|
: StreamLogger();
|
2016-04-14 19:12:43 -05:00
|
|
|
}
|
|
|
|
|
2016-10-29 20:15:00 -05:00
|
|
|
inline StreamLogger info()
|
2016-04-14 19:12:43 -05:00
|
|
|
{
|
2016-10-30 18:16:57 -05:00
|
|
|
return infoEnabled()
|
|
|
|
? StreamLogger([](const std::string& msg) { logger().information(msg); }, "INF")
|
|
|
|
: StreamLogger();
|
2016-04-14 19:12:43 -05:00
|
|
|
}
|
|
|
|
|
2016-10-29 20:15:00 -05:00
|
|
|
inline StreamLogger warn()
|
2016-04-14 19:12:43 -05:00
|
|
|
{
|
2016-10-30 18:16:57 -05:00
|
|
|
return warnEnabled()
|
|
|
|
? StreamLogger([](const std::string& msg) { logger().warning(msg); }, "WRN")
|
|
|
|
: StreamLogger();
|
2016-04-14 19:12:43 -05:00
|
|
|
}
|
|
|
|
|
2016-10-29 20:15:00 -05:00
|
|
|
inline StreamLogger error()
|
2016-04-14 19:12:43 -05:00
|
|
|
{
|
2016-10-30 18:16:57 -05:00
|
|
|
return errorEnabled()
|
|
|
|
? StreamLogger([](const std::string& msg) { logger().error(msg); }, "ERR")
|
|
|
|
: StreamLogger();
|
2016-04-14 19:12:43 -05:00
|
|
|
}
|
|
|
|
|
2016-10-29 20:15:00 -05:00
|
|
|
inline StreamLogger fatal()
|
2016-10-14 04:47:48 -05:00
|
|
|
{
|
2016-10-30 18:16:57 -05:00
|
|
|
return fatalEnabled()
|
|
|
|
? StreamLogger([](const std::string& msg) { logger().fatal(msg); }, "FTL")
|
|
|
|
: StreamLogger();
|
2016-10-14 04:47:48 -05:00
|
|
|
}
|
|
|
|
|
2016-04-14 19:12:43 -05:00
|
|
|
template <typename U>
|
2016-10-29 20:15:00 -05:00
|
|
|
StreamLogger& operator<<(StreamLogger& lhs, const U& rhs)
|
2016-04-14 19:12:43 -05:00
|
|
|
{
|
2016-10-30 18:07:41 -05:00
|
|
|
if (lhs.enabled())
|
|
|
|
{
|
|
|
|
lhs._stream << rhs;
|
|
|
|
}
|
|
|
|
|
2016-04-14 19:12:43 -05:00
|
|
|
return lhs;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename U>
|
2016-10-29 20:15:00 -05:00
|
|
|
StreamLogger& operator<<(StreamLogger&& lhs, U&& rhs)
|
2016-04-14 19:12:43 -05:00
|
|
|
{
|
2016-10-30 18:07:41 -05:00
|
|
|
if (lhs.enabled())
|
|
|
|
{
|
|
|
|
lhs._stream << rhs;
|
|
|
|
}
|
|
|
|
|
2016-04-14 19:12:43 -05:00
|
|
|
return lhs;
|
|
|
|
}
|
|
|
|
|
2016-10-29 20:15:00 -05:00
|
|
|
inline void operator<<(StreamLogger& lhs, const _end_marker&)
|
2016-04-14 19:12:43 -05:00
|
|
|
{
|
|
|
|
(void)end;
|
|
|
|
lhs.flush();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-06 21:10:32 -06:00
|
|
|
#define LOG_BODY(LVL, X) std::ostringstream oss_; oss_ << Log::prefix(LVL) << X << "| " << __FILE__ << ':' << __LINE__
|
2016-11-03 19:27:28 -05:00
|
|
|
#define LOG_TRC(X) if (Log::traceEnabled()) { LOG_BODY("TRC", X); Log::logger().trace(oss_.str()); }
|
|
|
|
#define LOG_DBG(X) if (Log::debugEnabled()) { LOG_BODY("DBG", X); Log::logger().debug(oss_.str()); }
|
|
|
|
#define LOG_INF(X) if (Log::infoEnabled()) { LOG_BODY("INF", X); Log::logger().information(oss_.str()); }
|
|
|
|
#define LOG_WRN(X) if (Log::warnEnabled()) { LOG_BODY("WRN", X); Log::logger().warning(oss_.str()); }
|
|
|
|
#define LOG_ERR(X) if (Log::errorEnabled()) { LOG_BODY("ERR", X); Log::logger().error(oss_.str()); }
|
2016-11-13 10:59:34 -06:00
|
|
|
#define LOG_SYS(X) if (Log::errorEnabled()) { LOG_BODY("ERR", X << " (errno: " << std::strerror(errno) << ")"); Log::logger().error(oss_.str()); }
|
2016-11-03 19:27:28 -05:00
|
|
|
#define LOG_FTL(X) if (Log::fatalEnabled()) { LOG_BODY("FTL", X); Log::logger().fatal(oss_.str()); }
|
2016-11-13 10:59:34 -06:00
|
|
|
#define LOG_SFL(X) if (Log::errorEnabled()) { LOG_BODY("FTL", X << " (errno: " << std::strerror(errno) << ")"); Log::logger().fatal(oss_.str()); }
|
2016-10-31 01:00:50 -05:00
|
|
|
|
2016-04-14 19:12:43 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|