试图打开流时,记录器接收分段错误
Logger receiving segmentation fault when trying to open ofstream
当我试图打开Logger类的文件时,我遇到了一些SIGSEGV的麻烦。我用这个类已经有一段时间了,但这是我第一次学到这个。从之前被调用过的简单的一行开始:
LOG(logDEBUG) << "Node " << nodeId << " in link layer state waitAck.";
I got this from strace:
open("simulation.log", O_WRONLY|O_CREAT|O_APPEND, 0666) = 3
lseek(3, 0, SEEK_END) = 16380108
--- SIGSEGV {si_signo=SIGSEGV, si_code=SI_KERNEL, si_addr=NULL} ---
+++ killed by SIGSEGV (core dumped) +++
get this gdb:
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7210289 in malloc_consolidate () from /usr/lib/libc.so.6
(gdb) backtrace
#0 0x00007ffff7210289 in malloc_consolidate () from /usr/lib/libc.so.6
#1 0x00007ffff7211d2a in _int_malloc () from /usr/lib/libc.so.6
#2 0x00007ffff7213d44 in malloc () from /usr/lib/libc.so.6
#3 0x00007ffff7ae1a48 in operator new (sz=8192)
at /build/gcc/src/gcc/libstdc++-v3/libsupc++/new_op.cc:50
#4 0x00007ffff7ae1af5 in operator new[] (sz=<optimized out>)
at /build/gcc/src/gcc/libstdc++-v3/libsupc++/new_opv.cc:32
#5 0x00007ffff7b41698 in std::basic_filebuf<char, std::char_traits<char> >::_M_allocate_internal_buffer
(this=0x7fffffffdc68)
at /build/gcc/src/gcc-build/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/fstream.tcc:55
#6 0x00007ffff7b45932 in std::basic_filebuf<char, std::char_traits<char> >::open (this=0x7fffffffdc68,
__s=0x690550 <Log::fileName[abi:cxx11]+16> "simulation.log", __mode=17)
at /build/gcc/src/gcc-build/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/fstream.tcc:187
#7 0x00007ffff7b45a53 in std::basic_filebuf<char, std::char_traits<char> >::open (
__mode=<optimized out>, __s=..., this=0x7fffffffdc68)
at /build/gcc/src/gcc-build/x86_64-pc-linux-gnu/libstdc++-v3/include/fstream:308
#8 std::basic_ofstream<char, std::char_traits<char> >::open (this=0x7fffffffdc60, __s=...,
__mode=<optimized out>)
at /build/gcc/src/gcc-build/x86_64-pc-linux-gnu/libstdc++-v3/include/fstream:823
#9 0x0000000000421c33 in Log::Log (this=0x7fffffffdc60) at src/log.cpp:21
#10 0x0000000000446198 in LinkCsma::iterate2Receive (this=0x6a9440, t=@0x7fffffffe1fc: 9387)
at src/link_csma.cpp:329
#11 0x00000000004232a4 in Node::iterate2Receive (this=0x6a52f0, t=@0x7fffffffe1fc: 9387)
at src/node.cpp:550
#12 0x000000000042f2bc in DirectionalNodes::iterate (this=0x6a6eb0, t=@0x7fffffffe1fc: 9387)
at src/directional_nodes.cpp:250
#13 0x0000000000460ce5 in Simulation::run (this=0x6a6f00) at src/simulation.cpp:184
#14 0x0000000000429e8c in main (argc=1, argv=0x7fffffffe878) at src/main.cpp:65
来自Log类的代码是:
#ifndef LOG_H__
#define LOG_H__
#include <fstream>
#include <sstream>
#include <string>
#include <cstdio>
#include <ctime>
#ifdef DEBUG
#define LOG(level)
Log().write(level)
#else
#define LOG(level)
if(level >= logDEBUG) ;
else Log().write(level)
#endif
enum LogLevel {
logERROR,
logWARNING,
logINFO,
logDEBUG
};
class Log
{
public:
Log();
~Log();
static void setFile(const std::string& s);
std::ofstream& write(LogLevel level = logINFO);
protected:
std::ofstream fileStream;
private:
Log(const Log&);
Log& operator =(const Log&);
std::string level2String(LogLevel level);
std::string timeString();
static std::string fileName;
static const std::string DEFAULT_LOG_FILE;
};
#endif //LOG_H__
:
#include "../include/log.h"
using namespace std;
string Log::fileName;
const string Log::DEFAULT_LOG_FILE = "logfile.log";
Log::Log()
{
fileStream.open(fileName, ios::out | ios::app);
}
Log::~Log()
{
fileStream << endl;
flush(fileStream);
fileStream.close();
}
std::string Log::level2String(LogLevel level)
{
static const char* const buffer[] = { "ERROR", "WARNING", "INFO", "DEBUG" };
return buffer[level];
}
void Log::setFile(const std::string& s)
{
fileName = s;
}
std::string Log::timeString()
{
time_t t = time(0);
struct tm * now = localtime(&t);
stringstream ss;
ss << now->tm_hour << ":" << now->tm_min << " "
<< now->tm_mday << "/" << (now->tm_mon+1) << "/" << (now->tm_year+1900);
return ss.str();
}
std::ofstream& Log::write(LogLevel level)
{
fileStream << timeString();
fileStream << " " << level2String(level) << ":t";
return fileStream;
}
似乎我通过将输出从ofstream更改为FILE*来解决这个问题,并在使用fprintf写入FILE*之前使用ostringstream作为缓冲区。如http://www.drdobbs.com/cpp/logging-in-c/201804215所示。
相关文章:
- 创建 Spdlog 异步文件记录器时遇到困难
- 如何将自定义记录器与websocketpp一起使用?
- 在C++中隐藏键盘记录器的控制台窗口
- 为什么Qt Creator的应用程序输出不能从spdlog记录器打印
- 麻烦得到提升::记录器编译
- 我正在尝试用 c++ 制作一个日志记录框架,但信息没有传递给记录器的子组件,我做错了什么?
- 多态记录器(虚拟模板化函数?
- 设置提升记录器的线程名称
- Boost Log的琐碎记录器"lazy evaluation"是如何工作的?
- boost::log 设置"Channel"通道记录器中的属性
- 区分键盘记录器中的密码和其他类型的键盘输入
- 记录器的流界面,C
- 如何监控Android和iOS上的所有击键操作,如Windows键盘记录器
- 如何声明和定义 Poco 记录器
- 记录器何时应刷新
- 带有 gtest 框架的 spdlog 记录器不起作用
- 使用常量字符*时如何使用带有模板的记录器
- 如何防止升压记录器在每条记录后添加?
- 异步记录器线程安全
- 试图打开流时,记录器接收分段错误