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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
|
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Diagnostics;
#if !ES_BUILD_AGAINST_DOTNET_V35
using Contract = System.Diagnostics.Contracts.Contract;
#else
using Contract = Microsoft.Diagnostics.Contracts.Internal.Contract;
#endif
#if ES_BUILD_STANDALONE
namespace Microsoft.Diagnostics.Tracing
#else
namespace System.Diagnostics.Tracing
#endif
{
/// <summary>
/// TraceLogging: Type handler for empty or unsupported types.
/// </summary>
internal sealed class NullTypeInfo : TraceLoggingTypeInfo
{
public NullTypeInfo() : base(typeof(EmptyStruct)) { }
public override void WriteMetadata(
TraceLoggingMetadataCollector collector,
string name,
EventFieldFormat format)
{
collector.AddGroup(name);
}
public override void WriteData(TraceLoggingDataCollector collector, PropertyValue value)
{
return;
}
public override object GetData(object value)
{
return null;
}
}
/// <summary>
/// Type handler for simple scalar types.
/// </summary>
sealed class ScalarTypeInfo : TraceLoggingTypeInfo
{
Func<EventFieldFormat, TraceLoggingDataType, TraceLoggingDataType> formatFunc;
TraceLoggingDataType nativeFormat;
private ScalarTypeInfo(
Type type,
Func<EventFieldFormat, TraceLoggingDataType, TraceLoggingDataType> formatFunc,
TraceLoggingDataType nativeFormat)
: base(type)
{
this.formatFunc = formatFunc;
this.nativeFormat = nativeFormat;
}
public override void WriteMetadata(TraceLoggingMetadataCollector collector, string name, EventFieldFormat format)
{
collector.AddScalar(name, formatFunc(format, nativeFormat));
}
public override void WriteData(TraceLoggingDataCollector collector, PropertyValue value)
{
collector.AddScalar(value);
}
public static TraceLoggingTypeInfo Boolean() { return new ScalarTypeInfo(typeof(Boolean), Statics.Format8, TraceLoggingDataType.Boolean8); }
public static TraceLoggingTypeInfo Byte() { return new ScalarTypeInfo(typeof(Byte), Statics.Format8, TraceLoggingDataType.UInt8); }
public static TraceLoggingTypeInfo SByte() { return new ScalarTypeInfo(typeof(SByte), Statics.Format8, TraceLoggingDataType.Int8); }
public static TraceLoggingTypeInfo Char() { return new ScalarTypeInfo(typeof(Char), Statics.Format16, TraceLoggingDataType.Char16); }
public static TraceLoggingTypeInfo Int16() { return new ScalarTypeInfo(typeof(Int16), Statics.Format16, TraceLoggingDataType.Int16); }
public static TraceLoggingTypeInfo UInt16() { return new ScalarTypeInfo(typeof(UInt16), Statics.Format16, TraceLoggingDataType.UInt16); }
public static TraceLoggingTypeInfo Int32() { return new ScalarTypeInfo(typeof(Int32), Statics.Format32, TraceLoggingDataType.Int32); }
public static TraceLoggingTypeInfo UInt32() { return new ScalarTypeInfo(typeof(UInt32), Statics.Format32, TraceLoggingDataType.UInt32); }
public static TraceLoggingTypeInfo Int64() { return new ScalarTypeInfo(typeof(Int64), Statics.Format64, TraceLoggingDataType.Int64); }
public static TraceLoggingTypeInfo UInt64() { return new ScalarTypeInfo(typeof(UInt64), Statics.Format64, TraceLoggingDataType.UInt64); }
public static TraceLoggingTypeInfo IntPtr() { return new ScalarTypeInfo(typeof(IntPtr), Statics.FormatPtr, Statics.IntPtrType); }
public static TraceLoggingTypeInfo UIntPtr() { return new ScalarTypeInfo(typeof(UIntPtr), Statics.FormatPtr, Statics.UIntPtrType); }
public static TraceLoggingTypeInfo Single() { return new ScalarTypeInfo(typeof(Single), Statics.Format32, TraceLoggingDataType.Float); }
public static TraceLoggingTypeInfo Double() { return new ScalarTypeInfo(typeof(Double), Statics.Format64, TraceLoggingDataType.Double); }
public static TraceLoggingTypeInfo Guid() { return new ScalarTypeInfo(typeof(Guid), (f, t) => Statics.MakeDataType(TraceLoggingDataType.Guid, f), TraceLoggingDataType.Guid); }
}
/// <summary>
/// Type handler for arrays of scalars
/// </summary>
internal sealed class ScalarArrayTypeInfo : TraceLoggingTypeInfo
{
Func<EventFieldFormat, TraceLoggingDataType, TraceLoggingDataType> formatFunc;
TraceLoggingDataType nativeFormat;
int elementSize;
private ScalarArrayTypeInfo(
Type type,
Func<EventFieldFormat, TraceLoggingDataType, TraceLoggingDataType> formatFunc,
TraceLoggingDataType nativeFormat,
int elementSize)
: base(type)
{
this.formatFunc = formatFunc;
this.nativeFormat = nativeFormat;
this.elementSize = elementSize;
}
public override void WriteMetadata(TraceLoggingMetadataCollector collector, string name, EventFieldFormat format)
{
collector.AddArray(name, formatFunc(format, nativeFormat));
}
public override void WriteData(TraceLoggingDataCollector collector, PropertyValue value)
{
collector.AddArray(value, elementSize);
}
public static TraceLoggingTypeInfo Boolean() { return new ScalarArrayTypeInfo(typeof(Boolean[]), Statics.Format8, TraceLoggingDataType.Boolean8, sizeof(Boolean)); }
public static TraceLoggingTypeInfo Byte() { return new ScalarArrayTypeInfo(typeof(Byte[]), Statics.Format8, TraceLoggingDataType.UInt8, sizeof(Byte)); }
public static TraceLoggingTypeInfo SByte() { return new ScalarArrayTypeInfo(typeof(SByte[]), Statics.Format8, TraceLoggingDataType.Int8, sizeof(SByte)); }
public static TraceLoggingTypeInfo Char() { return new ScalarArrayTypeInfo(typeof(Char[]), Statics.Format16, TraceLoggingDataType.Char16, sizeof(Char)); }
public static TraceLoggingTypeInfo Int16() { return new ScalarArrayTypeInfo(typeof(Int16[]), Statics.Format16, TraceLoggingDataType.Int16, sizeof(Int16)); }
public static TraceLoggingTypeInfo UInt16() { return new ScalarArrayTypeInfo(typeof(UInt16[]), Statics.Format16, TraceLoggingDataType.UInt16, sizeof(UInt16)); }
public static TraceLoggingTypeInfo Int32() { return new ScalarArrayTypeInfo(typeof(Int32[]), Statics.Format32, TraceLoggingDataType.Int32, sizeof(Int32)); }
public static TraceLoggingTypeInfo UInt32() { return new ScalarArrayTypeInfo(typeof(UInt32[]), Statics.Format32, TraceLoggingDataType.UInt32, sizeof(UInt32)); }
public static TraceLoggingTypeInfo Int64() { return new ScalarArrayTypeInfo(typeof(Int64[]), Statics.Format64, TraceLoggingDataType.Int64, sizeof(Int64)); }
public static TraceLoggingTypeInfo UInt64() { return new ScalarArrayTypeInfo(typeof(UInt64[]), Statics.Format64, TraceLoggingDataType.UInt64, sizeof(UInt64)); }
public static TraceLoggingTypeInfo IntPtr() { return new ScalarArrayTypeInfo(typeof(IntPtr[]), Statics.FormatPtr, Statics.IntPtrType, System.IntPtr.Size); }
public static TraceLoggingTypeInfo UIntPtr() { return new ScalarArrayTypeInfo(typeof(UIntPtr[]), Statics.FormatPtr, Statics.UIntPtrType, System.IntPtr.Size); }
public static TraceLoggingTypeInfo Single() { return new ScalarArrayTypeInfo(typeof(Single[]), Statics.Format32, TraceLoggingDataType.Float, sizeof(Single)); }
public static TraceLoggingTypeInfo Double() { return new ScalarArrayTypeInfo(typeof(Double[]), Statics.Format64, TraceLoggingDataType.Double, sizeof(Double)); }
public unsafe static TraceLoggingTypeInfo Guid() { return new ScalarArrayTypeInfo(typeof(Guid), (f, t) => Statics.MakeDataType(TraceLoggingDataType.Guid, f), TraceLoggingDataType.Guid, sizeof(Guid)); }
}
/// <summary>
/// TraceLogging: Type handler for String.
/// </summary>
internal sealed class StringTypeInfo : TraceLoggingTypeInfo
{
public StringTypeInfo() : base(typeof(string)) { }
public override void WriteMetadata(
TraceLoggingMetadataCollector collector,
string name,
EventFieldFormat format)
{
collector.AddNullTerminatedString(name, Statics.MakeDataType(TraceLoggingDataType.Utf16String, format));
}
public override void WriteData(TraceLoggingDataCollector collector, PropertyValue value)
{
collector.AddNullTerminatedString((string)value.ReferenceValue);
}
public override object GetData(object value)
{
if(value == null)
{
return "";
}
return value;
}
}
/// <summary>
/// TraceLogging: Type handler for DateTime.
/// </summary>
internal sealed class DateTimeTypeInfo : TraceLoggingTypeInfo
{
public DateTimeTypeInfo() : base(typeof(DateTime)) { }
public override void WriteMetadata(
TraceLoggingMetadataCollector collector,
string name,
EventFieldFormat format)
{
collector.AddScalar(name, Statics.MakeDataType(TraceLoggingDataType.FileTime, format));
}
public override void WriteData(TraceLoggingDataCollector collector, PropertyValue value)
{
DateTime dateTime = value.ScalarValue.AsDateTime;
const long UTCMinTicks = 504911232000000000;
long dateTimeTicks = 0;
// We cannot translate dates sooner than 1/1/1601 in UTC.
// To avoid getting an ArgumentOutOfRangeException we compare with 1/1/1601 DateTime ticks
if (dateTime.Ticks > UTCMinTicks)
dateTimeTicks = dateTime.ToFileTimeUtc();
collector.AddScalar(dateTimeTicks);
}
}
/// <summary>
/// TraceLogging: Type handler for DateTimeOffset.
/// </summary>
internal sealed class DateTimeOffsetTypeInfo : TraceLoggingTypeInfo
{
public DateTimeOffsetTypeInfo() : base(typeof(DateTimeOffset)) { }
public override void WriteMetadata(TraceLoggingMetadataCollector collector, string name, EventFieldFormat format)
{
var group = collector.AddGroup(name);
group.AddScalar("Ticks", Statics.MakeDataType(TraceLoggingDataType.FileTime, format));
group.AddScalar("Offset", TraceLoggingDataType.Int64);
}
public override void WriteData(TraceLoggingDataCollector collector, PropertyValue value)
{
var dateTimeOffset = value.ScalarValue.AsDateTimeOffset;
var ticks = dateTimeOffset.Ticks;
collector.AddScalar(ticks < 504911232000000000 ? 0 : ticks - 504911232000000000);
collector.AddScalar(dateTimeOffset.Offset.Ticks);
}
}
/// <summary>
/// TraceLogging: Type handler for TimeSpan.
/// </summary>
internal sealed class TimeSpanTypeInfo : TraceLoggingTypeInfo
{
public TimeSpanTypeInfo() : base(typeof(TimeSpan)) { }
public override void WriteMetadata(
TraceLoggingMetadataCollector collector,
string name,
EventFieldFormat format)
{
collector.AddScalar(name, Statics.MakeDataType(TraceLoggingDataType.Int64, format));
}
public override void WriteData(TraceLoggingDataCollector collector, PropertyValue value)
{
collector.AddScalar(value.ScalarValue.AsTimeSpan.Ticks);
}
}
/// <summary>
/// TraceLogging: Type handler for Decimal. (Note: not full-fidelity, exposed as Double.)
/// </summary>
internal sealed class DecimalTypeInfo : TraceLoggingTypeInfo
{
public DecimalTypeInfo() : base(typeof(Decimal)) { }
public override void WriteMetadata(
TraceLoggingMetadataCollector collector,
string name,
EventFieldFormat format)
{
collector.AddScalar(name, Statics.MakeDataType(TraceLoggingDataType.Double, format));
}
public override void WriteData(TraceLoggingDataCollector collector, PropertyValue value)
{
collector.AddScalar((double)value.ScalarValue.AsDecimal);
}
}
/// <summary>
/// TraceLogging: Type handler for Nullable.
/// </summary>
internal sealed class NullableTypeInfo : TraceLoggingTypeInfo
{
private readonly TraceLoggingTypeInfo valueInfo;
private readonly Func<PropertyValue, PropertyValue> hasValueGetter;
private readonly Func<PropertyValue, PropertyValue> valueGetter;
public NullableTypeInfo(Type type, List<Type> recursionCheck)
: base(type)
{
var typeArgs = type.GenericTypeArguments;
Debug.Assert(typeArgs.Length == 1);
this.valueInfo = TraceLoggingTypeInfo.GetInstance(typeArgs[0], recursionCheck);
this.hasValueGetter = PropertyValue.GetPropertyGetter(type.GetTypeInfo().GetDeclaredProperty("HasValue"));
this.valueGetter = PropertyValue.GetPropertyGetter(type.GetTypeInfo().GetDeclaredProperty("Value"));
}
public override void WriteMetadata(
TraceLoggingMetadataCollector collector,
string name,
EventFieldFormat format)
{
var group = collector.AddGroup(name);
group.AddScalar("HasValue", TraceLoggingDataType.Boolean8);
this.valueInfo.WriteMetadata(group, "Value", format);
}
public override void WriteData(TraceLoggingDataCollector collector, PropertyValue value)
{
var hasValue = hasValueGetter(value);
collector.AddScalar(hasValue);
var val = hasValue.ScalarValue.AsBoolean ? valueGetter(value) : valueInfo.PropertyValueFactory(Activator.CreateInstance(valueInfo.DataType));
this.valueInfo.WriteData(collector, val);
}
}
}
|