From 52dc317cfb70953f0c5abae3fea1903d042468b8 Mon Sep 17 00:00:00 2001 From: gabime Date: Mon, 3 Feb 2014 20:28:19 +0200 Subject: [PATCH] fixes --- build/gcc/makefile | 2 +- include/c11log/details/fast_oss.h | 35 ++++----- src/formatters.cpp | 5 +- src/test.cpp | 122 ++++++++---------------------- 4 files changed, 50 insertions(+), 114 deletions(-) diff --git a/build/gcc/makefile b/build/gcc/makefile index b99c7bf7..a778ed03 100644 --- a/build/gcc/makefile +++ b/build/gcc/makefile @@ -9,7 +9,7 @@ OBJS_DEBUG = $(patsubst %.cpp,debug/%.o,$(_SOURCES)) CXX = g++ CXXFLAGS = -march=native -Wall -std=c++11 -pthread -I../../include -CXX_RELEASE_FLAGS = -O2 -flto +CXX_RELEASE_FLAGS = -O2 -flto -g CXX_DEBUG_FLAGS= -g OUTLIB_RELEASE = libc11log.a diff --git a/include/c11log/details/fast_oss.h b/include/c11log/details/fast_oss.h index 1a5f9cd1..09903656 100644 --- a/include/c11log/details/fast_oss.h +++ b/include/c11log/details/fast_oss.h @@ -7,12 +7,12 @@ namespace details { class str_devicebuf:public std::streambuf { public: - str_devicebuf() - { - _str.reserve(128); - } + str_devicebuf() = default; + ~str_devicebuf() = default; + str_devicebuf(const str_devicebuf&) = delete; + str_devicebuf& operator=(const str_devicebuf&) = delete; - const std::string& str_ref() + const std::string& str_ref() const { return _str; } @@ -46,31 +46,28 @@ private: class fast_oss:public std::ostream { public: - fast_oss():std::ostream(&_dev) - {} - ~fast_oss() - {} + fast_oss():std::ostream(&_dev){} + ~fast_oss() = default; + fast_oss(const fast_oss&) = delete; + fast_oss operator=(const fast_oss&) = delete; + const std::string& str_ref() const - { - auto mydevice = static_cast(rdbuf()); - return mydevice->str_ref(); + { + return _dev.str_ref(); } const std::string str() const - { - auto mydevice = static_cast(rdbuf()); - return mydevice->str_ref(); + { + return _dev.str_ref(); } void clear() { - auto mydevice = static_cast(rdbuf()); - mydevice->clear(); + _dev.clear(); } private: str_devicebuf _dev; - }; } -} \ No newline at end of file +} diff --git a/src/formatters.cpp b/src/formatters.cpp index ad9c3745..6f1ea4ab 100644 --- a/src/formatters.cpp +++ b/src/formatters.cpp @@ -9,12 +9,13 @@ void c11log::formatters::format_time(const c11log::formatters::timepoint& tp, st //get ms //auto duration = tp.time_since_epoch(); //int millis = static_cast(std::chrono::duration_cast(duration).count() % 1000); - + + char buf[64]; 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); - + dest.write(buf, size); } diff --git a/src/test.cpp b/src/test.cpp index c52f9c8d..03dc178b 100644 --- a/src/test.cpp +++ b/src/test.cpp @@ -17,52 +17,29 @@ std::atomic active; -class A -{ -public: - A() - { - std::cout << "Regular ctor\n"; - } - A(const A&) - { - std::cout << "Copy ctor\n"; - } - A& operator=(const A&) - { - std::cout << "operator=\n"; - return *this; - } - - A& operator=(A&&) - { - std::cout << "operator=&&\n"; - return *this; - } - - A(A&& a) - { - std::cout << "Move ctor\n"; - } - ~A() - { - //std::cout << "Dtor\n"; - } - -}; - using std::string; using std::chrono::seconds; using Q = c11log::details::blocking_queue; void pusher(Q* q) { + auto &logger = c11log::get_logger("async"); + while(active) + { + logger.info()<<"Hello logger!"; + ++push_count; + + } + + + /* string a = "Hello"; while(active) { q->push(a); ++push_count; } + */ } void popper(Q* q) @@ -73,6 +50,7 @@ void popper(Q* q) q->pop(output); ++pop_count; } + } void testq(int size, int pushers, int poppers) @@ -81,9 +59,10 @@ void testq(int size, int pushers, int poppers) active = true; Q q{static_cast(size)}; + /* for(int i = 0; i < poppers; i++) - new std::thread(std::bind(popper, &q)); - + testq(qsize, pushers, poppers); + */ for(int i = 0; i < pushers; i++) new std::thread(std::bind(pusher, &q)); @@ -97,12 +76,11 @@ void testq(int size, int pushers, int poppers) 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; - cout << "Total/sec =\t" << format(push_count+pop_count) << endl << 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; - } - + } } @@ -118,68 +96,28 @@ int main(int argc, char* argv[]) int pushers = atoi(argv[2]); int poppers = atoi(argv[3]); - testq(qsize, pushers, poppers); + //testq(qsize, pushers, poppers); + - /* using namespace std::chrono; - - int nthreads = argc > 1 ? atoi(argv[1]) : 1; - int nlines = argc > 2 ? atoi(argv[2]) : 100000; - - - + auto null_sink = std::make_shared(); auto stdout_sink = std::make_shared(); auto async = std::make_shared(1000); - //auto fsink = std::make_shared("newlog", "txt", 1024*1024*10 , 2); + auto fsink = std::make_shared("newlog", "txt", 1024*1024*10 , 2); //auto fsink = std::make_shared("daily", "txt"); - //async->add_sink(fsink); - async->add_sink(null_sink); - - //console logger - auto &console = c11log::get_logger("console"); - console.add_sink(stdout_sink); - //c11log::details::blocking_queue q(1000); - //auto q_ptr = &q; - std::vector threads; - std::cout << "Starting " << nthreads << " threads x " << utils::format(nlines) << " lines each.." << std::endl; - for (int i = 0; i < nthreads; i++) - { - auto logger = std::make_shared("test"); - logger->add_sink(async); - - auto t = new std::thread([logger, nlines, i]() { - auto &console = c11log::get_logger("console"); - for(int j = 0 ; j < nlines; ++j) - { - logger->info() << "Hello from thread #" << i << "\tcounter: " << j ; - if(j % 2000 == 0) - console.info() << "Hello from thread " << i << "\tcounter: " << j; - } - }); - threads.push_back(t); - //std::this_thread::sleep_for(milliseconds(2)); - } - - - auto stime = steady_clock::now(); - int thread_joined = 0; - for(auto t:threads) - { - t->join(); - std::cout << "Joined " << ++thread_joined << " threads" << std::endl; - } - auto delta = steady_clock::now() - stime; - auto delta_seconds = duration_cast(delta).count()/1000.0; - - auto total = nthreads*nlines; - std::cout << "Total: " << utils::format(total) << " = " << utils::format(total/delta_seconds) << "/sec" << std::endl; + + //Async logger + async->add_sink(null_sink); + auto &logger = c11log::get_logger("async"); + logger.add_sink(async); + + testq(qsize, pushers, poppers); + - async->shutdown(seconds(1)); - */ }