header only\!
This commit is contained in:
		
							parent
							
								
									8176f82de2
								
							
						
					
					
						commit
						3feba27f8b
					
				@ -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
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
							
								
								
									
										23
									
								
								example/makefile
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								example/makefile
									
									
									
									
									
										Normal file
									
								
							@ -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
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -87,7 +87,7 @@ int main(int argc, char* argv[])
 | 
				
			|||||||
    auto fsink = std::make_shared<c11log::sinks::rotating_file_sink>("newlog", "txt", 1024*1024*50 , 5);
 | 
					    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");
 | 
					    //auto fsink = std::make_shared<c11log::sinks::daily_file_sink>("daily", "txt");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	async->add_sink(fsink);
 | 
						async->add_sink(null_sink);
 | 
				
			||||||
	auto &logger = c11log::get_logger("async");
 | 
						auto &logger = c11log::get_logger("async");
 | 
				
			||||||
	logger.add_sink(async);
 | 
						logger.add_sink(async);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -20,3 +20,24 @@ private:
 | 
				
			|||||||
};
 | 
					};
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					inline 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;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					inline c11log::details::factory & c11log::details::factory::instance()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						static c11log::details::factory instance;
 | 
				
			||||||
 | 
						return instance;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -9,9 +9,28 @@ namespace c11log
 | 
				
			|||||||
	{
 | 
						{
 | 
				
			||||||
		namespace os
 | 
							namespace os
 | 
				
			||||||
		{			
 | 
							{			
 | 
				
			||||||
			std::tm localtime(const std::time_t &time_t);			
 | 
								std::tm localtime(const std::time_t &time_tt);
 | 
				
			||||||
			std::tm localtime(); 
 | 
								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);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -39,3 +39,27 @@ private:
 | 
				
			|||||||
};
 | 
					};
 | 
				
			||||||
} //namespace formatter
 | 
					} //namespace formatter
 | 
				
			||||||
} //namespace c11log
 | 
					} //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<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;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -164,3 +164,4 @@ inline c11log::logger& c11log::get_logger(const std::string& name)
 | 
				
			|||||||
{
 | 
					{
 | 
				
			||||||
	return *(c11log::details::factory::instance().get_logger(name));
 | 
						return *(c11log::details::factory::instance().get_logger(name));
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -90,7 +90,6 @@ private:
 | 
				
			|||||||
		}
 | 
							}
 | 
				
			||||||
		_ofstream.open(_calc_filename(_base_filename, 0, _extension));
 | 
							_ofstream.open(_calc_filename(_base_filename, 0, _extension));
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					 | 
				
			||||||
	std::string _base_filename;
 | 
						std::string _base_filename;
 | 
				
			||||||
	std::string _extension;
 | 
						std::string _extension;
 | 
				
			||||||
	std::size_t _max_size;
 | 
						std::size_t _max_size;
 | 
				
			||||||
@ -98,7 +97,6 @@ private:
 | 
				
			|||||||
	std::size_t _current_size;
 | 
						std::size_t _current_size;
 | 
				
			||||||
	std::mutex mutex_;
 | 
						std::mutex mutex_;
 | 
				
			||||||
	std::ofstream _ofstream;
 | 
						std::ofstream _ofstream;
 | 
				
			||||||
 | 
					 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/*
 | 
					/*
 | 
				
			||||||
 | 
				
			|||||||
@ -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);
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user