]> git.cworth.org Git - apitrace/blobdiff - trace_writer.cpp
Merge branch 'master' into compression
[apitrace] / trace_writer.cpp
index 7511d3192420db31f65ee2e74dd5203db3ec201a..057e3957be289a63b9598c3c4344ae75171cb71e 100644 (file)
@@ -77,48 +77,6 @@ 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) {
     m_file->write(sBuffer, dwBytesToWrite);
@@ -177,12 +135,6 @@ inline bool lookup(std::vector<bool> &map, size_t index) {
 }
 
 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)) {
@@ -199,19 +151,15 @@ unsigned Writer::beginEnter(const FunctionSig *sig) {
 
 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) {
@@ -350,5 +298,75 @@ void Writer::writeOpaque(const void *addr) {
     _writeUInt((size_t)addr);
 }
 
+
+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();
+
+    if (!m_file->isOpened()) {
+        open();
+    }
+
+    return Writer::beginEnter(sig);
+}
+
+void LocalWriter::endEnter(void) {
+    Writer::endEnter();
+    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 */