christianpanov / lwlog Goto Github PK
View Code? Open in Web Editor NEWVery fast synchronous and asynchronous C++17 logging library
License: MIT License
Very fast synchronous and asynchronous C++17 logging library
License: MIT License
When you define LWLOG_LOCAL_TIME at the top of the file, the library should switch to local time instead of gmt, however, it has no effect whatsoever. The logic behind it is in the datetime.h file
The timestamp is only evaluated once, resulting in a singular timestamp that doesn't get updated.
The problem Is that the function lwlog::details::datetime::format_time() is too slow for it to be called on every log call.
It's execution is ~11μs, which is far too much if we want a log call to remain ~8μs/~10μs. A workaround needs to be found.
Since the formatting patterns are always literals, thus are known at compile-time, it will be beneficial to reimplement the pattern parsing to happen at compile-time through metaprogramming
EDIT: It will be impossible to implement compile-time pattern parsing because of the logger's runtime polymorphic nature. Even if constexpr parsing functions are implemented, they will be called from functions that cannot be compile-time, and thus these functions will not be called at compile-time themselves
Calling a log function from the global default logger causes an exception to be thrown. The peculiar thing is that if we have created a logger object manually, calling the global default logger does not throw an exception.
// The following code results in exception
#include "lwlog.h"
int main()
{
lwlog::critical("Critical message");
return 0;
}
// While this one does not
#include "lwlog.h"
int main()
{
auto console = std::make_shared<
lwlog::logger<
lwlog::default_log_policy,
lwlog::default_storage_policy,
lwlog::single_threaded_policy,
lwlog::sinks::stdout_sink
>
>("CONSOLE");
lwlog::critical("Critical message");
return 0;
}
Valgrind reports invalid memory read in pattern::request_flag_formatters()
while running lwlog_sandbox
:
$ valgrind ./lwlog_sandbox
==207789== Command: ./lwlog_sandbox
==207789==
==207789== Invalid read of size 1
==207789== at 0x484E95D: bcmp (vg_replace_strmem.c:1229)
==207789== by 0x40E57C: std::__detail::_Map_base<std::basic_string_view<char, std::char_traits<char> >, std::pair<std::basic_string_view<char, std::char_traits<char> > const, std::shared_ptr<lwlog::details::formatter> >, std::allocator<std::pair<std::basic_string_view<char, std::char_traits<char> > const, std::shared_ptr<lwlog::details::formatter> > >, std::__detail::_Select1st, std::equal_to<std::basic_string_view<char, std::char_traits<char> > >, std::hash<std::basic_string_view<char, std::char_traits<char> > >, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits<true, false, true>, true>::operator[](std::basic_string_view<char, std::char_traits<char> > const&) (in /lwlog/build/lwlog_sandbox)
==207789== by 0x4097EB: lwlog::details::pattern::request_flag_formatters() (in /lwlog/build/lwlog_sandbox)
==207789== by 0x4085DF: lwlog::sinks::sink<true, lwlog::single_threaded_policy>::set_pattern(std::basic_string_view<char, std::char_traits<char> >) (in /lwlog/build/lwlog_sandbox)
==207789== by 0x40789E: lwlog::logger<lwlog::configuration<lwlog::enable_time, lwlog::disable_local_time, lwlog::disable_precise_units, lwlog::disable_thread_id, lwlog::disable_process_id>, lwlog::asynchronous_policy<1024ul, lwlog::block_overflow_policy>, lwlog::buffered_flush_policy<4194304ul>, lwlog::single_threaded_policy, lwlog::sinks::stdout_sink>::set_pattern(std::basic_string_view<char, std::char_traits<char> >) (in /lwlog/build/lwlog_sandbox)
==207789== by 0x4061B4: main (in /lwlog/build/lwlog_sandbox)
==207789== Address 0x4ddcc7f is 15 bytes inside a block of size 31 free'd
==207789== at 0x48463F3: operator delete(void*) (vg_replace_malloc.c:1051)
==207789== by 0x49C2504: UnknownInlinedFun (new_allocator.h:172)
==207789== by 0x49C2504: UnknownInlinedFun (alloc_traits.h:517)
==207789== by 0x49C2504: UnknownInlinedFun (basic_string.h:289)
==207789== by 0x49C2504: UnknownInlinedFun (basic_string.h:283)
==207789== by 0x49C2504: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_mutate(unsigned long, unsigned long, char const*, unsigned long) (basic_string.tcc:342)
==207789== by 0x49C368A: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_replace(unsigned long, unsigned long, char const*, unsigned long) (basic_string.tcc:548)
==207789== by 0x4085CF: lwlog::sinks::sink<true, lwlog::single_threaded_policy>::set_pattern(std::basic_string_view<char, std::char_traits<char> >) (in /lwlog/build/lwlog_sandbox)
==207789== by 0x40789E: lwlog::logger<lwlog::configuration<lwlog::enable_time, lwlog::disable_local_time, lwlog::disable_precise_units, lwlog::disable_thread_id, lwlog::disable_process_id>, lwlog::asynchronous_policy<1024ul, lwlog::block_overflow_policy>, lwlog::buffered_flush_policy<4194304ul>, lwlog::single_threaded_policy, lwlog::sinks::stdout_sink>::set_pattern(std::basic_string_view<char, std::char_traits<char> >) (in /lwlog/build/lwlog_sandbox)
==207789== by 0x4061B4: main (in /lwlog/build/lwlog_sandbox)
==207789== Block was alloc'd at
==207789== at 0x4842F95: operator new(unsigned long) (vg_replace_malloc.c:483)
==207789== by 0x49C248D: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_mutate(unsigned long, unsigned long, char const*, unsigned long) (basic_string.tcc:332)
==207789== by 0x49C368A: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_replace(unsigned long, unsigned long, char const*, unsigned long) (basic_string.tcc:548)
==207789== by 0x40837B: lwlog::sinks::sink<true, lwlog::single_threaded_policy>::sink() (in /lwlog/build/lwlog_sandbox)
==207789== by 0x40820B: std::__shared_count<(__gnu_cxx::_Lock_policy)2>::__shared_count<lwlog::sinks::stdout_sink<lwlog::buffered_flush_policy<4194304ul>, lwlog::single_threaded_policy>, std::allocator<void>>(lwlog::sinks::stdout_sink<lwlog::buffered_flush_policy<4194304ul>, lwlog::single_threaded_policy>*&, std::_Sp_alloc_shared_tag<std::allocator<void> >) (in /lwlog/build/lwlog_sandbox)
==207789== by 0x407566: lwlog::logger<lwlog::configuration<lwlog::enable_time, lwlog::disable_local_time, lwlog::disable_precise_units, lwlog::disable_thread_id, lwlog::disable_process_id>, lwlog::asynchronous_policy<1024ul, lwlog::block_overflow_policy>, lwlog::buffered_flush_policy<4194304ul>, lwlog::single_threaded_policy, lwlog::sinks::stdout_sink>::logger<>(std::basic_string_view<char, std::char_traits<char> >) (in /lwlog/build/lwlog_sandbox)
==207789== by 0x406188: main (in /lwlog/build/lwlog_sandbox)
==207789==
Pattern flags don't get updated due to the static nature of the current formatter.
auto console = std::make_shared<lwlog::basic_logger<sinks::stdout_color_sink>>("CONSOLE");
auto console2 = std::make_shared<lwlog::basic_logger<sinks::stdout_color_sink>>("CONSOLE2");
console ->critical("First critical message");
Instead of outputting the expected result
[17:31:39] [CONSOLE] [critical]: First critical message
It will output
[17:31:39] [CONSOLE2] [critical]: First critical message
Currently, writing to a file sink is slower than it should be, because of the file writer class. It is probably not implemented in the best way possible and is quite inefficient compared to other implementations.
EDIT: File writing is now much more performant. It happens as ~20μs instead of the former ~60μs. However, there still is room for improvement,
Mentioned in #6 with some help regarding this.
Opening an issue for tracking.
The method in the file_sink
class, called sink_it
is intended to write to a file, but it does not. A file_helper
class object is used in the file_sink
class to deal with the opening and writing to the file.
In the file_helper class, the std::FILE* m_file
variable gets initialized to nullptr at an unknown point which is not meant to be nullptr at that time, since if the m_file
variable equals nullptr then it won't write to the file, which is maybe the reason for the error.
As of now, pattern formatting is done in a very stupidly simple way. A hash map holds keys and values.
The pattern is read, finds matching keys and replaces them with their corresponding values. That instigates the whole map to be iterated through.
A better, more efficient pattern formatting algorithm needs to be implemented
I am not going to use FMT for that since I don't want any external dependencies. For that reason, I am going to rely on C++20's std::format. As soon as it gets implemented in MSVC, such formatting capability should be implemented in lwlog
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.