X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=trace_writer.cpp;h=5a5f1f7d2400b13784fd9dedcc7421593c1e304f;hb=1a9f7af32205d900c88fef0b544546eb0d7c84ee;hp=700f51188eb13a9e4f0af905ca8cf22b6801cb9c;hpb=466753b9c94f4ea5c771c791e64c575da4b01701;p=apitrace diff --git a/trace_writer.cpp b/trace_writer.cpp index 700f511..5a5f1f7 100644 --- a/trace_writer.cpp +++ b/trace_writer.cpp @@ -30,10 +30,9 @@ #include #include -#include - #include "os.hpp" #include "trace_writer.hpp" +#include "trace_snappyfile.hpp" #include "trace_format.hpp" @@ -41,30 +40,29 @@ namespace Trace { Writer::Writer() : - g_gzFile(NULL), call_no(0) { + m_file = new Trace::SnappyFile; close(); -}; +} -Writer::~Writer() { +Writer::~Writer() +{ close(); -}; + delete m_file; + m_file = NULL; +} void Writer::close(void) { - if (g_gzFile != NULL) { - gzclose(g_gzFile); - g_gzFile = NULL; - } + m_file->close(); } bool Writer::open(const char *filename) { close(); - g_gzFile = gzopen(filename, "wb"); - if (!g_gzFile) { + if (!m_file->open(filename, File::Write)) { return false; } @@ -79,54 +77,9 @@ Writer::open(const char *filename) { return true; } -void -Writer::open(void) { - - static unsigned dwCounter = 0; - - const char *szExtension = "trace"; - char szFileName[PATH_MAX]; - const char *lpFileName; - - lpFileName = getenv("TRACE_FILE"); - if (lpFileName) { - strncpy(szFileName, lpFileName, PATH_MAX); - } - else { - char szProcessName[PATH_MAX]; - char szCurrentDir[PATH_MAX]; - OS::GetProcessName(szProcessName, PATH_MAX); - OS::GetCurrentDir(szCurrentDir, PATH_MAX); - - for (;;) { - FILE *file; - - if (dwCounter) - snprintf(szFileName, PATH_MAX, "%s%c%s.%u.%s", szCurrentDir, PATH_SEP, szProcessName, dwCounter, szExtension); - else - snprintf(szFileName, PATH_MAX, "%s%c%s.%s", szCurrentDir, PATH_SEP, szProcessName, szExtension); - - file = fopen(szFileName, "rb"); - if (file == NULL) - break; - - fclose(file); - - ++dwCounter; - } - } - - OS::DebugMessage("apitrace: tracing to %s\n", szFileName); - - open(szFileName); -} - void inline Writer::_write(const void *sBuffer, size_t dwBytesToWrite) { - if (g_gzFile == NULL) - return; - - gzwrite(g_gzFile, sBuffer, dwBytesToWrite); + m_file->write(sBuffer, dwBytesToWrite); } void inline @@ -182,12 +135,6 @@ inline bool lookup(std::vector &map, size_t index) { } unsigned Writer::beginEnter(const FunctionSig *sig) { - OS::AcquireMutex(); - - if (!g_gzFile) { - open(); - } - _writeByte(Trace::EVENT_ENTER); _writeUInt(sig->id); if (!lookup(functions, sig->id)) { @@ -204,20 +151,15 @@ unsigned Writer::beginEnter(const FunctionSig *sig) { void Writer::endEnter(void) { _writeByte(Trace::CALL_END); - gzflush(g_gzFile, Z_SYNC_FLUSH); - OS::ReleaseMutex(); } void Writer::beginLeave(unsigned call) { - OS::AcquireMutex(); _writeByte(Trace::EVENT_LEAVE); _writeUInt(call); } void Writer::endLeave(void) { _writeByte(Trace::CALL_END); - gzflush(g_gzFile, Z_SYNC_FLUSH); - OS::ReleaseMutex(); } void Writer::beginArg(unsigned index) { @@ -333,7 +275,7 @@ void Writer::writeBitmask(const BitmaskSig *sig, unsigned long long value) { _writeUInt(sig->num_flags); for (unsigned i = 0; i < sig->num_flags; ++i) { if (i != 0 && sig->flags[i].value == 0) { - OS::DebugMessage("apitrace: sig %s is zero but is not first flag\n", sig->flags[i].name); + OS::DebugMessage("apitrace: warning: sig %s is zero but is not first flag\n", sig->flags[i].name); } _writeString(sig->flags[i].name); _writeUInt(sig->flags[i].value); @@ -356,5 +298,6 @@ void Writer::writeOpaque(const void *addr) { _writeUInt((size_t)addr); } + } /* namespace Trace */