#include <signal.h> #include <execinfo.h> void OnProcessExceptionHandler(int sigl) { do { std::string str = ""; void* arrayList[128]; int count = backtrace(arrayList, 128); char** pStr = backtrace_symbols(arrayList, count); if (pStr == NULL) break; for (int i=0; i<count; i++) { str += pStr[i]; str += " "; } if (str.size() <= 0) break; char buffer[1024] = ""; time_t t = time(NULL); tm* pTm = localtime(&t); sprintf(buffer, "[TRACE]%02d:%02d:%02d : ", pTm->tm_hour,pTm->tm_min,pTm->tm_sec); std::string logFile = "";//writeable dir logFile += "exception.txt"; FILE* pFile = fopen(logFile.c_str(), "ab"); if (pFile == NULL) break; fwrite(buffer, strlen(buffer), 1, pFile); fwrite(str.c_str(), str.size(), 1, pFile); fclose(pFile); } while (false); exit(1); } int _tmain(int argc, _TCHAR* argv[]) { signal(SIGQUIT, CrashReportSystem::OnProcessExceptionHandler); signal(SIGILL, CrashReportSystem::OnProcessExceptionHandler); signal(SIGTRAP, CrashReportSystem::OnProcessExceptionHandler); signal(SIGABRT, CrashReportSystem::OnProcessExceptionHandler); signal(SIGEMT, CrashReportSystem::OnProcessExceptionHandler); signal(SIGFPE, CrashReportSystem::OnProcessExceptionHandler); signal(SIGBUS, CrashReportSystem::OnProcessExceptionHandler); signal(SIGSEGV, CrashReportSystem::OnProcessExceptionHandler); signal(SIGSYS, CrashReportSystem::OnProcessExceptionHandler); signal(SIGPIPE, CrashReportSystem::OnProcessExceptionHandler); signal(SIGALRM, CrashReportSystem::OnProcessExceptionHandler); signal(SIGXCPU, CrashReportSystem::OnProcessExceptionHandler); signal(SIGXFSZ, CrashReportSystem::OnProcessExceptionHandler); return 1; }