#include
#include
#include
#include
#include
const int MAX_STACK_FRAMES = 128;void sig_crash(intsig)
{
FILE*fd;structstat buf;
stat("./crash.log", &buf);if(buf.st_size > 10*1000*1000){ //超过10兆则清空内容
fd = fopen("./crash.log", "w");
}else{
fd= fopen("./crash.log", "at");
}if (NULL ==fd)
{
exit(0);
}try{char szLine[512] = {0, };
time_t t=time(NULL);
tm* now = localtime(&t);int nLen1 =sprintf(szLine,"#########################################################\n[d-d-d d:d:d][crash signal number:%d]\n",
now->tm_year + 1900,
now->tm_mon + 1,
now->tm_mday,
now->tm_hour,
now->tm_min,
now->tm_sec,
sig);
fwrite(szLine,1, strlen(szLine), fd);
#ifdef __linuxvoid*array[MAX_STACK_FRAMES];
size_t size= 0;char** strings =NULL;
size_t i, j;
signal(sig, SIG_DFL);
size=backtrace(array, MAX_STACK_FRAMES);
strings= (char**)backtrace_symbols(array, size);//fprintf(stderr, "oncrash;\n");
for (i = 0; i < size; ++i)
{char szLine[512] = {0, };
sprintf(szLine,"%d %s\n", i, strings[i]);
fwrite(szLine,1, strlen(szLine), fd);
std::stringsymbol(strings[i]);
size_t pos1= symbol.find_first_of("[");
size_t pos2= symbol.find_last_of("]");
std::string address = symbol.substr(pos1 + 1, pos2 - pos1 -1);char cmd[128] = {0, };
sprintf(cmd,"addr2line -e test %s", address.c_str()); // test为应用程序名称,需要改为用户自己的应用程序名
FILE*fPipe = popen(cmd, "r");if(fPipe !=NULL){char buff[1024];
memset(buff,0, sizeof(buff));char* ret = fgets(buff, sizeof(buff), fPipe);
pclose(fPipe);
fwrite(ret,1, strlen(ret), fd);
}
}free(strings);#endif //__linux}catch(...)
{//}
fflush(fd);
fclose(fd);
fd=NULL;
exit(0);
}
int main()
{// 捕捉崩溃日志
signal(SIGSEGV, sig_crash);