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 <string>
|
|
|
|
#include <sstream>
|
|
|
|
#include <functional>
|
|
|
|
|
|
|
|
#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();
|
|
|
|
std::string prefix();
|
|
|
|
|
|
|
|
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-04-15 14:46:44 -05:00
|
|
|
/// Signal safe prefix logging
|
|
|
|
void signalLogPrefix();
|
|
|
|
/// Signal safe logging
|
|
|
|
void signalLog(const char *message);
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
StreamLogger(std::function<void(const std::string&)> func)
|
2016-08-02 02:29:19 -05:00
|
|
|
: _func(std::move(func))
|
2016-04-14 19:12:43 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-08-03 03:00:38 -05:00
|
|
|
StreamLogger(StreamLogger&& sl) noexcept
|
2016-08-02 02:29:19 -05:00
|
|
|
: _stream(sl._stream.str())
|
2016-04-14 19:12:43 -05:00
|
|
|
, _func(std::move(sl._func))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void flush() const
|
|
|
|
{
|
|
|
|
_func(_stream.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::ostringstream _stream;
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::function<void(const std::string&)> _func;
|
|
|
|
};
|
|
|
|
|
|
|
|
inline
|
|
|
|
StreamLogger trace()
|
|
|
|
{
|
|
|
|
return StreamLogger([](const std::string& msg) { trace(msg);});
|
|
|
|
}
|
|
|
|
|
|
|
|
inline
|
|
|
|
StreamLogger debug()
|
|
|
|
{
|
|
|
|
return StreamLogger([](const std::string& msg) { debug(msg);});
|
|
|
|
}
|
|
|
|
|
|
|
|
inline
|
|
|
|
StreamLogger info()
|
|
|
|
{
|
|
|
|
return StreamLogger([](const std::string& msg) { info(msg);});
|
|
|
|
}
|
|
|
|
|
|
|
|
inline
|
|
|
|
StreamLogger warn()
|
|
|
|
{
|
|
|
|
return StreamLogger([](const std::string& msg) { warn(msg);});
|
|
|
|
}
|
|
|
|
|
|
|
|
inline
|
|
|
|
StreamLogger error()
|
|
|
|
{
|
|
|
|
return StreamLogger([](const std::string& msg) { error(msg);});
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename U>
|
|
|
|
StreamLogger& operator <<(StreamLogger& lhs, const U& rhs)
|
|
|
|
{
|
|
|
|
lhs._stream << rhs;
|
|
|
|
return lhs;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename U>
|
|
|
|
StreamLogger& operator <<(StreamLogger&& lhs, U&& rhs)
|
|
|
|
{
|
|
|
|
lhs._stream << rhs;
|
|
|
|
return lhs;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline
|
|
|
|
void operator <<(StreamLogger& lhs, const _end_marker&)
|
|
|
|
{
|
|
|
|
(void)end;
|
|
|
|
lhs.flush();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|