Skip to content

Commit

Permalink
fix: il2cpp boot time on PS4 (#1062)
Browse files Browse the repository at this point in the history
  • Loading branch information
bruno-garcia authored Jun 16, 2021
1 parent 5601155 commit e3bde53
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 33 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## Unreleased

### Fixes

- Handle error thrown while trying to get `BootTime` on PS4 with IL2CPP ([#1062](https://github.com/getsentry/sentry-dotnet/pull/1062))


### Changes

- Use SentryId for ISession.Id ([#1052](https://github.com/getsentry/sentry-dotnet/pull/1052))
Expand Down
85 changes: 57 additions & 28 deletions src/Sentry/Extensibility/DiagnosticLoggerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static void LogDebug<TArg>(
this IDiagnosticLogger logger,
string message,
TArg arg)
=> logger.LogIfEnabled(SentryLevel.Debug, message, arg);
=> logger.LogIfEnabled(SentryLevel.Debug, null, message, arg);

/// <summary>
/// Log a debug message.
Expand All @@ -27,23 +27,23 @@ public static void LogDebug<TArg, TArg2>(
string message,
TArg arg,
TArg2 arg2)
=> logger.LogIfEnabled(SentryLevel.Debug, message, arg, arg2);
=> logger.LogIfEnabled(SentryLevel.Debug, null, message, arg, arg2);

/// <summary>
/// Log a debug message.
/// </summary>
public static void LogDebug(
this IDiagnosticLogger logger,
string message)
=> logger.LogIfEnabled(SentryLevel.Debug, message);
=> logger.LogIfEnabled(SentryLevel.Debug, null, message);

/// <summary>
/// Log a info message.
/// </summary>
public static void LogInfo(
this IDiagnosticLogger logger,
string message)
=> logger.LogIfEnabled(SentryLevel.Info, message);
=> logger.LogIfEnabled(SentryLevel.Info, null, message);

/// <summary>
/// Log a info message.
Expand All @@ -52,7 +52,7 @@ public static void LogInfo<TArg>(
this IDiagnosticLogger logger,
string message,
TArg arg)
=> logger.LogIfEnabled(SentryLevel.Info, message, arg);
=> logger.LogIfEnabled(SentryLevel.Info, null, message, arg);

/// <summary>
/// Log a info message.
Expand All @@ -62,7 +62,7 @@ public static void LogInfo<TArg, TArg2>(
string message,
TArg arg,
TArg2 arg2)
=> logger.LogIfEnabled(SentryLevel.Info, message, arg, arg2);
=> logger.LogIfEnabled(SentryLevel.Info, null, message, arg, arg2);

/// <summary>
/// Log a info message.
Expand All @@ -73,15 +73,15 @@ public static void LogInfo<TArg, TArg2, TArg3>(
TArg arg,
TArg2 arg2,
TArg3 arg3)
=> logger.LogIfEnabled(SentryLevel.Info, message, arg, arg2, arg3);
=> logger.LogIfEnabled(SentryLevel.Info, null, message, arg, arg2, arg3);

/// <summary>
/// Log a warning message.
/// </summary>
public static void LogWarning(
this IDiagnosticLogger logger,
string message)
=> logger.LogIfEnabled(SentryLevel.Warning, message);
=> logger.LogIfEnabled(SentryLevel.Warning, null, message);

/// <summary>
/// Log a warning message.
Expand All @@ -90,7 +90,7 @@ public static void LogWarning<TArg>(
this IDiagnosticLogger logger,
string message,
TArg arg)
=> logger.LogIfEnabled(SentryLevel.Warning, message, arg);
=> logger.LogIfEnabled(SentryLevel.Warning, null, message, arg);

/// <summary>
/// Log a warning message.
Expand All @@ -100,49 +100,62 @@ public static void LogWarning<TArg, TArg2>(
string message,
TArg arg,
TArg2 arg2)
=> logger.LogIfEnabled(SentryLevel.Warning, message, arg, arg2);
=> logger.LogIfEnabled(SentryLevel.Warning, null, message, arg, arg2);

/// <summary>
/// Log a warning message.
/// Log a error message.
/// </summary>
public static void LogError(
this IDiagnosticLogger logger,
string message,
Exception? exception = null)
=> logger.LogIfEnabled(SentryLevel.Error, message, exception);
=> logger.LogIfEnabled(SentryLevel.Error, exception, message);

/// <summary>
/// Log a warning message.
/// Log a error message.
/// </summary>
public static void LogError<TArg>(
this IDiagnosticLogger logger,
string message,
Exception exception,
TArg arg)
=> logger.LogIfEnabled(SentryLevel.Error, message, exception, arg);
=> logger.LogIfEnabled(SentryLevel.Error, exception, message, arg);

/// <summary>
/// Log a warning message.
/// Log a error message.
/// </summary>
public static void LogError<TArg, TArg2>(
this IDiagnosticLogger logger,
string message,
Exception exception,
TArg arg,
TArg2 arg2)
=> logger.LogIfEnabled(SentryLevel.Error, message, exception, arg, arg2);
=> logger.LogIfEnabled(SentryLevel.Error, exception, message, arg, arg2);

/// <summary>
/// Log a warning message.
/// Log a error message.
/// </summary>
public static void LogError<TArg, TArg2, TArg3>(
public static void LogError<TArg, TArg2, TArg3, TArg4>(
this IDiagnosticLogger logger,
string message,
Exception exception,
TArg arg,
TArg2 arg2,
TArg3 arg3,
TArg4 arg4)
=> logger.LogIfEnabled(SentryLevel.Error, exception, message, arg, arg2, arg3, arg4);

/// <summary>
/// Log an error message.
/// </summary>
public static void LogError<TArg, TArg2, TArg3>(
this IDiagnosticLogger logger,
Exception exception,
string message,
TArg arg,
TArg2 arg2,
TArg3 arg3)
=> logger.LogIfEnabled(SentryLevel.Error, message, arg, arg2, arg3, exception);
=> logger.LogIfEnabled(SentryLevel.Error, exception, message, arg, arg2, arg3);

/// <summary>
/// Log a warning message.
Expand All @@ -151,13 +164,13 @@ public static void LogFatal(
this IDiagnosticLogger logger,
string message,
Exception? exception = null)
=> logger.LogIfEnabled(SentryLevel.Fatal, message, exception);
=> logger.LogIfEnabled(SentryLevel.Fatal, exception, message);

internal static void LogIfEnabled(
this IDiagnosticLogger logger,
SentryLevel level,
string message,
Exception? exception = null)
Exception? exception,
string message)
{
if (logger.IsEnabled(level))
{
Expand All @@ -168,9 +181,9 @@ internal static void LogIfEnabled(
internal static void LogIfEnabled<TArg>(
this IDiagnosticLogger logger,
SentryLevel level,
Exception? exception,
string message,
TArg arg,
Exception? exception = null)
TArg arg)
{
if (logger.IsEnabled(level))
{
Expand All @@ -181,10 +194,10 @@ internal static void LogIfEnabled<TArg>(
internal static void LogIfEnabled<TArg, TArg2>(
this IDiagnosticLogger logger,
SentryLevel level,
Exception? exception,
string message,
TArg arg,
TArg2 arg2,
Exception? exception = null)
TArg2 arg2)
{
if (logger.IsEnabled(level))
{
Expand All @@ -195,16 +208,32 @@ internal static void LogIfEnabled<TArg, TArg2>(
internal static void LogIfEnabled<TArg, TArg2, TArg3>(
this IDiagnosticLogger logger,
SentryLevel level,
Exception? exception,
string message,
TArg arg,
TArg2 arg2,
TArg3 arg3,
Exception? exception = null)
TArg3 arg3)
{
if (logger.IsEnabled(level))
{
logger.Log(level, message, exception, arg, arg2, arg3);
}
}

internal static void LogIfEnabled<TArg, TArg2, TArg3, TArg4>(
this IDiagnosticLogger logger,
SentryLevel level,
Exception? exception,
string message,
TArg arg,
TArg2 arg2,
TArg3 arg3,
TArg4 arg4)
{
if (logger.IsEnabled(level))
{
logger.Log(level, message, exception, arg, arg2, arg3, arg4);
}
}
}
}
22 changes: 17 additions & 5 deletions src/Sentry/Internal/ProcessInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,31 @@ internal ProcessInfo(
// Fast
var now = DateTimeOffset.UtcNow;
StartupTime = now;
long? timestamp = 0;
try
{
BootTime = now.AddTicks(-Stopwatch.GetTimestamp()
timestamp = Stopwatch.GetTimestamp();
BootTime = now.AddTicks(-timestamp.Value
/ (Stopwatch.Frequency
/ TimeSpan.TicksPerSecond));
}
catch (DivideByZeroException e)
// We can live with only `StartupTime` so lets handle the lack of `BootTime` and construct this object.
catch (Exception e)
{
// Seems to have failed on a single Windows Server 2012 on .NET Framework 4.8
// DivideByZeroException: Seems to have failed on a single Windows Server 2012 on .NET Framework 4.8
// https://github.com/getsentry/sentry-dotnet/issues/954

// Can fail on IL2CPP with an unclear line number and this is an optional information:
// ArgumentOutOfRangeException: The added or subtracted value results in an un-representable DateTime.
// https://github.com/getsentry/sentry-unity/issues/233

_options.DiagnosticLogger?.LogError(
"Failed to find BootTime: GetTimestamp {0}, Frequency {1}, TicksPerSecond: {2}",
e, Stopwatch.GetTimestamp(), Stopwatch.Frequency, TimeSpan.TicksPerSecond);
"Failed to find BootTime: Now {0}, GetTimestamp {1}, Frequency {2}, TicksPerSecond: {3}",
e,
now,
timestamp,
Stopwatch.Frequency,
TimeSpan.TicksPerSecond);
}

// An opt-out to the more precise approach (mainly due to IL2CPP):
Expand Down

0 comments on commit e3bde53

Please sign in to comment.