Iva*_*ica 14 c++ unix redirect daemon syslog
我打算为Debian 打包OpenTibia Server.我想要做的其中一件事是添加启动过程/etc/init.d和守护otserv进程.
事实是,我们应该将输出重定向到syslog.这通常通过该syslog()功能完成.目前,代码集中在:
std::cout << "Stuff to printout" << std::endl;
Run Code Online (Sandbox Code Playgroud)
是否有一种适当的,易于添加的方法将标准输出和标准错误输出重定向到syslog中,而无需将每一个"调用"替换为std :: cout和朋友?
Aln*_*tak 21
你可以管你stdout来syslog用logger命令:
名称
Run Code Online (Sandbox Code Playgroud)logger - a shell command interface to the syslog(3) system log module概要
Run Code Online (Sandbox Code Playgroud)logger [-isd] [-f file] [-p pri] [-t tag] [-u socket] [message ...]描述
Run Code Online (Sandbox Code Playgroud)Logger makes entries in the system log. It provides a shell command interface to the syslog(3) system log module.
如果您没有在命令行上提供消息,则会读取消息 stdin
您可以通过rdbuf()命令在C++中重定向任何流.这有点令人费解,但实施起来并不那么难.
您需要编写一个将在overflow()上输出到syslog的streambuf,并用streambuf替换std :: cout rdbuf.
一个例子,它将输出到一个文件(没有错误处理,未经测试的代码)
#include <iostream>
#include <fstream>
using namespace std;
int main (int argc, char** argv) {
streambuf * yourStreamBuffer = NULL;
ofstream outputFileStream;
outputFileStream.open ("theOutputFile.txt");
yourStreamBuffer = outputFileStream.rdbuf();
cout.rdbuf(yourStreamBuffer);
cout << "Ends up in the file, not std::cout!";
outputFileStream.close();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
不确定直接回答“C”是否足够;但在“C”中,您可以使用底层 stdio 功能将 (FILE*) 直接插入系统日志调用中,而无需介入“记录器”进程。查看 http://mischasan.wordpress.com/2011/05/25/redirecting-stderr-to-syslog/