A game about forced loneliness, made by TACStudios
1using System;
2using System.Collections.Generic;
3using System.IO;
4using UnityEditor.DeploymentTargets;
5using UnityEditor.Utils;
6using UnityEngine;
7
8namespace UnityEditor.TestTools.TestRunner.CommandLineTest
9{
10 internal class LogWriter : IDisposable
11 {
12 private string m_LogsDirectory;
13 private string m_DeviceID;
14 private Dictionary<string, StreamWriter> m_LogStreams;
15 private DeploymentTargetLogger m_Logger;
16
17 internal LogWriter(string logsDirectory, string deviceID, DeploymentTargetLogger logger)
18 {
19 m_LogStreams = new Dictionary<string, StreamWriter>();
20 m_Logger = logger;
21 m_LogsDirectory = logsDirectory;
22 m_DeviceID = deviceID;
23
24 logger.logMessage += WriteLogToFile;
25 }
26
27 private void WriteLogToFile(string id, string logLine)
28 {
29 StreamWriter logStream;
30 var streamExists = m_LogStreams.TryGetValue(id, out logStream);
31 if (!streamExists)
32 {
33 var filePath = GetLogFilePath(m_LogsDirectory, m_DeviceID, id);
34 logStream = CreateLogFile(filePath);
35
36 m_LogStreams.Add(id, logStream);
37 }
38
39 try
40 {
41 if (logLine != null)
42 logStream.WriteLine(logLine);
43 }
44 catch (Exception ex)
45 {
46 Debug.LogError($"Writing {id} log failed.");
47 Debug.LogException(ex);
48 }
49 }
50
51 public void Stop()
52 {
53 m_Logger.Stop();
54 foreach (var logStream in m_LogStreams)
55 {
56 logStream.Value.Close();
57 }
58 }
59
60 public void Dispose()
61 {
62 Stop();
63 }
64
65 private StreamWriter CreateLogFile(string path)
66 {
67 Debug.LogFormat(LogType.Log, LogOption.NoStacktrace, null, "Creating {0} device log: {1}", m_DeviceID, path);
68 StreamWriter streamWriter = null;
69 try
70 {
71 if (!Directory.Exists(path))
72 Directory.CreateDirectory(Path.GetDirectoryName(path));
73
74 streamWriter = File.CreateText(path);
75 }
76 catch (Exception ex)
77 {
78 Debug.LogError($"Creating device log {path} file failed.");
79 Debug.LogException(ex);
80 }
81
82 return streamWriter;
83 }
84
85 private string GetLogFilePath(string lgosDirectory, string deviceID, string logID)
86 {
87 var fileName = "Device-" + deviceID + "-" + logID + ".txt";
88 fileName = string.Join("_", fileName.Split(Path.GetInvalidFileNameChars()));
89 return Paths.Combine(lgosDirectory, fileName);
90 }
91 }
92}