Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit 5f2d932

Browse files
committed
Make sure the log isn't locked when rotating it
Also make sure we don't blow up with an uninitialized logger factory
1 parent 190e3c6 commit 5f2d932

File tree

3 files changed

+52
-29
lines changed

3 files changed

+52
-29
lines changed

src/GitHub.Logging/Logging.cs

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,37 @@
22

33
namespace GitHub.Unity
44
{
5+
class NullLogAdapter : LogAdapterBase
6+
{
7+
public NullLogAdapter() : base(null)
8+
{}
9+
10+
protected override void OnInfo(string message)
11+
{
12+
}
13+
14+
protected override void OnDebug(string message)
15+
{
16+
}
17+
18+
protected override void OnTrace(string message)
19+
{
20+
}
21+
22+
protected override void OnWarning(string message)
23+
{
24+
}
25+
26+
protected override void OnError(string message)
27+
{
28+
}
29+
}
30+
531
public static class Logging
632
{
7-
private static bool tracingEnabled;
33+
private static ILogging nullLogger = new NullLogAdapter();
834

35+
private static bool tracingEnabled;
936
public static bool TracingEnabled
1037
{
1138
get
@@ -23,19 +50,13 @@ public static bool TracingEnabled
2350
}
2451

2552
private static Func<string, ILogging> loggerFactory;
26-
2753
public static Func<string, ILogging> LoggerFactory
2854
{
2955
get { return loggerFactory; }
30-
set
31-
{
32-
loggerFactory = value;
33-
Instance = loggerFactory(null);
34-
}
56+
set { loggerFactory = value; }
3557
}
3658

3759
private static ILogging instance;
38-
3960
private static ILogging Instance
4061
{
4162
get {
@@ -60,82 +81,82 @@ public static ILogging GetLogger(Type type)
6081

6182
public static ILogging GetLogger(string context = null)
6283
{
63-
return loggerFactory(context);
84+
return loggerFactory?.Invoke(context) ?? nullLogger;
6485
}
6586

6687
public static void Info(string s)
6788
{
68-
Instance.Info(s);
89+
Instance?.Info(s);
6990
}
7091

7192
public static void Debug(string s)
7293
{
73-
Instance.Debug(s);
94+
Instance?.Debug(s);
7495
}
7596

7697
public static void Trace(string s)
7798
{
78-
Instance.Trace(s);
99+
Instance?.Trace(s);
79100
}
80101

81102
public static void Warning(string s)
82103
{
83-
Instance.Warning(s);
104+
Instance?.Warning(s);
84105
}
85106

86107
public static void Error(string s)
87108
{
88-
Instance.Error(s);
109+
Instance?.Error(s);
89110
}
90111

91112
public static void Info(string format, params object[] objects)
92113
{
93-
Instance.Info(format, objects);
114+
Instance?.Info(format, objects);
94115
}
95116

96117
public static void Debug(string format, params object[] objects)
97118
{
98-
Instance.Debug(format, objects);
119+
Instance?.Debug(format, objects);
99120
}
100121

101122
public static void Trace(string format, params object[] objects)
102123
{
103-
Instance.Trace(format, objects);
124+
Instance?.Trace(format, objects);
104125
}
105126

106127
public static void Warning(string format, params object[] objects)
107128
{
108-
Instance.Warning(format, objects);
129+
Instance?.Warning(format, objects);
109130
}
110131

111132
public static void Error(string format, params object[] objects)
112133
{
113-
Instance.Error(format, objects);
134+
Instance?.Error(format, objects);
114135
}
115136

116137
public static void Info(Exception ex, string s)
117138
{
118-
Instance.Info(ex, s);
139+
Instance?.Info(ex, s);
119140
}
120141

121142
public static void Debug(Exception ex, string s)
122143
{
123-
Instance.Debug(ex, s);
144+
Instance?.Debug(ex, s);
124145
}
125146

126147
public static void Trace(Exception ex, string s)
127148
{
128-
Instance.Trace(ex, s);
149+
Instance?.Trace(ex, s);
129150
}
130151

131152
public static void Warning(Exception ex, string s)
132153
{
133-
Instance.Warning(ex, s);
154+
Instance?.Warning(ex, s);
134155
}
135156

136157
public static void Error(Exception ex, string s)
137158
{
138-
Instance.Error(ex, s);
159+
Instance?.Error(ex, s);
139160
}
140161
}
141162
}

src/IntegrationTests/IntegrationTests.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@
129129
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
130130
</Content>
131131
</ItemGroup>
132+
<ItemGroup>
133+
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
134+
</ItemGroup>
132135
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
133136
<PropertyGroup>
134137
</PropertyGroup>

src/UnityExtension/Assets/Editor/GitHub.Unity/EntryPoint.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ namespace GitHub.Unity
1111
[InitializeOnLoad]
1212
class EntryPoint : ScriptableObject
1313
{
14-
private static ILogging logger;
15-
1614
private static ApplicationManager appManager;
1715

1816
// this may run on the loader thread if it's an appdomain restart
@@ -27,8 +25,7 @@ static EntryPoint()
2725

2826
var logPath = DefaultEnvironment.LogPath;
2927

30-
Logging.LoggerFactory = s => new FileLogAdapter(logPath, s);
31-
logger = Logging.GetLogger<EntryPoint>();
28+
Logging.LoggerFactory = s => new FileLogAdapter(logPath.FileNameWithoutExtension + "-startup" + logPath.ExtensionWithDot, s);
3229

3330
ServicePointManager.ServerCertificateValidationCallback = ServerCertificateValidationCallback;
3431
EditorApplication.update += Initialize;
@@ -38,12 +35,12 @@ static EntryPoint()
3835
private static void Initialize()
3936
{
4037
EditorApplication.update -= Initialize;
38+
var logPath = DefaultEnvironment.LogPath;
4139

4240
if (ApplicationCache.Instance.FirstRun)
4341
{
4442
Debug.Log("Initializing GitHub for Unity version " + ApplicationInfo.Version);
4543

46-
var logPath = DefaultEnvironment.LogPath;
4744
var oldLogPath = logPath.FileNameWithoutExtension + "-old" + logPath.ExtensionWithDot;
4845

4946
try
@@ -54,6 +51,8 @@ private static void Initialize()
5451
{
5552
Logging.Error(ex, "Error rotating log files");
5653
}
54+
55+
Logging.LoggerFactory = s => new FileLogAdapter(logPath, s);
5756
Debug.Log("Initializing GitHub for Unity log file: " + logPath);
5857
Logging.Info("Initializing GitHub for Unity log file: " + logPath);
5958
}

0 commit comments

Comments
 (0)