#include <stdlib.h>
#include <string.h>
-#include <zlib.h>
-
#include "os.hpp"
#include "trace_writer.hpp"
+#include "trace_file.hpp"
#include "trace_format.hpp"
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;
}
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
}
unsigned Writer::beginEnter(const FunctionSig *sig) {
- OS::AcquireMutex();
-
- if (!m_file->isOpened()) {
- open();
- }
-
_writeByte(Trace::EVENT_ENTER);
_writeUInt(sig->id);
if (!lookup(functions, sig->id)) {
void Writer::endEnter(void) {
_writeByte(Trace::CALL_END);
- OS::ReleaseMutex();
}
void Writer::beginLeave(unsigned call) {
- OS::AcquireMutex();
_writeByte(Trace::EVENT_LEAVE);
_writeUInt(call);
}
void Writer::endLeave(void) {
_writeByte(Trace::CALL_END);
- m_file->flush();
- OS::ReleaseMutex();
}
void Writer::beginArg(unsigned index) {
_writeUInt((size_t)addr);
}
- if (!g_gzFile) {
+
+ void
+ LocalWriter::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);
+
+ Writer::open(szFileName);
+ }
+
+ unsigned LocalWriter::beginEnter(const FunctionSig *sig) {
+ OS::AcquireMutex();
+
- gzflush(g_gzFile, Z_SYNC_FLUSH);
++ if (!m_file->isOpened()) {
+ open();
+ }
+
+ return Writer::beginEnter(sig);
+ }
+
+ void LocalWriter::endEnter(void) {
+ Writer::endEnter();
- gzflush(g_gzFile, Z_SYNC_FLUSH);
+ OS::ReleaseMutex();
+ }
+
+ void LocalWriter::beginLeave(unsigned call) {
+ OS::AcquireMutex();
+ Writer::beginLeave(call);
+ }
+
+ void LocalWriter::endLeave(void) {
+ Writer::endLeave();
++ m_file->flush();
+ OS::ReleaseMutex();
+ }
+
+
} /* namespace Trace */