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 ;
}
///////////////////////////////////////////////////////////
// ...
};
|