var names and some minor cleanups

This commit is contained in:
gabime 2014-03-22 16:26:08 +02:00
parent 8494590fb3
commit a287bccd40
5 changed files with 23 additions and 47 deletions

View File

@ -1,12 +1,10 @@
// example.cpp : Simple logger example // example.cpp : Simple logger example
// //
#include <string>
#include <functional>
#include "c11log/logger.h" #include "c11log/logger.h"
#include "c11log/sinks/async_sink.h" #include "c11log/sinks/async_sink.h"
#include "c11log/sinks/file_sinks.h" #include "c11log/sinks/file_sinks.h"
#include "c11log/sinks/console_sinks.h" #include "c11log/sinks/console_sinks.h"
#include "c11log/details/log_msg.h"
#include "utils.h" #include "utils.h"
@ -16,24 +14,25 @@ using namespace std::chrono;
using namespace c11log; using namespace c11log;
using namespace utils; using namespace utils;
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
if(argc || argv) {}; if(argc || argv) {};
const unsigned int howmany = 1000000;
auto fsink = std::make_shared<sinks::rotating_file_sink>("log", "txt", 1024*1024*50 , 5, 0); auto fsink = std::make_shared<sinks::rotating_file_sink>("log", "txt", 1024*1024*50 , 5, 0);
//auto fsink = std::make_shared<sinks::simple_file_sink>("simplelog", "txt"); //auto fsink = std::make_shared<sinks::simple_file_sink>("simplelog", "txt");
auto null_sink = std::make_shared<sinks::null_sink>(); auto null_sink = std::make_shared<sinks::null_sink>();
logger cout_logger ("cout", {null_sink, sinks::stdout_sink()}); logger cout_logger ("cout", {null_sink, sinks::stdout_sink()});
cout_logger.info() << "Hello cout logger!"; cout_logger.info() << "Hello cout logger!";
logger my_logger ("my_logger", {null_sink}); logger my_logger ("my_logger", {null_sink});
std::string s(100, '0'); std::string s(100, '0');
const unsigned int howmany = 5000000;
auto start = system_clock::now(); auto start = system_clock::now();
for(unsigned int i = 0; i < howmany ; i++) for(unsigned int i = 0; i < howmany ; i++)
my_logger.info() << s; my_logger.info() << s;

View File

@ -5,14 +5,11 @@
#include <cstring> #include <cstring>
#include <vector> #include <vector>
// Fast buffer // Fast memory storage
// stores its contents on the stack when possible, in vector<char> otherwise // stores its contents on the stack when possible, in vector<char> otherwise
// NOTE: User should be remember that returned buffer might be on the stack!! // NOTE: User should be remember that returned buffer might be on the stack!!
namespace c11log namespace c11log { namespace details {
{
namespace details
{
template<std::size_t STACK_SIZE=128> template<std::size_t STACK_SIZE=128>
class fast_buf class fast_buf

View File

@ -1,7 +1,7 @@
#pragma once #pragma once
// Faster than ostringstream--returns its string by ref // Faster than ostringstream--returns its string by ref
#include <ostream>
#include "c11log/details/fast_buf.h" #include "c11log/details/fast_buf.h"
namespace c11log namespace c11log
@ -19,21 +19,14 @@ public:
str_devicebuf(str_devicebuf&& other) = delete; str_devicebuf(str_devicebuf&& other) = delete;
str_devicebuf& operator=(const str_devicebuf&) = delete; str_devicebuf& operator=(const str_devicebuf&) = delete;
str_devicebuf& operator=(str_devicebuf&&) = delete; str_devicebuf& operator=(str_devicebuf&&) = delete;
/*
const std::string& str_ref() const
{
return _str;
}
*/
bufpair_t buf() bufpair_t buf()
{ {
return _fastbuf.get(); return _fastbuf.get();
} }
void reset_str() void reset_str()
{ {
//_str.clear();
_fastbuf.clear(); _fastbuf.clear();
} }
@ -46,21 +39,13 @@ protected:
// copy the give buffer into the accumulated string. // copy the give buffer into the accumulated string.
// reserve initially 128 bytes which should be enough for common log lines // reserve initially 128 bytes which should be enough for common log lines
std::streamsize xsputn(const char_type* s, std::streamsize count) override std::streamsize xsputn(const char_type* s, std::streamsize count) override
{ {
/*
if(_str.capacity() < k_initial_reserve)
{
_str.reserve(k_initial_reserve);
}
_str.append(s, static_cast<unsigned int>(count));
*/
_fastbuf.append(s, static_cast<unsigned int>(count)); _fastbuf.append(s, static_cast<unsigned int>(count));
return count; return count;
} }
int_type overflow(int_type ch) override int_type overflow(int_type ch) override
{ {
bool not_eofile = traits_type::not_eof(ch); bool not_eofile = traits_type::not_eof(ch);
if (not_eofile) if (not_eofile)
{ {
@ -70,7 +55,6 @@ protected:
return not_eofile; return not_eofile;
} }
private: private:
//std::string _str;
fast_buf<192> _fastbuf; fast_buf<192> _fastbuf;
}; };
@ -83,12 +67,7 @@ public:
fast_oss(const fast_oss& other) = delete; fast_oss(const fast_oss& other) = delete;
fast_oss(fast_oss&& other) = delete; fast_oss(fast_oss&& other) = delete;
fast_oss& operator=(const fast_oss& other) = delete; fast_oss& operator=(const fast_oss& other) = delete;
/*
const std::string& str_ref() const
{
return _dev.str_ref();
}
*/
bufpair_t buf() bufpair_t buf()
{ {
return _dev.buf(); return _dev.buf();

View File

@ -38,12 +38,13 @@ public:
line_logger& operator=(const line_logger&) = delete; line_logger& operator=(const line_logger&) = delete;
line_logger& operator=(line_logger&&) = delete; line_logger& operator=(line_logger&&) = delete;
// The move ctor should only be called on start of logging line,
// where no logging happened yet for this line so no need to copy the string from the other
line_logger(line_logger&& other) : line_logger(line_logger&& other) :
_callback_logger(other._callback_logger), _callback_logger(other._callback_logger),
// The move ctor should only be called on start of logging line,
// where no logging happened yet for this line so no need to copy the string from the other
_oss(), _oss(),
_level(other._level) {}; _level(other._level),
_enabled(other._enabled) {}
~line_logger() ~line_logger()

View File

@ -67,7 +67,7 @@ private:
std::string _logger_name = ""; std::string _logger_name = "";
formatter_ptr _formatter; formatter_ptr _formatter;
sinks_vector_t _sinks; sinks_vector_t _sinks;
std::atomic_int _atomic_level; std::atomic_int _logger_level;
void _log_it(const bufpair_t& buf, const level::level_enum level); void _log_it(const bufpair_t& buf, const level::level_enum level);
@ -91,7 +91,7 @@ inline c11log::logger::logger(const std::string& name, formatter_ptr f, sinks_in
_sinks(sinks_list) _sinks(sinks_list)
{ {
//Seems that vs2013 doesnt support atomic member initialization in ctor, so its done here //Seems that vs2013 doesnt support atomic member initialization in ctor, so its done here
_atomic_level = level::INFO; _logger_level = level::INFO;
} }
inline c11log::logger::logger(const std::string& name, sinks_init_list sinks_list) : inline c11log::logger::logger(const std::string& name, sinks_init_list sinks_list) :
@ -103,7 +103,7 @@ inline c11log::logger::logger(sinks_init_list sinks_list) :
inline c11log::details::line_logger c11log::logger::log(c11log::level::level_enum msg_level) inline c11log::details::line_logger c11log::logger::log(c11log::level::level_enum msg_level)
{ {
return details::line_logger(this, msg_level, msg_level >= _atomic_level); return details::line_logger(this, msg_level, msg_level >= _logger_level);
} }
inline c11log::details::line_logger c11log::logger::debug() inline c11log::details::line_logger c11log::logger::debug()
@ -136,17 +136,17 @@ inline const std::string& c11log::logger::get_name() const
inline void c11log::logger::set_level(c11log::level::level_enum level) inline void c11log::logger::set_level(c11log::level::level_enum level)
{ {
_atomic_level.store(level); _logger_level.store(level);
} }
inline c11log::level::level_enum c11log::logger::get_level() const inline c11log::level::level_enum c11log::logger::get_level() const
{ {
return static_cast<c11log::level::level_enum>(_atomic_level.load()); return static_cast<c11log::level::level_enum>(_logger_level.load());
} }
inline bool c11log::logger::should_log(c11log::level::level_enum level) const inline bool c11log::logger::should_log(c11log::level::level_enum level) const
{ {
return level >= _atomic_level.load(); return level >= _logger_level.load();
} }
inline void c11log::logger::_log_it(const bufpair_t& buf, const level::level_enum level) inline void c11log::logger::_log_it(const bufpair_t& buf, const level::level_enum level)