X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=trace_file.cpp;h=74cc3829e2b4ce5a18a3d69beb614e90b87ed6de;hb=ce04047159832b49f55618d76478ef264cbee462;hp=27b3eb18c07d10eb7e0a2fc3e5dc1bdd9e366b26;hpb=6dce37c812e3fd86171934b72ad8286a3302a571;p=apitrace diff --git a/trace_file.cpp b/trace_file.cpp index 27b3eb1..74cc382 100644 --- a/trace_file.cpp +++ b/trace_file.cpp @@ -6,13 +6,59 @@ #include #include +#include "os.hpp" + #include +#include using namespace Trace; #define SNAPPY_BYTE1 'a' #define SNAPPY_BYTE2 't' +static void cleanupHandler(int sig); + +class FileCleanup +{ +public: + FileCleanup() + { + OS::CatchInterrupts(cleanupHandler); + } + + ~FileCleanup() + { + flush(); + m_files.clear(); + } + + void addFile(Trace::File *file) + { + m_files.insert(file); + } + void removeFile(Trace::File *file) + { + m_files.erase(file); + } + + void flush() + { + std::set::const_iterator itr; + for (itr = m_files.begin(); itr != m_files.end(); ++itr) { + (*itr)->flush(File::FlushDeep); + } + } + +private: + std::set m_files; +}; +static FileCleanup s_cleaner; + +static void cleanupHandler(int sig) +{ + s_cleaner.flush(); +} + File::File(const std::string &filename, File::Mode mode) : m_filename(filename), @@ -52,6 +98,10 @@ bool File::open(const std::string &filename, File::Mode mode) } m_isOpened = rawOpen(filename, mode); m_mode = mode; + + if (m_isOpened) { + s_cleaner.addFile(this); + } return m_isOpened; } @@ -76,12 +126,13 @@ void File::close() if (m_isOpened) { rawClose(); m_isOpened = false; + s_cleaner.removeFile(this); } } -void File::flush() +void File::flush(FlushType type) { - rawFlush(); + rawFlush(type); } int File::getc() @@ -165,7 +216,7 @@ void ZLibFile::rawClose() } } -void ZLibFile::rawFlush() +void ZLibFile::rawFlush(FlushType type) { gzflush(m_gzFile, Z_SYNC_FLUSH); } @@ -267,6 +318,8 @@ bool SnappyFile::rawRead(void *buffer, int length) sizeToRead -= chunkSize; if (sizeToRead > 0) flushCache(); + if (!m_cacheSize) + break; } } @@ -290,8 +343,11 @@ void SnappyFile::rawClose() m_cachePtr = NULL; } -void SnappyFile::rawFlush() +void SnappyFile::rawFlush(FlushType type) { + if (type == FlushDeep) { + flushCache(); + } m_stream.flush(); }