C++ simple and easy to use Logger

Hello cplusplus community,

I finished a project I was working on, CodeDiary. CodeDiary is a really small Logger I wrote for personal usage and just wanted to share it with you. I used some simple template metaprogramming techniques to try and make it as extensible and easy to use as possible.

I'm just a beginner in templates so any feedback would be greatly appreciated.

Github page: https://github.com/TheCrafter/CodeDiary

It's my first open source project on GitHub and I would really appreciate any feedback on:

- My design
- Code quality
- Bugs/mistakes
- Porject structure

Thanks you all for your time.
A few suggestions:

In DefaultDispatcher, consider using std::clog instead of std::cout

You may want to provide a mechanism by which user can directly format log messages instead of first having to format a string. For instance, quintessential C: minimal, limited type support, non-extensible and type-unsafe:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
template<typename Formatter = DefaultFormatter, typename Dispatcher = DefaultDispatcher>
class Logger : private Formatter, private Dispatcher
{
    public:
        
        // ...

        static void Log( const std::string& msg, LogType type )
        { Dispatcher::Dispatch( Formatter::Format( msg, LogTypeToString(type) ) ); }

        /////////////////////////////////////////////////////////// 
        static int Log( LogType type, const char* format, ... )
        {
            va_list arg_list;
            va_start( arg_list, format );
            va_list arg_list_cpy;
            va_copy( arg_list_cpy, arg_list );

            const auto nchars = std::vsnprintf( nullptr, 0, format, arg_list );
            va_end(arg_list);

            if( nchars > 0 )
            {
                std::vector<char> buffer( nchars + 1 );
                std::vsnprintf( buffer.data(), buffer.size(), format, arg_list_cpy );
                Log( { buffer.begin(), buffer.end() }, type );
            }

            va_end(arg_list_cpy);
            return nchars ;
        }
        /////////////////////////////////////////////////////////// 

        // ...
};

http://coliru.stacked-crooked.com/a/89af6ff49a0997a6

TODO (version 2):
Make this extensible (support user defined types) and type-safe.
Perhaps model the log on std::ostream - standard and custom manipulators, overloaded operators etc.

Implementation suggestion: Maintain an internal string stream into which the formatted data is written. When a new line is encountered, extract the string and log it. Something like:
1
2
3
4
5
static void on_newline( LogType type )
{ 
    Dispatcher::Dispatch( Formatter::Format( ostrstm.str(), LogTypeToString(type) ) ); 
    ostrstm.str("") ;
}
Thanks JLBorges for your suggestions. I'll keep them in mind!

Although, I'm not so sure about ostream.. I think it is not such a useful feature for a logging class.
Topic archived. No new replies allowed.