Commit 6f81dea5 authored by Gerhard Raven's avatar Gerhard Raven

more std::string_view in MessageSvc

parent f88a75e5
Pipeline #1400143 passed with stages
in 13 minutes and 55 seconds
......@@ -82,9 +82,9 @@ void InertMessageSvc::reportMessage( const Message& msg ) {
//---------------------------------------------------------------------------
void InertMessageSvc::reportMessage( const StatusCode& code, const std::string& source ) {
void InertMessageSvc::reportMessage( const StatusCode& code, std::string_view source ) {
// msg has to be copied as the source may become invalid by the time it's used
m_messageActionsQueue.emplace( [this, code, s = std::string( source )]() { this->i_reportMessage( code, s ); } );
m_messageActionsQueue.emplace( [this, code, s = std::string{source}]() { this->i_reportMessage( code, s ); } );
}
//---------------------------------------------------------------------------
......@@ -53,7 +53,7 @@ public:
void reportMessage( const Message& msg, int outputLevel ) override;
/// Implementation of IMessageSvc::reportMessage()
void reportMessage( const StatusCode& code, const std::string& source = "" ) override;
void reportMessage( const StatusCode& code, std::string_view source = "" ) override;
private:
void m_activate();
......
......@@ -53,26 +53,20 @@ namespace {
return erase_if( c, std::move( range.first ), std::move( range.second ), std::forward<Predicate>( pred ) );
}
std::string colTrans( const std::string& col, int offset ) {
int icol = 0;
if ( col == "black" )
icol = MSG::BLACK;
else if ( col == "red" )
icol = MSG::RED;
else if ( col == "green" )
icol = MSG::GREEN;
else if ( col == "yellow" )
icol = MSG::YELLOW;
else if ( col == "blue" )
icol = MSG::BLUE;
else if ( col == "purple" )
icol = MSG::PURPLE;
else if ( col == "cyan" )
icol = MSG::CYAN;
else if ( col == "white" )
icol = MSG::WHITE;
else
icol = 8;
std::string colTrans( std::string_view col, int offset ) {
int icol =
( col == "black"
? MSG::BLACK
: col == "red"
? MSG::RED
: col == "green"
? MSG::GREEN
: col == "yellow"
? MSG::YELLOW
: col == "blue"
? MSG::BLUE
: col == "purple" ? MSG::PURPLE
: col == "cyan" ? MSG::CYAN : col == "white" ? MSG::WHITE : 8 );
return std::to_string( icol + offset );
}
} // namespace
......@@ -141,25 +135,25 @@ StatusCode MessageSvc::reinitialize() {
//#############################################################################
void MessageSvc::setupColors( Gaudi::Details::PropertyBase& prop ) {
const std::string& pname = prop.name();
int level;
if ( pname == "fatalColorCode" )
level = MSG::FATAL;
else if ( pname == "errorColorCode" )
level = MSG::ERROR;
else if ( pname == "warningColorCode" )
level = MSG::WARNING;
else if ( pname == "infoColorCode" )
level = MSG::INFO;
else if ( pname == "debugColorCode" )
level = MSG::DEBUG;
else if ( pname == "verboseColorCode" )
level = MSG::VERBOSE;
else if ( pname == "alwaysColorCode" )
level = MSG::ALWAYS;
else {
throw GaudiException( "ERROR: Unknown message color parameter: " + pname, name(), StatusCode::FAILURE );
}
const auto& pname = prop.name();
int level = ( pname == "fatalColorCode"
? MSG::FATAL
: pname == "errorColorCode"
? MSG::ERROR
: pname == "warningColorCode"
? MSG::WARNING
: pname == "infoColorCode"
? MSG::INFO
: pname == "debugColorCode"
? MSG::DEBUG
: pname == "verboseColorCode"
? MSG::VERBOSE
: pname == "alwaysColorCode"
? MSG::ALWAYS
: ( throw GaudiException(
"ERROR: Unknown message color parameter: " + pname,
name(), StatusCode::FAILURE ),
-1 ) );
auto& code = m_logColorCodes[level];
......@@ -424,18 +418,8 @@ void MessageSvc::reportMessage( const Message& msg ) { reportMessage( msg, outpu
// Purpose: dispatches a message to the relevant streams.
// ---------------------------------------------------------------------------
//
void MessageSvc::reportMessage( const char* source, int type, const char* message ) {
reportMessage( Message{source, type, message} );
}
//#############################################################################
// ---------------------------------------------------------------------------
// Routine: reportMessage
// Purpose: dispatches a message to the relevant streams.
// ---------------------------------------------------------------------------
//
void MessageSvc::reportMessage( const std::string& source, int type, const std::string& message ) {
reportMessage( Message{source, type, message} );
void MessageSvc::reportMessage( std::string source, int type, std::string message ) {
reportMessage( Message{std::move( source ), type, std::move( message )} );
}
//#############################################################################
......@@ -444,16 +428,16 @@ void MessageSvc::reportMessage( const std::string& source, int type, const std::
// Purpose: finds a message for a given status code and dispatches it.
// ---------------------------------------------------------------------------
//
void MessageSvc::reportMessage( const StatusCode& code, const std::string& source ) {
void MessageSvc::reportMessage( const StatusCode& code, std::string_view source ) {
auto lock = std::unique_lock{m_messageMapMutex};
i_reportMessage( code, source );
}
void MessageSvc::i_reportMessage( const StatusCode& code, const std::string& source ) {
void MessageSvc::i_reportMessage( const StatusCode& code, std::string_view source ) {
int level = outputLevel( source );
auto report = [&]( Message mesg ) {
mesg.setSource( source );
Message stat_code( source, mesg.getType(), "Status Code " + std::to_string( code.getCode() ) );
Message stat_code( std::string{source}, mesg.getType(), "Status Code " + std::to_string( code.getCode() ) );
i_reportMessage( std::move( stat_code ), level );
i_reportMessage( std::move( mesg ), level );
};
......@@ -529,9 +513,9 @@ void MessageSvc::eraseStream( std::ostream* stream ) {
// ---------------------------------------------------------------------------
//
void MessageSvc::insertMessage( const StatusCode& key, const Message& msg ) {
void MessageSvc::insertMessage( const StatusCode& key, Message msg ) {
auto lock = std::unique_lock{m_messageMapMutex};
m_messageMap.emplace( key, msg );
m_messageMap.emplace( key, std::move( msg ) );
}
//#############################################################################
......@@ -616,12 +600,14 @@ std::string MessageSvc::getLogColor( int logLevel ) const {
int MessageSvc::messageCount( MSG::Level level ) const { return m_msgCount[level]; }
// ---------------------------------------------------------------------------
void MessageSvc::incrInactiveCount( MSG::Level level, const std::string& source ) {
++( m_inactiveMap[source].msg[level] );
void MessageSvc::incrInactiveCount( MSG::Level level, std::string_view source ) {
auto entry = m_inactiveMap.find( source );
if ( entry == m_inactiveMap.end() ) { entry = m_inactiveMap.emplace( source, MsgAry{} ).first; }
++entry->second.msg[level];
if ( std::find( begin( m_tracedInactiveSources ), end( m_tracedInactiveSources ), source ) !=
end( m_tracedInactiveSources ) ) {
std::cout << "== inactive message detected from " << source << " ==" << std::endl;
std::cout << "== inactive message detected from " << source << " ==\n";
std::string t;
System::backTrace( t, 25, 0 );
std::cout << t << std::endl;
......@@ -629,38 +615,28 @@ void MessageSvc::incrInactiveCount( MSG::Level level, const std::string& source
}
// ---------------------------------------------------------------------------
void MessageSvc::setupLogStreams() {
// reset state
m_loggedStreams.clear();
// make the unique set of output filenames
std::set<std::string_view> outFileNames;
std::transform( m_loggedStreamsName.begin(), m_loggedStreamsName.end(),
std::inserter( outFileNames, outFileNames.end() ),
[]( const auto& p ) -> std::string_view { return p.second; } );
// map each unique filename to an ofstream
std::map<std::string_view, std::shared_ptr<std::ofstream>> outStreams;
std::transform( outFileNames.begin(), outFileNames.end(), std::inserter( outStreams, outStreams.end() ),
[this]( std::string_view fname ) {
return std::pair{fname, std::make_shared<std::ofstream>(
std::string{fname}, std::ios_base::out | std::ios_base::trunc )};
} );
// associate the stream to ofstream...
for ( auto& iProp : m_loggedStreamsName ) {
std::set<std::string> outFileNames;
for ( auto& jProp : m_loggedStreamsName ) {
if ( jProp.first != iProp.first ) { outFileNames.insert( jProp.second ); }
}
tee( iProp.first, iProp.second, outFileNames );
} //> loop over property entries
auto& stream = outStreams.at( iProp.second );
if ( stream->good() ) m_loggedStreams.emplace( iProp.first, stream );
}
}
// ---------------------------------------------------------------------------
void MessageSvc::tee( const std::string& sourceName, const std::string& outFileName,
const std::set<std::string>& outFileNames ) {
const std::ios_base::openmode openMode = std::ios_base::out | std::ios_base::trunc;
auto iStream = m_loggedStreams.find( sourceName );
if ( iStream != std::end( m_loggedStreams ) ) { m_loggedStreams.erase( iStream ); }
// before creating a new ofstream, make sure there is no already existing
// one with the same file name...
for ( auto& iStream : m_loggedStreams ) {
if ( outFileNames.find( outFileName ) != outFileNames.end() ) {
m_loggedStreams[sourceName] = m_loggedStreams[iStream.first];
return;
}
}
auto out = std::make_shared<std::ofstream>( outFileName, openMode );
if ( out->good() ) m_loggedStreams[sourceName] = std::move( out );
}
......@@ -61,16 +61,13 @@ public:
void reportMessage( const Message& msg, int outputLevel ) override;
// Implementation of IMessageSvc::reportMessage()
void reportMessage( const StatusCode& code, const std::string& source = "" ) override;
void reportMessage( const StatusCode& code, std::string_view source = "" ) override;
// Implementation of IMessageSvc::reportMessage()
void reportMessage( const char* source, int type, const char* message ) override;
// Implementation of IMessageSvc::reportMessage()
void reportMessage( const std::string& source, int type, const std::string& message ) override;
void reportMessage( std::string source, int type, std::string message ) override;
// Implementation of IMessageSvc::insertMessage()
void insertMessage( const StatusCode& code, const Message& message ) override;
void insertMessage( const StatusCode& code, Message message ) override;
// Implementation of IMessageSvc::eraseMessage()
void eraseMessage() override;
......@@ -127,14 +124,14 @@ public:
int messageCount( MSG::Level logLevel ) const override;
// Implementation of IInactiveMessageCounter::incrInactiveCount()
void incrInactiveCount( MSG::Level level, const std::string& src ) override;
void incrInactiveCount( MSG::Level level, std::string_view src ) override;
protected:
/// Internal implementation of reportMessage(const Message&,int) without lock.
virtual void i_reportMessage( const Message& msg, int outputLevel );
/// Internal implementation of reportMessage(const StatusCode&,const std::string&) without lock.
virtual void i_reportMessage( const StatusCode& code, const std::string& source );
virtual void i_reportMessage( const StatusCode& code, std::string_view source );
private:
Gaudi::Property<std::string> m_defaultFormat{this, "Format", Message::getDefaultFormat(), ""};
......@@ -180,7 +177,7 @@ private:
{},
"for each message source specified, print a stack trace for the unprotected and unseen messages"};
Gaudi::Property<std::map<std::string, std::string>> m_loggedStreamsName{
Gaudi::Property<std::map<std::string, std::string, std::less<>>> m_loggedStreamsName{
this, "loggedStreams", {}, "MessageStream sources we want to dump into a logfile"};
std::ostream* m_defaultStream = &std::cout; ///< Pointer to the output stream.
......@@ -199,11 +196,11 @@ private:
MsgAry() = default;
};
std::map<std::string, MsgAry> m_sourceMap, m_inactiveMap;
std::map<std::string, MsgAry, std::less<>> m_sourceMap, m_inactiveMap;
std::array<int, MSG::NUM_LEVELS> m_msgCount;
std::map<std::string, std::shared_ptr<std::ostream>> m_loggedStreams;
std::map<std::string, std::shared_ptr<std::ostream>, std::less<>> m_loggedStreams;
void setupColors( Gaudi::Details::PropertyBase& prop );
void setupLimits( Gaudi::Details::PropertyBase& prop );
......@@ -212,9 +209,6 @@ private:
void setupLogStreams();
void tee( const std::string& sourceName, const std::string& logFileName,
const std::set<std::string>& declaredOutFileNames );
/// Mutex to synchronize multiple threads printing.
mutable std::recursive_mutex m_reportMutex;
......
......@@ -23,8 +23,8 @@ void TBBMessageSvc::reportMessage( const Message& msg, int outputLevel ) {
void TBBMessageSvc::reportMessage( const Message& msg ) { m_messageQueue.add( new MessageWithoutLevel( *this, msg ) ); }
void TBBMessageSvc::reportMessage( const StatusCode& code, const std::string& source ) {
m_messageQueue.add( new StatusCodeMessage( *this, code, source ) );
void TBBMessageSvc::reportMessage( const StatusCode& code, std::string_view source ) {
m_messageQueue.add( new StatusCodeMessage( *this, code, std::string{source} ) );
}
// ============================================================================
......@@ -40,7 +40,7 @@ public:
void reportMessage( const Message& msg, int outputLevel ) override;
/// Implementation of IMessageSvc::reportMessage()
void reportMessage( const StatusCode& code, const std::string& source = "" ) override;
void reportMessage( const StatusCode& code, std::string_view source = "" ) override;
private:
// ============================================================================
......@@ -84,8 +84,8 @@ private:
/// Specialized class to report a StatusCode message.
class StatusCodeMessage : public MessageTaskCommon {
public:
StatusCodeMessage( TBBMessageSvc& svc, const StatusCode& sc, const std::string& source )
: MessageTaskCommon( svc ), m_sc( sc ), m_source( source ) {}
StatusCodeMessage( TBBMessageSvc& svc, const StatusCode& sc, std::string source )
: MessageTaskCommon( svc ), m_sc( sc ), m_source( std::move( source ) ) {}
void run() override { m_svc.i_reportMessage( m_sc, m_source ); }
private:
......
......@@ -65,28 +65,20 @@ public:
@param code Error code number
@param source Message source. Typically the alg/svc name
*/
virtual void reportMessage( const StatusCode& code, const std::string& source = "" ) = 0;
virtual void reportMessage( const StatusCode& code, std::string_view source = "" ) = 0;
/** Report a message by specifying the source, severity level and text.
@param source Message source. Typically the alg/svc name
@param type Severity level
@param message Text message
*/
virtual void reportMessage( const std::string& source, int type, const std::string& message ) = 0;
/** Report a message by specifying the source, severity level and text. The text is
passed as C like character string to avoid extra copying.
@param source Message source. Typically the alg/svc name
@param type Severity level
@param message Text message
*/
virtual void reportMessage( const char* source, int type, const char* message = "" ) = 0;
virtual void reportMessage( std::string source, int type, std::string message ) = 0;
/** Insert a message to be sent for a given status code into the error code repository.
@param code Status error code
@param message Message associated
*/
virtual void insertMessage( const StatusCode& code, const Message& message ) = 0;
virtual void insertMessage( const StatusCode& code, Message message ) = 0;
/// Erase all messages associated to all status codes.
virtual void eraseMessage() = 0;
......@@ -166,13 +158,13 @@ public:
class GAUDI_API IInactiveMessageCounter : virtual public IInterface {
public:
/// InterfaceID
DeclareInterfaceID( IInactiveMessageCounter, 1, 0 );
DeclareInterfaceID( IInactiveMessageCounter, 2, 0 );
/** Increment deactivated message count.
* Used by MsgStream to record the sources of messages that are prepared, but
* not printed (because if insufficient level).
*/
virtual void incrInactiveCount( MSG::Level level, const std::string& src ) = 0;
virtual void incrInactiveCount( MSG::Level level, std::string_view src ) = 0;
};
#endif // GAUDIKERNEL_IMESSAGESVC_H
......@@ -54,7 +54,7 @@ public:
const std::string& getSource() const;
/// Set the message source.
void setSource( std::string src );
void setSource( std::string_view src );
/// Get the format string.
const std::string& getFormat() const;
......@@ -75,7 +75,7 @@ public:
void setTimeFormat( std::string timeFormat ) const;
/// Needed to build maps
bool operator<( const Message& test );
friend bool operator<( const Message& lhs, const Message& rhs );
/// Insert the message into a stream.
friend std::ostream& operator<<( std::ostream& stream, const Message& msg );
......@@ -169,10 +169,4 @@ private:
static constexpr const char* DEFAULT_TIME_FORMAT = "%Y-%m-%d %H:%M:%S,%f";
};
/// Insert the message into a stream.
GAUDI_API std::ostream& operator<<( std::ostream& stream, const Message& msg );
/// Insert the message into a stream.
GAUDI_API bool operator==( const Message& a, const Message& b );
#endif
......@@ -93,7 +93,7 @@ const std::string& Message::getSource() const { return m_source; }
// Purpose: Set the message source.
// ---------------------------------------------------------------------------
//
void Message::setSource( std::string src ) { m_source = std::move( src ); }
void Message::setSource( std::string_view src ) { m_source = src; }
//#############################################################################
// ---------------------------------------------------------------------------
......@@ -113,8 +113,8 @@ std::ostream& operator<<( std::ostream& stream, const Message& msg ) {
// Purpose: comparison operator needed for maps
// ---------------------------------------------------------------------------
//
bool Message::operator<( const Message& b ) {
return m_type < b.m_type || m_source < b.m_source || m_message < b.m_message;
bool operator<( const Message& lhs, const Message& rhs ) {
return lhs.m_type < rhs.m_type || lhs.m_source < rhs.m_source || lhs.m_message < rhs.m_message;
}
//#############################################################################
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment