Skip to content

Commit e169066

Browse files
committed
WIP on logger fix
1 parent 28fc2ae commit e169066

File tree

3 files changed

+108
-2
lines changed

3 files changed

+108
-2
lines changed

Runtime/Scripts/BugReportUI.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ private IEnumerator SubmitBugReportCoroutine()
402402
// skip if size over 200MB
403403
if (new FileInfo(_logger.LogPath).Length < 200 * 1024 * 1024)
404404
{
405-
logFiles.Add(new Issue.LogFileReference { path = _logger.LogPath, removeAfterUpload = false });
405+
logFiles.Add(new Issue.LogFileReference { logger = _logger, removeAfterUpload = false });
406406
}
407407
}
408408
}

Runtime/Scripts/Issue.cs

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public struct LogFileReference
4646
{
4747
public string path;
4848
public bool removeAfterUpload;
49+
public Logger logger; // Optional: if set, use logger.ReadLogFileBytes() instead of path
4950
}
5051

5152
public struct ScreenshotFileReference
@@ -381,8 +382,25 @@ private IEnumerator PostScreenshot(ScreenshotFileReference screenshot)
381382

382383
private IEnumerator PostLogFile(LogFileReference logFile)
383384
{
384-
if (File.Exists(logFile.path))
385+
if (logFile.logger != null)
385386
{
387+
// Use logger's safe read method to avoid sharing violations
388+
Debug.Log("Reading BetaHub log file safely using Logger instance");
389+
byte[] fileData = logFile.logger.ReadLogFileBytes();
390+
391+
if (fileData != null)
392+
{
393+
string fileName = Path.GetFileName(logFile.logger.LogPath) ?? "BH_Player.log";
394+
yield return UploadStringAsFile("log_files", "log_file[file]", fileData, fileName, "text/plain");
395+
}
396+
else
397+
{
398+
Debug.LogError("Failed to read log file data from Logger instance");
399+
}
400+
}
401+
else if (File.Exists(logFile.path))
402+
{
403+
// Original file path logic
386404
yield return UploadFile("log_files", "log_file[file]", logFile.path, "text/plain");
387405

388406
if (logFile.removeAfterUpload)
@@ -433,6 +451,37 @@ private IEnumerator UploadFile(string endpoint, string fieldName, string filePat
433451
}
434452
}
435453

454+
private IEnumerator UploadStringAsFile(string endpoint, string fieldName, byte[] fileData, string fileName, string contentType)
455+
{
456+
if (fileData == null)
457+
{
458+
Debug.LogError($"Cannot upload {fileName}: file data is null");
459+
yield break;
460+
}
461+
462+
WWWForm form = new WWWForm();
463+
form.AddBinaryData(fieldName, fileData, fileName, contentType);
464+
465+
string url = $"{_betahubEndpoint}projects/{_projectId}/issues/g-{Id}/{endpoint}";
466+
using (UnityWebRequest www = UnityWebRequest.Post(url, form))
467+
{
468+
www.SetRequestHeader("Authorization", "Bearer " + _updateIssueAuthToken);
469+
www.SetRequestHeader("BetaHub-Project-ID", _projectId);
470+
www.SetRequestHeader("Accept", "application/json");
471+
472+
yield return www.SendWebRequest();
473+
474+
if (www.result != UnityWebRequest.Result.Success)
475+
{
476+
Debug.LogError($"Error uploading {fileName}: {www.error}");
477+
}
478+
else
479+
{
480+
Debug.Log($"{fileName} uploaded successfully!");
481+
}
482+
}
483+
}
484+
436485
private IEnumerator PublishNow()
437486
{
438487
// Ensure we have valid parameters before proceeding

Runtime/Scripts/Logger.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,63 @@ public void ForceFlush()
124124
}
125125
}
126126

127+
/// <summary>
128+
/// Safely reads the entire log file by temporarily closing the file stream.
129+
/// This prevents file sharing violations when other parts of the application need to read the log.
130+
/// </summary>
131+
/// <returns>The complete log file content as a byte array, or null if an error occurs</returns>
132+
public byte[] ReadLogFileBytes()
133+
{
134+
if (disposed || string.IsNullOrEmpty(_logPath))
135+
{
136+
Debug.LogWarning("Cannot read log file: Logger is disposed or log path is not set");
137+
return null;
138+
}
139+
140+
lock (lockObject)
141+
{
142+
try
143+
{
144+
// First, flush any buffered data
145+
FlushBuffer();
146+
147+
// Temporarily close the file streams
148+
writer?.Dispose();
149+
fileStream?.Dispose();
150+
writer = null;
151+
fileStream = null;
152+
153+
// Now we can safely read the file since we've closed our handle
154+
byte[] fileData = null;
155+
if (File.Exists(_logPath))
156+
{
157+
fileData = File.ReadAllBytes(_logPath);
158+
}
159+
160+
// Reopen the file streams to continue logging
161+
InitializeFileStream();
162+
163+
return fileData;
164+
}
165+
catch (Exception e)
166+
{
167+
Debug.LogError("Error reading log file: " + e.Message);
168+
169+
// Ensure we reopen the file streams even if reading failed
170+
try
171+
{
172+
InitializeFileStream();
173+
}
174+
catch (Exception reopenEx)
175+
{
176+
Debug.LogError("Error reopening log file after failed read: " + reopenEx.Message);
177+
}
178+
179+
return null;
180+
}
181+
}
182+
}
183+
127184
public void Dispose()
128185
{
129186
if (disposed) return;

0 commit comments

Comments
 (0)