提供了一个方便的信号处理程序,当程序在某些信号(如SIGSEGV)上崩溃时,它将转储有用的信息。
信号处理程序可以通过 google::InstallFailureSignalHandler() 安装。以下是信号处理程序输出的示例:
            
            
              cpp
              
              
            
          
          #include <iostream>
#include <glog/logging.h>
int main(int argc, char* argv[])
{
    (void)argc;//avoid unused warning
    google::InitGoogleLogging(argv[0]);
    // install failure signal handler
    google::InstallFailureSignalHandler();
    //模拟触发段错误
    *(int*)0 = 0;
    //后续不会执行,因为段错误导致程序崩溃
    LOG(FATAL) << "FATAL log message";
    return 0;
}
        输出:
            
            
              shell
              
              
            
          
          # ./demo
*** Aborted at 1758626944 (unix time) try "date -d @1758626944" if you are using GNU date ***
PC: @                0x0 (unknown)
*** SIGSEGV (@0x0) received by PID 57 (TID 0x743bde537e40) from PID 0; stack trace: ***
    @     0x743bdd036400 (unknown)
    @           0x400a8e main
    @     0x743bdd022555 __libc_start_main
    @           0x400999 (unknown)
    @                0x0 (unknown)
Segmentation fault (core dumped)
        默认情况下,信号处理程序将故障转储写入标准错误。你可以通过 InstallFailureWriter() 自定义目标。
添加相关逻辑,有点怪,原来打印的SIGSEGV错误字样,哪里都找不见了,这不是画蛇添足么...
貌似这个还得建议搭配libunwind使用,等待后续有需要再探索吧。
            
            
              cpp
              
              
            
          
          #include <iostream>
#include <string>
#include <glog/logging.h>
void FailureWriter(const char* data, size_t size)
{
    LOG(FATAL) << std::string(data,size);
}
int main(int argc, char* argv[])
{
    (void)argc;//avoid unused warning
    google::InitGoogleLogging(argv[0]);
    // install failure signal handler
    google::InstallFailureSignalHandler();
    //默认输出到标准错误,可以自定义一个函数来将错误输出到其他地方
    google::InstallFailureWriter(&FailureWriter);
    //模拟触发段错误
    *(int*)0 = 0;
    //后续不会执行,因为段错误导致程序崩溃
    LOG(FATAL) << "FATAL log message";
    return 0;
}
        错误输出:
            
            
              shell
              
              
            
          
          # ./demo
F20250923 19:39:11.574347    86 07.cpp:8] *** Aborted at 1758627551 (unix time) try "date -d @1758627551" if you are using GNU date ***
*** Check failure stack trace: ***
    @     0x7af9321d816a  google::LogMessage::Fail()
    @     0x7af9321d80ca  google::LogMessage::SendToLog()
    @     0x7af9321d7917  google::LogMessage::Flush()
    @     0x7af9321db104  google::LogMessageFatal::~LogMessageFatal()
    @           0x400e50  FailureWriter()
    @     0x7af9321e4575  google::(anonymous namespace)::DumpTimeInfo()
    @     0x7af9321e4a26  google::(anonymous namespace)::FailureSignalHandler()
    @     0x7af931236400  (unknown)
    @           0x400ebc  main
    @     0x7af931222555  __libc_start_main
    @           0x400cf9  (unknown)
    @              (nil)  (unknown)
Aborted (core dumped)
        日志文件:
            
            
              shell
              
              
            
          
          Log file created at: 2025/09/23 19:39:11
Running on machine: DESKTOP-RR46KEP
Running duration (h:mm:ss): 0:00:00
Log line format: [IWEF]yyyymmdd hh:mm:ss.uuuuuu threadid file:line] msg
F20250923 19:39:11.574347    86 07.cpp:8] *** Aborted at 1758627551 (unix time) try "date -d @1758627551" if you are using GNU date ***