Skip to content

Commit

Permalink
Annotate System.Diagnostics.TextWriterTraceListener for nullable refe…
Browse files Browse the repository at this point in the history
…rence types (#37267)
  • Loading branch information
stephentoub authored Jun 3, 2020
1 parent 06adea8 commit b40bd47
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,50 +16,50 @@ public override void Close() { }
public partial class DelimitedListTraceListener : System.Diagnostics.TextWriterTraceListener
{
public DelimitedListTraceListener(System.IO.Stream stream) { }
public DelimitedListTraceListener(System.IO.Stream stream, string name) { }
public DelimitedListTraceListener(System.IO.Stream stream, string? name) { }
public DelimitedListTraceListener(System.IO.TextWriter writer) { }
public DelimitedListTraceListener(System.IO.TextWriter writer, string name) { }
public DelimitedListTraceListener(string fileName) { }
public DelimitedListTraceListener(string fileName, string name) { }
public DelimitedListTraceListener(System.IO.TextWriter writer, string? name) { }
public DelimitedListTraceListener(string? fileName) { }
public DelimitedListTraceListener(string? fileName, string? name) { }
public string Delimiter { get { throw null; } set { } }
protected override string[] GetSupportedAttributes() { throw null; }
public override void TraceData(System.Diagnostics.TraceEventCache eventCache, string source, System.Diagnostics.TraceEventType eventType, int id, object data) { }
public override void TraceData(System.Diagnostics.TraceEventCache eventCache, string source, System.Diagnostics.TraceEventType eventType, int id, params object[] data) { }
public override void TraceEvent(System.Diagnostics.TraceEventCache eventCache, string source, System.Diagnostics.TraceEventType eventType, int id, string message) { }
public override void TraceEvent(System.Diagnostics.TraceEventCache eventCache, string source, System.Diagnostics.TraceEventType eventType, int id, string format, params object[] args) { }
public override void TraceData(System.Diagnostics.TraceEventCache? eventCache, string source, System.Diagnostics.TraceEventType eventType, int id, object? data) { }
public override void TraceData(System.Diagnostics.TraceEventCache? eventCache, string source, System.Diagnostics.TraceEventType eventType, int id, params object?[]? data) { }
public override void TraceEvent(System.Diagnostics.TraceEventCache? eventCache, string source, System.Diagnostics.TraceEventType eventType, int id, string? message) { }
public override void TraceEvent(System.Diagnostics.TraceEventCache? eventCache, string source, System.Diagnostics.TraceEventType eventType, int id, string format, params object?[]? args) { }
}
public partial class TextWriterTraceListener : System.Diagnostics.TraceListener
{
public TextWriterTraceListener() { }
public TextWriterTraceListener(System.IO.Stream stream) { }
public TextWriterTraceListener(System.IO.Stream stream, string name) { }
public TextWriterTraceListener(System.IO.Stream stream, string? name) { }
public TextWriterTraceListener(System.IO.TextWriter writer) { }
public TextWriterTraceListener(System.IO.TextWriter writer, string name) { }
public TextWriterTraceListener(string fileName) { }
public TextWriterTraceListener(string fileName, string name) { }
public System.IO.TextWriter Writer { get { throw null; } set { } }
public TextWriterTraceListener(System.IO.TextWriter writer, string? name) { }
public TextWriterTraceListener(string? fileName) { }
public TextWriterTraceListener(string? fileName, string? name) { }
public System.IO.TextWriter? Writer { get { throw null; } set { } }
public override void Close() { }
protected override void Dispose(bool disposing) { }
public override void Flush() { }
public override void Write(string message) { }
public override void WriteLine(string message) { }
public override void Write(string? message) { }
public override void WriteLine(string? message) { }
}
public partial class XmlWriterTraceListener : System.Diagnostics.TextWriterTraceListener
{
public XmlWriterTraceListener(System.IO.Stream stream) { }
public XmlWriterTraceListener(System.IO.Stream stream, string name) { }
public XmlWriterTraceListener(System.IO.Stream stream, string? name) { }
public XmlWriterTraceListener(System.IO.TextWriter writer) { }
public XmlWriterTraceListener(System.IO.TextWriter writer, string name) { }
public XmlWriterTraceListener(string filename) { }
public XmlWriterTraceListener(string filename, string name) { }
public XmlWriterTraceListener(System.IO.TextWriter writer, string? name) { }
public XmlWriterTraceListener(string? filename) { }
public XmlWriterTraceListener(string? filename, string? name) { }
public override void Close() { }
public override void Fail(string message, string detailMessage) { }
public override void TraceData(System.Diagnostics.TraceEventCache eventCache, string source, System.Diagnostics.TraceEventType eventType, int id, object data) { }
public override void TraceData(System.Diagnostics.TraceEventCache eventCache, string source, System.Diagnostics.TraceEventType eventType, int id, params object[] data) { }
public override void TraceEvent(System.Diagnostics.TraceEventCache eventCache, string source, System.Diagnostics.TraceEventType eventType, int id, string message) { }
public override void TraceEvent(System.Diagnostics.TraceEventCache eventCache, string source, System.Diagnostics.TraceEventType eventType, int id, string format, params object[] args) { }
public override void TraceTransfer(System.Diagnostics.TraceEventCache eventCache, string source, int id, string message, System.Guid relatedActivityId) { }
public override void Write(string message) { }
public override void WriteLine(string message) { }
public override void Fail(string? message, string? detailMessage) { }
public override void TraceData(System.Diagnostics.TraceEventCache? eventCache, string source, System.Diagnostics.TraceEventType eventType, int id, object? data) { }
public override void TraceData(System.Diagnostics.TraceEventCache? eventCache, string source, System.Diagnostics.TraceEventType eventType, int id, params object?[]? data) { }
public override void TraceEvent(System.Diagnostics.TraceEventCache? eventCache, string source, System.Diagnostics.TraceEventType eventType, int id, string? message) { }
public override void TraceEvent(System.Diagnostics.TraceEventCache? eventCache, string source, System.Diagnostics.TraceEventType eventType, int id, string format, params object?[]? args) { }
public override void TraceTransfer(System.Diagnostics.TraceEventCache? eventCache, string source, int id, string? message, System.Guid relatedActivityId) { }
public override void Write(string? message) { }
public override void WriteLine(string? message) { }
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>$(NetCoreAppCurrent)</TargetFrameworks>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Compile Include="System.Diagnostics.TextWriterTraceListener.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<RootNamespace>System.Diagnostics.TextWriterTraceListener</RootNamespace>
<AssemblyName>System.Diagnostics.TextWriterTraceListener</AssemblyName>
<TargetFrameworks>$(NetCoreAppCurrent)-Windows_NT;$(NetCoreAppCurrent)-Unix</TargetFrameworks>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Compile Include="System\Diagnostics\ConsoleTraceListener.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// 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.Text;
using System.Globalization;
using System.IO;
Expand All @@ -12,6 +11,8 @@ namespace System.Diagnostics
{
public class DelimitedListTraceListener : TextWriterTraceListener
{
private const string DelimiterKey = "delimiter";

private string _delimiter = ";";
private string _secondaryDelim = ",";
private bool _initializedDelim;
Expand All @@ -20,23 +21,23 @@ public DelimitedListTraceListener(Stream stream) : base(stream)
{
}

public DelimitedListTraceListener(Stream stream, string name) : base(stream, name)
public DelimitedListTraceListener(Stream stream, string? name) : base(stream, name)
{
}

public DelimitedListTraceListener(TextWriter writer) : base(writer)
{
}

public DelimitedListTraceListener(TextWriter writer, string name) : base(writer, name)
public DelimitedListTraceListener(TextWriter writer, string? name) : base(writer, name)
{
}

public DelimitedListTraceListener(string fileName) : base(fileName)
{
}

public DelimitedListTraceListener(string fileName, string name) : base(fileName, name)
public DelimitedListTraceListener(string fileName, string? name) : base(fileName, name)
{
}

Expand All @@ -48,9 +49,13 @@ public string Delimiter
{
if (!_initializedDelim)
{
if (Attributes.ContainsKey("delimiter"))
if (Attributes.ContainsKey(DelimiterKey))
{
_delimiter = Attributes["delimiter"];
string? delimiter = Attributes[DelimiterKey];
if (!string.IsNullOrEmpty(delimiter))
{
_delimiter = delimiter;
}
}
_initializedDelim = true;
}
Expand Down Expand Up @@ -80,9 +85,9 @@ public string Delimiter

// base class method is protected internal but since its base class is in another assembly can't override it as protected internal because a CS0507
// warning would be hitted.
protected override string[] GetSupportedAttributes() => new string[] { "delimiter" };
protected override string[] GetSupportedAttributes() => new string[] { DelimiterKey };

public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id, string format, params object[] args)
public override void TraceEvent(TraceEventCache? eventCache, string source, TraceEventType eventType, int id, string format, params object?[]? args)
{
if (Filter != null && !Filter.ShouldTrace(eventCache, source, eventType, id, format, args, null, null))
return;
Expand All @@ -101,7 +106,7 @@ public override void TraceEvent(TraceEventCache eventCache, string source, Trace
WriteFooter(eventCache);
}

public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id, string message)
public override void TraceEvent(TraceEventCache? eventCache, string source, TraceEventType eventType, int id, string? message)
{
if (Filter != null && !Filter.ShouldTrace(eventCache, source, eventType, id, message, null, null, null))
return;
Expand All @@ -117,7 +122,7 @@ public override void TraceEvent(TraceEventCache eventCache, string source, Trace
WriteFooter(eventCache);
}

public override void TraceData(TraceEventCache eventCache, string source, TraceEventType eventType, int id, object data)
public override void TraceData(TraceEventCache? eventCache, string source, TraceEventType eventType, int id, object? data)
{
if (Filter != null && !Filter.ShouldTrace(eventCache, source, eventType, id, null, null, data, null))
return;
Expand All @@ -127,13 +132,13 @@ public override void TraceData(TraceEventCache eventCache, string source, TraceE
// first a delimiter for the message
Write(Delimiter); // Use get_Delimiter

WriteEscaped(data.ToString());
WriteEscaped(data?.ToString());
Write(Delimiter); // Use get_Delimiter

WriteFooter(eventCache);
}

public override void TraceData(TraceEventCache eventCache, string source, TraceEventType eventType, int id, params object[] data)
public override void TraceData(TraceEventCache? eventCache, string source, TraceEventType eventType, int id, params object?[]? data)
{
if (Filter != null && !Filter.ShouldTrace(eventCache, source, eventType, id, null, null, null, data))
return;
Expand All @@ -149,7 +154,7 @@ public override void TraceData(TraceEventCache eventCache, string source, TraceE
{
if (i != 0)
Write(_secondaryDelim);
WriteEscaped(data[i].ToString());
WriteEscaped(data[i]?.ToString());
}
}
Write(Delimiter); // Use get_Delimiter
Expand All @@ -169,7 +174,7 @@ private void WriteHeader(string source, TraceEventType eventType, int id)
Write(Delimiter); // Use get_Delimiter
}

private void WriteFooter(TraceEventCache eventCache)
private void WriteFooter(TraceEventCache? eventCache)
{
if (eventCache != null)
{
Expand Down Expand Up @@ -205,7 +210,7 @@ private void WriteFooter(TraceEventCache eventCache)
WriteLine("");
}

private void WriteEscaped(string message)
private void WriteEscaped(string? message)
{
if (!string.IsNullOrEmpty(message))
{
Expand All @@ -220,7 +225,7 @@ private void WriteStackEscaped(Stack stack)
{
StringBuilder sb = new StringBuilder("\"");
bool first = true;
foreach (object obj in stack)
foreach (object? obj in stack)
{
if (!first)
{
Expand All @@ -231,26 +236,29 @@ private void WriteStackEscaped(Stack stack)
first = false;
}

string operation = obj.ToString();
string? operation = obj?.ToString();
EscapeMessage(operation, sb);
}

sb.Append('"');
Write(sb.ToString());
}

private void EscapeMessage(string message, StringBuilder sb)
private void EscapeMessage(string? message, StringBuilder sb)
{
int index;
int lastindex = 0;
while ((index = message.IndexOf('"', lastindex)) != -1)
if (!string.IsNullOrEmpty(message))
{
sb.Append(message, lastindex, index - lastindex);
sb.Append("\"\"");
lastindex = index + 1;
}
int index;
int lastindex = 0;
while ((index = message.IndexOf('"', lastindex)) != -1)
{
sb.Append(message, lastindex, index - lastindex);
sb.Append("\"\"");
lastindex = index + 1;
}

sb.Append(message, lastindex, message.Length - lastindex);
sb.Append(message, lastindex, message.Length - lastindex);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
// 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.IO;
using System.Text;
using System.Runtime.InteropServices;

namespace System.Diagnostics
{
Expand All @@ -16,8 +14,8 @@ namespace System.Diagnostics
/// </devdoc>
public class TextWriterTraceListener : TraceListener
{
internal TextWriter _writer;
private string _fileName;
internal TextWriter? _writer;
private string? _fileName;

/// <devdoc>
/// <para>Initializes a new instance of the <see cref='System.Diagnostics.TextWriterTraceListener'/> class with
Expand All @@ -41,7 +39,7 @@ public TextWriterTraceListener(Stream stream)
/// <para>Initializes a new instance of the <see cref='System.Diagnostics.TextWriterTraceListener'/> class with the
/// specified name and using the stream as the recipient of the debugging and tracing output.</para>
/// </devdoc>
public TextWriterTraceListener(Stream stream, string name)
public TextWriterTraceListener(Stream stream, string? name)
: base(name)
{
if (stream == null) throw new ArgumentNullException(nameof(stream));
Expand All @@ -63,7 +61,7 @@ public TextWriterTraceListener(TextWriter writer)
/// debugging
/// output.</para>
/// </devdoc>
public TextWriterTraceListener(TextWriter writer, string name)
public TextWriterTraceListener(TextWriter writer, string? name)
: base(name)
{
if (writer == null) throw new ArgumentNullException(nameof(writer));
Expand All @@ -74,7 +72,7 @@ public TextWriterTraceListener(TextWriter writer, string name)
/// <para>Initializes a new instance of the <see cref='System.Diagnostics.TextWriterTraceListener'/> class with the
/// specified file name.</para>
/// </devdoc>
public TextWriterTraceListener(string fileName)
public TextWriterTraceListener(string? fileName)
{
_fileName = fileName;
}
Expand All @@ -83,7 +81,7 @@ public TextWriterTraceListener(string fileName)
/// <para>Initializes a new instance of the <see cref='System.Diagnostics.TextWriterTraceListener'/> class with the
/// specified name and the specified file name.</para>
/// </devdoc>
public TextWriterTraceListener(string fileName, string name)
public TextWriterTraceListener(string? fileName, string? name)
: base(name)
{
_fileName = fileName;
Expand All @@ -93,7 +91,7 @@ public TextWriterTraceListener(string fileName, string name)
/// <para> Indicates the text writer that receives the tracing
/// or debugging output.</para>
/// </devdoc>
public TextWriter Writer
public TextWriter? Writer
{
get
{
Expand Down Expand Up @@ -163,7 +161,7 @@ public override void Flush()
/// <para>Writes a message
/// to this instance's <see cref='System.Diagnostics.TextWriterTraceListener.Writer'/>.</para>
/// </devdoc>
public override void Write(string message)
public override void Write(string? message)
{
EnsureWriter();
if (_writer != null)
Expand All @@ -182,7 +180,7 @@ public override void Write(string message)
/// to this instance's <see cref='System.Diagnostics.TextWriterTraceListener.Writer'/> followed by a line terminator. The
/// default line terminator is a carriage return followed by a line feed (\r\n).</para>
/// </devdoc>
public override void WriteLine(string message)
public override void WriteLine(string? message)
{
EnsureWriter();
if (_writer != null)
Expand All @@ -209,11 +207,9 @@ private static Encoding GetEncodingWithFallback(Encoding encoding)

internal void EnsureWriter()
{
bool success = true;

if (_writer == null)
{
success = false;
bool success = false;

if (_fileName == null)
return;
Expand All @@ -232,7 +228,7 @@ internal void EnsureWriter()
// IO errors, we will prefix the file name with a unique GUID value
// and try one more time
string fullPath = Path.GetFullPath(_fileName);
string dirPath = Path.GetDirectoryName(fullPath);
string dirPath = Path.GetDirectoryName(fullPath)!;
string fileNameOnly = Path.GetFileName(fullPath);

for (int i = 0; i < 2; i++)
Expand Down
Loading

0 comments on commit b40bd47

Please sign in to comment.