Browse Source

Switched all uses of DateTime.Now to a slightly safer version

pull/15/head
Anairkoen Schno 5 years ago
parent
commit
9680ef6804
7 changed files with 54 additions and 12 deletions
  1. +3
    -2
      IPA.Injector/Backups/BackupUnit.cs
  2. +1
    -1
      IPA.Loader/Config/Config.cs
  3. +3
    -2
      IPA.Loader/Logging/Printers/GlobalLogFilePrinter.cs
  4. +3
    -2
      IPA.Loader/Logging/Printers/PluginLogFilePrinter.cs
  5. +2
    -1
      IPA.Loader/Logging/Printers/PluginSubLogPrinter.cs
  6. +5
    -4
      IPA.Loader/Logging/StandardLogger.cs
  7. +37
    -0
      IPA.Loader/Utilities/Utils.cs

+ 3
- 2
IPA.Injector/Backups/BackupUnit.cs View File

@ -1,4 +1,5 @@
using System;
using IPA.Utilities;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
@ -16,7 +17,7 @@ namespace IPA.Injector.Backups
private readonly FileInfo _manifestFile; private readonly FileInfo _manifestFile;
private const string ManifestFileName = "$manifest$.txt"; private const string ManifestFileName = "$manifest$.txt";
public BackupUnit(string dir) : this(dir, DateTime.Now.ToString("yyyy-MM-dd_h-mm-ss"))
public BackupUnit(string dir) : this(dir, Utils.CurrentTime().ToString("yyyy-MM-dd_h-mm-ss"))
{ {
} }


+ 1
- 1
IPA.Loader/Config/Config.cs View File

@ -212,7 +212,7 @@ namespace IPA.Config
try try
{ {
provider.Item2.Save(); provider.Item2.Save();
provider.Item1.Value = DateTime.Now;
provider.Item1.Value = Utils.CurrentTime();
} }
catch (Exception e) catch (Exception e)
{ {


+ 3
- 2
IPA.Loader/Logging/Printers/GlobalLogFilePrinter.cs View File

@ -1,4 +1,5 @@
using System;
using IPA.Utilities;
using System;
using System.IO; using System.IO;
namespace IPA.Logging.Printers namespace IPA.Logging.Printers
@ -35,7 +36,7 @@ namespace IPA.Logging.Printers
{ {
var logsDir = new DirectoryInfo("Logs"); var logsDir = new DirectoryInfo("Logs");
logsDir.Create(); logsDir.Create();
var finfo = new FileInfo(Path.Combine(logsDir.FullName, $"{DateTime.Now:yyyy.MM.dd.HH.mm.ss}.log"));
var finfo = new FileInfo(Path.Combine(logsDir.FullName, $"{Utils.CurrentTime():yyyy.MM.dd.HH.mm.ss}.log"));
return finfo; return finfo;
} }
} }

+ 3
- 2
IPA.Loader/Logging/Printers/PluginLogFilePrinter.cs View File

@ -1,4 +1,5 @@
using System;
using IPA.Utilities;
using System;
using System.IO; using System.IO;
namespace IPA.Logging.Printers namespace IPA.Logging.Printers
@ -24,7 +25,7 @@ namespace IPA.Logging.Printers
{ {
var logsDir = new DirectoryInfo(Path.Combine("Logs", name)); var logsDir = new DirectoryInfo(Path.Combine("Logs", name));
logsDir.Create(); logsDir.Create();
var finfo = new FileInfo(Path.Combine(logsDir.FullName, $"{DateTime.Now:yyyy.MM.dd.HH.mm.ss}.log"));
var finfo = new FileInfo(Path.Combine(logsDir.FullName, $"{Utils.CurrentTime():yyyy.MM.dd.HH.mm.ss}.log"));
return finfo; return finfo;
} }


+ 2
- 1
IPA.Loader/Logging/Printers/PluginSubLogPrinter.cs View File

@ -1,5 +1,6 @@
using System; using System;
using System.IO; using System.IO;
using IPA.Utilities;
namespace IPA.Logging.Printers namespace IPA.Logging.Printers
{ {
@ -25,7 +26,7 @@ namespace IPA.Logging.Printers
{ {
var logsDir = new DirectoryInfo(Path.Combine("Logs", mainName, name)); var logsDir = new DirectoryInfo(Path.Combine("Logs", mainName, name));
logsDir.Create(); logsDir.Create();
var finfo = new FileInfo(Path.Combine(logsDir.FullName, $"{DateTime.Now:yyyy.MM.dd.HH.mm.ss}.log"));
var finfo = new FileInfo(Path.Combine(logsDir.FullName, $"{Utils.CurrentTime():yyyy.MM.dd.HH.mm.ss}.log"));
return finfo; return finfo;
} }


+ 5
- 4
IPA.Loader/Logging/StandardLogger.cs View File

@ -1,5 +1,6 @@
using IPA.Config; using IPA.Config;
using IPA.Logging.Printers; using IPA.Logging.Printers;
using IPA.Utilities;
using System; using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
@ -194,7 +195,7 @@ namespace IPA.Logging
Level = level, Level = level,
Message = message, Message = message,
Logger = this, Logger = this,
Time = DateTime.Now
Time = Utils.CurrentTime()
}); });
} }
@ -285,7 +286,7 @@ namespace IPA.Logging
} }
// update last use time and print // update last use time and print
printer.LastUse = DateTime.Now;
printer.LastUse = Utils.CurrentTime();
printer.Print(msg.Level, msg.Time, msg.Logger.logName, msg.Message); printer.Print(msg.Level, msg.Time, msg.Logger.logName, msg.Message);
} }
} }
@ -328,14 +329,14 @@ namespace IPA.Logging
Level = Level.Warning, Level = Level.Warning,
Logger = loggerLogger, Logger = loggerLogger,
Message = $"{loggerLogger.logName.ToUpper()}: Messages omitted to improve performance", Message = $"{loggerLogger.logName.ToUpper()}: Messages omitted to improve performance",
Time = DateTime.Now
Time = Utils.CurrentTime()
}); });
// resume log calls // resume log calls
logWaitEvent.Set(); logWaitEvent.Set();
} }
var now = DateTime.Now;
var now = Utils.CurrentTime();
var copy = new List<LogPrinter>(started); var copy = new List<LogPrinter>(started);
foreach (var printer in copy) foreach (var printer in copy)
{ {


+ 37
- 0
IPA.Loader/Utilities/Utils.cs View File

@ -129,5 +129,42 @@ namespace IPA.Utilities
CopyAll(diSourceSubDir, nextTargetSubDir, appendFileName, onCopyException); CopyAll(diSourceSubDir, nextTargetSubDir, appendFileName, onCopyException);
} }
} }
/// <summary>
/// Whether you can safely use <see cref="DateTime.Now"/> without Mono throwing a fit.
/// </summary>
/// <value><see langword="true"/> if you can use <see cref="DateTime.Now"/> safely, <see langword="false"/> otherwise</value>
public static bool CanUseDateTimeNowSafely { get; private set; } = true;
private static bool DateTimeSafetyUnknown = true;
private static ulong UnsafeAdvanceTicks = 1;
/// <summary>
/// Gets the current <see cref="DateTime"/> if supported, otherwise, if Mono would throw a fit,
/// returns <see cref="DateTime.MinValue"/> plus some value, such that each time it is called
/// the value will be greater than the previous result. Not suitable for timing.
/// </summary>
/// <returns>the current <see cref="DateTime"/> if supported, otherwise some indeterminant increasing value.</returns>
public static DateTime CurrentTime()
{
if (DateTimeSafetyUnknown)
{
DateTime time = DateTime.MinValue;
try
{
time = DateTime.Now;
}
catch (TimeZoneNotFoundException)
{ // Mono did a fucky wucky and we need to avoid this call
CanUseDateTimeNowSafely = false;
}
DateTimeSafetyUnknown = false;
return time;
}
else
{
if (CanUseDateTimeNowSafely) return DateTime.Now;
else return DateTime.MinValue.AddTicks((long)UnsafeAdvanceTicks++); // return MinValue as a fallback
}
}
} }
} }

Loading…
Cancel
Save