mirror of
https://github.com/gabime/spdlog.git
synced 2024-11-15 08:25:43 +08:00
fixes and imprvts
This commit is contained in:
parent
0bf2391d4a
commit
470c23ad92
@ -17,7 +17,7 @@ OUTLIB_DEBUG = libc11log-debug.a
|
||||
TEST_RELEASE = testme
|
||||
TEST_DEBUG = testme-debug
|
||||
|
||||
.PHONY: all mkdirs release debug build clean
|
||||
.PHONY: all mkdirs release debug build clean rebuild
|
||||
|
||||
all: release
|
||||
|
||||
@ -49,4 +49,6 @@ debug/%.o: $(SRC_DIR)/%.cpp
|
||||
clean:
|
||||
rm -rf release debug daily.* $(TEST_RELEASE) $(TEST_DEBUG) $(OUTLIB_RELEASE) $(OUTLIB_DEBUG)
|
||||
|
||||
rebuild: clean all
|
||||
|
||||
|
||||
|
@ -4,16 +4,20 @@
|
||||
|
||||
void c11log::formatters::format_time(const c11log::formatters::timepoint& tp, std::ostream &dest)
|
||||
{
|
||||
|
||||
std::tm tm = details::os::localtime(std::chrono::system_clock::to_time_t(tp));
|
||||
//get ms
|
||||
auto duration = tp.time_since_epoch();
|
||||
int millis = static_cast<int>(std::chrono::duration_cast<std::chrono::milliseconds>(duration).count() % 1000);
|
||||
//std::put_time(&tm, "[ %Y-%m-%d %H:%M:%S ]") - seems too slow
|
||||
//auto duration = tp.time_since_epoch();
|
||||
//int millis = static_cast<int>(std::chrono::duration_cast<std::chrono::milliseconds>(duration).count() % 1000);
|
||||
|
||||
|
||||
char buf[64];
|
||||
auto size = sprintf(buf, "[%d-%02d-%02d %02d:%02d:%02d.%03d]",
|
||||
auto size = sprintf(buf, "[%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, millis);
|
||||
tm.tm_hour, tm.tm_min, tm.tm_sec);
|
||||
|
||||
dest.write(buf, size);
|
||||
|
||||
}
|
||||
|
||||
void c11log::formatters::format_time(std::ostream& dest)
|
||||
|
39
src/test.cpp
39
src/test.cpp
@ -13,49 +13,48 @@
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
using namespace std::chrono;
|
||||
int nthreads = argc > 1 ? atoi(argv[1]) : 1;
|
||||
int nlines = argc > 2 ? atoi(argv[2]) : 1000000;
|
||||
|
||||
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 async = std::make_shared<c11log::sinks::async_sink>(100);
|
||||
//auto fsink = std::make_shared<c11log::sinks::rotating_file_sink>("newlog", "txt", 1024*1024*10 , 2);
|
||||
auto fsink = std::make_shared<c11log::sinks::daily_file_sink>("daily", "txt");
|
||||
|
||||
async->add_sink(fsink);
|
||||
//async->add_sink(null_sink);
|
||||
|
||||
c11log::logger logger("test");
|
||||
logger.add_sink(async);
|
||||
|
||||
std::atomic<uint32_t> counter { 0 };
|
||||
auto counter_ptr = &counter;
|
||||
std::atomic<bool> active{true};
|
||||
auto active_ptr = &active;
|
||||
|
||||
std::vector<std::thread*> threads;
|
||||
std::cout << "Starting " << nthreads << " threads for 3 seconds.." << std::endl;
|
||||
std::cout << "Starting " << nthreads << " threads x " << utils::format(nlines) << " lines each.." << std::endl;
|
||||
for (int i = 0; i < nthreads; i++)
|
||||
{
|
||||
auto t = new std::thread([&logger, counter_ptr, active_ptr]() {
|
||||
while (*active_ptr)
|
||||
auto t = new std::thread([&logger, nlines]() {
|
||||
|
||||
for(int i = 0 ; i < nlines; ++i)
|
||||
{
|
||||
logger.info() << "Hello from thread " << std::this_thread::get_id() << "\tcounter: " << counter_ptr->load();
|
||||
(*counter_ptr)++;
|
||||
logger.info() << "Hello from thread " << std::this_thread::get_id() << "\tcounter: " << i ;
|
||||
}
|
||||
|
||||
});
|
||||
threads.push_back(t);
|
||||
}
|
||||
|
||||
int seconds = 0;
|
||||
while (seconds++ < 3)
|
||||
{
|
||||
counter = 0;
|
||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||
std::cout << "Counter = " << utils::format(counter.load()) << std::endl;
|
||||
}
|
||||
active = false;
|
||||
|
||||
auto stime = steady_clock::now();
|
||||
for(auto t:threads)
|
||||
t->join();
|
||||
async->shutdown(std::chrono::seconds(1));
|
||||
auto delta = steady_clock::now() - stime;
|
||||
auto delta_seconds = duration_cast<milliseconds>(delta).count()/1000.0;
|
||||
|
||||
auto total = nthreads*nlines;
|
||||
std::cout << "Total: " << utils::format(total) << " = " << utils::format(total/delta_seconds) << "/sec" << std::endl;
|
||||
|
||||
async->shutdown(seconds(1));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user