Add support of std::stream for logger

Logger can be used as a std::ostream.

// Console logger with color
auto console = spd::stdout_color_mt("console");
console->info() << "Welcome to spdlog!";
console->error() << "Some error message with arg" << 1 << "..";
pull/363/head
Benichou 9 years ago
parent 35865ee54e
commit b50d520999

@ -197,8 +197,42 @@ inline void spdlog::logger::critical(const T& msg)
log(level::critical, msg);
}
inline spdlog::ostream spdlog::logger::log(level::level_enum lvl)
{
if (!should_log(lvl))
return ostream(this, lvl);
}
inline spdlog::ostream spdlog::logger::trace()
{
return log(level::trace);
}
inline spdlog::ostream spdlog::logger::debug()
{
return log(level::debug);
}
inline spdlog::ostream spdlog::logger::info()
{
return log(level::info);
}
inline spdlog::ostream spdlog::logger::warn()
{
return log(level::warn);
}
inline spdlog::ostream spdlog::logger::error()
{
return log(level::err);
}
inline spdlog::ostream spdlog::logger::critical()
{
return log(level::critical);
}
//
// name and level

@ -0,0 +1,29 @@
//
// Copyright(c) 2017 Benoit Leforestier.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
//
#pragma once
template <class CharT, class Traits>
spdlog::basic_streambuf<CharT, Traits>::basic_streambuf(spdlog::logger* plogger, spdlog::level::level_enum lvl) :
{
}
template <class CharT, class Traits>
spdlog::basic_streambuf<CharT, Traits>::basic_streambuf(basic_streambuf&& rhs) :
{
}
template <class CharT, class Traits>
spdlog::basic_streambuf<CharT, Traits>::~basic_streambuf()
{
}
template <class CharT, class Traits>
std::streamsize spdlog::basic_streambuf<CharT, Traits>::xsputn(const char_type* __s, std::streamsize __n)
{
}

@ -14,6 +14,7 @@
#include <spdlog/sinks/base_sink.h>
#include <spdlog/common.h>
#include <spdlog/stream.h>
#include <vector>
#include <memory>
@ -52,6 +53,14 @@ public:
template <typename T> void error(const T&);
template <typename T> void critical(const T&);
ostream log(level::level_enum lvl);
ostream trace();
ostream debug();
ostream info();
ostream warn();
ostream error();
ostream critical();
bool should_log(level::level_enum) const;
void set_level(level::level_enum);
level::level_enum level() const;
@ -71,6 +80,9 @@ public:
const std::vector<sink_ptr>& sinks() const;
protected:
template <class CharT, class Traits>
friend class basic_streambuf;
virtual void _sink_it(details::log_msg&);
virtual void _set_pattern(const std::string&);
virtual void _set_formatter(formatter_ptr);
@ -92,3 +104,4 @@ protected:
}
#include <spdlog/details/logger_impl.h>
#include <spdlog/details/stream_impl.h>

@ -0,0 +1,26 @@
//
// Copyright(c) 2017 Benoit Leforestier.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
//
#pragma once
#include <ostream>
namespace spdlog
{
}
Loading…
Cancel
Save