Skip to main content
GameDev.net gamedev.net
🔒 Locked

Logger

Started by jperalta Jul 1, 2004 at 4:50 PM 1 replies 450+ views
Original Post
jperalta
jperalta
Below is the source for my new logging class, take a look at it and tell me what you think. Any comments/criticisms/suggestions are welcome so long as they are constructive.

using System;
using System.IO;

namespace Utilities
{
	/// <summary>
	/// The Logger class is a class to be used for implementing logging in any type of application.
	/// The class also contains a function to be used for printing exceptions to the screen in
	/// console applications.
	/// </summary>
	public class Logger
	{
		FileStream logStream;
		StreamWriter logWriter;
		bool logToScreen;

		public bool LogToScreen 
		{
			get { return logToScreen; }
			set { logToScreen = value; }
		}

		public Logger() : this(true) {}

		public Logger(bool a_logToScreen) 
		{
			logToScreen = a_logToScreen;
		}

		public Logger(string path) : this(true) 
		{
			StartLog(path);
		}

		public Logger(string path, bool a_logToScreen) : this(a_logToScreen) 
		{
			StartLog(path);
		}

		public void StartLog(string path) 
		{	
			try 
			{
				logWriter = File.AppendText(path);
				logStream = (FileStream)logWriter.BaseStream;
			}
			catch (Exception e) 
			{
				// Do not log this error because the log should not be open.
				PrintError(e);
			}
			WriteToLog("Log started.",true);
		}

		public void CloseLog() 
		{
			WriteToLog("Log stopped.",true);
			try 
			{
				logWriter.Close();
				logStream.Close();
			}
			catch (Exception e) 
			{
				// Do not log this exception because the log should not be open.
				PrintError(e);
			}
		}

		public void WriteToLog(string s) 
		{
			try 
			{
				logWriter.WriteLine("{0} : {1}",DateTime.Now.ToLongTimeString(),s);
				logWriter.Flush();
				if (logToScreen)
					Console.Out.WriteLine("{0} : {1}",DateTime.Now.ToLongTimeString(),s);
			}
			catch (Exception e) 
			{
				// Do not log exceptions during logging because we're probably
				// just going to create more exceptions
				PrintError(e);
			}
		}

		public void WriteToLog(string s, bool logDate) 
		{
			try 
			{
				if (logDate)
					logWriter.Write("{0} : ",DateTime.Now.ToLongDateString());
				logWriter.WriteLine("{0} : {1}",DateTime.Now.ToLongTimeString(),s);
				if (logToScreen)
					Console.Out.WriteLine("{0} : {1}",DateTime.Now.ToLongTimeString(),s);
			}
			catch (Exception e) 
			{
				// Do not log exceptions during logging because we're probably
				// just going to create more exceptions
				PrintError(e);
			}
		}

		~Logger() 
		{
			try 
			{
				logWriter.Close();
				logWriter = null;
				logStream.Close();
				logStream = null;
			}
			catch (Exception e) 
			{
				// Theoretically an exception here means the stream is already closed so
				// we probably shouldn't try to log this exception.
				PrintError(e);
			}
		}

		/// <summary>
		/// This function is used to print exceptions to the screen.
		/// </summary>
		/// <param name="e">The exception to be printed.</param>
		public static void PrintError(Exception e) 
		{
			Console.Error.WriteLine("{0} : {1}",e.GetType().ToString(),e.Message);
		}
	}
}
antareus
antareus
Rename WriteToLog to Write(). Provide an optional WriteLine() to mimic the stream classes. Write() and WriteLine() should accept a format string just like the stream classes do. Also consider printing to debug output -- look up OutputDebugString() in the Win32 API. Ditch the StartLog() method, it seems a bit redundant. And consider renaming CloseLog to just Close(). The PrintError function should be called WriteError() to match the Write() method, but WriteError seems extraneous - I'm pretty sure you can just print an Exception out as is.

You should let the exception propagate into the client class, too. I'd be a bit peeved if it silently failed and I couldn't tell why.

Stylistically, I like to emphasize that class member variables are private, but thats just me.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
Chris Hare
Chris Hare
Quote:
Original post by antareus

Stylistically, I like to emphasize that class member variables are private, but thats just me.


Agreed, in both Java and C++.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.