Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce LogEventLevel boxing #1254

Merged
merged 2 commits into from
Apr 26, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
87 changes: 46 additions & 41 deletions src/Serilog/Formatting/Display/LevelOutputFormat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using Serilog.Events;
using Serilog.Rendering;

Expand All @@ -26,37 +27,41 @@ namespace Serilog.Formatting.Display
static class LevelOutputFormat
{
static readonly string[][] _titleCaseLevelMap = {
new []{ "V", "Vb", "Vrb", "Verb" },
new []{ "D", "De", "Dbg", "Dbug" },
new []{ "I", "In", "Inf", "Info" },
new []{ "W", "Wn", "Wrn", "Warn" },
new []{ "E", "Er", "Err", "Eror" },
new []{ "F", "Fa", "Ftl", "Fatl" }
new []{ "V", "Vb", "Vrb", "Verb", "Verbo", "Verbos", "Verbose" },
new []{ "D", "De", "Dbg", "Dbug", "Debug" },
new []{ "I", "In", "Inf", "Info", "Infor", "Inform", "Informa", "Informat", "Informati", "Informatio", "Information" },
new []{ "W", "Wn", "Wrn", "Warn", "Warni", "Warnin", "Warning" },
new []{ "E", "Er", "Err", "Eror", "Error" },
new []{ "F", "Fa", "Ftl", "Fatl", "Fatal" }
};

static readonly string[][] _lowercaseLevelMap = {
new []{ "v", "vb", "vrb", "verb" },
new []{ "d", "de", "dbg", "dbug" },
new []{ "i", "in", "inf", "info" },
new []{ "w", "wn", "wrn", "warn" },
new []{ "e", "er", "err", "eror" },
new []{ "f", "fa", "ftl", "fatl" }
static readonly string[][] _lowerCaseLevelMap = {
new []{ "v", "vb", "vrb", "verb", "verbo", "verbos", "verbose" },
new []{ "d", "de", "dbg", "dbug", "debug" },
new []{ "i", "in", "inf", "info", "infor", "inform", "informa", "informat", "informati", "informatio", "information" },
new []{ "w", "wn", "wrn", "warn", "warni", "warnin", "warning" },
new []{ "e", "er", "err", "eror", "error" },
new []{ "f", "fa", "ftl", "fatl", "fatal" }
};

static readonly string[][] _uppercaseLevelMap = {
new []{ "V", "VB", "VRB", "VERB" },
new []{ "D", "DE", "DBG", "DBUG" },
new []{ "I", "IN", "INF", "INFO" },
new []{ "W", "WN", "WRN", "WARN" },
new []{ "E", "ER", "ERR", "EROR" },
new []{ "F", "FA", "FTL", "FATL" }
static readonly string[][] _upperCaseLevelMap = {
new []{ "V", "VB", "VRB", "VERB", "VERBO", "VERBOS", "VERBOSE" },
new []{ "D", "DE", "DBG", "DBUG", "DEBUG" },
new []{ "I", "IN", "INF", "INFO", "INFOR", "INFORM", "INFORMA", "INFORMAT", "INFORMATI", "INFORMATIO", "INFORMATION" },
new []{ "W", "WN", "WRN", "WARN", "WARNI", "WARNIN", "WARNING" },
new []{ "E", "ER", "ERR", "EROR", "ERROR" },
new []{ "F", "FA", "FTL", "FATL", "FATAL" }
};

public static string GetLevelMoniker(LogEventLevel value, string format = null)
{
if (format == null || format.Length != 2 && format.Length != 3)
var index = (int)value;
if (index < 0 || index > (int) LogEventLevel.Fatal)
return Casing.Format(value.ToString(), format);

if (format == null || format.Length != 2 && format.Length != 3)
return Casing.Format(GetLevelMoniker(_titleCaseLevelMap, index), format);

// Using int.Parse() here requires allocating a string to exclude the first character prefix.
// Junk like "wxy" will be accepted but produce benign results.
var width = format[1] - '0';
Expand All @@ -69,29 +74,29 @@ public static string GetLevelMoniker(LogEventLevel value, string format = null)
if (width < 1)
return string.Empty;

if (width > 4)
switch (format[0])
{
var stringValue = value.ToString();
if (stringValue.Length > width)
stringValue = stringValue.Substring(0, width);
return Casing.Format(stringValue);
case 'w':
return GetLevelMoniker(_lowerCaseLevelMap, index, width);
case 'u':
return GetLevelMoniker(_upperCaseLevelMap, index, width);
case 't':
return GetLevelMoniker(_titleCaseLevelMap, index, width);
}

var index = (int)value;
if (index >= 0 && index <= (int) LogEventLevel.Fatal)
{
switch (format[0])
{
case 'w':
return _lowercaseLevelMap[index][width - 1];
case 'u':
return _uppercaseLevelMap[index][width - 1];
case 't':
return _titleCaseLevelMap[index][width - 1];
}
}
return Casing.Format(GetLevelMoniker(_titleCaseLevelMap, index), format);
}

static string GetLevelMoniker(string[][] caseLevelMap, int index, int width)
{
var caseLevel = caseLevelMap[index];
return caseLevel[Math.Min(width, caseLevel.Length) - 1];
}

return Casing.Format(value.ToString(), format);
static string GetLevelMoniker(string[][] caseLevelMap, int index)
{
var caseLevel = caseLevelMap[index];
return caseLevel[caseLevel.Length - 1];
}
}
}
}