Skip to content

Commit 1d44416

Browse files
committed
Logger ReadLogFileBytes uses the pause and resume functions
1 parent b4e9904 commit 1d44416

File tree

4 files changed

+66
-70
lines changed

4 files changed

+66
-70
lines changed

CLAUDE.md.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/Scripts/BugReportUI.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ public class BugReportUI : MonoBehaviour
124124
private List<Issue.LogFileReference> _logFiles = new List<Issue.LogFileReference>();
125125

126126
private static Logger _logger;
127+
128+
public static Logger Logger => _logger;
127129
private bool _cursorStateChanged;
128130
private CursorLockMode _previousCursorLockMode;
129131

Runtime/Scripts/Issue.cs

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -428,53 +428,53 @@ private IEnumerator PostVideo(GameRecorder gameRecorder)
428428
private IEnumerator UploadFile(string endpoint, string fieldName, string filePath, string contentType)
429429
{
430430
bool isLogFile = Path.GetExtension(filePath).Equals(".log", StringComparison.OrdinalIgnoreCase);
431-
if (isLogFile)
432-
{
433-
BugReportUI.PauseLogger();
434-
yield return new WaitForSeconds(0.1f);
435-
}
436-
437431
byte[] fileData;
438-
try
439-
{
440-
fileData = File.ReadAllBytes(filePath);
441-
}
442-
catch (Exception ex)
432+
433+
if (isLogFile)
443434
{
444-
Debug.LogError($"Error reading file {filePath}: {ex.Message}");
445-
if (isLogFile)
435+
if (BugReportUI.Logger != null && filePath == BugReportUI.Logger.LogPath)
446436
{
437+
Debug.Log("Reading log file safely using Logger instance");
438+
fileData = BugReportUI.Logger.ReadLogFileBytes();
439+
if (fileData == null)
440+
{
441+
Debug.LogError("Failed to read log file data from Logger instance");
442+
yield break;
443+
}
444+
}
445+
else
446+
{
447+
BugReportUI.PauseLogger();
448+
449+
try
450+
{
451+
fileData = File.ReadAllBytes(filePath);
452+
}
453+
catch (Exception ex)
454+
{
455+
Debug.LogError($"Error reading file {filePath}: {ex.Message}");
456+
BugReportUI.ResumeLogger();
457+
yield break;
458+
}
459+
447460
BugReportUI.ResumeLogger();
448461
}
449-
yield break;
450-
}
451-
452-
if (isLogFile)
453-
{
454-
BugReportUI.ResumeLogger();
455462
}
456-
457-
WWWForm form = new WWWForm();
458-
form.AddBinaryData(fieldName, fileData, Path.GetFileName(filePath), contentType);
459-
460-
string url = $"{_betahubEndpoint}projects/{_projectId}/issues/g-{Id}/{endpoint}";
461-
using (UnityWebRequest www = UnityWebRequest.Post(url, form))
463+
else
462464
{
463-
www.SetRequestHeader("Authorization", "Bearer " + _updateIssueAuthToken);
464-
www.SetRequestHeader("BetaHub-Project-ID", _projectId);
465-
www.SetRequestHeader("Accept", "application/json");
466-
467-
yield return www.SendWebRequest();
468-
469-
if (www.result != UnityWebRequest.Result.Success)
465+
// For non-log files, read normally
466+
try
470467
{
471-
Debug.LogError($"Error uploading {Path.GetFileName(filePath)}: {www.error}");
468+
fileData = File.ReadAllBytes(filePath);
472469
}
473-
else
470+
catch (Exception ex)
474471
{
475-
Debug.Log($"{Path.GetFileName(filePath)} uploaded successfully!");
472+
Debug.LogError($"Error reading file {filePath}: {ex.Message}");
473+
yield break;
476474
}
477475
}
476+
477+
yield return UploadStringAsFile(endpoint, fieldName, fileData, Path.GetFileName(filePath), contentType);
478478
}
479479

480480
private IEnumerator UploadStringAsFile(string endpoint, string fieldName, byte[] fileData, string fileName, string contentType)

Runtime/Scripts/Logger.cs

Lines changed: 22 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ public void ResumeLogging()
157157
}
158158

159159
/// <summary>
160-
/// Safely reads the entire log file by temporarily closing the file stream.
160+
/// Safely reads the entire log file by temporarily pausing logging.
161161
/// This prevents file sharing violations when other parts of the application need to read the log.
162162
/// </summary>
163163
/// <returns>The complete log file content as a byte array, or null if an error occurs</returns>
@@ -169,47 +169,34 @@ public byte[] ReadLogFileBytes()
169169
return null;
170170
}
171171

172-
lock (lockObject)
172+
try
173173
{
174-
try
174+
PauseLogging();
175+
176+
byte[] fileData = null;
177+
if (File.Exists(_logPath))
175178
{
176-
// First, flush any buffered data
177-
FlushBuffer();
178-
179-
// Temporarily close the file streams
180-
writer?.Dispose();
181-
fileStream?.Dispose();
182-
writer = null;
183-
fileStream = null;
184-
185-
// Now we can safely read the file since we've closed our handle
186-
byte[] fileData = null;
187-
if (File.Exists(_logPath))
188-
{
189-
fileData = File.ReadAllBytes(_logPath);
190-
}
179+
fileData = File.ReadAllBytes(_logPath);
180+
}
181+
182+
ResumeLogging();
191183

192-
// Reopen the file streams to continue logging
193-
InitializeFileStream();
184+
return fileData;
185+
}
186+
catch (Exception e)
187+
{
188+
Debug.LogError("Error reading log file: " + e.Message);
194189

195-
return fileData;
190+
try
191+
{
192+
ResumeLogging();
196193
}
197-
catch (Exception e)
194+
catch (Exception resumeEx)
198195
{
199-
Debug.LogError("Error reading log file: " + e.Message);
200-
201-
// Ensure we reopen the file streams even if reading failed
202-
try
203-
{
204-
InitializeFileStream();
205-
}
206-
catch (Exception reopenEx)
207-
{
208-
Debug.LogError("Error reopening log file after failed read: " + reopenEx.Message);
209-
}
210-
211-
return null;
196+
Debug.LogError("Error resuming log file after failed read: " + resumeEx.Message);
212197
}
198+
199+
return null;
213200
}
214201
}
215202

0 commit comments

Comments
 (0)