Skip to content

Commit

Permalink
use function instead of actual object for logging
Browse files Browse the repository at this point in the history
  • Loading branch information
turleypol committed Jan 3, 2024
1 parent 3a174fe commit e99d526
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 21 deletions.
18 changes: 11 additions & 7 deletions pol-core/clib/logfacility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,16 +239,17 @@ Message<Sink>::~Message()
else
global_logger->save<Sink>( _formater->str(), std::move( _id ) );
}
if ( !_msg.empty() )
{
if ( global_logger == nullptr )
printf( "%s", _msg.c_str() );
else
global_logger->save<Sink>( std::move( _msg ), std::move( _id ) );
}
}


template <typename Sink>
void Message2<Sink>::send( std::string msg )
{
if ( global_logger == nullptr )
printf( "%s\n", msg.c_str() );
else
global_logger->save<Sink>( std::move( msg + '\n' ), "" ); // TODO always add \n?
}
// create and get a sink
template <typename Sink>
Sink* getSink()
Expand Down Expand Up @@ -516,12 +517,15 @@ bool Clib::Logging::LogSink_debuglog::Disabled = false;
// could reduce the compilation time a bit

#define SINK_TEMPLATE_DEFINES( sink ) \
template struct Pol::Clib::Logging::Message2<Pol::Clib::Logging::sink>; \
template class Pol::Clib::Logging::Message<Pol::Clib::Logging::sink>; \
template Pol::Clib::Logging::sink* Pol::Clib::Logging::getSink<Pol::Clib::Logging::sink>(); \
template void Pol::Clib::Logging::LogFacility::save<Pol::Clib::Logging::sink>( \
std::string message, std::string id );

#define SINK_TEMPLATE_DEFINES_DUAL( sink1, sink2 ) \
template struct Pol::Clib::Logging::Message2< \
Pol::Clib::Logging::LogSink_dual<Pol::Clib::Logging::sink1, Pol::Clib::Logging::sink2>>; \
template class Pol::Clib::Logging::Message< \
Pol::Clib::Logging::LogSink_dual<Pol::Clib::Logging::sink1, Pol::Clib::Logging::sink2>>; \
template Pol::Clib::Logging::LogSink_dual<Pol::Clib::Logging::sink1, Pol::Clib::Logging::sink2>* \
Expand Down
34 changes: 20 additions & 14 deletions pol-core/clib/logfacility.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,16 +177,6 @@ class Message
{
public:
Message();
Message( std::string msg )
{
_msg = std::move( msg );
_msg += '\n'; // TODO do we need nonflushing logs?
};
template <typename... T>
Message( std::string_view format, T&&... args )
{
_msg = fmt::format( format, args... ) + '\n';
}
Message( LogWithIDTag, const std::string& id );
~Message(); // auto flush

Expand All @@ -195,10 +185,25 @@ class Message
private:
std::unique_ptr<fmt::Writer> _formater;
std::string _id = {};
std::string _msg = {};
};


template <typename Sink>
struct Message2
{
template <typename... T>
static void log( std::string_view format, T&&... args )
{
if constexpr ( sizeof...( args ) == 0 )
send( std::string( format ) );
else
send( fmt::format( format, args... ) );
};

private:
static void send( std::string msg );
};

extern LogFacility* global_logger; // pointer to the instance of the main class
void initLogging( LogFacility* logger ); // initalize the logging
} // namespace Logging
Expand All @@ -217,15 +222,16 @@ void initLogging( LogFacility* logger ); // initalize the logging
Clib::Logging::Message< \
Clib::Logging::LogSink_dual<Clib::Logging::LogSink_cout, Clib::Logging::LogSink_pollog>>() \
.message()
using POLLOG_INFO2 = Clib::Logging::Message<
Clib::Logging::LogSink_dual<Clib::Logging::LogSink_cout, Clib::Logging::LogSink_pollog>>;
#define POLLOG_INFO2 \
Clib::Logging::Message2<Clib::Logging::LogSink_dual<Clib::Logging::LogSink_cout, \
Clib::Logging::LogSink_pollog>>::log

// log into pol.log
#define POLLOG Clib::Logging::Message<Clib::Logging::LogSink_pollog>().message()

// log only into std::cout
#define INFO_PRINT Clib::Logging::Message<Clib::Logging::LogSink_cout>().message()
using INFO_PRINT2 = Clib::Logging::Message<Clib::Logging::LogSink_cout>;
#define INFO_PRINT2 Clib::Logging::Message2<Clib::Logging::LogSink_cout>::log
// log only into std::cout if level is equal or higher
#define INFO_PRINT_TRACE( n ) \
if ( Plib::systemstate.config.debug_level >= n ) \
Expand Down

0 comments on commit e99d526

Please sign in to comment.