Files
soccar2d/Assets/Cupid/Scripts/Logger.cs
T

81 lines
2.2 KiB
C#

using System;
using System.IO;
using UnityEngine;
public class Logger
{
public static bool Enabled = true;
private static Logger m_instance = null;
private static string ApplicationDirectory
{
get
{
// Android (and iOS): dataPath is inside the package/APK and is not writable.
if (Application.platform == RuntimePlatform.Android ||
Application.platform == RuntimePlatform.IPhonePlayer)
{
return Application.persistentDataPath;
}
string path = Application.dataPath;
if (Application.platform == RuntimePlatform.OSXPlayer)
{
path = Path.Combine(path, "..", "..");
}
else if (Application.platform == RuntimePlatform.WindowsPlayer)
{
path = Path.Combine(path, "..");
}
return path;
}
}
public string LogFilePath {get; private set;}
public static Logger instance
{
get
{
if (m_instance == null)
{
m_instance = new Logger();
}
return m_instance;
}
}
public Logger()
{
if(!Enabled){return;}
Debug.Log("Starting logger @ " + ApplicationDirectory);
if(LogFilePath == null){
LogFilePath = Path.Combine(ApplicationDirectory, "Log.txt");
}
File.WriteAllText(LogFilePath, "Logger initiated at " + DateTime.Now + "\n\n");
}
public void log(string message){
if(!Enabled){return;}
File.AppendAllText(LogFilePath,$"[{DateTime.Now}] {message}\n");
Debug.Log(message);
}
public static void Log(string message){
instance.log(message);
}
public static void SetFileName(string fileName){
EnsureDirectoryExists(Path.Combine(ApplicationDirectory, "Logs"));
instance.LogFilePath = Path.Combine(ApplicationDirectory, "Logs", fileName + ".txt");
}
public static void EnsureDirectoryExists(string path){
if(!Directory.Exists(path)){
Directory.CreateDirectory(path);
}
}
public static void SetFilePath(string path){
instance.LogFilePath= path;
}
}