【问题标题】:Redirect C++ std::clog to syslog on Unix将 C++ std::clog 重定向到 Unix 上的 syslog
【发布时间】:2011-02-07 23:32:23
【问题描述】:

我在 Unix 上开发一个向 syslog 发送消息的 C++ 程序。

当前代码使用类似于 printf 的 syslog 系统调用。

现在我更愿意为此目的使用流,通常是内置的 std::clog。但是 clog 只是将输出重定向到 stderr,而不是 syslog,这对我来说毫无用处,因为我还将 stderr 和 stdout 用于其他目的。

我在another answer 中看到,使用 rdbuf() 将其重定向到文件很容易,但我认为无法应用该方法来调用 syslog,因为 openlog 不返回我可以用来绑定的文件处理程序上面有一条流。

还有其他方法吗? (对于unix编程来说看起来很基础)?

编辑:我正在寻找不使用外部库的解决方案。 @Chris 提出的建议可能是一个好的开始,但要成为公认的答案仍然有点模糊。

编辑:使用 Boost.IOStreams 没问题,因为我的项目已经在使用 Boost。

与外部库链接是可能的,但也是一个问题,因为它是 GPL 代码。依赖关系也是一种负担,因为它们可能与其他组件发生冲突,在我的 Linux 发行版上不可用,引入第三方错误等。如果这是唯一的解决方案,我可能会考虑完全避免流......(可惜)。

【问题讨论】:

  • syslog 需要的不仅仅是消息字符串;它还需要error level 等等。我不确定这是否可以使用流。也许有机械手(就像std::hex)?
  • 查看可用的日志库。许多人将允许您编写自己的后端,将您的消息写入您想要写入的任何位置。许多还带有内置过滤和其他不错的功能。只有很少的重量轻的行李很少,但如果你愿意,你可以找到它们。我正在使用这个:templog.org 它只是一些源文件,几乎都在头文件中,并且擅长在编译时(对于时间关键代码)以及运行时进行过滤。但你可能会喜欢其他的。只是不要重新发明轮子。
  • 如果不能使用内置 clog 完成,另一个用户定义的专用流几乎一样好,如果使用操纵器或成员函数设置级别,我不太在意。
  • @kriss:您几乎从不想专门化 stream 类,但几乎总是编写自己的流 buffer
  • @sbi:我正在寻找真正轻量级的东西。看起来很奇怪,20 行 C++ 无法完成如此简单的事情。感谢您指出 templog.org 它看起来不错,但似乎正在做 syslog 已经做的事情(如日志过滤)。我不想为我的操作系统添加第二个轮子,或者用其他轮子替换现有的 syslog 轮子。实际上感觉就像内置的 C++ 轮子 (std::clog) 是方形的......

标签: c++ stream syslog clog


【解决方案1】:

我也需要这样简单的东西,所以我把它放在一起:

log.h:

#include <streambuf>
#include <syslog.h>
enum LogPriority {
    kLogEmerg   = LOG_EMERG,   // system is unusable
    kLogAlert   = LOG_ALERT,   // action must be taken immediately
    kLogCrit    = LOG_CRIT,    // critical conditions
    kLogErr     = LOG_ERR,     // error conditions
    kLogWarning = LOG_WARNING, // warning conditions
    kLogNotice  = LOG_NOTICE,  // normal, but significant, condition
    kLogInfo    = LOG_INFO,    // informational message
    kLogDebug   = LOG_DEBUG    // debug-level message
};

std::ostream& operator<< (std::ostream& os, const LogPriority& log_priority);

class Log : public std::basic_streambuf<char, std::char_traits<char> > {
public:
    explicit Log(std::string ident, int facility);

protected:
    int sync();
    int overflow(int c);

private:
    friend std::ostream& operator<< (std::ostream& os, const LogPriority& log_priority);
    std::string buffer_;
    int facility_;
    int priority_;
    char ident_[50];
};

log.cc:

#include <cstring>
#include <ostream>
#include "log.h"

Log::Log(std::string ident, int facility) {
    facility_ = facility;
    priority_ = LOG_DEBUG;
    strncpy(ident_, ident.c_str(), sizeof(ident_));
    ident_[sizeof(ident_)-1] = '\0';

    openlog(ident_, LOG_PID, facility_);
}

int Log::sync() {
    if (buffer_.length()) {
        syslog(priority_, "%s", buffer_.c_str());
        buffer_.erase();
        priority_ = LOG_DEBUG; // default to debug for each message
    }
    return 0;
}

int Log::overflow(int c) {
    if (c != EOF) {
        buffer_ += static_cast<char>(c);
    } else {
        sync();
    }
    return c;
}

std::ostream& operator<< (std::ostream& os, const LogPriority& log_priority) {
    static_cast<Log *>(os.rdbuf())->priority_ = (int)log_priority;
    return os;
}

main()我初始化clog:

std::clog.rdbuf(new Log("foo", LOG_LOCAL0));

然后每当我想登录时,都很容易:

std::clog << kLogNotice << "test log message" << std::endl;

std::clog << "the default is debug level" << std::endl;

【讨论】:

    【解决方案2】:

    您可以定义一个调用 syslog 的 streambuf。例如:

    // Pseudo-code
    class syslog_streambuf : public streambuf { 
    private: 
        void internal_log(string& log) { 
            syslog(..., log, ...); 
        }
    public: 
        int sputc ( char c ) { 
            internal_log(...); 
        }
        streamsize sputn ( const char * s, streamsize n ) { 
            internal_log(...); 
        } 
    }
    

    那么您只需编写以下内容来重定向阻塞:

    clog.rdbuf( new syslog_streambuf ); 
    

    还有一些你可能需要重写的函数,这里有一个很好的reference to the streambuf api

    【讨论】:

    • 我认为为这个特定任务创建一个单独的 ostream 对象是一个好习惯。当心cplusplus.com/reference/iostream/ios_base/sync_with_stdio .
    • @Basilevs:感谢提供样本。它仍然很短,这绝对是我一直在寻找的那种东西。无论如何,我将不得不整理套接字特定的东西以适应系统日志的代码......看起来我至少需要几天时间:-(
    【解决方案3】:

    另一个版本的部分灵感来自食者。它本身并不重定向 std::clog,而是使用熟悉的流语法。

    #ifndef SYSLOG_HPP
    #define SYSLOG_HPP
    
    #include <ostream>
    #include <streambuf>
    #include <string>
    
    #include <syslog.h>
    
    namespace log
    {
    
    enum level
    {
        emergency = LOG_EMERG,
        alert     = LOG_ALERT,
        critical  = LOG_CRIT,
        error     = LOG_ERR,
        warning   = LOG_WARNING,
        notice    = LOG_NOTICE,
        info      = LOG_INFO,
        debug     = LOG_DEBUG,
    };
    
    enum type
    {
        auth   = LOG_AUTH,
        cron   = LOG_CRON,
        daemon = LOG_DAEMON,
        local0 = LOG_LOCAL0,
        local1 = LOG_LOCAL1,
        local2 = LOG_LOCAL2,
        local3 = LOG_LOCAL3,
        local4 = LOG_LOCAL4,
        local5 = LOG_LOCAL5,
        local6 = LOG_LOCAL6,
        local7 = LOG_LOCAL7,
        print  = LOG_LPR,
        mail   = LOG_MAIL,
        news   = LOG_NEWS,
        user   = LOG_USER,
        uucp   = LOG_UUCP,
    };
    
    }
    
    class syslog_stream;
    
    class syslog_streambuf: public std::basic_streambuf<char>
    {
    public:
        explicit syslog_streambuf(const std::string& name, log::type type):
            std::basic_streambuf<char>()
        {
            openlog(name.size() ? name.data() : nullptr, LOG_PID, type);
        }
        ~syslog_streambuf() override { closelog(); }
    
    protected:
        int_type overflow(int_type c = traits_type::eof()) override
        {
            if(traits_type::eq_int_type(c, traits_type::eof()))
                sync();
            else buffer += traits_type::to_char_type(c);
    
            return c;
        }
    
        int sync() override
        {
            if(buffer.size())
            {
                syslog(level, "%s", buffer.data());
    
                buffer.clear();
                level = ini_level;
            }
            return 0;
        }
    
        friend class syslog_stream;
        void set_level(log::level new_level) noexcept { level = new_level; }
    
    private:
        static constexpr log::level ini_level = log::info;
        log::level level = ini_level;
    
        std::string buffer;
    };
    
    class syslog_stream: public std::basic_ostream<char>
    {
    public:
        explicit syslog_stream(const std::string& name = std::string(), log::type type = log::user):
            std::basic_ostream<char>(&streambuf),
            streambuf(name, type)
        { }
    
        syslog_stream& operator<<(log::level level) noexcept
        {
            streambuf.set_level(level);
            return (*this);
        }
    
    private:
        syslog_streambuf streambuf;
    };
    
    #endif // SYSLOG_HPP
    

    要使用它,您可以执行以下操作:

    syslog_stream clog;
    
    clog << "Hello, world!" << std::endl;
    clog << log::emergency << "foo" << "bar" << "baz" << 42 << std::endl;
    

    【讨论】:

      【解决方案4】:

      我设计了一个与上面显示的非常相似的 OStreamedLog 类,除了我的 OStreamedLog 对象被设置为使用任意 ostringstream 对象,就像@Basilevs 建议的那样。

      首先是 Log 的类定义,与上面提到的@eater 和@Chris Kaminski 非常相似。然后,我的 OStreamedLog 类定义,其中包含一个 Log 对象:

      class OStreamedLog : public ostringstream
      {
         public:
           OStreamedLog (const char* ident, int facility)
           {
                log = new Log (ident, facility);
                (static_cast<ostream*>(this))->rdbuf (log);
           }
         private:
           Log* log;
      };
      

      现在,当您需要登录时,只需调用:

      OStreamedLog someLog ("MyOwnProgramThatNeedsLogging", LOG_LOCAL1);
      someLog << "Log testing" << endl;
      someLog << LOG_ERR << "some other error log" << endl;
      

      当然,您可以将整个 Log 定义折叠到您的 OStreamedLog 类中,但您可能希望在基本 Log 对象中执行其他操作,并使用上述包装器来区分不同类型的日志。例如,您可以拥有人类可读的诊断日志(以 ASCII 文本形式发送)、二进制日志(用于稍后处理)或 TLS 流日志(例如,发送到北向服务器)。

      【讨论】:

        猜你喜欢
        • 2012-06-16
        • 2017-07-09
        • 2021-01-13
        • 2012-02-23
        • 2017-07-18
        • 2016-04-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多