81 lines
2.0 KiB
C#
81 lines
2.0 KiB
C#
using UnityEngine;
|
|||
|
|
using System;
|
||
|
|
using System.IO;
|
||
|
|
using System.Collections.Generic;
|
||
|
|
|
||
|
|
namespace Douyin.LiveOpenSDK
|
||
|
|
{
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
public static class LogManager
|
||
|
|
{
|
||
|
|
private static string logPath = Path.Combine(Application.persistentDataPath, "GameLogs");
|
||
|
|
private static int maxLogFiles = 7;
|
||
|
|
private static LogLevel currentLogLevel = LogLevel.Verbose;
|
||
|
|
|
||
|
|
public enum LogLevel
|
||
|
|
{
|
||
|
|
Error = 0,
|
||
|
|
Warning = 1,
|
||
|
|
Info = 2,
|
||
|
|
Verbose = 3
|
||
|
|
}
|
||
|
|
|
||
|
|
[RuntimeInitializeOnLoadMethod]
|
||
|
|
private static void Initialize()
|
||
|
|
{
|
||
|
|
if (!Directory.Exists(logPath))
|
||
|
|
{
|
||
|
|
Directory.CreateDirectory(logPath);
|
||
|
|
}
|
||
|
|
CleanOldLogs();
|
||
|
|
}
|
||
|
|
|
||
|
|
public static void Log(string message, LogLevel level = LogLevel.Info)
|
||
|
|
{
|
||
|
|
if (level > currentLogLevel) return;
|
||
|
|
|
||
|
|
string logFile = Path.Combine(logPath, $"{DateTime.Now:yyyy-MM-dd}.log");
|
||
|
|
string logMessage = $"[{DateTime.Now:HH:mm:ss}] [{level}] {message}\n";
|
||
|
|
|
||
|
|
try
|
||
|
|
{
|
||
|
|
File.AppendAllText(logFile, logMessage);
|
||
|
|
}
|
||
|
|
catch (Exception e)
|
||
|
|
{
|
||
|
|
Debug.LogError($"Failed to write log: {e.Message}");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private static void CleanOldLogs()
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
var files = new DirectoryInfo(logPath).GetFiles("*.log");
|
||
|
|
if (files.Length > maxLogFiles)
|
||
|
|
{
|
||
|
|
Array.Sort(files, (a, b) => b.CreationTime.CompareTo(a.CreationTime));
|
||
|
|
for (int i = maxLogFiles; i < files.Length; i++)
|
||
|
|
{
|
||
|
|
files[i].Delete();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
catch (Exception e)
|
||
|
|
{
|
||
|
|
Debug.LogError($"Failed to clean logs: {e.Message}");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public static void SetLogLevel(LogLevel level)
|
||
|
|
{
|
||
|
|
currentLogLevel = level;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
|