Zader

Logger

Jan 12th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.80 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using Sandbox.ModAPI;
  4.  
  5. namespace GSF.Logging
  6. {
  7.     public class Log
  8.     {
  9.         private static Log _instance = null;
  10.         private TextWriter _file = null;
  11.         private string _fileName = "";
  12.  
  13.         private Log()
  14.         {
  15.  
  16.         }
  17.  
  18.         private static Log GetInstance()
  19.         {
  20.             if (Log._instance == null)
  21.             {
  22.                 Log._instance = new Log();
  23.             }
  24.  
  25.             return _instance;
  26.         }
  27.  
  28.         public static bool Init(string name)
  29.         {
  30.  
  31.             bool output = false;
  32.  
  33.             if (GetInstance()._file == null)
  34.             {
  35.  
  36.                 try
  37.                 {
  38.                     MyAPIGateway.Utilities.ShowNotification(name, 5000);
  39.                     GetInstance()._fileName = name;
  40.                     GetInstance()._file = MyAPIGateway.Utilities.WriteFileInLocalStorage(name, typeof(Log));
  41.                     output = true;
  42.                 }
  43.                 catch (Exception e)
  44.                 {
  45.                     MyAPIGateway.Utilities.ShowNotification(e.Message, 5000);
  46.                 }
  47.             }
  48.             else
  49.             {
  50.                 output = true;
  51.             }
  52.  
  53.             return output;
  54.         }
  55.  
  56.         public static void Line(string text)
  57.         {
  58.             try
  59.             {
  60.                 if (GetInstance()._file != null)
  61.                 {
  62.                     var time = $"{DateTime.Now:MM-dd-yy_HH-mm-ss-fff} - ";
  63.                     GetInstance()._file.WriteLine(time + text);
  64.                     GetInstance()._file.Flush();
  65.                 }
  66.             }
  67.             catch (Exception e)
  68.             {
  69.             }
  70.         }
  71.  
  72.         public static void Chars(string text)
  73.         {
  74.             try
  75.             {
  76.                 if (GetInstance()._file != null)
  77.                 {
  78.                     GetInstance()._file.Write(text);
  79.                     GetInstance()._file.Flush();
  80.                 }
  81.             }
  82.             catch (Exception e)
  83.             {
  84.             }
  85.         }
  86.  
  87.         public static void CleanLine(string text)
  88.         {
  89.             try
  90.             {
  91.                 if (GetInstance()._file != null)
  92.                 {
  93.                     GetInstance()._file.WriteLine(text);
  94.                     GetInstance()._file.Flush();
  95.                 }
  96.             }
  97.             catch (Exception e)
  98.             {
  99.             }
  100.         }
  101.  
  102.         public static void Close()
  103.         {
  104.             try
  105.             {
  106.                 if (GetInstance()._file != null)
  107.                 {
  108.                     GetInstance()._file.Flush();
  109.                     GetInstance()._file.Close();
  110.                 }
  111.             }
  112.             catch (Exception e)
  113.             {
  114.             }
  115.         }
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment