MaximilianPs

ConsoleLogger

Jun 9th, 2025 (edited)
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.97 KB | Source Code | 0 0
  1. using System;
  2. using System.IO;
  3. using UnityEngine;
  4.  
  5. namespace M3D.Utilities
  6. {
  7.     public class Logger
  8.     {
  9.         private static string logFilePath = "Assets/debug.log";
  10.  
  11.         public static string SystemTime { get { return DateTime.Now.ToString("HH:mm:ss"); } }
  12.        
  13.         // Pulisce il file di log all'inizio
  14.         public static void ClearLog()
  15.         {
  16.             if (File.Exists(logFilePath))
  17.             {
  18.                 File.Delete(logFilePath);
  19.             }
  20.         }
  21.  
  22.         public static void Log(string message)
  23.         {
  24.             try
  25.             {
  26.                 using (StreamWriter writer = new StreamWriter(logFilePath, true))
  27.                 {
  28.                     writer.WriteLine($"[{SystemTime}] {message}");
  29.                 }
  30.             }
  31.             catch (Exception ex)
  32.             {
  33.                 Debug.LogError($"[{SystemTime}] Logger failed: {ex.Message}");
  34.             }
  35.         }
  36.  
  37.         public static void LogWarning(string message)
  38.         {
  39.             using (StreamWriter writer = new StreamWriter(logFilePath, true))
  40.             {
  41.                 //writer.WriteLine($"{System.DateTime.Now}: {message}");
  42.                 writer.WriteLine($"[{SystemTime}] !!! WARNING !!! {message}");
  43.             }
  44.         }
  45.  
  46.         public static void LogError(string message)
  47.         {
  48.             using (StreamWriter writer = new StreamWriter(logFilePath, true))
  49.             {
  50.                 //writer.WriteLine($"{System.DateTime.Now}: {message}");
  51.                 writer.WriteLine($"[{SystemTime}] *** ERROR *** {message}");
  52.             }
  53.         }
  54.  
  55.         public static void SaveTexturePNG(Texture2D texture, string fileName)
  56.         {
  57.             byte[] bytes = texture.EncodeToPNG();
  58.             string filePath = System.IO.Path.Combine(Application.dataPath, fileName + ".png");
  59.             System.IO.File.WriteAllBytes(filePath, bytes);
  60.             Debug.Log($"Saved texture to: {filePath}");
  61.         }
  62.     }
  63. }
  64.  
Advertisement
Add Comment
Please, Sign In to add comment