-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode-etw.cc
192 lines (159 loc) · 5.23 KB
/
node-etw.cc
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include <node.h>
#include <v8.h>
#include <evntprov.h>
using namespace v8;
void LogEvents_LogDebugEvent(LPCSTR message);
void LogEvents_LogVerboseEvent(LPCSTR message);
void LogEvents_LogInfoEvent(LPCSTR message);
void LogEvents_LogWarningEvent(LPCSTR message);
void LogEvents_LogErrorEvent(LPCSTR message);
Handle<Value> LogDebug(const Arguments& args)
{
HandleScope scope;
const char *str = (args.Length() > 0) ? *v8::String::Utf8Value(args[0]->ToString()) : "";
LogEvents_LogDebugEvent(str);
return scope.Close(Undefined());
}
Handle<Value> LogVerbose(const Arguments& args)
{
HandleScope scope;
const char *str = (args.Length() > 0) ? *v8::String::Utf8Value(args[0]->ToString()) : "";
LogEvents_LogVerboseEvent(str);
return scope.Close(Undefined());
}
Handle<Value> LogInfo(const Arguments& args)
{
HandleScope scope;
const char *str = (args.Length() > 0) ? *v8::String::Utf8Value(args[0]->ToString()) : "";
LogEvents_LogInfoEvent(str);
return scope.Close(Undefined());
}
Handle<Value> LogWarning(const Arguments& args)
{
HandleScope scope;
const char *str = (args.Length() > 0) ? *v8::String::Utf8Value(args[0]->ToString()) : "";
LogEvents_LogWarningEvent(str);
return scope.Close(Undefined());
}
Handle<Value> LogError(const Arguments& args)
{
HandleScope scope;
const char *str = (args.Length() > 0) ? *v8::String::Utf8Value(args[0]->ToString()) : "";
LogEvents_LogErrorEvent(str);
return scope.Close(Undefined());
}
/***
* ETW HELPERS
***/
#define LOG_EVENT_ID 1
#define LOG_EVENT_VERSION 0
#define LOG_EVENT_CHANNEL 0
#define LOG_EVENT_TASK 0
#define LOG_EVENT_KEYWORD 1
#define ETW_PROVIDER_GUID(l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \
const GUID ProviderGuid = { l, w1, w2, { b1, b2, b3, b4, b5, b6, b7, b8 } }
ETW_PROVIDER_GUID(0x10535c9f, 0xbf2e, 0x4a80, 0x84, 0x10, 0xeb, 0xbe, 0xba, 0x2c, 0x74, 0x00);
extern "C"
{
ULONG
EVNTAPI
EventRegister(
_In_ LPCGUID ProviderId,
_In_opt_ PENABLECALLBACK EnableCallback,
_In_opt_ PVOID CallbackContext,
_Out_ PREGHANDLE RegHandle
);
ULONG
EVNTAPI
EventWrite(
_In_ REGHANDLE RegHandle,
_In_ PCEVENT_DESCRIPTOR EventDescriptor,
_In_ ULONG UserDataCount,
_In_reads_opt_(UserDataCount) PEVENT_DATA_DESCRIPTOR UserData
);
}
REGHANDLE RegistrationHandle = 0;
void EtwInitProvider()
{
if (0 != RegistrationHandle)
return;
EventRegister(
&ProviderGuid, // GUID that identifies the provider
NULL, // Callback not used
NULL, // Context noot used
&RegistrationHandle // Used when calling EventWrite and EventUnregister
);
}
FORCEINLINE
VOID
etw_CreateDescriptor_LPCSTR(
EVENT_DATA_DESCRIPTOR* pEventDataDescriptor,
LPCSTR* value)
{
pEventDataDescriptor->Ptr = (ULONGLONG)(*value);
pEventDataDescriptor->Size =(ULONG) (*value ? strlen((*value)) + 1 : 0);
pEventDataDescriptor->Reserved = 0;
return;
}
static void Etw_Trace1_LPCSTR (EVENT_DESCRIPTOR* eventDescriptor, LPCSTR a0)
{
DWORD status = ERROR_SUCCESS;
EVENT_DATA_DESCRIPTOR Descriptors[1];
EtwInitProvider();
etw_CreateDescriptor_LPCSTR (&Descriptors[0], &a0);
status = EventWrite(
RegistrationHandle,
eventDescriptor,
1,
Descriptors
);
}
void Log(USHORT id, UCHAR level, LPCSTR message)
{
EVENT_DESCRIPTOR descriptor = {
id, /* USHORT Id; */
LOG_EVENT_VERSION, /* UCHAR Version; */
LOG_EVENT_CHANNEL, /* UCHAR Channel; */
level, /* UCHAR Level; */
0, /* UCHAR Opcode; */
LOG_EVENT_TASK, /* USHORT Task; */
LOG_EVENT_KEYWORD, /* ULONGLONG Keyword; */
};
Etw_Trace1_LPCSTR(&descriptor, message);
};
void LogEvents_LogDebugEvent(LPCSTR message)
{
Log(5, 5, message);
}
void LogEvents_LogVerboseEvent(LPCSTR message)
{
Log(4, 4, message);
}
void LogEvents_LogInfoEvent(LPCSTR message)
{
Log(3, 3, message);
}
void LogEvents_LogWarningEvent(LPCSTR message)
{
Log(2, 2, message);
}
void LogEvents_LogErrorEvent(LPCSTR message)
{
Log(1, 1, message);
}
void init(Handle<Object> exports)
{
exports->Set(String::NewSymbol("debug"),
FunctionTemplate::New(LogDebug)->GetFunction());
exports->Set(String::NewSymbol("log"),
FunctionTemplate::New(LogVerbose)->GetFunction());
exports->Set(String::NewSymbol("info"),
FunctionTemplate::New(LogInfo)->GetFunction());
exports->Set(String::NewSymbol("warn"),
FunctionTemplate::New(LogWarning)->GetFunction());
exports->Set(String::NewSymbol("warning"),
FunctionTemplate::New(LogWarning)->GetFunction());
exports->Set(String::NewSymbol("error"),
FunctionTemplate::New(LogError)->GetFunction());
}
NODE_MODULE(etw, init)