From 3feba27f8b18565a9eed726286fb7cca051b1fb7 Mon Sep 17 00:00:00 2001 From: gabime Date: Thu, 20 Feb 2014 21:39:58 +0200 Subject: [PATCH] header only\! --- build/gcc/makefile | 56 -------------------------- example/makefile | 23 +++++++++++ {build/gcc => example}/makefile.clang | 0 {src => example}/test.cpp | 2 +- include/c11log/details/factory.h | 21 ++++++++++ include/c11log/details/os.h | 21 +++++++++- include/c11log/formatters/formatters.h | 24 +++++++++++ include/c11log/logger.h | 1 + include/c11log/sinks/file_sinks.h | 2 - src/factory.cpp | 24 ----------- src/formatters.cpp | 41 ------------------- src/os.cpp | 27 ------------- 12 files changed, 90 insertions(+), 152 deletions(-) delete mode 100644 build/gcc/makefile create mode 100644 example/makefile rename {build/gcc => example}/makefile.clang (100%) rename {src => example}/test.cpp (98%) delete mode 100644 src/factory.cpp delete mode 100644 src/formatters.cpp delete mode 100644 src/os.cpp diff --git a/build/gcc/makefile b/build/gcc/makefile deleted file mode 100644 index b61603ca..00000000 --- a/build/gcc/makefile +++ /dev/null @@ -1,56 +0,0 @@ -SRC_DIR=../../src - -_SOURCES = factory.cpp formatters.cpp os.cpp - -SOURCES = $(patsubst %,$(SRC_DIR)/%,$(_SOURCES)) -OBJS_RELEASE = $(patsubst %.cpp,release/%.o,$(_SOURCES)) -OBJS_DEBUG = $(patsubst %.cpp,debug/%.o,$(_SOURCES)) - - -CXX = g++ -CXXFLAGS = -march=native -Wall -Wextra -Wshadow -pedantic -std=c++11 -pthread -I../../include -CXX_RELEASE_FLAGS = -O3 -flto -CXX_DEBUG_FLAGS= -g - -OUTLIB_RELEASE = libc11log.a -OUTLIB_DEBUG = libc11log-debug.a - -TEST_RELEASE = testme -TEST_DEBUG = testme-debug - -.PHONY: all mkdirs release debug build clean rebuild - -all: release - -release: CXXFLAGS += $(CXX_RELEASE_FLAGS) -release: mkdirs build-release - - -debug: CXXFLAGS += $(CXX_DEBUG_FLAGS) -debug: mkdirs build-debug - -mkdirs: - @mkdir -p release debug - -build-release: $(OBJS_RELEASE) - ar rs $(OUTLIB_RELEASE) $^ - $(CXX) $(SRC_DIR)/test.cpp $(OUTLIB_RELEASE) -o $(TEST_RELEASE) $(CXXFLAGS) - -build-debug: $(OBJS_DEBUG) - ar rs $(OUTLIB_DEBUG) $^ - $(CXX) $(SRC_DIR)/test.cpp $(OUTLIB_DEBUG) -o $(TEST_DEBUG) $(CXXFLAGS) - - -release/%.o: $(SRC_DIR)/%.cpp - $(CXX) -c $< -o $@ $(CXXFLAGS) - - - -debug/%.o: $(SRC_DIR)/%.cpp - $(CXX) -c $< -o $@ $(CXXFLAGS) - -clean: - rm -rf release debug daily.* $(TEST_RELEASE) $(TEST_DEBUG) $(OUTLIB_RELEASE) $(OUTLIB_DEBUG) - -rebuild: clean all - diff --git a/example/makefile b/example/makefile new file mode 100644 index 00000000..7b308944 --- /dev/null +++ b/example/makefile @@ -0,0 +1,23 @@ +CXX = g++ +CXXFLAGS = -march=native -Wall -Wextra -Wshadow -pedantic -std=c++11 -pthread -I../include +CXX_RELEASE_FLAGS = -O3 -flto +CXX_DEBUG_FLAGS= -g + +OUTBIN = testme + +all: test.cpp + $(CXX) test.cpp -o $(OUTBIN) $(CXXFLAGS) $(CXX_RELEASE_FLAGS) + + +debug: test.cpp + $(CXX) test.cpp -o $(OUTBIN)-debug $(CXXFLAGS) $(CXX_DEBUG_FLAFS) + + + +clean: + rm -f *.text $(OUTBIN) $(OUTBIN)-debug + +rebuild: clean all +rebuild-debug: clean debug + + diff --git a/build/gcc/makefile.clang b/example/makefile.clang similarity index 100% rename from build/gcc/makefile.clang rename to example/makefile.clang diff --git a/src/test.cpp b/example/test.cpp similarity index 98% rename from src/test.cpp rename to example/test.cpp index fae9adc9..cb8afb04 100644 --- a/src/test.cpp +++ b/example/test.cpp @@ -87,7 +87,7 @@ int main(int argc, char* argv[]) auto fsink = std::make_shared("newlog", "txt", 1024*1024*50 , 5); //auto fsink = std::make_shared("daily", "txt"); - async->add_sink(fsink); + async->add_sink(null_sink); auto &logger = c11log::get_logger("async"); logger.add_sink(async); diff --git a/include/c11log/details/factory.h b/include/c11log/details/factory.h index f57883dd..30fb1ade 100644 --- a/include/c11log/details/factory.h +++ b/include/c11log/details/factory.h @@ -20,3 +20,24 @@ private: }; } } + + +inline c11log::details::factory::logger_ptr c11log::details::factory::get_logger(const std::string &name) +{ + std::lock_guard lock(_loggers_mutex); + auto found = _loggers.find(name); + if (found == _loggers.end()) { + auto new_logger_ptr = std::make_shared(name); + _loggers.insert(std::make_pair(name, new_logger_ptr)); + return new_logger_ptr; + } + else { + return found->second; + } +} + +inline c11log::details::factory & c11log::details::factory::instance() +{ + static c11log::details::factory instance; + return instance; +} diff --git a/include/c11log/details/os.h b/include/c11log/details/os.h index 7aa70e39..73eb938a 100644 --- a/include/c11log/details/os.h +++ b/include/c11log/details/os.h @@ -9,9 +9,28 @@ namespace c11log { namespace os { - std::tm localtime(const std::time_t &time_t); + std::tm localtime(const std::time_t &time_tt); std::tm localtime(); } } } + + +inline std::tm c11log::details::os::localtime(const std::time_t &time_tt) +{ + + std::tm tm; +#ifdef _MSC_VER + localtime_s(&tm, &time_tt); +#else + localtime_r(&time_tt, &tm); +#endif + return tm; +} + +inline std::tm c11log::details::os::localtime() +{ + std::time_t now_t = time(0); + return localtime(now_t); +} diff --git a/include/c11log/formatters/formatters.h b/include/c11log/formatters/formatters.h index a6561ec5..26575183 100644 --- a/include/c11log/formatters/formatters.h +++ b/include/c11log/formatters/formatters.h @@ -39,3 +39,27 @@ private: }; } //namespace formatter } //namespace c11log + + + +inline void c11log::formatters::default_formatter::_format_time(const time_point& tp, std::ostream &dest) +{ + using namespace std::chrono; + + static thread_local c11log::formatters::time_point last_tp; + static thread_local char timestamp_cache[64]; + + + if(duration_cast(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; +} diff --git a/include/c11log/logger.h b/include/c11log/logger.h index 689421a4..de7b0918 100644 --- a/include/c11log/logger.h +++ b/include/c11log/logger.h @@ -164,3 +164,4 @@ inline c11log::logger& c11log::get_logger(const std::string& name) { return *(c11log::details::factory::instance().get_logger(name)); } + diff --git a/include/c11log/sinks/file_sinks.h b/include/c11log/sinks/file_sinks.h index 3c57115f..ccac9821 100644 --- a/include/c11log/sinks/file_sinks.h +++ b/include/c11log/sinks/file_sinks.h @@ -90,7 +90,6 @@ private: } _ofstream.open(_calc_filename(_base_filename, 0, _extension)); } - std::string _base_filename; std::string _extension; std::size_t _max_size; @@ -98,7 +97,6 @@ private: std::size_t _current_size; std::mutex mutex_; std::ofstream _ofstream; - }; /* diff --git a/src/factory.cpp b/src/factory.cpp deleted file mode 100644 index 163422d7..00000000 --- a/src/factory.cpp +++ /dev/null @@ -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 lock(_loggers_mutex); - auto found = _loggers.find(name); - if (found == _loggers.end()) { - auto new_logger_ptr = std::make_shared(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; -} diff --git a/src/formatters.cpp b/src/formatters.cpp deleted file mode 100644 index 40365a2b..00000000 --- a/src/formatters.cpp +++ /dev/null @@ -1,41 +0,0 @@ -#include "stdafx.h" -#include - -#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(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(); -} diff --git a/src/os.cpp b/src/os.cpp deleted file mode 100644 index 7fca8fc0..00000000 --- a/src/os.cpp +++ /dev/null @@ -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); -} -} -} -}