Details

    • Type: New Feature New Feature
    • Status: Resolved
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: 0.10.0
    • Fix Version/s: 0.10.0
    • Component/s: None
    • Labels:
      None
    • Environment:
      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

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

        Activity

        No work has yet been logged on this issue.

          People

          • Assignee:
            Curt Arnold
            Reporter:
            Christopher Smith
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development