IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
GenericChannelDiagnosticFormatterFactory.cs
Go to the documentation of this file.
1using System;
2using System.Linq;
3
5{
6
13 where TFormatter : IDiagnosticFormatter
14 where TOptions : DiagnosticChannelFormatterOptions, new()
15 {
16
17 static readonly char[] SEPARATOR = ['='];
18
19 readonly DiagnosticChannelProvider _channels;
20 readonly string _format;
21 readonly string _formatPrefix;
22
29 {
30 _channels = channels ?? throw new ArgumentNullException(nameof(channels));
31 _format = format ?? throw new ArgumentNullException(nameof(format));
32 _formatPrefix = $"{_format},";
33 }
34
37 {
38 if (spec != _format && spec.StartsWith(_formatPrefix) == false)
39 return null;
40
41 // strip format
42 var specArray = spec.Split(',');
43 if (specArray.Length == 0 || specArray[0] != _format)
44 return null;
45
46 // split and parse option information
47 var opts = new TOptions();
48 foreach (var optionSpec in specArray.Skip(1))
49 ParseOption(opts, optionSpec);
50
51 // set default channel if missing
52 opts.TraceChannel ??= GetOrCreateChannel(FileDiagnosticChannelFactory.STDOUT);
53 opts.InfoChannel ??= GetOrCreateChannel(FileDiagnosticChannelFactory.STDOUT);
54 opts.WarningChannel ??= GetOrCreateChannel(FileDiagnosticChannelFactory.STDERR);
55 opts.ErrorChannel ??= GetOrCreateChannel(FileDiagnosticChannelFactory.STDERR);
56 opts.FatalChannel ??= GetOrCreateChannel(FileDiagnosticChannelFactory.STDERR);
57
58 // process options and create formatter
59 return CreateFormatter(opts);
60 }
61
67 protected abstract TFormatter CreateFormatter(TOptions options);
68
74 void ParseOption(TOptions options, string spec)
75 {
76 var a = spec.Split(SEPARATOR, 2, StringSplitOptions.None);
77 var key = a.Length >= 1 ? a[0] : "";
78 var val = a.Length >= 2 ? a[1] : "";
79
80 if (string.IsNullOrWhiteSpace(key))
81 return;
82
83 if (key is "file" or "trace:file")
84 options.TraceChannel = GetOrCreateChannel(val);
85 if (key is "file" or "info:file")
86 options.InfoChannel = GetOrCreateChannel(val);
87 if (key is "file" or "warning:file")
88 options.WarningChannel = GetOrCreateChannel(val);
89 if (key is "file" or "error:file")
90 options.ErrorChannel = GetOrCreateChannel(val);
91 if (key is "file" or "fatal:file")
92 options.FatalChannel = GetOrCreateChannel(val);
93 }
94
102 IDiagnosticChannel GetOrCreateChannel(string spec)
103 {
104 return _channels.GetOrCreateChannel(spec) ?? throw new InvalidOperationException($"Cannot find channel for specification '{spec}'.");
105 }
106
107 }
108
109}
Provides the capability to look up diagnostic channels.
Generate channel factory for a formatter that accepts the standard set of chanel options.
TFormatter CreateFormatter(TOptions options)
Creates the TFormatter instance with the specified options.
GenericChannelDiagnosticFormatterFactory(DiagnosticChannelProvider channels, string format)
Initializes a new instance.
IDiagnosticFormatter? GetFormatter(string spec)
Returns a IDiagnosticFormatter if the given spec can be handled by this factory.
Handles consuming and outputting diagnostic events.