Uploaded image for project: 'Log4cxx'
  1. Log4cxx
  2. LOGCXX-18

LoggerStream Feature

    XMLWordPrintableJSON

Details

    • New Feature
    • Status: Resolved
    • Minor
    • Resolution: Fixed
    • 0.10.0
    • 0.10.0
    • None
    • None
    • n/a

    Description

      A stream which provides a nice abstracted way to only do formatting when your current log level is enabled. See list archives for details.

      #ifndef LOGGER_STREAM_HPP__INCLUDE
      #define LOGGER_STREAM_HPP__INCLUDE

      #include <log4cxx/logger.h>
      #ifndef DISABLE_IOSTREAMS
      #include <sstream>
      #endif
      #include <cassert>

      template<typename B>
      class LoggerStream {
      public:
      typedef enum

      { endl }

      modifiers;
      typedef B buffer_type;
      typedef LoggerStream<buffer_type> self_type;

      inline LoggerStream(log4cxx::LoggerPtr& aLogger,
      const log4cxx::LevelPtr& aLevel,
      const char* aFile = 0, int aLine = -1)
      : logger(aLogger),
      level(aLevel),
      file(aFile),
      line(aLine),
      buffer((aLogger->isEnabledFor(aLevel)) ? (new buffer_type()) : 0) {
      }

      inline ~LoggerStream()

      { delete(buffer); //no need to check for null }

      inline bool isEnabled() const

      { return buffer != 0; }

      template<typename T>
      inline LoggerStream& operator<<(const T& aValue) {
      if (isEnabled())

      { (*buffer) << aValue; }

      return *this;
      }

      inline LoggerStream& operator<<(const modifiers& aValue)

      { assert(aValue == endl); flush(); }

      inline LoggerStream& flush() {
      if (isEnabled())

      { logger->forcedLog(level, buffer->str().c_str(), file, line); }

      return *this;
      }

      private:
      log4cxx::LoggerPtr& logger;
      const log4cxx::LevelPtr& level;
      const char* file;
      const int line;
      buffer_type* const buffer;
      };

      //helpful macros that use STL IOSTREAMS
      #define LOG_STREAM_EX(stream_type, logger, level) LoggerStream<stream_type>(logger, level, _FILE, __LINE_)
      #define LOG_STREAM(logger, level) LOG_STREAM_EX(std::ostringstream, logger, level)
      #define WLOG_STREAM(logger, level) LOG_STREAM_EX(std::wostringstream, logger, level)
      #define ENDL LoggerStream<std::ostringstream>::endl
      #define WENDL LoggerStream<std::wostringstream>::endl
      #endif

      Attachments

        1. benchmark.tar.gz
          2 kB
          Christopher Smith
        2. LoggerStream.hpp
          2 kB
          Christopher Smith
        3. LoggerStream.hpp
          2 kB
          Christopher Smith
        4. stream.patch
          19 kB
          Curt Arnold
        5. stream.patch2
          8 kB

        Activity

          People

            carnold@apache.org Curt Arnold
            cbsmith Christopher Smith
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: