mirror of
https://github.com/gabime/spdlog.git
synced 2025-10-01 02:49:03 +08:00
header only\!
This commit is contained in:
@@ -1,24 +0,0 @@
|
||||
#include "stdafx.h"
|
||||
#include "c11log/details/factory.h"
|
||||
#include "c11log/logger.h"
|
||||
|
||||
c11log::details::factory::logger_ptr c11log::details::factory::get_logger(const std::string &name)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_loggers_mutex);
|
||||
auto found = _loggers.find(name);
|
||||
if (found == _loggers.end()) {
|
||||
auto new_logger_ptr = std::make_shared<c11log::logger>(name);
|
||||
_loggers.insert(std::make_pair(name, new_logger_ptr));
|
||||
return new_logger_ptr;
|
||||
}
|
||||
else {
|
||||
return found->second;
|
||||
}
|
||||
}
|
||||
|
||||
c11log::details::factory & c11log::details::factory::instance()
|
||||
{
|
||||
static c11log::details::factory instance;
|
||||
|
||||
return instance;
|
||||
}
|
@@ -1,41 +0,0 @@
|
||||
#include "stdafx.h"
|
||||
#include <memory.h>
|
||||
|
||||
#include "c11log/formatters/formatters.h"
|
||||
#include "c11log/level.h"
|
||||
|
||||
thread_local c11log::formatters::time_point last_tp;
|
||||
thread_local char timestamp_cache[64];
|
||||
|
||||
void c11log::formatters::default_formatter::_format_time(const time_point& tp, std::ostream &dest)
|
||||
{
|
||||
|
||||
// Cache timestamp string of last second
|
||||
using namespace std::chrono;
|
||||
if(duration_cast<milliseconds>(tp-last_tp).count() >= 950)
|
||||
{
|
||||
auto tm = details::os::localtime(clock::to_time_t(tp));
|
||||
sprintf(timestamp_cache, "[%d-%02d-%02d %02d:%02d:%02d]", tm.tm_year + 1900,
|
||||
tm.tm_mon + 1,
|
||||
tm.tm_mday,
|
||||
tm.tm_hour,
|
||||
tm.tm_min,
|
||||
tm.tm_sec);
|
||||
last_tp = tp;
|
||||
}
|
||||
dest << timestamp_cache;
|
||||
}
|
||||
|
||||
|
||||
static const char _hex_chars[17] = "0123456789ABCDEF";
|
||||
|
||||
std::string c11log::formatters::to_hex(const unsigned char* buf, std::size_t size)
|
||||
{
|
||||
std::ostringstream oss;
|
||||
|
||||
for (std::size_t i = 0; i < size; i++) {
|
||||
oss << _hex_chars[buf[i] >> 4];
|
||||
oss << _hex_chars[buf[i] & 0x0F];
|
||||
}
|
||||
return oss.str();
|
||||
}
|
27
src/os.cpp
27
src/os.cpp
@@ -1,27 +0,0 @@
|
||||
#include "stdafx.h"
|
||||
|
||||
#include "c11log/details/os.h"
|
||||
|
||||
namespace c11log {
|
||||
namespace details {
|
||||
namespace os {
|
||||
std::tm localtime(const std::time_t &time_t)
|
||||
{
|
||||
|
||||
std::tm tm;
|
||||
#ifdef _MSC_VER
|
||||
localtime_s(&tm, &time_t);
|
||||
#else
|
||||
localtime_r(&time_t, &tm);
|
||||
#endif
|
||||
return tm;
|
||||
}
|
||||
|
||||
std::tm localtime()
|
||||
{
|
||||
std::time_t now_t = time(0);
|
||||
return localtime(now_t);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
97
src/test.cpp
97
src/test.cpp
@@ -1,97 +0,0 @@
|
||||
// test.cpp : Defines the entry point for the console application.
|
||||
//
|
||||
#include "stdafx.h"
|
||||
#include <functional>
|
||||
|
||||
#include "c11log/logger.h"
|
||||
#include "c11log/sinks/async_sink.h"
|
||||
#include "c11log/sinks/file_sinks.h"
|
||||
#include "c11log/sinks/stdout_sinks.h"
|
||||
|
||||
#include "utils.h"
|
||||
|
||||
|
||||
std::atomic<uint64_t> push_count, pop_count;
|
||||
std::atomic<bool> active;
|
||||
|
||||
|
||||
|
||||
using std::string;
|
||||
using std::chrono::seconds;
|
||||
using Q = c11log::details::blocking_queue<string>;
|
||||
|
||||
void pusher(Q* )
|
||||
{
|
||||
auto &logger = c11log::get_logger("async");
|
||||
while(active)
|
||||
{
|
||||
logger.info()<<"Hello logger!";
|
||||
++push_count;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
void testq(int size, int pushers /*int poppers*/)
|
||||
{
|
||||
|
||||
active = true;
|
||||
Q q{static_cast<Q::size_type>(size)};
|
||||
|
||||
/*
|
||||
for(int i = 0; i < poppers; i++)
|
||||
testq(qsize, pushers, poppers);
|
||||
*/
|
||||
for(int i = 0; i < pushers; i++)
|
||||
new std::thread(std::bind(pusher, &q));
|
||||
|
||||
while(active)
|
||||
{
|
||||
using std::endl;
|
||||
using std::cout;
|
||||
using utils::format;
|
||||
|
||||
push_count = 0;
|
||||
pop_count = 0;
|
||||
std::this_thread::sleep_for(seconds(1));
|
||||
cout << "Pushes/sec =\t" << format(push_count.load()) << endl;
|
||||
//cout << "Pops/sec =\t" << format(pop_count.load()) << endl << endl;
|
||||
//cout << "Total/sec =\t" << format(push_count+pop_count) << endl;
|
||||
cout << "Queue size =\t" << format(q.size()) << endl;
|
||||
cout << "---------------------------------------------------------------------" << endl;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
|
||||
if(argc !=4)
|
||||
{
|
||||
std::cerr << "Usage: " << argv[0] << " qsize, pushers, poppers" << std::endl;
|
||||
return 0;
|
||||
}
|
||||
int qsize = atoi(argv[1]);
|
||||
int pushers = atoi(argv[2]);
|
||||
//int poppers = atoi(argv[3]);
|
||||
|
||||
//testq(qsize, pushers, poppers);
|
||||
|
||||
|
||||
using namespace std::chrono;
|
||||
|
||||
|
||||
auto null_sink = std::make_shared<c11log::sinks::null_sink>();
|
||||
auto stdout_sink = std::make_shared<c11log::sinks::stdout_sink>();
|
||||
auto async = std::make_shared<c11log::sinks::async_sink>(1000);
|
||||
auto fsink = std::make_shared<c11log::sinks::rotating_file_sink>("newlog", "txt", 1024*1024*50 , 5);
|
||||
//auto fsink = std::make_shared<c11log::sinks::daily_file_sink>("daily", "txt");
|
||||
|
||||
async->add_sink(fsink);
|
||||
auto &logger = c11log::get_logger("async");
|
||||
logger.add_sink(async);
|
||||
|
||||
testq(qsize, pushers);
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user