2022-03-28 23:59:19 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
namespace CoopClient
|
|
|
|
|
{
|
|
|
|
|
internal static class Logger
|
|
|
|
|
{
|
|
|
|
|
public enum LogLevel
|
|
|
|
|
{
|
|
|
|
|
Normal,
|
|
|
|
|
Server,
|
|
|
|
|
Custom
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-30 16:14:25 +02:00
|
|
|
|
private static string GetFilePath(LogLevel level, string filepath)
|
2022-03-28 23:59:19 +02:00
|
|
|
|
{
|
|
|
|
|
string newFilePath = null;
|
|
|
|
|
|
|
|
|
|
switch (level)
|
|
|
|
|
{
|
|
|
|
|
case LogLevel.Normal:
|
|
|
|
|
newFilePath = "scripts\\CoopLog.txt";
|
|
|
|
|
break;
|
|
|
|
|
case LogLevel.Server:
|
|
|
|
|
newFilePath = $"scripts\\resources\\{Main.MainSettings.LastServerAddress.Replace(":", ".")}\\CoopLog.txt";
|
|
|
|
|
break;
|
|
|
|
|
case LogLevel.Custom:
|
|
|
|
|
if (string.IsNullOrEmpty(filepath))
|
|
|
|
|
{
|
|
|
|
|
goto case LogLevel.Normal;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
newFilePath = filepath;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-30 16:14:25 +02:00
|
|
|
|
return newFilePath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void Write(string message, LogLevel level = LogLevel.Normal, string filepath = null)
|
|
|
|
|
{
|
|
|
|
|
string newFilePath = GetFilePath(level, filepath);
|
|
|
|
|
|
2022-03-28 23:59:19 +02:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (File.Exists(newFilePath))
|
|
|
|
|
{
|
|
|
|
|
// Check if the rows are under 20 and delete the first 10 if so to avoid large logs
|
|
|
|
|
// Firstly get all lines
|
|
|
|
|
string[] oldLines = File.ReadAllLines(newFilePath);
|
|
|
|
|
|
|
|
|
|
// Check the length of the lines
|
|
|
|
|
if (oldLines.Length >= 20)
|
|
|
|
|
{
|
|
|
|
|
// Now overwrite the file without the first 10 lines
|
|
|
|
|
File.WriteAllLines(newFilePath, oldLines.Skip(10).ToArray());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using (StreamWriter sw = new StreamWriter(newFilePath, true))
|
|
|
|
|
{
|
2022-03-30 16:14:25 +02:00
|
|
|
|
sw.WriteLine($"[{DateTime.Now.ToLongTimeString()} {DateTime.Now.ToLongDateString()}] : {message}");
|
2022-03-28 23:59:19 +02:00
|
|
|
|
|
|
|
|
|
sw.Flush();
|
|
|
|
|
sw.Close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{ }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|