Skip to content

Commit

Permalink
Cache DateTimeFormatInfo to reduce allocations in every Logger.Log call
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownShadow200 committed Oct 22, 2017
1 parent 4371ae1 commit b956ede
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions fCraft/System/Logger.cs
Expand Up @@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
Expand Down Expand Up @@ -32,6 +33,7 @@ public static class Logger {

static readonly string SessionStart = DateTime.Now.ToString( LongDateFormat ); // localized
static readonly Queue<string> RecentMessages = new Queue<string>();
static DateTimeFormatInfo formatter = CultureInfo.InvariantCulture.DateTimeFormat;
const int MaxRecentMessages = 25;

/// <summary> Name of the file that log messages are currently being written to.
Expand All @@ -42,7 +44,7 @@ public static class Logger {
case LogSplittingType.SplitBySession:
return SessionStart + ".log";
case LogSplittingType.SplitByDay:
return DateTime.Now.ToString( ShortDateFormat ) + ".log"; // localized
return DateTime.Now.ToString( ShortDateFormat, formatter ) + ".log"; // localized
default:
return DefaultLogFileName;
}
Expand Down Expand Up @@ -116,7 +118,7 @@ public static class Logger {
if( !Enabled ) return;

message = Color.StripColors( message, true );
string line = DateTime.Now.ToString( TimeFormat ) + " > " + GetPrefix( type ) + message; // localized
string line = DateTime.Now.ToString( TimeFormat, formatter ) + " > " + GetPrefix( type ) + message; // localized

lock( LogLock ) {
RaiseLoggedEvent( message, line, type );
Expand All @@ -133,7 +135,7 @@ public static class Logger {
} catch( Exception ex ) {
string errorMessage = "Logger.Log: " + ex;
line = String.Format( "{0} > {1}{2}",
DateTime.Now.ToString( TimeFormat ),// localized
DateTime.Now.ToString( TimeFormat, formatter ),// localized
GetPrefix( LogType.Error ),
errorMessage );
RaiseLoggedEvent( errorMessage,
Expand Down

0 comments on commit b956ede

Please sign in to comment.