]> git.cworth.org Git - apitrace/blobdiff - common/trace_writer_local.cpp
First stab at tracing thread IDs.
[apitrace] / common / trace_writer_local.cpp
index 4d3949037951999563dd6271f7c5b918ab1d1e3b..bb06d90f74783f53f9d7bed795811fc7ac9fa251 100644 (file)
@@ -31,6 +31,8 @@
 #include <string.h>
 
 #include "os.hpp"
+#include "os_thread.hpp"
+#include "os_string.hpp"
 #include "trace_file.hpp"
 #include "trace_writer.hpp"
 #include "trace_format.hpp"
@@ -73,30 +75,33 @@ LocalWriter::~LocalWriter()
 
 void
 LocalWriter::open(void) {
+    os::String szFileName;
 
-    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 {
-        os::Path szProcessName = os::getProcessName();
-        os::Path szCurrentDir = os::getCurrentDir();
+    if (!lpFileName) {
+        static unsigned dwCounter = 0;
+
+        os::String process = os::getProcessName();
+#ifdef _WIN32
+        process.trimExtension();
+#endif
+        process.trimDirectory();
+
+        os::String prefix = os::getCurrentDir();
+        prefix.join(process);
 
         for (;;) {
             FILE *file;
 
             if (dwCounter)
-                snprintf(szFileName, PATH_MAX, "%s%c%s.%u.%s", szCurrentDir.str(), PATH_SEP, szProcessName.str(), dwCounter, szExtension);
+                szFileName = os::String::format("%s.%u.trace", prefix.str(), dwCounter);
             else
-                snprintf(szFileName, PATH_MAX, "%s%c%s.%s", szCurrentDir.str(), PATH_SEP, szProcessName.str(), szExtension);
+                szFileName = os::String::format("%s.trace", prefix.str());
 
-            file = fopen(szFileName, "rb");
+            lpFileName = szFileName;
+            file = fopen(lpFileName, "rb");
             if (file == NULL)
                 break;
 
@@ -106,9 +111,12 @@ LocalWriter::open(void) {
         }
     }
 
-    os::log("apitrace: tracing to %s\n", szFileName);
+    os::log("apitrace: tracing to %s\n", lpFileName);
 
-    Writer::open(szFileName);
+    if (!Writer::open(lpFileName)) {
+        os::log("apitrace: error: failed to open %s\n", lpFileName);
+        os::abort();
+    }
 
 #if 0
     // For debugging the exception handler
@@ -124,7 +132,9 @@ unsigned LocalWriter::beginEnter(const FunctionSig *sig) {
         open();
     }
 
-    return Writer::beginEnter(sig);
+    os::thread::id id = os::this_thread::get_id();
+
+    return Writer::beginEnter(sig, static_cast<unsigned>(id));
 }
 
 void LocalWriter::endEnter(void) {