177 lines
4.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.IO;
using UnityEngine;
public class Logger : MonoBehaviour, ILogger
{
private static Logger instance;
private string logFilePath;
private StreamWriter writer;
public static Logger Instance
{
get
{
return instance;
}
}
public ILogHandler logHandler { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
public bool logEnabled { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
public LogType filterLogType { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
/// <summary>
/// Инициализирует логгер и создает файл лога
/// </summary>
public void Initialize()
{
try
{
instance = this;
// Получаем путь к папке PersistentDataPath
string persistentPath = Application.persistentDataPath;
// Создаем имя файла с датой и временем
string fileName = $"comlog_{DateTime.Now:yyyy-MM-dd_HH-mm-ss}.txt";
logFilePath = Path.Combine(persistentPath,"LOGS", fileName);
// Создаем директорию если не существует
string directory = Path.GetDirectoryName(logFilePath);
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
// Создаем файл и записываем начальное сообщение
writer = new StreamWriter(logFilePath, true);
string startMessage = $"Начало логгирования {DateTime.Now:yyyy-MM-dd HH:mm:ss}";
writer.WriteLine(startMessage);
writer.Flush();
//Debug.Log($"Логгер инициализирован. Файл: {logFilePath}");
}
catch (Exception e)
{
Debug.LogError($"Ошибка инициализации логгера: {e.Message}");
}
}
/// <summary>
/// Записывает сообщение в лог
/// </summary>
/// <param name="message">Сообщение для записи</param>
public void Log(object message)
{
if (writer == null)
{
Debug.LogWarning("Логгер не инициализирован. Сначала вызовите Initialize().");
return;
}
try
{
string logEntry = $"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] {message}";
writer.WriteLine(logEntry);
writer.Flush(); // Принудительно записываем в файл
}
catch (Exception e)
{
Debug.LogError($"Ошибка записи в лог: {e.Message}");
}
}
/// <summary>
/// Закрывает логгер и освобождает ресурсы
/// </summary>
public void Close()
{
if (writer != null)
{
try
{
string endMessage = $"Конец логгирования {DateTime.Now:yyyy-MM-dd HH:mm:ss}";
writer.WriteLine(endMessage);
writer.Flush();
writer.Close();
writer = null;
}
catch (Exception e)
{
Debug.LogError($"Ошибка закрытия логгера: {e.Message}");
}
}
}
void OnApplicationQuit()
{
Close();
}
void OnDestroy()
{
Close();
}
public bool IsLogTypeAllowed(LogType logType)
{
return false;
}
public void Log(LogType logType, object message)
{
}
public void Log(LogType logType, object message, UnityEngine.Object context)
{
}
public void Log(LogType logType, string tag, object message)
{
}
public void Log(LogType logType, string tag, object message, UnityEngine.Object context)
{
}
public void Log(string tag, object message)
{
}
public void Log(string tag, object message, UnityEngine.Object context)
{
}
public void LogWarning(string tag, object message)
{
}
public void LogWarning(string tag, object message, UnityEngine.Object context)
{
}
public void LogError(string tag, object message)
{
}
public void LogError(string tag, object message, UnityEngine.Object context)
{
}
public void LogFormat(LogType logType, string format, params object[] args)
{
}
public void LogException(Exception exception)
{
}
public void LogFormat(LogType logType, UnityEngine.Object context, string format, params object[] args)
{
}
public void LogException(Exception exception, UnityEngine.Object context)
{
}
}