]> git.cworth.org Git - apitrace/blobdiff - common/trace_writer_local.cpp
Use AddVectoredExceptionHandler
[apitrace] / common / trace_writer_local.cpp
index 2b1ef4304a8bdde0cbdfdc59eef0dd8926c5b377..77c8bd67de3785db908e02a7dbf574f2385f0167 100644 (file)
@@ -31,7 +31,8 @@
 #include <string.h>
 
 #include "os.hpp"
-#include "os_path.hpp"
+#include "os_thread.hpp"
+#include "os_string.hpp"
 #include "trace_file.hpp"
 #include "trace_writer.hpp"
 #include "trace_format.hpp"
@@ -74,7 +75,7 @@ LocalWriter::~LocalWriter()
 
 void
 LocalWriter::open(void) {
-    os::Path szFileName;
+    os::String szFileName;
 
     const char *lpFileName;
 
@@ -82,22 +83,22 @@ LocalWriter::open(void) {
     if (!lpFileName) {
         static unsigned dwCounter = 0;
 
-        os::Path process = os::getProcessName();
+        os::String process = os::getProcessName();
 #ifdef _WIN32
         process.trimExtension();
 #endif
         process.trimDirectory();
 
-        os::Path prefix = os::getCurrentDir();
+        os::String prefix = os::getCurrentDir();
         prefix.join(process);
 
         for (;;) {
             FILE *file;
 
             if (dwCounter)
-                szFileName = os::Path::format("%s.%u.trace", prefix.str(), dwCounter);
+                szFileName = os::String::format("%s.%u.trace", prefix.str(), dwCounter);
             else
-                szFileName = os::Path::format("%s.trace", prefix.str());
+                szFileName = os::String::format("%s.trace", prefix.str());
 
             lpFileName = szFileName;
             file = fopen(lpFileName, "rb");
@@ -112,7 +113,10 @@ LocalWriter::open(void) {
 
     os::log("apitrace: tracing to %s\n", lpFileName);
 
-    Writer::open(lpFileName);
+    if (!Writer::open(lpFileName)) {
+        os::log("apitrace: error: failed to open %s\n", lpFileName);
+        os::abort();
+    }
 
 #if 0
     // For debugging the exception handler
@@ -120,6 +124,9 @@ LocalWriter::open(void) {
 #endif
 }
 
+static unsigned next_thread_id = 0;
+static os::thread_specific_ptr<unsigned> thread_id_specific_ptr;
+
 unsigned LocalWriter::beginEnter(const FunctionSig *sig) {
     os::acquireMutex();
     ++acquired;
@@ -128,7 +135,18 @@ unsigned LocalWriter::beginEnter(const FunctionSig *sig) {
         open();
     }
 
-    return Writer::beginEnter(sig);
+    unsigned *thread_id_ptr = thread_id_specific_ptr.get();
+    unsigned thread_id;
+    if (thread_id_ptr) {
+        thread_id = *thread_id_ptr;
+    } else {
+        thread_id = next_thread_id++;
+        thread_id_ptr = new unsigned;
+        *thread_id_ptr = thread_id;
+        thread_id_specific_ptr.reset(thread_id_ptr);
+    }
+
+    return Writer::beginEnter(sig, thread_id);
 }
 
 void LocalWriter::endEnter(void) {