Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-21 10:00:33

0001 /***********************************************************************************\
0002 * (c) Copyright 1998-2022 CERN for the benefit of the LHCb and ATLAS collaborations *
0003 *                                                                                   *
0004 * This software is distributed under the terms of the Apache version 2 licence,     *
0005 * copied verbatim in the file "LICENSE".                                            *
0006 *                                                                                   *
0007 * In applying this licence, CERN does not waive the privileges and immunities       *
0008 * granted to it by virtue of its status as an Intergovernmental Organization        *
0009 * or submit itself to any jurisdiction.                                             *
0010 \***********************************************************************************/
0011 #ifndef GAUDIKERNEL_MESSAGE_H
0012 #define GAUDIKERNEL_MESSAGE_H
0013 
0014 #include "GaudiKernel/EventContext.h"
0015 #include "GaudiKernel/Kernel.h" // for GAUDI_API
0016 #include <iostream>
0017 #include <string>
0018 
0019 /** @class Message Message.h GaudiKernel/Message.h
0020 
0021     The Message class. This class is used to contain messages which can then
0022     be formatted and sent to a message service
0023 
0024     @author Iain Last
0025 */
0026 class GAUDI_API Message final {
0027 public:
0028   /// Default constructor
0029   Message() = default;
0030 
0031   /// Constructor.
0032   Message( const char* src, int type, const char* msg );
0033 
0034   /// Constructor.
0035   Message( std::string src, int type, std::string msg );
0036 
0037   /// Default destructor.
0038   ~Message() = default;
0039 
0040   /// Get the message string.
0041   const std::string& getMessage() const { return m_message; }
0042 
0043   /// Set the message string.
0044   void setMessage( std::string msg ) { m_message = std::move( msg ); }
0045 
0046   /// Get the message type.
0047   int getType() const { return m_type; }
0048 
0049   /// Set the message type.
0050   void setType( int msg_type ) { m_type = msg_type; }
0051 
0052   /// Get the message source.
0053   const std::string& getSource() const { return m_source; }
0054 
0055   /// Set the message source.
0056   void setSource( std::string_view src ) { m_source = src; }
0057 
0058   /** @name Event identifiers of message */
0059   //@{
0060   EventContext::ContextID_t  getEventSlot() const { return m_ecSlot; }
0061   EventContext::ContextEvt_t getEventNumber() const { return m_ecEvt; }
0062   EventIDBase                getEventID() const { return m_ecEvtId; }
0063   //@}
0064 
0065   /// Get the format string.
0066   const std::string& getFormat() const { return m_format; }
0067 
0068   /// Get the default format string.
0069   static const std::string getDefaultFormat() { return DEFAULT_FORMAT; }
0070 
0071   /// Set the format string.
0072   void setFormat( std::string msg ) const;
0073 
0074   /// Get the time format string.
0075   const std::string& getTimeFormat() const { return m_time_format; }
0076 
0077   /// Get the default time format string
0078   static const std::string getDefaultTimeFormat() { return DEFAULT_TIME_FORMAT; }
0079 
0080   /// Set the time format string.
0081   void setTimeFormat( std::string timeFormat ) const;
0082 
0083   /// Needed to build maps
0084   friend bool operator<( const Message& lhs, const Message& rhs );
0085 
0086   /// Insert the message into a stream.
0087   friend std::ostream& operator<<( std::ostream& stream, const Message& msg );
0088 
0089   /// Insert the message into a stream.
0090   friend bool operator==( const Message& a, const Message& b );
0091 
0092 private:
0093   /// Called when an invalid format string is encountered.
0094   void invalidFormat() const;
0095 
0096   /// Format the message.
0097   void makeFormattedMsg( const std::string& format ) const;
0098 
0099   /// Decode format.
0100   void decodeFormat( const std::string& format ) const;
0101 
0102   /// Truncate or pad the output string to the field width. If middle is true, cut the central part, otherwise the end
0103   void sizeField( const std::string& text, bool middle = false ) const;
0104 
0105   /// Set the width of a stream.
0106   void setWidth( const std::string& formatArg ) const;
0107 
0108   std::string         m_message;                            ///< The message text
0109   std::string         m_source{ "UNKNOWN" };                ///< The message source
0110   int                 m_type{ 0 };                          ///< The message type/level
0111   mutable std::string m_format{ DEFAULT_FORMAT };           ///< The format string
0112   mutable std::string m_time_format{ DEFAULT_TIME_FORMAT }; ///< Time format string
0113   mutable std::string m_formatted_msg;                      ///< Formatted message
0114   mutable char        m_fill{ ' ' };                        ///< Current fill character
0115   mutable int         m_width{ 0 };                         ///< Current field width
0116   mutable bool        m_left{ true };                       ///< The message alignment
0117 
0118   /** @name Event identifiers */
0119   //@{
0120   EventContext::ContextID_t  m_ecSlot;  ///< Event slot
0121   EventContext::ContextEvt_t m_ecEvt;   ///< Event number
0122   EventIDBase                m_ecEvtId; ///< Full event ID
0123   pthread_t                  m_ecThrd;  ///< Thread ID
0124   //@}
0125 
0126   /** @name Formatting characters */
0127   //@{
0128   /// The character used to prefix formatting commands.
0129   static const char FORMAT_PREFIX = '%';
0130 
0131   /// The character used to indicate start of left text justification.
0132   static const char JUSTIFY_LEFT = 'L';
0133 
0134   /// The character used to indicate start of right text justification.
0135   static const char JUSTIFY_RIGHT = 'R';
0136 
0137   /// The character used to indicate that the message should be printed.
0138   static const char MESSAGE = 'M';
0139 
0140   /// The character used to indicate that the message type should be printed.
0141   static const char TYPE = 'T';
0142 
0143   /// The character used to indicate that the message timestamp should be printed.
0144   static const char TIME = 't';
0145 
0146   /// The character used to indicate that the message timestamp should be printed in UTC time.
0147   static const char UTIME = 'u';
0148 
0149   /// The character used to indicate that the message source should be printed.
0150   static const char SOURCE = 'S';
0151 
0152   /// The character used to indicate that the message source should be printed, focus on the component
0153   static const char COMP = 'C';
0154 
0155   /// The character used to indicate that the slot number should be printed.
0156   static const char SLOT = 's';
0157 
0158   /// The character used to indicate that the event number should be printed.
0159   static const char EVTNUM = 'e';
0160 
0161   /// The character used to indicate that the thread ID should be printed.
0162   static const char THREAD = 'X';
0163 
0164   /// The character used to indicate that the full event ID should be printed.
0165   static const char EVENTID = 'E';
0166 
0167   /// The character used to indicate that the previous character is used to pad fields if the text is not long enough.
0168   static const char FILL = 'F';
0169 
0170   /// The character used to indicate that the previous decimal characters should be taken as the field width.
0171   static const char WIDTH = 'W';
0172   //@}
0173 
0174   /// The default message format.
0175   static constexpr const char* DEFAULT_FORMAT = "% F%18W%S%7W%R%T %0W%M";
0176 
0177   /// The default time format (accepts strftime formatters plus \%f for milliseconds).
0178   static constexpr const char* DEFAULT_TIME_FORMAT = "%Y-%m-%d %H:%M:%S,%f";
0179 };
0180 
0181 #endif