问题描述
当前代码使用系统日志系统调用,它的工作方式类似于printf。
现在我更喜欢使用流来代替,通常是内置的 std :: clog 。但是阻塞只是将输出重定向到stderr,而不是syslog,这对我来说是无用的,因为我也使用stderr和stdout用于其他目的。
我在另一个答案,很容易使用rdbuf()重定向到一个文件,但我看不到方法来应用该方法来调用syslog作为openlog不返回一个文件处理程序,我可以用来绑定一个流。
还有另一个方法吗? (看起来很unix编程基本)?
编辑:我正在寻找一个不使用外部库的解决方案。
编辑:使用Boost.IOStreams是可以的,因为我的项目已经使用Boost。
与外部库链接是可能的,但也是一个问题,因为它的GPL代码。依赖性也是一个负担,因为它们可能与其他组件冲突,在我的Linux发行版上不可用,引入第三方错误等。如果这是唯一的解决方案,我可以考虑完全避免流...(可惜)。
我需要这么简单的东西,所以我把它放在一起:
log.h:
枚举LogPriority {
kLogEmerg = LOG_EMERG,//系统不可用
kLogAlert = LOG_ALERT,//必须立即采取行动
kLogCrit = LOG_CRIT,//关键条件
kLogErr = LOG_ERR,//错误条件
kLogWarning = LOG_WARNING,//警告条件
kLogNotice = LOG_NOTICE,//正常,但重要,条件
kLogInfo = LOG_INFO,//信息消息
kLogDebug = LOG_DEBUG //调试级消息
};
std :: ostream&运算符<< (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&运算符<< (std :: ostream& os,const LogPriority& log_priority);
std :: string buffer_;
int facility_;
int priority_;
char ident_ [50];
};
log.cc:
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_,buffer_.c_str
buffer_.erase();
priority_ = LOG_DEBUG; //默认为每个消息调试
}
return 0;
}
int Log :: overflow(int c){
if(c!= EOF){
buffer_ + = static_cast< char>(c);
} else {
sync();
}
return c;
}
std :: ostream&运算符<< (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< 测试日志消息< std :: endl;
std :: clog<< 默认是调试级别< std :: endl;
I work on Unix on a C++ program that send messages to syslog.
The current code uses the syslog system call that works like printf.
Now I would prefer to use a stream for that purpose instead, typically the built-in std::clog. But clog merely redirect output to stderr, not to syslog and that is useless for me as I also use stderr and stdout for other purposes.
I've seen in another answer that it's quite easy to redirect it to a file using rdbuf() but I see no way to apply that method to call syslog as openlog does not return a file handler I could use to tie a stream on it.
Is there another method to do that ? (looks pretty basic for unix programming) ?
Edit: I'm looking for a solution that does not use external library. What @Chris is proposing could be a good start but is still a bit vague to become the accepted answer.
Edit: using Boost.IOStreams is OK as my project already use Boost anyway.
Linking with external library is possible but is also a concern as it's GPL code. Dependencies are also a burden as they may conflict with other components, not be available on my Linux distribution, introduce third-party bugs, etc. If this is the only solution I may consider completely avoiding streams... (a pity).
I needed something simple like this too, so I just put this together:
log.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:
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_, 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;
}
In main()
I initialize clog:
std::clog.rdbuf(new Log("foo", LOG_LOCAL0));
Then whenever I want to log, it's easy:
std::clog << kLogNotice << "test log message" << std::endl;
std::clog << "the default is debug level" << std::endl;
这篇关于在Unix上将C ++ std :: clog重定向到syslog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!