Skip to content

Commit

Permalink
XX
Browse files Browse the repository at this point in the history
Signed-off-by: Matthew Ballance <[email protected]>
  • Loading branch information
mballance committed Dec 7, 2024
1 parent d687dfb commit 9979425
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/DebugMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ void DebugMgr::registerSignalHandlers() {
#ifndef _WIN32
signal(SIGSEGV, &DebugMgr::signal_handler);
signal(SIGBUS, &DebugMgr::signal_handler);
signal(SIGILL, &DebugMgr::signal_handler);
#endif
}
}
Expand Down Expand Up @@ -103,6 +104,10 @@ void DebugMgr::fatal(IDebug *dbg, const char *fmt, va_list ap) {
throw std::runtime_error("");
}

void DebugMgr::crashClose() {

}

void DebugMgr::flush() {
m_out->flush();
}
Expand All @@ -113,7 +118,7 @@ void DebugMgr::signal_handler(int sigid) {

void DebugMgr::crash_handler() {
m_dbg->error("Application Crashed");
flush();
m_out->crashClose();
_exit(1);
}

Expand Down
2 changes: 2 additions & 0 deletions src/DebugMgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ class DebugMgr : public IDebugMgr {

virtual void fatal(IDebug *dbg, const char *fmt, va_list ap) override;

virtual void crashClose() override;

virtual void flush() override;

private:
Expand Down
33 changes: 33 additions & 0 deletions src/DebugOutFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,24 @@
* Created on:
* Author:
*/
#include <alloca.h>
#include <stdio.h>
#include <string.h>
#include "dmgr/IDebug.h"
#include "DebugOutFile.h"
#ifndef _WIN32
#include <unistd.h>
#include <execinfo.h>
#endif


namespace dmgr {

DebugOutFile::DebugOutFile(const std::string &name) :
m_path(name), m_fp(0), m_close_fp(true) {

m_fp = fopen(name.c_str(), "w");
}

DebugOutFile::DebugOutFile(FILE *fp, bool close_fp) :
m_fp(fp), m_close_fp(close_fp), m_flush(false) {
Expand Down Expand Up @@ -78,6 +90,27 @@ void DebugOutFile::fatal(IDebug *dbg, const char *fmt, va_list ap) {
fflush(m_fp);
}

void DebugOutFile::crashClose() {
fflush(m_fp);
#ifndef _WIN32
if (m_path.size()) {
static void *bt_buffer[64];
char *rename_path = (char *)alloca(m_path.size()+64);
int32_t bt_size;

bt_size = backtrace(bt_buffer, sizeof(bt_buffer)/sizeof(void *));

backtrace_symbols_fd(bt_buffer, bt_size, fileno(m_fp));

// Rename the file
fclose(m_fp);

sprintf(rename_path, "%s.%d", m_path.c_str(), getpid());
::rename(m_path.c_str(), rename_path);
}
#endif
}

void DebugOutFile::flush() {
fflush(m_fp);
}
Expand Down
6 changes: 6 additions & 0 deletions src/DebugOutFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ namespace dmgr {

class DebugOutFile : public virtual IDebugOut {
public:
DebugOutFile(const std::string &name);

DebugOutFile(FILE *fp, bool close_fp);

virtual ~DebugOutFile();
Expand All @@ -37,9 +39,13 @@ class DebugOutFile : public virtual IDebugOut {
virtual void debug(IDebug *dbg, const char *fmt, va_list ap) override;
virtual void error(IDebug *dbg, const char *fmt, va_list ap) override;
virtual void fatal(IDebug *dbg, const char *fmt, va_list ap) override;

virtual void crashClose() override;

virtual void flush() override;

private:
std::string m_path;
FILE *m_fp;
bool m_close_fp;
bool m_flush;
Expand Down
8 changes: 8 additions & 0 deletions src/DebugOutList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ void DebugOutList::fatal(IDebug *dbg, const char *fmt, va_list ap) {
}
}

void DebugOutList::crashClose() {
for (std::vector<IDebugOutUP>::const_iterator
it=m_outputs.begin();
it!=m_outputs.end(); it++) {
(*it)->crashClose();
}
}

void DebugOutList::flush() {
for (std::vector<IDebugOutUP>::const_iterator
it=m_outputs.begin();
Expand Down
3 changes: 3 additions & 0 deletions src/DebugOutList.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ class DebugOutList : public virtual IDebugOutList {
virtual void debug(IDebug *dbg, const char *fmt, va_list ap) override;
virtual void error(IDebug *dbg, const char *fmt, va_list ap) override;
virtual void fatal(IDebug *dbg, const char *fmt, va_list ap) override;

virtual void crashClose() override;

virtual void flush() override;

private:
Expand Down
4 changes: 4 additions & 0 deletions src/Factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ IDebugOut *Factory::mkDebugOutFile(FILE *fp, bool close_fp) {
return new DebugOutFile(fp, close_fp);
}

IDebugOut *Factory::mkDebugOutPath(const std::string &name) {
return new DebugOutFile(name);
}

IDebugOutList *Factory::mkDebugOutList() {
return new DebugOutList();
}
Expand Down
2 changes: 2 additions & 0 deletions src/Factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class Factory : public virtual IFactory {

virtual IDebugOut *mkDebugOutFile(FILE *fp, bool close_fp) override;

virtual IDebugOut *mkDebugOutPath(const std::string &name) override;

virtual IDebugOutList *mkDebugOutList() override;

static IFactory *inst();
Expand Down
2 changes: 2 additions & 0 deletions src/include/dmgr/IDebugOut.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class IDebugOut {
virtual void error(IDebug *dbg, const char *fmt, va_list ap) = 0;
virtual void fatal(IDebug *dbg, const char *fmt, va_list ap) = 0;

virtual void crashClose() = 0;

virtual void flush() = 0;

};
Expand Down
2 changes: 2 additions & 0 deletions src/include/dmgr/IFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class IFactory {

virtual IDebugOut *mkDebugOutFile(FILE *fp, bool close_fp) = 0;

virtual IDebugOut *mkDebugOutPath(const std::string &name) = 0;

virtual IDebugOutList *mkDebugOutList() = 0;

};
Expand Down

0 comments on commit 9979425

Please sign in to comment.