Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Simple Logger Class
- Author: Satik
- */
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.IO;
- namespace Tools
- {
- public static class Logger
- {
- private static LogType logType = LogType.NOLOG;
- private static String fileName = "log.txt";
- private static FileStream fs;
- private static bool flushAll = true;
- /// <summary>
- /// Inits logging, opens logging file streams etc.
- /// </summary>
- public static bool Init(LogType lt, String filename)
- {
- logType = lt;
- if (((byte)lt & (byte)LogType.FILE) > 0)
- {
- if (filename != null && filename.Trim() != "")
- {
- fileName = filename;
- }
- else
- return false;
- }
- return Init();
- }
- /// <summary>
- /// Closes logging filestream!
- /// </summary>
- public static void Close()
- {
- if (fs != null)
- {
- fs.Close();
- fs.Dispose();
- }
- }
- private static bool Init()
- {
- try
- {
- if (((byte)logType & (byte)LogType.FILE) > 0)
- {
- fs = new FileStream(fileName, FileMode.Create);
- }
- }
- catch (Exception)
- {
- return false;
- }
- Log("===================", false);
- Log(Constants.GameName + " v " + Constants.Version, false);
- Log("Logging initialized", false);
- Log("Current dir: " + Environment.CurrentDirectory, false);
- Log("Date/Time: " + CurDateTime(), false);
- Log("Computer name: " + Environment.MachineName, false);
- Log("OS: " + Environment.OSVersion, false);
- Log("CPUs: " + Environment.ProcessorCount, false);
- //Log("GPU: "+Microsoft.Xna.Framework.GraphicsDeviceManager.);
- return true;
- }
- public static void LogGPU(Microsoft.Xna.Framework.GraphicsDeviceManager gdm)
- {
- Log("GPU Description: " + gdm.GraphicsDevice.CreationParameters.Adapter.Description);
- Log("GPU Driver: " + gdm.GraphicsDevice.CreationParameters.Adapter.DriverDll + ", version " + gdm.GraphicsDevice.CreationParameters.Adapter.DriverVersion);
- 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 );
- Log("===================", false);
- Log(" ", false);
- }
- /// <summary>
- /// Logs message, automatically adds timestamp.
- /// </summary>
- public static void Log(String log)
- {
- Log(log, true);
- }
- /// <summary>
- /// Logs message with optional timestamp.
- /// </summary>
- public static void Log(String log, bool addTimestamp)
- {
- if (addTimestamp) log = CurDateTime() + " - " + log;
- if (logType == LogType.FILE || logType == LogType.CONSOLEANDFILE)
- {
- logFile(log);
- }
- if (logType == LogType.CONSOLE || logType == LogType.CONSOLEANDFILE)
- {
- logConsole(log);
- }
- }
- // logs to console
- private static void logConsole(String log)
- {
- Console.WriteLine(log);
- }
- // logs to file
- private static void logFile(String log)
- {
- log += Environment.NewLine;
- fs.Write(Encoding.Default.GetBytes(log), 0, Encoding.Default.GetBytes(log).Length);
- if (flushAll)
- fs.Flush();
- }
- // returns current date and time in format we like :)
- private static String CurDateTime()
- {
- return DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss");
- }
- public static void LogError(Exception e)
- {
- LogError("", e);
- }
- public static void LogError(String msg, Exception e)
- {
- Log("ERROR: " + msg + " " + e.Message);
- }
- }
- public enum LogType
- {
- NOLOG = 0, // no logging
- FILE = 1, // logging to file only
- CONSOLE = 2, // logging to console only
- CONSOLEANDFILE = 4, // logging to console and to file
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement