Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
GameLogSink.cs
浏览该文件的文档.
1using System.Globalization;
2using Engine;
3#if WINDOWS
4using System.Runtime.InteropServices;
5#endif
6
7namespace Game {
8 public class GameLogSink : ILogSink, IDisposable {
9 public static Stream m_stream;
10
11 public static StreamWriter m_writer;
12
13 public static string errorOfInstantiation;
14
15 public const string fName = "GameLogSink";
16 public GameLogSink() {
17 try {
18 if (m_stream != null) {
19 throw new InvalidOperationException("GameLogSink already created.");
20 }
23 }
24 string path = Storage.CombinePaths(ModsManager.LogPath, "Game.log");
25 FileInfo fileInfo = Storage.GetFileInfo(path);
26 if (!fileInfo.Exists) {
27 m_stream = fileInfo.Create();
28 }
29 else {
30 if (fileInfo.Length > 2097152) //2MiB
31 {
32 CultureInfo cultureInfo = Program.SystemLanguage == null
33 ? CultureInfo.CurrentCulture
34 : new CultureInfo(Program.SystemLanguage);
35 string destination = Storage.ProcessPath(
36 Storage.CombinePaths(ModsManager.LogPath, Storage.SanitizeFileName($"Game {DateTime.Now.ToString(cultureInfo)}.log")),
37 true,
38 false
39 );
40 fileInfo.MoveTo(destination, true);
41 m_stream = Storage.OpenFile(path, OpenFileMode.CreateOrOpen);
42 }
43 else {
44 m_stream = fileInfo.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.Read);
45 }
46 }
47 m_stream.Position = m_stream.Length;
48 m_writer = new StreamWriter(m_stream);
49 }
50 catch (Exception ex) {
51#if !MOBILE && !BROWSER
52#if WINDOWS
53 AllocConsole();
54 Window.Closed += () => FreeConsole();
55#endif
56#pragma warning disable CA1416
57 Console.Title = "Logs of Survivalcraft API";
58#pragma warning restore CA1416
59 errorOfInstantiation = $"Error creating GameLogSink, and a console window for viewing logs is created. Reason: {ex.Message}";
61#else
62 errorOfInstantiation = $"Error creating GameLogSink. Reason: {ex.Message}";
64#endif
65 }
66 }
67
68 public static string GetRecentLog(int bytesCount) {
69 if (m_stream == null) {
70 return LanguageControl.Get(fName, "1");
71 }
72 lock (m_stream) {
73 try {
74 m_stream.Position = Math.Max(m_stream.Position - bytesCount, 0L);
75 return new StreamReader(m_stream).ReadToEnd();
76 }
77 finally {
78 m_stream.Position = m_stream.Length;
79 }
80 }
81 }
82
83 public static List<string> GetRecentLogLines(int bytesCount) {
84 if (m_stream == null) {
86 }
87 lock (m_stream) {
88 try {
89 m_stream.Position = Math.Max(m_stream.Position - bytesCount, 0L);
90 StreamReader streamReader = new(m_stream);
91 List<string> list = new();
92 while (true) {
93 string text = streamReader.ReadLine();
94 if (text == null) {
95 break;
96 }
97 list.Add(text);
98 }
99 return list;
100 }
101 finally {
102 m_stream.Position = m_stream.Length;
103 }
104 }
105 }
106
107 public void Log(LogType type, string message) {
108 if (m_stream != null) {
109 lock (m_stream) {
110 string value;
111 switch (type) {
112 case LogType.Debug: value = "DEBUG: "; break;
113 case LogType.Verbose: value = "INFO: "; break;
114 case LogType.Information: value = "INFO: "; break;
115 case LogType.Warning: value = "WARNING: "; break;
116 case LogType.Error: value = "ERROR: "; break;
117 default: value = string.Empty; break;
118 }
119 m_writer.Write(DateTime.Now.ToString("HH:mm:ss.fff"));
120 m_writer.Write(" ");
121 m_writer.Write(value);
122 m_writer.WriteLine(message);
123 m_writer.Flush();
124 }
125 }
126 }
127
128 public void Dispose() {
129 if (m_writer != null) {
130 m_writer.Dispose();
131 m_writer = null;
132 }
133 if (m_stream != null) {
134 m_stream.Dispose();
135 m_stream = null;
136 }
137 }
138
139#if WINDOWS
140 [DllImport("kernel32.dll", SetLastError = true)]
141 static extern bool AllocConsole();
142
143 [DllImport("kernel32.dll", SetLastError = true)]
144 static extern bool FreeConsole();
145#endif
146 }
147}
static void Error(object message)
定义 Log.cs:80
static void Information(object message)
定义 Log.cs:56
static void CreateDirectory(string path)
static bool DirectoryExists(string path)
static Stream OpenFile(string path, OpenFileMode openFileMode)
static string SanitizeFileName(string filename, string replacement="-")
static FileInfo GetFileInfo(string path)
static string ProcessPath(string path, bool writeAccess, bool failIfApp)
static string CombinePaths(params string[] paths)
static Action Closed
static List< string > GetRecentLogLines(int bytesCount)
static string GetRecentLog(int bytesCount)
static string errorOfInstantiation
static StreamWriter m_writer
void Log(LogType type, string message)
static Stream m_stream
static string Get(string className, int key)
获取在当前语言类名键对应的字符串
static string SystemLanguage
static string LogPath