Make localWriter a proper singleton.
authorJosé Fonseca <jose.r.fonseca@gmail.com>
Wed, 24 Aug 2011 15:58:13 +0000 (16:58 +0100)
committerJosé Fonseca <jose.r.fonseca@gmail.com>
Wed, 24 Aug 2011 15:58:13 +0000 (16:58 +0100)
d3d8.py
d3d9.py
gltrace.py
trace.py
trace_local_writer.cpp
trace_writer.hpp

diff --git a/d3d8.py b/d3d8.py
index f5fb76e..f579a9b 100644 (file)
--- a/d3d8.py
+++ b/d3d8.py
@@ -288,7 +288,7 @@ class D3D8Tracer(DllTracer):
     def dump_arg_instance(self, function, arg):
         # Dump shaders as strings
         if function.name in ('CreateVertexShader', 'CreatePixelShader') and arg.name == 'pFunction':
-            print '    DumpShader(__writer, %s);' % (arg.name)
+            print '    DumpShader(Trace::localWriter, %s);' % (arg.name)
             return
 
         DllTracer.dump_arg_instance(self, function, arg)
diff --git a/d3d9.py b/d3d9.py
index a63b805..640d218 100644 (file)
--- a/d3d9.py
+++ b/d3d9.py
@@ -455,7 +455,7 @@ class D3D9Tracer(DllTracer):
     def dump_arg_instance(self, function, arg):
         # Dump shaders as strings
         if function.name in ('CreateVertexShader', 'CreatePixelShader') and arg.name == 'pFunction':
-            print '    DumpShader(__writer, %s);' % (arg.name)
+            print '    DumpShader(Trace::localWriter, %s);' % (arg.name)
             return
 
         DllTracer.dump_arg_instance(self, function, arg)
index af57142..9224bbe 100644 (file)
@@ -412,13 +412,13 @@ class GlTracer(Tracer):
                     # Emit a fake function
                     print '        {'
                     print '            static const Trace::FunctionSig &__sig = %s ? __glEnableClientState_sig : __glDisableClientState_sig;' % flag_name
-                    print '            unsigned __call = __writer.beginEnter(&__sig);'
-                    print '            __writer.beginArg(0);'
+                    print '            unsigned __call = Trace::localWriter.beginEnter(&__sig);'
+                    print '            Trace::localWriter.beginArg(0);'
                     dump_instance(glapi.GLenum, enable_name)
-                    print '            __writer.endArg();'
-                    print '            __writer.endEnter();'
-                    print '            __writer.beginLeave(__call);'
-                    print '            __writer.endLeave();'
+                    print '            Trace::localWriter.endArg();'
+                    print '            Trace::localWriter.endEnter();'
+                    print '            Trace::localWriter.beginLeave(__call);'
+                    print '            Trace::localWriter.endLeave();'
                     print '        }'
 
             print '        return;'
@@ -523,19 +523,19 @@ class GlTracer(Tracer):
         Tracer.dispatch_function(self, function)
 
     def emit_memcpy(self, dest, src, length):
-        print '        unsigned __call = __writer.beginEnter(&__memcpy_sig);'
-        print '        __writer.beginArg(0);'
-        print '        __writer.writeOpaque(%s);' % dest
-        print '        __writer.endArg();'
-        print '        __writer.beginArg(1);'
-        print '        __writer.writeBlob(%s, %s);' % (src, length)
-        print '        __writer.endArg();'
-        print '        __writer.beginArg(2);'
-        print '        __writer.writeUInt(%s);' % length
-        print '        __writer.endArg();'
-        print '        __writer.endEnter();'
-        print '        __writer.beginLeave(__call);'
-        print '        __writer.endLeave();'
+        print '        unsigned __call = Trace::localWriter.beginEnter(&__memcpy_sig);'
+        print '        Trace::localWriter.beginArg(0);'
+        print '        Trace::localWriter.writeOpaque(%s);' % dest
+        print '        Trace::localWriter.endArg();'
+        print '        Trace::localWriter.beginArg(1);'
+        print '        Trace::localWriter.writeBlob(%s, %s);' % (src, length)
+        print '        Trace::localWriter.endArg();'
+        print '        Trace::localWriter.beginArg(2);'
+        print '        Trace::localWriter.writeUInt(%s);' % length
+        print '        Trace::localWriter.endArg();'
+        print '        Trace::localWriter.endEnter();'
+        print '        Trace::localWriter.beginLeave(__call);'
+        print '        Trace::localWriter.endLeave();'
        
     buffer_targets = [
         'ARRAY_BUFFER',
@@ -637,15 +637,15 @@ class GlTracer(Tracer):
             print '    __glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &__element_array_buffer);'
             print '    if (!__element_array_buffer) {'
             if isinstance(arg.type, stdapi.Array):
-                print '        __writer.beginArray(%s);' % arg.type.length
+                print '        Trace::localWriter.beginArray(%s);' % arg.type.length
                 print '        for(GLsizei i = 0; i < %s; ++i) {' % arg.type.length
-                print '            __writer.beginElement();'
-                print '            __writer.writeBlob(%s[i], count[i]*__gl_type_size(type));' % (arg.name)
-                print '            __writer.endElement();'
+                print '            Trace::localWriter.beginElement();'
+                print '            Trace::localWriter.writeBlob(%s[i], count[i]*__gl_type_size(type));' % (arg.name)
+                print '            Trace::localWriter.endElement();'
                 print '        }'
-                print '        __writer.endArray();'
+                print '        Trace::localWriter.endArray();'
             else:
-                print '        __writer.writeBlob(%s, count*__gl_type_size(type));' % (arg.name)
+                print '        Trace::localWriter.writeBlob(%s, count*__gl_type_size(type));' % (arg.name)
             print '    } else {'
             Tracer.dump_arg_instance(self, function, arg)
             print '    }'
@@ -660,7 +660,7 @@ class GlTracer(Tracer):
             print '        GLint __unpack_buffer = 0;'
             print '        __glGetIntegerv(GL_PIXEL_UNPACK_BUFFER_BINDING, &__unpack_buffer);'
             print '        if (__unpack_buffer) {'
-            print '            __writer.writeOpaque(%s);' % arg.name
+            print '            Trace::localWriter.writeOpaque(%s);' % arg.name
             print '        } else {'
             Tracer.dump_arg_instance(self, function, arg)
             print '        }'
@@ -718,19 +718,19 @@ class GlTracer(Tracer):
 
             # Emit a fake function
             self.array_trace_intermezzo(api, uppercase_name)
-            print '            unsigned __call = __writer.beginEnter(&__%s_sig);' % (function.name,)
+            print '            unsigned __call = Trace::localWriter.beginEnter(&__%s_sig);' % (function.name,)
             for arg in function.args:
                 assert not arg.output
-                print '            __writer.beginArg(%u);' % (arg.index,)
+                print '            Trace::localWriter.beginArg(%u);' % (arg.index,)
                 if arg.name != 'pointer':
                     dump_instance(arg.type, arg.name)
                 else:
-                    print '            __writer.writeBlob((const void *)%s, __size);' % (arg.name)
-                print '            __writer.endArg();'
+                    print '            Trace::localWriter.writeBlob((const void *)%s, __size);' % (arg.name)
+                print '            Trace::localWriter.endArg();'
             
-            print '            __writer.endEnter();'
-            print '            __writer.beginLeave(__call);'
-            print '            __writer.endLeave();'
+            print '            Trace::localWriter.endEnter();'
+            print '            Trace::localWriter.beginLeave(__call);'
+            print '            Trace::localWriter.endLeave();'
             print '        }'
             print '    }'
             self.array_epilog(api, uppercase_name)
@@ -791,19 +791,19 @@ class GlTracer(Tracer):
             print '                    size_t __size = __%s_size(%s, maxindex);' % (function.name, arg_names)
 
             # Emit a fake function
-            print '                    unsigned __call = __writer.beginEnter(&__%s_sig);' % (function.name,)
+            print '                    unsigned __call = Trace::localWriter.beginEnter(&__%s_sig);' % (function.name,)
             for arg in function.args:
                 assert not arg.output
-                print '                    __writer.beginArg(%u);' % (arg.index,)
+                print '                    Trace::localWriter.beginArg(%u);' % (arg.index,)
                 if arg.name != 'pointer':
                     dump_instance(arg.type, arg.name)
                 else:
-                    print '                    __writer.writeBlob((const void *)%s, __size);' % (arg.name)
-                print '                    __writer.endArg();'
+                    print '                    Trace::localWriter.writeBlob((const void *)%s, __size);' % (arg.name)
+                print '                    Trace::localWriter.endArg();'
             
-            print '                    __writer.endEnter();'
-            print '                    __writer.beginLeave(__call);'
-            print '                    __writer.endLeave();'
+            print '                    Trace::localWriter.endEnter();'
+            print '                    Trace::localWriter.beginLeave(__call);'
+            print '                    Trace::localWriter.endLeave();'
             print '                }'
             print '            }'
             print '        }'
@@ -859,15 +859,15 @@ class GlTracer(Tracer):
         self.fake_call(function, [texture])
 
     def fake_call(self, function, args):
-        print '            unsigned __fake_call = __writer.beginEnter(&__%s_sig);' % (function.name,)
+        print '            unsigned __fake_call = Trace::localWriter.beginEnter(&__%s_sig);' % (function.name,)
         for arg, instance in zip(function.args, args):
             assert not arg.output
-            print '            __writer.beginArg(%u);' % (arg.index,)
+            print '            Trace::localWriter.beginArg(%u);' % (arg.index,)
             dump_instance(arg.type, instance)
-            print '            __writer.endArg();'
-        print '            __writer.endEnter();'
-        print '            __writer.beginLeave(__fake_call);'
-        print '            __writer.endLeave();'
+            print '            Trace::localWriter.endArg();'
+        print '            Trace::localWriter.endEnter();'
+        print '            Trace::localWriter.beginLeave(__fake_call);'
+        print '            Trace::localWriter.endLeave();'
 
 
 
index 90cc426..7e40e46 100644 (file)
--- a/trace.py
+++ b/trace.py
@@ -60,10 +60,10 @@ class DumpDeclarator(stdapi.OnceVisitor):
         print '    static const Trace::StructSig sig = {'
         print '       %u, "%s", %u, members' % (int(struct.id), struct.name, len(struct.members))
         print '    };'
-        print '    __writer.beginStruct(&sig);'
+        print '    Trace::localWriter.beginStruct(&sig);'
         for type, name in struct.members:
             dump_instance(type, 'value.%s' % (name,))
-        print '    __writer.endStruct();'
+        print '    Trace::localWriter.endStruct();'
         print '}'
         print
 
@@ -90,10 +90,10 @@ class DumpDeclarator(stdapi.OnceVisitor):
             print '        sig = &sig%u;' % i
             print '        break;'
         print '    default:'
-        print '        __writer.writeSInt(value);'
+        print '        Trace::localWriter.writeSInt(value);'
         print '        return;'
         print '    }'
-        print '    __writer.writeEnum(sig);'
+        print '    Trace::localWriter.writeEnum(sig);'
         print '}'
         print
 
@@ -140,13 +140,13 @@ class DumpImplementer(stdapi.Visitor):
     '''Dump an instance.'''
 
     def visit_literal(self, literal, instance):
-        print '    __writer.write%s(%s);' % (literal.format, instance)
+        print '    Trace::localWriter.write%s(%s);' % (literal.format, instance)
 
     def visit_string(self, string, instance):
         if string.length is not None:
-            print '    __writer.writeString((const char *)%s, %s);' % (instance, string.length)
+            print '    Trace::localWriter.writeString((const char *)%s, %s);' % (instance, string.length)
         else:
-            print '    __writer.writeString((const char *)%s);' % instance
+            print '    Trace::localWriter.writeString((const char *)%s);' % instance
 
     def visit_const(self, const, instance):
         self.visit(const.type, instance)
@@ -159,35 +159,35 @@ class DumpImplementer(stdapi.Visitor):
         index = '__i' + array.type.id
         print '    if (%s) {' % instance
         print '        size_t %s = %s;' % (length, array.length)
-        print '        __writer.beginArray(%s);' % length
+        print '        Trace::localWriter.beginArray(%s);' % length
         print '        for (size_t %s = 0; %s < %s; ++%s) {' % (index, index, length, index)
-        print '            __writer.beginElement();'
+        print '            Trace::localWriter.beginElement();'
         self.visit(array.type, '(%s)[%s]' % (instance, index))
-        print '            __writer.endElement();'
+        print '            Trace::localWriter.endElement();'
         print '        }'
-        print '        __writer.endArray();'
+        print '        Trace::localWriter.endArray();'
         print '    } else {'
-        print '        __writer.writeNull();'
+        print '        Trace::localWriter.writeNull();'
         print '    }'
 
     def visit_blob(self, blob, instance):
-        print '    __writer.writeBlob(%s, %s);' % (instance, blob.size)
+        print '    Trace::localWriter.writeBlob(%s, %s);' % (instance, blob.size)
 
     def visit_enum(self, enum, instance):
         print '    __traceEnum%s(%s);' % (enum.id, instance)
 
     def visit_bitmask(self, bitmask, instance):
-        print '    __writer.writeBitmask(&__bitmask%s_sig, %s);' % (bitmask.id, instance)
+        print '    Trace::localWriter.writeBitmask(&__bitmask%s_sig, %s);' % (bitmask.id, instance)
 
     def visit_pointer(self, pointer, instance):
         print '    if (%s) {' % instance
-        print '        __writer.beginArray(1);'
-        print '        __writer.beginElement();'
+        print '        Trace::localWriter.beginArray(1);'
+        print '        Trace::localWriter.beginElement();'
         dump_instance(pointer.type, "*" + instance)
-        print '        __writer.endElement();'
-        print '        __writer.endArray();'
+        print '        Trace::localWriter.endElement();'
+        print '        Trace::localWriter.endArray();'
         print '    } else {'
-        print '        __writer.writeNull();'
+        print '        Trace::localWriter.writeNull();'
         print '    }'
 
     def visit_handle(self, handle, instance):
@@ -197,10 +197,10 @@ class DumpImplementer(stdapi.Visitor):
         self.visit(alias.type, instance)
 
     def visit_opaque(self, opaque, instance):
-        print '    __writer.writeOpaque((const void *)%s);' % instance
+        print '    Trace::localWriter.writeOpaque((const void *)%s);' % instance
 
     def visit_interface(self, interface, instance):
-        print '    __writer.writeOpaque((const void *)&%s);' % instance
+        print '    Trace::localWriter.writeOpaque((const void *)&%s);' % instance
 
 
 dump_instance = DumpImplementer().visit
@@ -309,8 +309,7 @@ class Tracer:
         self.footer(api)
 
     def header(self, api):
-        print 'Trace::LocalWriter __writer;'
-        print
+        pass
 
     def footer(self, api):
         pass
@@ -347,21 +346,21 @@ class Tracer:
         print
 
     def trace_function_impl_body(self, function):
-        print '    unsigned __call = __writer.beginEnter(&__%s_sig);' % (function.name,)
+        print '    unsigned __call = Trace::localWriter.beginEnter(&__%s_sig);' % (function.name,)
         for arg in function.args:
             if not arg.output:
                 self.unwrap_arg(function, arg)
                 self.dump_arg(function, arg)
-        print '    __writer.endEnter();'
+        print '    Trace::localWriter.endEnter();'
         self.dispatch_function(function)
-        print '    __writer.beginLeave(__call);'
+        print '    Trace::localWriter.beginLeave(__call);'
         for arg in function.args:
             if arg.output:
                 self.dump_arg(function, arg)
                 self.wrap_arg(function, arg)
         if function.type is not stdapi.Void:
             self.dump_ret(function, "__result")
-        print '    __writer.endLeave();'
+        print '    Trace::localWriter.endLeave();'
 
     def dispatch_function(self, function):
         if function.type is stdapi.Void:
@@ -372,9 +371,9 @@ class Tracer:
         print '    %s%s(%s);' % (result, dispatch, ', '.join([str(arg.name) for arg in function.args]))
 
     def dump_arg(self, function, arg):
-        print '    __writer.beginArg(%u);' % (arg.index,)
+        print '    Trace::localWriter.beginArg(%u);' % (arg.index,)
         self.dump_arg_instance(function, arg)
-        print '    __writer.endArg();'
+        print '    Trace::localWriter.endArg();'
 
     def dump_arg_instance(self, function, arg):
         dump_instance(arg.type, arg.name)
@@ -386,9 +385,9 @@ class Tracer:
         unwrap_instance(arg.type, arg.name)
 
     def dump_ret(self, function, instance):
-        print '    __writer.beginReturn();'
+        print '    Trace::localWriter.beginReturn();'
         dump_instance(function.type, instance)
-        print '    __writer.endReturn();'
+        print '    Trace::localWriter.endReturn();'
 
     def wrap_ret(self, function, instance):
         wrap_instance(function.type, instance)
@@ -412,10 +411,10 @@ class Tracer:
         print method.prototype(interface_wrap_name(interface) + '::' + method.name) + ' {'
         print '    static const char * __args[%u] = {%s};' % (len(method.args) + 1, ', '.join(['"this"'] + ['"%s"' % arg.name for arg in method.args]))
         print '    static const Trace::FunctionSig __sig = {%u, "%s", %u, __args};' % (int(method.id), interface.name + '::' + method.name, len(method.args) + 1)
-        print '    unsigned __call = __writer.beginEnter(&__sig);'
-        print '    __writer.beginArg(0);'
-        print '    __writer.writeOpaque((const void *)m_pInstance);'
-        print '    __writer.endArg();'
+        print '    unsigned __call = Trace::localWriter.beginEnter(&__sig);'
+        print '    Trace::localWriter.beginArg(0);'
+        print '    Trace::localWriter.writeOpaque((const void *)m_pInstance);'
+        print '    Trace::localWriter.endArg();'
         for arg in method.args:
             if not arg.output:
                 self.unwrap_arg(method, arg)
@@ -425,19 +424,19 @@ class Tracer:
         else:
             print '    %s __result;' % method.type
             result = '__result = '
-        print '    __writer.endEnter();'
+        print '    Trace::localWriter.endEnter();'
         print '    %sm_pInstance->%s(%s);' % (result, method.name, ', '.join([str(arg.name) for arg in method.args]))
-        print '    __writer.beginLeave(__call);'
+        print '    Trace::localWriter.beginLeave(__call);'
         for arg in method.args:
             if arg.output:
                 self.dump_arg(method, arg)
                 self.wrap_arg(method, arg)
         if method.type is not stdapi.Void:
-            print '    __writer.beginReturn();'
+            print '    Trace::localWriter.beginReturn();'
             dump_instance(method.type, "__result")
-            print '    __writer.endReturn();'
+            print '    Trace::localWriter.endReturn();'
             wrap_instance(method.type, '__result')
-        print '    __writer.endLeave();'
+        print '    Trace::localWriter.endLeave();'
         if method.name == 'QueryInterface':
             print '    if (ppvObj && *ppvObj) {'
             print '        if (*ppvObj == m_pInstance) {'
index 062f088..9dc746e 100644 (file)
@@ -40,6 +40,9 @@
 namespace Trace {
 
 
+LocalWriter::LocalWriter() {
+}
+
 void
 LocalWriter::open(void) {
 
@@ -110,5 +113,8 @@ void LocalWriter::endLeave(void) {
 }
 
 
+LocalWriter localWriter;
+
+
 } /* namespace Trace */
 
index 20a1530..d959714 100644 (file)
@@ -116,6 +116,11 @@ namespace Trace {
     class LocalWriter : public Writer {
     protected:
     public:
+        /**
+         * Should never called directly -- use localWriter singleton below instead.
+         */
+        LocalWriter();
+
         void open(void);
 
         unsigned beginEnter(const FunctionSig *sig);
@@ -124,6 +129,11 @@ namespace Trace {
         void beginLeave(unsigned call);
         void endLeave(void);
     };
+
+    /**
+     * Singleton.
+     */
+    extern LocalWriter localWriter;
 }
 
 #endif /* _TRACE_WRITER_HPP_ */