cdsfiui 发表于 2015-11-09 10:13

linux下面的fd, 能否区分log stream和标准错误stream?

我在linux下做c++的开发,尝试着看一下cerr和clog这两个东西的区别。
我写了一个简单的测试程序,发现clog是和cerr一样,绑定在2号描述符上面的:当我把2>/dev/null重定向了以后,发现cerr和clog同时都没有了。

问题来了,如果看clog的说明,似乎不是绑定在cerr/cout上面的,而是依赖于系统的logging stream实现。

所以我的疑问是: linux系统下面是否有相关的设定来制定log输出到哪里?
----------------------------------------------------------------
The global objects std::clog and std::wclog control output to a stream buffer of implementation-defined type (derived from std::streambuf), associated with the standard C output stream stderr, but, unlike std::cerr/std::wcerr, these streams are not automatically flushed and not automatically tie()'d with cout.
These objects are guaranteed to be initialized during or before the first time an object of type std::ios_base::Init is constructed and are available for use in the constructors and destructors of static objects (as long as <iostream> is included).
Unless sync_with_stdio(false) has been issued, it is safe to concurrently access these objects from multiple threads for both formatted and unformatted output.

yjh777 发表于 2015-11-09 13:37

0 1 2 三个是进程生成,自动打开占用的;

你可以再进程里面重定向;把你的 log 绑定到 3 自己写log函数

看看 APUE 相关章节获得更多信息

cdsfiui 发表于 2015-11-10 13:57

yjh777 发表于 2015-11-09 13:37 static/image/common/back.gif
0 1 2 三个是进程生成,自动打开占用的;

你可以再进程里面重定向;把你的 log 绑定到 3 自己写log函数
...

哪个章节提到了"把你的 log 绑定到 3 自己写log函数"如何做呢?
还请指点,谢谢。

yjh777 发表于 2015-11-10 16:15

哈哈,没有章节提把 log 绑定,,,

我的意思是你看看 APUE 先理解: 文件描述符 、stdin stdout stderr 等等概念 之后
    你就不会再问这些问题了,因为你已经知道问题的原因和怎么解决问题了。

cdsfiui 发表于 2015-11-12 14:22

yjh777 发表于 2015-11-10 16:15 static/image/common/back.gif
哈哈,没有章节提把 log 绑定,,,

我的意思是你看看 APUE 先理解: 文件描述符 、stdin stdout stderr...

谢谢,我知道文件描述符的含义,然后我想知道这个log本身是不是已经对应了某个系统默认的描述符数字了。
如果没有的话,clog就是绑定到cerr上面啊,那我有cerr就可以了,干嘛还弄个clog呢?
谢谢。

yjh777 发表于 2015-11-12 18:34

回复 5# cdsfiui

系统默认只打开三个 0 stdin 1 stdout 2 stderr
没听说有默认绑定log的,不排除有什么库会这么做,,,
页: [1]
查看完整版本: linux下面的fd, 能否区分log stream和标准错误stream?