Document of c-modernization-kit (util) 1.0.0

Loading...
Searching...
No Matches
etw-provider.c
Go to the documentation of this file.
1#ifdef _WIN32
2
3#include <windows.h>
4#include <TraceLoggingProvider.h>
5#pragma comment(lib, "Advapi32.lib")
6#include <trace-etw-util.h>
7#include <stdlib.h>
8
12 struct etw_provider
13{
15 etw_provider_ref_t provider_ref;
16};
17
18etw_provider_t *TRACE_ETW_UTIL_API
19 etw_provider_init(etw_provider_ref_t provider_ref)
20{
21 etw_provider_t *handle;
22 TLG_STATUS status;
23
24 if (provider_ref == NULL)
25 {
26 return NULL;
27 }
28
29 handle = (etw_provider_t *)malloc(sizeof(etw_provider_t));
30 if (handle == NULL)
31 {
32 return NULL;
33 }
34
35 handle->provider_ref = provider_ref;
36
37 status = TraceLoggingRegister(provider_ref);
38 if (status != S_OK)
39 {
40 free(handle);
41 return NULL;
42 }
43
44 return handle;
45}
46
51 static void write_trace_event(etw_provider_ref_t ref, int level,
52 const char *service, const char *message)
53{
54 if (service != NULL)
55 {
56 switch (level)
57 {
58 case 1:
59 TraceLoggingWrite(ref, "Trace",
60 TraceLoggingLevel(1),
61 TraceLoggingString(service, "Service"),
62 TraceLoggingString(message, "Message"));
63 break;
64 case 2:
65 TraceLoggingWrite(ref, "Trace",
66 TraceLoggingLevel(2),
67 TraceLoggingString(service, "Service"),
68 TraceLoggingString(message, "Message"));
69 break;
70 case 3:
71 TraceLoggingWrite(ref, "Trace",
72 TraceLoggingLevel(3),
73 TraceLoggingString(service, "Service"),
74 TraceLoggingString(message, "Message"));
75 break;
76 case 4:
77 TraceLoggingWrite(ref, "Trace",
78 TraceLoggingLevel(4),
79 TraceLoggingString(service, "Service"),
80 TraceLoggingString(message, "Message"));
81 break;
82 default:
83 TraceLoggingWrite(ref, "Trace",
84 TraceLoggingLevel(5),
85 TraceLoggingString(service, "Service"),
86 TraceLoggingString(message, "Message"));
87 break;
88 }
89 }
90 else
91 {
92 switch (level)
93 {
94 case 1:
95 TraceLoggingWrite(ref, "Trace",
96 TraceLoggingLevel(1),
97 TraceLoggingString(message, "Message"));
98 break;
99 case 2:
100 TraceLoggingWrite(ref, "Trace",
101 TraceLoggingLevel(2),
102 TraceLoggingString(message, "Message"));
103 break;
104 case 3:
105 TraceLoggingWrite(ref, "Trace",
106 TraceLoggingLevel(3),
107 TraceLoggingString(message, "Message"));
108 break;
109 case 4:
110 TraceLoggingWrite(ref, "Trace",
111 TraceLoggingLevel(4),
112 TraceLoggingString(message, "Message"));
113 break;
114 default:
115 TraceLoggingWrite(ref, "Trace",
116 TraceLoggingLevel(5),
117 TraceLoggingString(message, "Message"));
118 break;
119 }
120 }
121}
122
123 int TRACE_ETW_UTIL_API
124 etw_provider_write(etw_provider_t *handle, int level,
125 const char *service, const char *message)
126{
127 if (handle == NULL || message == NULL)
128 {
129 return 0;
130 }
131
132 write_trace_event(handle->provider_ref, level, service, message);
133
134 return 0;
135}
136
137 void TRACE_ETW_UTIL_API
138 etw_provider_dispose(etw_provider_t *handle)
139{
140 if (handle == NULL)
141 {
142 return;
143 }
144
145 TraceLoggingUnregister(handle->provider_ref);
146 free(handle);
147}
148
149#endif /* _WIN32 */
trace-etw-util.h
ETW (Event Tracing for Windows) ヘルパーライブラリ。