mirror of
https://github.com/gabime/spdlog.git
synced 2024-12-24 09:31:34 +08:00
logger templates
This commit is contained in:
parent
c09df09b9c
commit
1c4be7fb88
@ -19,28 +19,26 @@ int main(int argc, char* argv[])
|
||||
{
|
||||
|
||||
if(argc || argv) {};
|
||||
const unsigned int howmany = argc <= 1 ? 1500000 :atoi(argv[1]);
|
||||
const unsigned int howmany = argc <= 1 ? 1000 :atoi(argv[1]);
|
||||
|
||||
auto fsink = std::make_shared<sinks::rotating_file_sink>("log", "txt", 1024*1024*50 , 5, 0);
|
||||
auto null_sink = std::make_shared<sinks::null_sink>();
|
||||
|
||||
|
||||
logger cout_logger ("cout", {sinks::stdout_sink()});
|
||||
cout_logger.info() << "Hello cout logger!";
|
||||
|
||||
logger my_logger ("my_logger", {null_sink});
|
||||
|
||||
std::string s(100, '0');
|
||||
|
||||
auto start = system_clock::now();
|
||||
for(unsigned int i = 0; i < howmany ; i++)
|
||||
my_logger.info() << s;
|
||||
my_logger.info("Hello logger");
|
||||
|
||||
|
||||
auto delta = system_clock::now() - start;
|
||||
auto delta_d = duration_cast<duration<double>> (delta).count();
|
||||
cout_logger.info() << "Total " << format(howmany);
|
||||
cout_logger.info() << "Delta " << format(delta_d);
|
||||
cout_logger.info() << "Rate: " << format(howmany/delta_d) << "/sec";
|
||||
cout_logger.info("Total ") << format(howmany);
|
||||
cout_logger.info("Delta ") << format(delta_d);
|
||||
cout_logger.info("Rate: ") << format(howmany/delta_d) << "/sec";
|
||||
|
||||
|
||||
return 0;
|
||||
|
@ -16,11 +16,12 @@ typedef enum
|
||||
INFO,
|
||||
WARNING,
|
||||
ERROR,
|
||||
CRITICAL,
|
||||
FATAL,
|
||||
NONE = 99
|
||||
} level_enum;
|
||||
|
||||
static const char* level_names[] { "debug", "info", "warning", "error", "fatal" };
|
||||
static const char* level_names[] { "debug", "info", "warning", "error", "critical", "fatal" };
|
||||
inline const char* to_str(c11log::level::level_enum l)
|
||||
{
|
||||
return level_names[l];
|
||||
|
@ -8,7 +8,6 @@
|
||||
// line logger class. should be used by the logger as an rvalue only.
|
||||
// aggregates logging string until the end of the line and then calls the logger upon destruction
|
||||
|
||||
|
||||
namespace c11log
|
||||
{
|
||||
//class logger;
|
||||
@ -49,7 +48,6 @@ public:
|
||||
_oss(),
|
||||
_enabled(other._enabled) {}
|
||||
|
||||
|
||||
~line_logger()
|
||||
{
|
||||
if (_enabled)
|
||||
@ -59,15 +57,13 @@ public:
|
||||
_callback_logger->_log_it(_log_msg);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
template<typename T>
|
||||
line_logger&& operator<<(const T& what)
|
||||
line_logger& operator<<(const T& what)
|
||||
{
|
||||
if (_enabled)
|
||||
_oss << what;
|
||||
|
||||
return std::move(*this);
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
@ -75,8 +71,6 @@ private:
|
||||
log_msg _log_msg;
|
||||
details::stack_oss _oss;
|
||||
bool _enabled;
|
||||
|
||||
|
||||
};
|
||||
} //Namespace details
|
||||
} // Namespace c11log
|
||||
|
@ -53,12 +53,14 @@ public:
|
||||
const std::string& get_name() const;
|
||||
bool should_log(c11log::level::level_enum) const;
|
||||
|
||||
details::line_logger log(level::level_enum);
|
||||
details::line_logger debug();
|
||||
details::line_logger info();
|
||||
details::line_logger warn();
|
||||
details::line_logger error();
|
||||
details::line_logger fatal();
|
||||
details::line_logger log(c11log::level::level_enum);
|
||||
template<typename T> details::line_logger debug(const T&);
|
||||
template<typename T> details::line_logger info(const T&);
|
||||
template<typename T> details::line_logger warn(const T&);
|
||||
template<typename T> details::line_logger error(const T&);
|
||||
template<typename T> details::line_logger critical(const T&);
|
||||
template<typename T> details::line_logger fatal(const T&);
|
||||
|
||||
|
||||
private:
|
||||
friend details::line_logger;
|
||||
@ -103,34 +105,77 @@ inline c11log::details::line_logger c11log::logger::log(c11log::level::level_enu
|
||||
return details::line_logger(this, msg_level, msg_level >= _logger_level);
|
||||
}
|
||||
|
||||
inline c11log::details::line_logger c11log::logger::debug()
|
||||
|
||||
template<typename T>
|
||||
inline c11log::details::line_logger c11log::logger::debug(const T& what)
|
||||
{
|
||||
return log(c11log::level::DEBUG);
|
||||
bool really_log = should_log(level::DEBUG);
|
||||
details::line_logger l(this, level::DEBUG, really_log);
|
||||
if(really_log)
|
||||
l << what;
|
||||
return l;
|
||||
}
|
||||
inline c11log::details::line_logger c11log::logger::info()
|
||||
|
||||
template<typename T>
|
||||
inline c11log::details::line_logger c11log::logger::info(const T& what)
|
||||
{
|
||||
return log(c11log::level::INFO);
|
||||
bool really_log = should_log(level::INFO);
|
||||
details::line_logger l(this, level::INFO, really_log);
|
||||
if(really_log)
|
||||
l << what;
|
||||
return l;
|
||||
}
|
||||
inline c11log::details::line_logger c11log::logger::warn()
|
||||
|
||||
|
||||
template<typename T>
|
||||
inline c11log::details::line_logger c11log::logger::warn(const T& what)
|
||||
{
|
||||
return log(c11log::level::WARNING);
|
||||
bool really_log = should_log(level::WARNING);
|
||||
details::line_logger l(this, level::WARNING, really_log);
|
||||
if(really_log)
|
||||
l << what;
|
||||
return l;
|
||||
}
|
||||
inline c11log::details::line_logger c11log::logger::error()
|
||||
|
||||
|
||||
template<typename T>
|
||||
inline c11log::details::line_logger c11log::logger::error(const T& what)
|
||||
{
|
||||
return log(level::ERROR);
|
||||
bool really_log = should_log(level::ERROR);
|
||||
details::line_logger l(this, level::ERROR, really_log);
|
||||
if(really_log)
|
||||
l << what;
|
||||
return l;
|
||||
}
|
||||
inline c11log::details::line_logger c11log::logger::fatal()
|
||||
|
||||
|
||||
template<typename T>
|
||||
inline c11log::details::line_logger c11log::logger::critical(const T& what)
|
||||
{
|
||||
return log(c11log::level::FATAL);
|
||||
bool really_log = should_log(level::CRITICAL);
|
||||
details::line_logger l(this, level::CRITICAL, really_log);
|
||||
if(really_log)
|
||||
l << what;
|
||||
return l;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline c11log::details::line_logger c11log::logger::fatal(const T& what)
|
||||
{
|
||||
bool really_log = should_log(level::FATAL);
|
||||
details::line_logger l(this, level::FATAL, really_log);
|
||||
if(really_log)
|
||||
l << what;
|
||||
return l;
|
||||
}
|
||||
|
||||
|
||||
|
||||
inline const std::string& c11log::logger::get_name() const
|
||||
{
|
||||
return _logger_name;
|
||||
}
|
||||
|
||||
|
||||
inline void c11log::logger::set_level(c11log::level::level_enum level)
|
||||
{
|
||||
_logger_level.store(level);
|
||||
|
@ -74,6 +74,7 @@ inline void c11log::sinks::async_sink::_sink_it(const details::log_msg& msg)
|
||||
auto new_shared = std::shared_ptr<details::log_msg>(new_msg, [](details::log_msg* msg_to_delete)
|
||||
{
|
||||
delete []msg_to_delete->msg_buf.first;
|
||||
delete msg_to_delete;
|
||||
});
|
||||
_q.push(new_shared);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user