-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathinterceptor-sample.cpp
More file actions
122 lines (103 loc) · 4.78 KB
/
Copy pathinterceptor-sample.cpp
File metadata and controls
122 lines (103 loc) · 4.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
/*
Demonstrates how to replace the libtracepoint library with an alternative implementation
of the tracepoint.h interface.
This sample uses the implementation from tracepoint-file.cpp which writes events to a
simple file. This means the events are not written to user_events and we don't link
against libtracepoint.
*/
#include <eventheader/TraceLoggingProvider.h>
#include <stdio.h>
#include <errno.h>
#include "tracepoint-file.h" // g_interceptorFileName
// Define a symbol for a "provider" (a group of related events).
TRACELOGGING_DEFINE_PROVIDER(
MyProvider,
"MyProviderName",
// {b7aa4d18-240c-5f41-5852-817dbf477472}
(0xb7aa4d18, 0x240c, 0x5f41, 0x58, 0x52, 0x81, 0x7d, 0xbf, 0x47, 0x74, 0x72));
// A program can have multiple providers.
// In this case, we set the group name.
TRACELOGGING_DEFINE_PROVIDER(
OtherProvider,
"OtherProviderName",
// {8ec53ac6-09b4-535e-5d19-e499de8832b4}
(0x8ec53ac6, 0x09b4, 0x535e, 0x5d, 0x19, 0xe4, 0x99, 0xde, 0x88, 0x32, 0xb4),
TraceLoggingOptionGroupName("mygroup"));
int main(int argc, char* argv[])
{
// Initialize tracepoint-file:
// Override the default output file name if appropriate, then delete the output file
// if it already exists.
if (argc > 1)
{
g_interceptorFileName = argv[1];
}
if (remove(g_interceptorFileName))
{
printf("Error %u clearing output file %s\n", errno, g_interceptorFileName);
}
// The rest of the program is the same as provider-sample.cpp, i.e. the same as
// when using the default (libtracepoint) implementation:
// At component initialization, register any providers.
// If a provider is not registered or if registration fails, subsequent operations
// on that provider (e.g. TraceLoggingWrite and TraceLoggingUnregister) will be safe
// no-ops.
TraceLoggingRegister(MyProvider);
TraceLoggingRegister(OtherProvider);
for (unsigned iteration = 1;; iteration += 1)
{
event_level const event1_level = event_level_information;
uint64_t const event1_keyword = 0x1;
// For sample purposes, show whether Event1 is currently enabled.
// TraceLoggingProviderEnabled is usually unnecessary because every
// TraceLoggingWrite automatically checks its own enable state.
printf("MyProviderName_L4K1 Event1 status=%x\n",
TraceLoggingProviderEnabled(MyProvider, event1_level, event1_keyword));
// If Event1 is enabled then evaluate args, pack fields, write the event.
TraceLoggingWrite(
MyProvider, // Provider to use for the event.
"Event1", // Event name.
TraceLoggingLevel(event1_level), // Event severity level.
TraceLoggingKeyword(event1_keyword), // Event category bits.
TraceLoggingInt32(argc, "ArgC"), // Int32 field named "ArgC".
TraceLoggingStruct(2, "Structure"), // The following 2 fields are part of "Structure".
TraceLoggingValue(argc, "ArgCount"), // int field named "ArgCount".
TraceLoggingString(argv[0], "Arg0"), // char string field named "Arg0".
TraceLoggingUInt32(iteration)); // uint32 field named "iteration".
event_level const event2_level = event_level_verbose;
uint64_t const event2_keyword = 0x23;
// For sample purposes, show whether Event2 is currently enabled.
printf("OtherProviderName_L5K23Gmygroup Event2 status=%x\n",
TraceLoggingProviderEnabled(OtherProvider, event2_level, event2_keyword));
// If Event2 is enabled then evaluate args, pack fields, write the event.
TraceLoggingWrite(
OtherProvider,
"Event2",
TraceLoggingLevel(event2_level),
TraceLoggingKeyword(event2_keyword),
TraceLoggingUInt32(iteration),
TraceLoggingString(NULL),
TraceLoggingString(argv[0], "argv0"),
TraceLoggingStruct(1, "struct"),
TraceLoggingCountedString(argv[0], (uint16_t)strlen(argv[0]), "cargv0"),
TraceLoggingBinary(argv[0], 2, "bin", "desc"),
TraceLoggingCharArray(argv[0], 2, "vchar", "desc", 123),
TraceLoggingCharFixedArray(argv[0], 2, "cchar"));
printf("Press enter to refresh, x + enter to exit...\n");
char ch = (char)getchar();
if (ch == 'x' || ch == 'X')
{
break;
}
while (ch != '\n')
{
ch = (char)getchar();
}
}
// At component cleanup, unregister the providers.
TraceLoggingUnregister(MyProvider);
TraceLoggingUnregister(OtherProvider);
return 0;
}