Tag: 三通

我怎样才能撰写输出stream,所以输出一次会有多个地方?

我想编写两个(或更多)stream成一个。 我的目标是任何针对cout , cerr和clog输出都会与原始stream一起输出到文件中。 (例如,当事情被logging到控制台,例如closures后,我想仍然能够返回并查看输出。) 我正在考虑做这样的事情: class stream_compose : public streambuf, private boost::noncopyable { public: // take two streams, save them in stream_holder, // this set their buffers to `this`. stream_compose; // implement the streambuf interface, routing to both // … private: // saves the streambuf of an ios class, // upon destruction restores it, […]

如何将sys.stdout复制到Python中的日志文件?

编辑:因为它似乎没有解决方案,或者我正在做一些非标准的,没有人知道的东西 – 我会修改我的问题,也问:当一个python应用程序正在做一个完成日志记录的最好方法是什么很多系统调用? 我的应用程序有两种模式。 在交互模式下,我希望所有的输出都转到屏幕以及日志文件,包括任何系统调用的输出。 在守护进程模式下,所有的输出都进入日志。 守护进程模式使用os.dup2()很有效。 我无法找到一种方式在交互模式下将所有输出“开”到日志中,而无需修改每个系统调用。 换句话说,我想为python应用程序生成的任何输出命令行“tee”的功能, 包括系统调用输出 。 澄清: 为了重定向所有的输出,我做了这样的事情,而且效果很好: # open our log file so = se = open("%s.log" % self.name, 'w', 0) # re-open stdout without buffering sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0) # redirect stdout and stderr to the log file opened above os.dup2(so.fileno(), sys.stdout.fileno()) os.dup2(se.fileno(), sys.stderr.fileno()) 关于这一点的好处是它不需要其他代码的特殊打印调用。 该代码还运行一些shell命令,所以不必单独处理每个输出。 简单地说,我想要做同样的事情,除了重复,而不是重定向。 […]