carnold     2004/10/07 21:10:59

  Modified:    include/log4cxx stream.h
               include/log4cxx/spi/location locationinfo.h
  Log:
  LOGCXX-18: Performance tweaks
  
  Revision  Changes    Path
  1.2       +67 -26    logging-log4cxx/include/log4cxx/stream.h
  
  Index: stream.h
  ===================================================================
  RCS file: /home/cvs/logging-log4cxx/include/log4cxx/stream.h,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- stream.h  7 Oct 2004 18:52:12 -0000       1.1
  +++ stream.h  8 Oct 2004 04:10:59 -0000       1.2
  @@ -38,15 +38,15 @@
                           ::std::basic_stringbuf<Elem, 
Tr>(::std::ios_base::out),
                           logger(logger),
                           level(level)  {
  -             isLoggerEnabled = logger->isEnabledFor(level);
  +             enabled = logger->isEnabledFor(level);
           }
   
          /**
           * Gets whether logger is currently enabled for the specified level.
           * @returns true if enabled
           */
  -        bool isEnabled() const {
  -               return isLoggerEnabled;
  +        inline bool isEnabled() const {
  +               return enabled;
           }
   
           /**
  @@ -62,8 +62,17 @@
            * @param level level
            */
           void setLevel(const ::log4cxx::LevelPtr& level) {
  +               bool newState = logger->isEnabledFor(level);
  +               //
  +               //    if not previously enabled but soon to be enabled then
  +               //       reset the buffer to clear any previously inserted 
content
  +               if (newState && !enabled) {
  +                  //
  +                  //   reset the stream buffer
  +                  seekoff(0, ::std::ios_base::beg, ::std::ios_base::out);
  +               }
                  this->level = level;
  -               isLoggerEnabled = logger->isEnabledFor(level);
  +               enabled = newState;
           }
   
          protected:
  @@ -72,23 +81,31 @@
           *
           */
           int sync() {
  -               isLoggerEnabled = logger->isEnabledFor(level);
  -               if (isLoggerEnabled) {
  -                      ::std::basic_string<Elem, Tr> msg(str());
  -                      if (msg.length() > 0) {
  -                          logger->forcedLog(level,
  -                              msg,
  -                              location.getFileName(),
  -                              location.getLineNumber());
  -                      }
  +               //
  +               //  if previously enabled
  +               if (enabled) {
  +                      //  check (and cache) whether it is still enabled
  +                  enabled = logger->isEnabledFor(level);
  +                  //  log message if still enabled
  +                  if (enabled) {
  +                       logger->forcedLog(level,
  +                           str(),
  +                           location.getFileName(),
  +                           location.getLineNumber());
  +                  }
                  }
  -                  location.clear();
  +               //  clear call site information
  +          location.clear();
                  //
                  //   reset the stream buffer
                  seekoff(0, ::std::ios_base::beg, ::std::ios_base::out);
                  return 0;
           }
   
  +       bool isEnabledFor(const ::log4cxx::LevelPtr& level) const {
  +                return logger.isEnabledFor(level);
  +       }
  +
           private:
           /**
            * logger.
  @@ -103,10 +120,10 @@
            */
           ::log4cxx::spi::location::LocationInfo location;
   
  -           /**
  -           * State of logger at last sync or level changes.
  -           */
  -           bool isLoggerEnabled;
  +       /**
  +        * State of logger at last sync or level changes.
  +        */
  +       bool enabled;
   
        };
   
  @@ -154,9 +171,13 @@
                  buffer.setLevel(level);
           }
   
  -           bool isEnabled() const {
  -             return buffer.isEnabled();
  -           }
  +       inline bool isEnabled() const {
  +           return buffer.isEnabled();
  +       }
  +       
  +       bool isEnabledFor(const ::log4cxx::LevelPtr& level) const {
  +                return buffer.isEnabledFor(level);
  +       }
   
   
           private:
  @@ -175,11 +196,29 @@
   ::log4cxx::basic_logstream<Elem, Tr>& operator<<(
      ::log4cxx::basic_logstream<Elem, Tr>& lhs,
      const ::log4cxx::spi::location::LocationInfo& rhs) {
  -   lhs.setLocation(rhs);
  +   if (LOG4CXX_UNLIKELY(lhs.isEnabled())) {
  +      lhs.setLocation(rhs);
  +   }
      return lhs;
   }
   
   /**
  +* Insertion operator for LocationFlush.
  +*
  +*/
  +template<class Elem, class Tr>
  +::log4cxx::basic_logstream<Elem, Tr>& operator<<(
  +   ::log4cxx::basic_logstream<Elem, Tr>& lhs,
  +   const ::log4cxx::spi::location::LocationFlush& rhs) {
  +   if (LOG4CXX_UNLIKELY(lhs.isEnabled())) {
  +       lhs.setLocation(rhs);
  +       lhs.flush();
  +   }
  +   return lhs;
  +}
  +
  +
  +/**
   * Insertion operator for LocationInfo.
   *
   */
  @@ -197,12 +236,14 @@
   ::log4cxx::basic_logstream<Elem, Tr>& operator<<(          \
      ::log4cxx::basic_logstream<Elem, Tr>& lhs,              \
      InsType rhs) {                                          \
  -   if (lhs.isEnabled()) {                                  \
  +   if (LOG4CXX_UNLIKELY(lhs.isEnabled())) {                \
         ((::std::basic_ostream<Elem, Tr>&) lhs) << rhs;      \
      }                                                       \
      return lhs;                                             \
   }
   
  +
  +
   /*
   * Insertion operators for common types.
   * Can't use template or would get ambiguities.
  @@ -228,15 +269,15 @@
   ::log4cxx::basic_logstream<Elem, Tr>& operator<<(
      ::log4cxx::basic_logstream<Elem, Tr>& lhs,
      const ::std::basic_string<Elem, Tr>& rhs) {
  -   if (lhs.isEnabled()) {
  +   if (LOG4CXX_UNLIKELY(lhs.isEnabled())) {
         ((::std::basic_ostream<Elem, Tr>&) lhs) << rhs;
      }
      return lhs;
   }
   
   
  -#if !defined(LOG4CXX_ENDL)
  -#define LOG4CXX_ENDMSG LOG4CXX_LOCATION << ::std::flush
  +#if !defined(LOG4CXX_ENDMSG)
  +#define LOG4CXX_ENDMSG LOG4CXX_LOCATION_FLUSH
   #endif
   
   
  
  
  
  1.2       +29 -0     
logging-log4cxx/include/log4cxx/spi/location/locationinfo.h
  
  Index: locationinfo.h
  ===================================================================
  RCS file: 
/home/cvs/logging-log4cxx/include/log4cxx/spi/location/locationinfo.h,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- locationinfo.h    7 Oct 2004 18:52:12 -0000       1.1
  +++ locationinfo.h    8 Oct 2004 04:10:59 -0000       1.2
  @@ -156,6 +156,23 @@
   
   
         };
  +      
  +      
  +      class LocationFlush : public LocationInfo {
  +             public:
  +       /**
  +        *   Constructor.
  +        *   @remarks Used by LOG4CXX_LOCATION_FLUSH to generate
  +        *       location info for current code site and
  +        *       flush a logging stream
  +        */
  +        LocationFlush( const char * const fileName,
  +                      const char * const className,
  +                      const char * const methodName,
  +                      int lineNumber )
  +             : LocationInfo( fileName, className, methodName, lineNumber ) {
  +        }
  +      };
       }
     }
   }
  @@ -166,14 +183,26 @@
              __PRETTY_FUNCTION__,                                              
\
              __func__,                                                         
\
              __LINE__)
  +      #define LOG4CXX_LOCATION_FLUSH 
::log4cxx::spi::location::LocationFlush(__FILE__, \
  +           __PRETTY_FUNCTION__,                                              
\
  +           __func__,                                                         
\
  +           __LINE__)
       #else
         #if defined(_MSC_VER)
           #define LOG4CXX_LOCATION 
::log4cxx::spi::location::LocationInfo(__FILE__, \
                __FUNCSIG__,                                                    
  \
                __FUNCTION__,                                                   
  \
                __LINE__)
  +        #define LOG4CXX_LOCATION_FLUSH 
::log4cxx::spi::location::LocationFlush(__FILE__, \
  +             __FUNCSIG__,                                                    
  \
  +             __FUNCTION__,                                                   
  \
  +             __LINE__)
         #else
           #define LOG4CXX_LOCATION 
::log4cxx::spi::location::LocationInfo(__FILE__, \
  +             ::log4cxx::spi::location::LocationInfo::NA,                     
  \
  +             __func__,                                                       
  \
  +             __LINE__)
  +        #define LOG4CXX_LOCATION_FLUSH 
::log4cxx::spi::location::LocationFlush(__FILE__, \
                ::log4cxx::spi::location::LocationInfo::NA,                     
  \
                __func__,                                                       
  \
                __LINE__)
  
  
  

Reply via email to