void Abort(void);
+void CatchInterrupts(void (*func)(int));
+
} /* namespace OS */
#endif /* _OS_HPP_ */
#include <pthread.h>
#include <sys/stat.h>
#include <fcntl.h>
+#include <signal.h>
#ifdef __APPLE__
#include <mach-o/dyld.h>
}
+void
+CatchInterrupts(void (*func)(int))
+{
+ struct sigaction new_action, old_action;
+
+ new_action.sa_handler = func;
+ sigemptyset(&new_action.sa_mask);
+ new_action.sa_flags = 0;
+
+#define SET_IF_NOT_IGNORED(sig) \
+ do { \
+ sigaction(sig, NULL, &old_action); \
+ if (old_action.sa_handler != SIG_IGN) \
+ sigaction(sig, &new_action, NULL); \
+ } while (0)
+
+ SET_IF_NOT_IGNORED(SIGINT);
+ SET_IF_NOT_IGNORED(SIGHUP);
+ SET_IF_NOT_IGNORED(SIGTERM);
+
+#undef SET_IF_NOT_IGNORED
+}
+
} /* namespace OS */
**************************************************************************/
#include <windows.h>
+#include <signal.h>
#include <string.h>
#include <stdio.h>
#endif
}
+void
+CatchInterrupts(void (*func)(int))
+{
+ signal(SIGINT, func);
+ signal(SIGHUP, func);
+ signal(SIGTERM, func);
+}
+
+
} /* namespace OS */
#include <zlib.h>
#include <snappy.h>
+#include "os.hpp"
+
#include <iostream>
+#include <set>
using namespace Trace;
#define SNAPPY_BYTE1 'a'
#define SNAPPY_BYTE2 't'
+static void cleanupHandler(int sig);
+
+class FileCleanup
+{
+public:
+ FileCleanup()
+ : m_quitOnFlush(false)
+ {
+ OS::CatchInterrupts(cleanupHandler);
+ }
+
+ ~FileCleanup()
+ {
+ }
+
+ void addFile(Trace::File *file)
+ {
+ m_files.insert(file);
+ }
+ void removeFile(Trace::File *file)
+ {
+ m_files.erase(file);
+ if (m_files.empty() && m_quitOnFlush) {
+ OS::Abort();
+ }
+ }
+
+ void setQuitOnFlush(bool quit) {
+ m_quitOnFlush = quit;
+ }
+ bool quitOnFlush() const
+ {
+ return m_quitOnFlush;
+ }
+
+private:
+ std::set<Trace::File*> m_files;
+ bool m_quitOnFlush;
+};
+static FileCleanup s_cleaner;
+
+static void cleanupHandler(int sig)
+{
+ s_cleaner.setQuitOnFlush(true);
+}
+
File::File(const std::string &filename,
File::Mode mode)
: m_filename(filename),
}
m_isOpened = rawOpen(filename, mode);
m_mode = mode;
+
+ if (m_isOpened) {
+ s_cleaner.addFile(this);
+ }
return m_isOpened;
}
if (m_isOpened) {
rawClose();
m_isOpened = false;
+ s_cleaner.removeFile(this);
}
}
void File::flush()
{
rawFlush();
+ if (s_cleaner.quitOnFlush()) {
+ close();
+ }
}
int File::getc()
void Writer::endEnter(void) {
_writeByte(Trace::CALL_END);
- m_file->flush();
OS::ReleaseMutex();
}