HawkTracer
0.10.0
|
In this tutorial we will learn how to define custom event types.
We recommend you to go through the Tutorial: Hello world! tutorial to make sure you have the HawkTracer library properly installed.
Let's say we'd like to track all the fopen()
and fclose()
calls, and additionally we'd like to know how many files are currently open by the application, whether the operation passed or failed, and what were the arguments.
Please copy the text below to a file named hawktracer-custom-event-type.c
(you can find this file in the repository as well).
You can see Integrate HawkTracer to existing project in order to see how to integrate HawkTracer to your project. For the purpose of the example, here are few shortcuts for most popular platforms:
We assume you already went through the Tutorial: Hello world! tutorial, so we only focus here on the new part.
Before we even start writing the logic, in order to define custom event types, we should include an extra header which defines helper macros for defining custom event types:
As discussed earlier, we want to track to operations - open and close file - and for each of the event we want to know how many files we currently have open in the application, whether the operation failed or completed successfully, and what were the parameters of those functions. Since number of opened files and the result are common values for both open
and close
operation, we can have a base class with those two fields. However, parameters for open
and close
are different, so we create two new classes which inherit from the base class:
HT_DECLARE_EVENT_KLASS() as a first argument takes the type name of the new class, as a second argument - base class, and then list of field definitions. Every field definition consists of data kind (see HT_MKCREFLECT_Types_Ext enum), C type, and the field name. The code generates an actual C struct, e.g. for the OpenFileEvent
, it is:
Additionally, the macro defines a few extra helper functions which are used for type registration or data serialization.
Let's move to the main function now. Every new data type must be registered to the HawkTracer registry, so the library knows how to deal with a particular event. It's done by calling HT_REGISTER_EVENT_KLASS() macro, which takes a class name as a parameter:
Once we registered our event classes, we can now start using them. HawkTracer provides a convenient macro HT_TIMELINE_PUSH_EVENT() for creating and pushing events to a timeline:
The macro takes 3 arguments:
The macro automatically creates an instance of the particular event, and all you need to do is to pass all the values in the order you defined earlier. The macro also sets correct timestamp for you.
After running the application, it should generate a list following events: