summaryrefslogtreecommitdiff
path: root/src/mscorlib/shared/System/Diagnostics/Tracing/TraceLogging/FieldMetadata.cs
blob: 309226b84d96e240e962986a7505036e5a7f056f (plain)
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
// 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 Encoding = System.Text.Encoding;

#if ES_BUILD_STANDALONE
using Environment = Microsoft.Diagnostics.Tracing.Internal.Environment;
namespace Microsoft.Diagnostics.Tracing
#else
namespace System.Diagnostics.Tracing
#endif
{
    /// <summary>
    /// TraceLogging: Contains the information needed to generate tracelogging
    /// metadata for an event field.
    /// </summary>
    internal class FieldMetadata
    {
        /// <summary>
        /// Name of the field
        /// </summary>
        private readonly string name;

        /// <summary>
        /// The number of bytes in the UTF8 Encoding of 'name' INCLUDING a null terminator.  
        /// </summary>
        private readonly int nameSize;
        private readonly EventFieldTags tags;
        private readonly byte[] custom;

        /// <summary>
        /// ETW supports fixed sized arrays. If inType has the InTypeFixedCountFlag then this is the
        /// statically known count for the array. It is also used to encode the number of bytes of
        /// custom meta-data if InTypeCustomCountFlag set.
        /// </summary>
        private readonly ushort fixedCount;

        private byte inType;
        private byte outType;

        /// <summary>
        /// Scalar or variable-length array.
        /// </summary>
        public FieldMetadata(
            string name,
            TraceLoggingDataType type,
            EventFieldTags tags,
            bool variableCount)
            : this(
                name,
                type,
                tags,
                variableCount ? Statics.InTypeVariableCountFlag : (byte)0,
                0,
                null)
        {
            return;
        }

        /// <summary>
        /// Fixed-length array.
        /// </summary>
        public FieldMetadata(
            string name,
            TraceLoggingDataType type,
            EventFieldTags tags,
            ushort fixedCount)
            : this(
                name,
                type,
                tags,
                Statics.InTypeFixedCountFlag,
                fixedCount,
                null)
        {
            return;
        }

        /// <summary>
        /// Custom serializer
        /// </summary>
        public FieldMetadata(
            string name,
            TraceLoggingDataType type,
            EventFieldTags tags,
            byte[] custom)
            : this(
                name,
                type,
                tags,
                Statics.InTypeCustomCountFlag,
                checked((ushort)(custom == null ? 0 : custom.Length)),
                custom)
        {
            return;
        }

        private FieldMetadata(
            string name,
            TraceLoggingDataType dataType,
            EventFieldTags tags,
            byte countFlags,
            ushort fixedCount = 0,
            byte[] custom = null)
        {
            if (name == null)
            {
                throw new ArgumentNullException(
                    nameof(name),
                    "This usually means that the object passed to Write is of a type that"
                    + " does not support being used as the top-level object in an event,"
                    + " e.g. a primitive or built-in type.");
            }

            Statics.CheckName(name);
            var coreType = (int)dataType & Statics.InTypeMask;
            this.name = name;
            this.nameSize = Encoding.UTF8.GetByteCount(this.name) + 1;
            this.inType = (byte)(coreType | countFlags);
            this.outType = (byte)(((int)dataType >> 8) & Statics.OutTypeMask);
            this.tags = tags;
            this.fixedCount = fixedCount;
            this.custom = custom;

            if (countFlags != 0)
            {
                if (coreType == (int)TraceLoggingDataType.Nil)
                {
                    throw new NotSupportedException(Resources.GetResourceString("EventSource_NotSupportedArrayOfNil"));
                }
                if (coreType == (int)TraceLoggingDataType.Binary)
                {
                    throw new NotSupportedException(Resources.GetResourceString("EventSource_NotSupportedArrayOfBinary"));
                }
#if !BROKEN_UNTIL_M3
                if (coreType == (int)TraceLoggingDataType.Utf16String ||
                    coreType == (int)TraceLoggingDataType.MbcsString)
                {
                    throw new NotSupportedException(Resources.GetResourceString("EventSource_NotSupportedArrayOfNullTerminatedString"));
                }
#endif
            }

            if (((int)this.tags & 0xfffffff) != 0)
            {
                this.outType |= Statics.OutTypeChainFlag;
            }

            if (this.outType != 0)
            {
                this.inType |= Statics.InTypeChainFlag;
            }
        }

        public void IncrementStructFieldCount()
        {
            this.inType |= Statics.InTypeChainFlag;
            this.outType++;
            if ((this.outType & Statics.OutTypeMask) == 0)
            {
                throw new NotSupportedException(Resources.GetResourceString("EventSource_TooManyFields"));
            }
        }

        /// <summary>
        /// This is the main routine for FieldMetaData.  Basically it will serialize the data in
        /// this structure as TraceLogging style meta-data into the array 'metaArray' starting at
        /// 'pos' (pos is updated to reflect the bytes written).  
        /// 
        /// Note that 'metaData' can be null, in which case it only updates 'pos'.  This is useful
        /// for a 'two pass' approach where you figure out how big to make the array, and then you
        /// fill it in.   
        /// </summary>
        public void Encode(ref int pos, byte[] metadata)
        {
            // Write out the null terminated UTF8 encoded name
            if (metadata != null)
            {
                Encoding.UTF8.GetBytes(this.name, 0, this.name.Length, metadata, pos);
            }
            pos += this.nameSize;

            // Write 1 byte for inType
            if (metadata != null)
            {
                metadata[pos] = this.inType;
            }
            pos += 1;

            // If InTypeChainFlag set, then write out the outType
            if (0 != (this.inType & Statics.InTypeChainFlag))
            {
                if (metadata != null)
                {
                    metadata[pos] = this.outType;
                }
                pos += 1;

                // If OutTypeChainFlag set, then write out tags
                if (0 != (this.outType & Statics.OutTypeChainFlag))
                {
                    Statics.EncodeTags((int)this.tags, ref pos, metadata);
                }
            }

            // If InTypeFixedCountFlag set, write out the fixedCount (2 bytes little endian)
            if (0 != (this.inType & Statics.InTypeFixedCountFlag))
            {
                if (metadata != null)
                {
                    metadata[pos + 0] = unchecked((byte)this.fixedCount);
                    metadata[pos + 1] = (byte)(this.fixedCount >> 8);
                }
                pos += 2;

                // If InTypeCustomCountFlag set, write out the blob of custom meta-data.  
                if (Statics.InTypeCustomCountFlag == (this.inType & Statics.InTypeCountMask) &&
                    this.fixedCount != 0)
                {
                    if (metadata != null)
                    {
                        Buffer.BlockCopy(this.custom, 0, metadata, pos, this.fixedCount);
                    }
                    pos += this.fixedCount;
                }
            }
        }
    }
}