Commit 796ec3db authored by gabi's avatar gabi

Added registry

parent 319db7bc
...@@ -95,8 +95,8 @@ ...@@ -95,8 +95,8 @@
<ClInclude Include="..\..\include\c11log\details\null_mutex.h" /> <ClInclude Include="..\..\include\c11log\details\null_mutex.h" />
<ClInclude Include="..\..\include\c11log\details\os.h" /> <ClInclude Include="..\..\include\c11log\details\os.h" />
<ClInclude Include="..\..\include\c11log\details\pattern_formatter.h" /> <ClInclude Include="..\..\include\c11log\details\pattern_formatter.h" />
<ClInclude Include="..\..\include\c11log\details\registry.h" />
<ClInclude Include="..\..\include\c11log\details\stack_buf.h" /> <ClInclude Include="..\..\include\c11log\details\stack_buf.h" />
<ClInclude Include="..\..\include\c11log\factory.h" />
<ClInclude Include="..\..\include\c11log\formatter.h" /> <ClInclude Include="..\..\include\c11log\formatter.h" />
<ClInclude Include="..\..\include\c11log\logger.h" /> <ClInclude Include="..\..\include\c11log\logger.h" />
<ClInclude Include="..\..\include\c11log\sinks\async_sink.h" /> <ClInclude Include="..\..\include\c11log\sinks\async_sink.h" />
...@@ -114,6 +114,9 @@ ...@@ -114,6 +114,9 @@
<ClCompile Include="..\..\example\example.cpp" /> <ClCompile Include="..\..\example\example.cpp" />
<ClCompile Include="stdafx.cpp" /> <ClCompile Include="stdafx.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<None Include="ClassDiagram.cd" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets"> <ImportGroup Label="ExtensionTargets">
</ImportGroup> </ImportGroup>
......
...@@ -84,9 +84,6 @@ ...@@ -84,9 +84,6 @@
<ClInclude Include="..\..\include\c11log\common.h"> <ClInclude Include="..\..\include\c11log\common.h">
<Filter>Header Files\c11log</Filter> <Filter>Header Files\c11log</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\..\include\c11log\factory.h">
<Filter>Header Files\c11log</Filter>
</ClInclude>
<ClInclude Include="..\..\include\c11log\details\fast_istostr.h"> <ClInclude Include="..\..\include\c11log\details\fast_istostr.h">
<Filter>Header Files\c11log\details</Filter> <Filter>Header Files\c11log\details</Filter>
</ClInclude> </ClInclude>
...@@ -96,6 +93,9 @@ ...@@ -96,6 +93,9 @@
<ClInclude Include="..\..\include\c11log\details\pattern_formatter.h"> <ClInclude Include="..\..\include\c11log\details\pattern_formatter.h">
<Filter>Header Files\c11log\details</Filter> <Filter>Header Files\c11log\details</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\..\include\c11log\details\registry.h">
<Filter>Header Files\c11log</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="stdafx.cpp"> <ClCompile Include="stdafx.cpp">
...@@ -108,4 +108,7 @@ ...@@ -108,4 +108,7 @@
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup>
<None Include="ClassDiagram.cd" />
</ItemGroup>
</Project> </Project>
\ No newline at end of file
...@@ -2,63 +2,40 @@ ...@@ -2,63 +2,40 @@
// //
#include <mutex> #include <mutex>
#include "c11log/logger.h" #include "c11log/logger.h"
#include "c11log/factory.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/stdout_sinks.h" #include "c11log/sinks/stdout_sinks.h"
#include "c11log/sinks/null_sink.h" #include "c11log/sinks/null_sink.h"
#include "utils.h" #include "utils.h"
#include "c11log/details/registry.h"
using namespace std::chrono; using namespace std::chrono;
using namespace c11log; using namespace c11log;
using namespace utils; using namespace utils;
std::ostringstream f1(int i)
{
std::ostringstream oss;
oss << "Hello oss " << i;
return oss;
}
details::fast_oss f2(int i)
{
details::fast_oss oss;
oss << "Hello oss " << i;
return oss;
}
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
const unsigned int howmany = argc <= 1 ? 600000 : atoi(argv[1]); const unsigned int howmany = argc <= 1 ? 600000 : atoi(argv[1]);
const std::string pattern = "%+"; auto console = c11log::create<sinks::stderr_sink_st>("reporter");
auto formatter = std::make_shared<details::pattern_formatter>(pattern); console->set_format("[%n %l] %t");
//logger cout_logger("bench", { std::make_shared<sinks::stderr_sink_mt>() }, pattern); console->set_level(c11log::level::INFO);
//logger::default_formatter(formatter); console->info("Starting bench with", howmany, "iterations..");
logger cout_logger("bench", { std::make_shared<sinks::stderr_sink_mt>() });
logger::default_formatter(formatter);
cout_logger.info() << "Hello logger " << 12.4 << 5 << ',' << 6 << 7 <<8<<9<<10;
auto nullsink = std::make_shared<sinks::null_sink_st>();
auto rotating = std::make_shared<sinks::rotating_file_sink_st>("myrotating", "txt", 1024 * 1024 * 5, 20, 100);
logger my_logger("my_logger", { nullsink });
auto bench = c11log::create<sinks::rotating_file_sink_st>("bench", "myrotating", "txt", 1024 * 1024 * 5, 3, 100);
auto start = system_clock::now(); auto start = system_clock::now();
for (unsigned int i = 1; i <= howmany; ++i) for (unsigned int i = 1; i <= howmany; ++i)
{ {
my_logger.info() << "Hello logger: msg #" << i; bench->info() << "Hello logger: msg number " << i;
} }
auto delta = system_clock::now() - start; auto delta = system_clock::now() - start;
auto delta_d = duration_cast<duration<double>> (delta).count(); auto delta_d = duration_cast<duration<double>> (delta).count();
cout_logger.info("Total:") << format(howmany); console->info("Total:") << format(howmany);
cout_logger.info("Delta:") << format(delta_d); console->info("Delta:") << format(delta_d);
cout_logger.info("Rate:") << format(howmany / delta_d) << "/sec"; console->info("Rate:") << format(howmany / delta_d) << "/sec";
return 0; return 0;
} }
......
...@@ -4,7 +4,6 @@ ...@@ -4,7 +4,6 @@
#include <iostream> #include <iostream>
#include "c11log/logger.h" #include "c11log/logger.h"
#include "c11log/factory.h"
#include "c11log/sinks/stdout_sinks.h" #include "c11log/sinks/stdout_sinks.h"
#include "c11log/sinks/file_sinks.h" #include "c11log/sinks/file_sinks.h"
using namespace std; using namespace std;
......
...@@ -25,8 +25,8 @@ typedef enum ...@@ -25,8 +25,8 @@ typedef enum
TRACE, TRACE,
DEBUG, DEBUG,
INFO, INFO,
WARNING, WARN,
ERROR, ERR,
CRITICAL, CRITICAL,
NONE = 99 NONE = 99
} level_enum; } level_enum;
......
...@@ -19,7 +19,9 @@ public: ...@@ -19,7 +19,9 @@ public:
virtual void format(details::log_msg& msg) = 0; virtual void format(details::log_msg& msg) = 0;
}; };
// log name appender ///////////////////////////////////////////////////////////////////////
// name & level pattern appenders
///////////////////////////////////////////////////////////////////////
class name_formatter :public flag_formatter class name_formatter :public flag_formatter
{ {
void format(details::log_msg& msg) override void format(details::log_msg& msg) override
......
#pragma once
// Loggers registy of unique name->logger pointer
// If 2 loggers with same name are added, the last will be used
// If user requests a non existing logger, nullptr will be returned
// This class is thread safe
#include <string>
#include <mutex>
#include <unordered_map>
#include "../logger.h"
#include "../common.h"
namespace c11log {
namespace details {
class registry {
public:
std::shared_ptr<logger> get(const std::string& name)
{
std::lock_guard<std::mutex> l(_mutex);
auto found = _loggers.find(name);
return found == _loggers.end() ? nullptr : found->second;
}
std::shared_ptr<logger> create(const std::string& logger_name, sinks_init_list sinks)
{
std::lock_guard<std::mutex> l(_mutex);
return _loggers[logger_name] = std::make_shared<logger>(logger_name, sinks);
}
std::shared_ptr<logger> create(const std::string& logger_name, sink_ptr sink)
{
create(logger_name, { sink });
}
template<class It>
std::shared_ptr<logger> create (const std::string& logger_name, const It& sinks_begin, const It& sinks_end)
{
std::lock_guard<std::mutex> l(_mutex);
return _loggers[logger_name] = std::make_shared<logger>(logger_name, sinks_begin, sinks_end);
}
static registry& instance()
{
static registry s_instance;
return s_instance;
}
private:
registry() = default;
registry(const registry&) = delete;
std::mutex _mutex;
std::unordered_map <std::string, std::shared_ptr<logger>> _loggers;
};
}
}
#pragma once
#include <memory>
#include "logger.h"
#include "sinks/file_sinks.h"
#include "sinks/stdout_sinks.h"
//
// logger creation shotcuts
//
namespace c11log
{
namespace factory
{
//
//
//std::shared_ptr<logger> get(const std::string& name);
//std::shared_ptr<logger> create(const std::string& name, sinks_init_list , formatter_ptr);
//std::shared_ptr<logger> create(const std::string& name, sinks_init_list, string format);
//template<class It>
//std::shared_ptr<logger> create (const std::string& name, const It& begin, const It& end);
//
//
////
//// console loggers single/multi threaded
////
//std::unique_ptr<logger> stdout_logger(const std::string& name = "")
//{
// auto sink = std::make_shared<sinks::stderr_sink_st>();
// return std::unique_ptr<logger>(new logger(name, { sink }));
//}
//
//std::unique_ptr<logger> stdout_logger_mt(const std::string& name = "")
//{
// auto sink = std::make_shared<sinks::stderr_sink_mt>();
// return std::unique_ptr<logger>(new logger(name, { sink }));
//}
//
////
//// simple file logger single/multi threaded
////
//std::unique_ptr<logger> simple_file_logger(const std::string& filename, const std::string& logger_name = "" )
//{
// auto fsink = std::make_shared<sinks::simple_file_sink_st>(filename);
// return std::unique_ptr<logger>(new c11log::logger(logger_name, { fsink }));
//
//}
//std::unique_ptr<logger> simple_file_logger_mt(const std::string& filename, const std::string& logger_name = "")
//{
// auto fsink = std::make_shared<sinks::simple_file_sink_mt>(filename);
// return std::unique_ptr<logger>(new c11log::logger(logger_name, { fsink }));
//}
//
////
//// daily file logger single/multi threaded
////
//std::unique_ptr<logger> daily_file_logger(
// const std::string &filename,
// const std::string &extension,
// const std::size_t flush_every,
// const std::string& logger_name = "")
//{
// auto fsink = std::make_shared<sinks::daily_file_sink_st>(filename, extension, flush_every);
// return std::unique_ptr<logger>(new c11log::logger(logger_name, { fsink }));
//}
//
//std::unique_ptr<logger> daily_file_logger_mt(
// const std::string &filename,
// const std::string &extension,
// const std::size_t flush_every,
// const std::string& logger_name = "")
//{
// auto fsink = std::make_shared<sinks::daily_file_sink_mt>(filename, extension, flush_every);
// return std::unique_ptr<logger>(new c11log::logger(logger_name, { fsink }));
//}
//
////
//// rotating file logger single/multi threaded
////
//std::unique_ptr<logger> rotating_file_logger(
// const std::string &filename,
// const std::string &extension,
// const std::size_t max_size,
// const std::size_t max_files,
// const std::size_t flush_every,
// const std::string& logger_name = "")
//{
// auto fsink = std::make_shared<sinks::rotating_file_sink_st>(filename, extension, max_size, max_files, flush_every);
// return std::unique_ptr<logger>(new c11log::logger(logger_name, { fsink }));
//}
//
//std::unique_ptr<logger> rotating_file_logger_mt(
// const std::string &filename,
// const std::string &extension,
// const std::size_t max_size,
// const std::size_t max_files,
// const std::size_t flush_every,
// const std::string& logger_name = "")
//{
// auto fsink = std::make_shared<sinks::rotating_file_sink_mt>(filename, extension, max_size, max_files, flush_every);
// return std::unique_ptr<logger>(new c11log::logger(logger_name, { fsink }));
//}
} // ns factory
} // ns c11log
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#include "common.h" #include "common.h"
#include "details/pattern_formatter.h" #include "details/pattern_formatter.h"
namespace c11log namespace c11log
{ {
...@@ -32,20 +33,15 @@ public: ...@@ -32,20 +33,15 @@ public:
template<class It> template<class It>
logger(const std::string& name, const It& begin, const It& end); logger(const std::string& name, const It& begin, const It& end);
void c11log::logger::set_format(const std::string& format);
//get/set default formatter
static formatter_ptr& default_formatter(formatter_ptr formatter = nullptr);
void set_formatter(formatter_ptr); void set_formatter(formatter_ptr);
formatter_ptr get_formatter() const; formatter_ptr get_formatter() const;
logger(const logger&) = delete; logger(const logger&) = delete;
logger& operator=(const logger&) = delete; logger& operator=(const logger&) = delete;
void level(level::level_enum); void set_level(level::level_enum);
level::level_enum level() const; level::level_enum level() const;
const std::string& name() const; const std::string& name() const;
...@@ -59,6 +55,11 @@ public: ...@@ -59,6 +55,11 @@ public:
template <typename... Args> details::line_logger error(const Args&... args); template <typename... Args> details::line_logger error(const Args&... args);
template <typename... Args> details::line_logger critical(const Args&... args); template <typename... Args> details::line_logger critical(const Args&... args);
//static functions
//get/set default formatter
static formatter_ptr default_formatter(formatter_ptr formatter = nullptr);
static formatter_ptr default_format(const std::string& format);
private: private:
friend details::line_logger; friend details::line_logger;
std::string _name; std::string _name;
...@@ -70,10 +71,27 @@ private: ...@@ -70,10 +71,27 @@ private:
void _variadic_log(details::line_logger&l, const First& first, const Rest&... rest); void _variadic_log(details::line_logger&l, const First& first, const Rest&... rest);
void _log_msg(details::log_msg& msg); void _log_msg(details::log_msg& msg);
}; };
//
// Registry functions for easy loggers creation and retrieval
// example
// auto console_logger = c11log::create("my_logger", c11log::sinks<stdout_sink_mt>);
// auto same_logger = c11log::get("my_logger");
// auto file_logger = c11
//
std::shared_ptr<logger> get(const std::string& name);
std::shared_ptr<logger> create(const std::string& logger_name, sinks_init_list sinks);
template <typename Sink, typename... Args>
std::shared_ptr<c11log::logger> create(const std::string& logger_name, const Args&... args);
template<class It>
std::shared_ptr<logger> create(const std::string& logger_name, const It& sinks_begin, const It& sinks_end);
} }
// //
// trace & debug macros // Trace & debug macros
// //
#ifdef FFLOG_ENABLE_TRACE #ifdef FFLOG_ENABLE_TRACE
#define FFLOG_TRACE(logger, ...) logger->log(c11log::level::TRACE, __FILE__, " #", __LINE__,": " __VA_ARGS__) #define FFLOG_TRACE(logger, ...) logger->log(c11log::level::TRACE, __FILE__, " #", __LINE__,": " __VA_ARGS__)
...@@ -114,12 +132,17 @@ inline void c11log::logger::set_formatter(c11log::formatter_ptr msg_formatter) ...@@ -114,12 +132,17 @@ inline void c11log::logger::set_formatter(c11log::formatter_ptr msg_formatter)
_formatter = msg_formatter; _formatter = msg_formatter;
} }
inline void c11log::logger::set_format(const std::string& format)
{
_formatter = std::make_shared<details::pattern_formatter>(format);
}
inline c11log::formatter_ptr c11log::logger::get_formatter() const inline c11log::formatter_ptr c11log::logger::get_formatter() const
{ {
return _formatter; return _formatter;
} }
inline c11log::formatter_ptr& c11log::logger::default_formatter(formatter_ptr formatter) inline c11log::formatter_ptr c11log::logger::default_formatter(formatter_ptr formatter)
{ {
static formatter_ptr g_default_formatter = std::make_shared<details::pattern_formatter>("[%Y-%m-%d %H:%M:%S.%e] [%n] [%l] %t"); static formatter_ptr g_default_formatter = std::make_shared<details::pattern_formatter>("[%Y-%m-%d %H:%M:%S.%e] [%n] [%l] %t");
if (formatter) if (formatter)
...@@ -127,6 +150,11 @@ inline c11log::formatter_ptr& c11log::logger::default_formatter(formatter_ptr fo ...@@ -127,6 +150,11 @@ inline c11log::formatter_ptr& c11log::logger::default_formatter(formatter_ptr fo
return g_default_formatter; return g_default_formatter;
} }
inline c11log::formatter_ptr c11log::logger::default_format(const std::string& format)
{
return default_formatter(std::make_shared<details::pattern_formatter>(format));
}
template <typename... Args> template <typename... Args>
inline c11log::details::line_logger c11log::logger::log(level::level_enum lvl, const Args&... args) { inline c11log::details::line_logger c11log::logger::log(level::level_enum lvl, const Args&... args) {
bool msg_enabled = should_log(lvl); bool msg_enabled = should_log(lvl);
...@@ -157,13 +185,13 @@ inline c11log::details::line_logger c11log::logger::info(const Args&... args) ...@@ -157,13 +185,13 @@ inline c11log::details::line_logger c11log::logger::info(const Args&... args)
template <typename... Args> template <typename... Args>
inline c11log::details::line_logger c11log::logger::warn(const Args&... args) inline c11log::details::line_logger c11log::logger::warn(const Args&... args)
{ {
return log(level::WARNING, args...); return log(level::WARN, args...);
} }
template <typename... Args> template <typename... Args>
inline c11log::details::line_logger c11log::logger::error(const Args&... args) inline c11log::details::line_logger c11log::logger::error(const Args&... args)
{ {
return log(level::ERROR, args...); return log(level::ERR, args...);
} }
template <typename... Args> template <typename... Args>
...@@ -177,7 +205,7 @@ inline const std::string& c11log::logger::name() const ...@@ -177,7 +205,7 @@ inline const std::string& c11log::logger::name() const
return _name; return _name;
} }
inline void c11log::logger::level(c11log::level::level_enum log_level) inline void c11log::logger::set_level(c11log::level::level_enum log_level)
{ {
_level.store(log_level); _level.store(log_level);
} }
...@@ -201,15 +229,49 @@ template <typename First, typename... Rest> ...@@ -201,15 +229,49 @@ template <typename First, typename... Rest>
void c11log::logger::_variadic_log(c11log::details::line_logger& l, const First& first, const Rest&... rest) void c11log::logger::_variadic_log(c11log::details::line_logger& l, const First& first, const Rest&... rest)
{ {
l.write(first); l.write(first);
l.write(' ');
_variadic_log(l, rest...); _variadic_log(l, rest...);
} }
inline void c11log::logger::_log_msg(details::log_msg& msg) inline void c11log::logger::_log_msg(details::log_msg& msg)
{ {
auto& formatter = _formatter ? _formatter : logger::default_formatter(); if(!_formatter)
formatter->format(msg); _formatter = logger::default_formatter();
_formatter->format(msg);
for (auto &sink : _sinks) for (auto &sink : _sinks)
sink->log(msg); sink->log(msg);
} }
//
// Global registry functions
//
#include "details/registry.h"
inline std::shared_ptr<c11log::logger> c11log::get(const std::string& name)
{
return details::registry::instance().get(name);
}
inline std::shared_ptr<c11log::logger> c11log::create(const std::string& logger_name, c11log::sinks_init_list sinks)
{
return details::registry::instance().create(logger_name, sinks);
}
template <typename Sink, typename... Args>
inline std::shared_ptr<c11log::logger> c11log::create(const std::string& logger_name, const Args&... args)
{
sink_ptr sink = std::make_shared<Sink>(args...);
return details::registry::instance().create(logger_name, { sink });
}
template<class It>
inline std::shared_ptr<c11log::logger> c11log::create(const std::string& logger_name, const It& sinks_begin, const It& sinks_end)
{
std::lock_guard<std::mutex> l(_mutex);
return details::registry::instance().create(logger_name, std::forward(sinks_begin), std::forward(sinks_end));
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment