Advertisement
Guest User

Simple logging class

a guest
Mar 7th, 2013
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.86 KB | None | 0 0
  1. /*
  2. Simple Logger Class
  3. Author: Satik
  4. */
  5.  
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Text;
  9. using System.IO;
  10.  
  11. namespace Tools
  12. {
  13.     public static class Logger
  14.     {
  15.         private static LogType logType = LogType.NOLOG;
  16.         private static String fileName = "log.txt";
  17.         private static FileStream fs;
  18.         private static bool flushAll = true;
  19.  
  20.         /// <summary>
  21.         /// Inits logging, opens logging file streams etc.
  22.         /// </summary>
  23.         public static bool Init(LogType lt, String filename)
  24.         {
  25.             logType = lt;
  26.  
  27.             if (((byte)lt & (byte)LogType.FILE) > 0)
  28.             {
  29.                 if (filename != null && filename.Trim() != "")
  30.                 {
  31.                     fileName = filename;
  32.                 }
  33.                 else
  34.                     return false;
  35.             }
  36.  
  37.             return Init();
  38.         }
  39.  
  40.         /// <summary>
  41.         /// Closes logging filestream!
  42.         /// </summary>
  43.         public static void Close()
  44.         {
  45.             if (fs != null)
  46.             {
  47.                 fs.Close();
  48.                 fs.Dispose();
  49.             }
  50.         }
  51.  
  52.         private static bool Init()
  53.         {
  54.             try
  55.             {
  56.                 if (((byte)logType & (byte)LogType.FILE) > 0)
  57.                 {
  58.                     fs = new FileStream(fileName, FileMode.Create);
  59.                 }
  60.             }
  61.             catch (Exception)
  62.             {
  63.                 return false;
  64.             }
  65.  
  66.             Log("===================", false);
  67.             Log(Constants.GameName + " v " + Constants.Version, false);
  68.             Log("Logging initialized", false);
  69.             Log("Current dir: " + Environment.CurrentDirectory, false);
  70.             Log("Date/Time: " + CurDateTime(), false);
  71.             Log("Computer name: " + Environment.MachineName, false);
  72.             Log("OS: " + Environment.OSVersion, false);
  73.             Log("CPUs: " + Environment.ProcessorCount, false);
  74.             //Log("GPU: "+Microsoft.Xna.Framework.GraphicsDeviceManager.);
  75.  
  76.             return true;
  77.         }
  78.  
  79.         public static void LogGPU(Microsoft.Xna.Framework.GraphicsDeviceManager gdm)
  80.         {
  81.             Log("GPU Description: " + gdm.GraphicsDevice.CreationParameters.Adapter.Description);
  82.             Log("GPU Driver: " + gdm.GraphicsDevice.CreationParameters.Adapter.DriverDll + ", version " + gdm.GraphicsDevice.CreationParameters.Adapter.DriverVersion);
  83.             Log("Graphic mode: " + gdm.GraphicsDevice.CreationParameters.Adapter.CurrentDisplayMode.Width +"x"+ gdm.GraphicsDevice.CreationParameters.Adapter.CurrentDisplayMode.Height+"x"+gdm.GraphicsDevice.CreationParameters.Adapter.CurrentDisplayMode.Format+"x"+gdm.GraphicsDevice.CreationParameters.Adapter.CurrentDisplayMode.RefreshRate+"@"+"Hz, aspect ratio "+gdm.GraphicsDevice.CreationParameters.Adapter.CurrentDisplayMode.AspectRatio );
  84.             Log("===================", false);
  85.             Log("                   ", false);
  86.         }
  87.  
  88.         /// <summary>
  89.         /// Logs message, automatically adds timestamp.
  90.         /// </summary>
  91.         public static void Log(String log)
  92.         {
  93.             Log(log, true);
  94.         }
  95.  
  96.         /// <summary>
  97.         /// Logs message with optional timestamp.
  98.         /// </summary>
  99.         public static void Log(String log, bool addTimestamp)
  100.         {
  101.             if (addTimestamp) log = CurDateTime() + " - " + log;
  102.  
  103.             if (logType == LogType.FILE || logType == LogType.CONSOLEANDFILE)
  104.             {
  105.                 logFile(log);
  106.             }
  107.  
  108.             if (logType == LogType.CONSOLE || logType == LogType.CONSOLEANDFILE)
  109.             {
  110.                 logConsole(log);
  111.             }
  112.         }
  113.  
  114.         // logs to console
  115.         private static void logConsole(String log)
  116.         {
  117.             Console.WriteLine(log);
  118.         }
  119.  
  120.         // logs to file
  121.         private static void logFile(String log)
  122.         {
  123.             log += Environment.NewLine;
  124.             fs.Write(Encoding.Default.GetBytes(log), 0, Encoding.Default.GetBytes(log).Length);
  125.  
  126.             if (flushAll)
  127.                 fs.Flush();
  128.         }
  129.  
  130.         // returns current date and time in format we like :)
  131.         private static String CurDateTime()
  132.         {
  133.             return DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss");
  134.         }
  135.  
  136.         public static void LogError(Exception e)
  137.         {
  138.             LogError("", e);
  139.         }
  140.  
  141.         public static void LogError(String msg, Exception e)
  142.         {
  143.             Log("ERROR: " + msg + " " + e.Message);
  144.         }
  145.     }
  146.  
  147.     public enum LogType
  148.     {
  149.         NOLOG = 0,              // no logging
  150.         FILE = 1,               // logging to file only
  151.         CONSOLE = 2,            // logging to console only
  152.         CONSOLEANDFILE = 4,     // logging to console and to file
  153.     }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement