]> git.cworth.org Git - apitrace/blobdiff - trace_writer.hpp
Merge branch 'master' into compression
[apitrace] / trace_writer.hpp
index d41acdef1d26e994ac635d0f0009fb3ca1db625e..4d46f739f8f231fcdee4e39de053b21949193448 100644 (file)
 #ifndef _TRACE_WRITER_HPP_
 #define _TRACE_WRITER_HPP_
 
+
 #include <stddef.h>
 
-namespace Trace {
+#include <vector>
 
-    typedef unsigned Id;
+#include "trace_model.hpp"
 
-    struct FunctionSig {
-        Id id;
-        const char *name;
-        unsigned num_args;
-        const char **args;
-    };
-
-    struct StructSig {
-        Id id;
-        const char *name;
-        unsigned num_members;
-        const char **members;
-    };
 
-    struct EnumSig {
-        Id id;
-        const char *name;
-        signed long long value;
-    };
+namespace Trace {
+    class File;
+
+    class Writer {
+    protected:
+        File *m_file;
+        unsigned call_no;
+
+        std::vector<bool> functions;
+        std::vector<bool> structs;
+        std::vector<bool> enums;
+        std::vector<bool> bitmasks;
+
+    public:
+        Writer();
+        ~Writer();
+
+        bool open(const char *filename);
+        void close(void);
+
+        unsigned beginEnter(const FunctionSig *sig);
+        void endEnter(void);
+
+        void beginLeave(unsigned call);
+        void endLeave(void);
+
+        void beginArg(unsigned index);
+        inline void endArg(void) {}
+
+        void beginReturn(void);
+        inline void endReturn(void) {}
+
+        void beginArray(size_t length);
+        inline void endArray(void) {}
+
+        inline void beginElement(void) {}
+        inline void endElement(void) {}
+
+        void beginStruct(const StructSig *sig);
+        inline void endStruct(void) {}
+
+        void writeBool(bool value);
+        void writeSInt(signed long long value);
+        void writeUInt(unsigned long long value);
+        void writeFloat(float value);
+        void writeDouble(double value);
+        void writeString(const char *str);
+        void writeString(const char *str, size_t size);
+        void writeWString(const wchar_t *str);
+        void writeBlob(const void *data, size_t size);
+        void writeEnum(const EnumSig *sig);
+        void writeBitmask(const BitmaskSig *sig, unsigned long long value);
+        void writeNull(void);
+        void writeOpaque(const void *ptr);
+
+        void writeCall(Call *call);
+
+    protected:
+        void inline _write(const void *sBuffer, size_t dwBytesToWrite);
+        void inline _writeByte(char c);
+        void inline _writeUInt(unsigned long long value);
+        void inline _writeFloat(float value);
+        void inline _writeDouble(double value);
+        void inline _writeString(const char *str);
 
-    struct BitmaskVal {
-        const char *name;
-        unsigned long long value;
     };
 
-    struct BitmaskSig {
-        Id id;
-        unsigned count;
-        const BitmaskVal *values;
+    /**
+     * A specialized Writer class, mean to trace the current process.
+     *
+     * In particular:
+     * - it creates a trace file based on the current process name
+     * - uses mutexes to allow tracing from multiple threades
+     * - flushes the output to ensure the last call is traced in event of
+     *   abnormal termination
+     */
+    class LocalWriter : public Writer {
+    protected:
+    public:
+        void open(void);
+
+        unsigned beginEnter(const FunctionSig *sig);
+        void endEnter(void);
+
+        void beginLeave(unsigned call);
+        void endLeave(void);
     };
-
-    void Open(void);
-    void Close(void);
-
-    unsigned BeginEnter(const FunctionSig &function);
-    void EndEnter(void);
-
-    void BeginLeave(unsigned call);
-    void EndLeave(void);
-
-    void BeginArg(unsigned index);
-    inline void EndArg(void) {}
-
-    void BeginReturn(void);
-    inline void EndReturn(void) {}
-
-    void BeginArray(size_t length);
-    inline void EndArray(void) {}
-
-    inline void BeginElement(void) {}
-    inline void EndElement(void) {}
-
-    void BeginStruct(const StructSig *sig);
-    inline void EndStruct(void) {}
-
-    void LiteralBool(bool value);
-    void LiteralSInt(signed long long value);
-    void LiteralUInt(unsigned long long value);
-    void LiteralFloat(float value);
-    void LiteralDouble(double value);
-    void LiteralString(const char *str);
-    void LiteralString(const char *str, size_t size);
-    void LiteralWString(const wchar_t *str);
-    void LiteralBlob(const void *data, size_t size);
-    void LiteralEnum(const EnumSig *sig);
-    void LiteralBitmask(const BitmaskSig &bitmask, unsigned long long value);
-    void LiteralNull(void);
-    void LiteralOpaque(const void *ptr);
-
-    void Abort(void);
 }
 
 #endif /* _TRACE_WRITER_HPP_ */