- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
在试图弄清楚如何回答 https://stackoverflow.com/questions/33601384/what-is-the-file-descriptor-of-linuxs-environments-standard-logging-stream 时, 我注意到一个链接 an answer to a related SO post .我用 g++ 4.8.4 尝试了上面链接答案中的代码,但在程序终止之前出现了段错误。
程序如下:
#include <iostream>
#include <fstream>
int main()
{
std::ofstream of("cout.txt");
std::cout.rdbuf(of.rdbuf());
std::cout << "test. test. test." << std::endl;
return 0;
}
构建程序的命令:
g++ -Wall -std=c++11 -g socc.cc -o socc
gdb
的输出:
GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from socc...done.
(gdb) run
Starting program: /.........../socc (removed some text here)
Traceback (most recent call last):
File "/usr/share/gdb/auto-load/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19-gdb.py", line 63, in <module>
from libstdcxx.v6.printers import register_libstdcxx_printers
ImportError: No module named 'libstdcxx'
Program received signal SIGSEGV, Segmentation fault.
0x000000000040073c in ?? ()
(gdb) bt
#0 0x000000000040073c in ?? ()
#1 0x0000000000000000 in ?? ()
(gdb)
我更新了程序以保留 cout
的旧 rdbuf
并在程序结束前重置它。
#include <iostream>
#include <fstream>
int main()
{
std::ofstream of("cout.txt");
auto cout_buff = std::cout.rdbuf();
std::cout.rdbuf(of.rdbuf());
std::cout << "test. test. test." << std::endl;
std::cout.rdbuf(cout_buff);
return 0;
}
有了这个改动,程序运行没有任何问题。
我对 cerr
和 clog
进行了类似的实验,结果相同。
这引出了我的问题:
如果cout
、cerr
和clog
改变了,是否总是需要重新设置rdbuf
被重定向到一个文件?
如果不是,这是 g++ 缺陷吗?
最佳答案
引自 The C++ Standard Library - A tutorial and reference Nicolai Josuttis 的第 2 版,
Ch。 15.12.13,重定向标准流 pp. 822
...
std::cout.rdbuf (file.rdbuf());
Caution! The object
file
is local and is destroyed at the end of the block. This also destroys the corresponding stream buffer. This differs from the “normal” streams because file streams allocate their stream buffer objects at construction time and destroy them on destruction. Thus, in this example,cout
can no longer be used for writing. In fact, it cannot even be destroyed safely at program termination. Thus, the old buffer should always be saved and restored later!
所以答案似乎是肯定的,尽管我没有标准的引述,但上面的作者是一位专家。
关于c++ - cout、cerr、clog的rdbuf改成重定向到文件后,是否需要重新设置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33601886/
有没有人有一个例子来说明如何重新定义 C++ 内置的 clog,而不是有一个新的关联 rdbuf(),它被处理为原始 clog.rdbuf() 和 ofstream 对象的 rdbuf() 的三通到磁
最近我开始尝试大量抓取一个网站以用于存档目的,我认为让多个 Web 请求异步工作以加快处理速度是个好主意(10,000,000 个页面肯定需要大量存档)等等我冒险进入并行性的严酷女主人,三分钟后我开始
我正在做什么:我正在浏览数据库中的公司表...每个公司都有一个文本description字段,并且在该字段内可以有许多超链接(很少超过 4 个)。我想做的是使用 curl 测试这些链接的“不良”响应(
我已经安装了主-主复制。 一切正常,直到您断开两个服务器之间的链接。 一旦被破坏,如果超过某种限制,数据将不会从一个数据转移到另一个数据。 Max_packet 大小设置为 500mb。 但是,如果您
我要编译CLOGS library.I follow this手动的。我在 windows 10 x64 中使用 Visual Studio 2010,python 3.4。我对waf构建系统一无所知
我需要一个类来包装对 std::clog 的调用这样: 每条消息都带有标题前缀,其中包括时间和生成消息的实体的名称。 消息根据错误类型(例如调试、警告、错误)进行着色。 使用方式完全等同于std::c
两个排序的整数数组 A[1..N] 和 B[1..N] 按升序提供。 问题:设计一个O(log N)-time 算法来找出所有 2N 个整数的中位数。 N 可能不是 2 的幂。 为了简单起见,我们可以
在开发代码时,我有很多控制台日志记录 (std::clog) 和一些控制台输出 (std::cout)。但是现在,我想在线提交我的源代码,我想禁用所有控制台日志记录 (clog) 但保留控制台输出 (
这个问题在这里已经有了答案: What is the point of clog? (6 个答案) 关闭 7 年前。 我的代码中有一个基本的调试消息,它打印一条关于调用什么函数的消息。 #ifdef
以下程序在使用 g++ 退出后出现段错误: #include #include
这里的一个相关问题展示了如何只用木屐来做到这一点: How to redefine clog to tee to original clog and a log file? 现在的问题是如何同时为 c
我想创建一个类似流的类,通过它我可以写入 std::out 和 std::clog。 我有以下代码,但问题是它只写入 std::clog,而控制台上的输出并不像预期的那样(奇怪的是,它覆盖了自己)。
我知道目前的C++中没有线程的概念,但是this article is saying : A typesafe, threadsafe, portable logging mechanism ....
iostream 对象 cin、cout、cerr 和 clog 是在 iostream header 中声明的对象。 我知道在某些编译器中可能会在构造这些 iostream 对象之前尝试使用它们,因
cout、cin、cerr 和 clog 名称中的“c”是什么意思? 我会说 char 但我没有找到任何可以确认的东西。 最佳答案 The "c" stands for "character" bec
我在 Unix 上开发一个向 syslog 发送消息的 C++ 程序。 当前代码使用类似于 printf 的 syslog 系统调用。 现在我更愿意为此目的使用流,通常是内置的 std::clog。但
我在 ng-model 文档中摆弄 Angular 自己的站点使用的 plunker: http://plnkr.co/edit/?p=preview 如果将 $scope.val 设置为其中包含“”
log() = log base 2 of ()log()^2 = log^2 base 2 of ()我被这个归纳证明困住了。我有以下递归关系T(n) = T(n/2) + Theta(log(n)
我是一名优秀的程序员,十分优秀!