⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 applicationlogs.cs

📁 包括Pheromones Algorythm、Memory Algorythm和Hill Climbing Algorythm I
💻 CS
字号:
using System;
using System.Collections;
using System.IO;
using System.Windows.Forms;
using System.Text;

namespace PathFinder
{
	/// <summary>
	/// basic adding and getting log functions
	/// </summary>
	public class ApplicationLog
	{
		/// <summary>
		/// array to hold the progress logs
		/// </summary>
		private static ArrayList arrayLogs;


		public static ArrayList Logs
		{
			get
			{
				return arrayLogs;
			}
		}

		/// <summary>
		/// basic constructor
		/// </summary>
		static ApplicationLog()
		{
			arrayLogs = new ArrayList();
		}

	
		/// <summary>
		/// add an entry to the log
		/// </summary>
		/// <param name="strLog"></param>
		public static void Log( string name, string message )
		{
			bool bFound = false;

			for( int i=0; i<arrayLogs.Count; i++ )
			{
				if( ( ( IndividualLog )arrayLogs[ i ] ).LogName == name )
				{
					bFound = true;

					IndividualLog log = ( IndividualLog )arrayLogs[ i ];

					if( log.LogAll == false )
					{
						while( log.Logs.Count >= log.LogCount )
						{
							log.Logs.RemoveAt( 0 );
						}
					}

					log.Log = message;

					break;
				}
			}

			/// if no log found for log name create one
			if( bFound == false )
			{
				IndividualLog log = new IndividualLog( name );
				Logs.Add( log );
				log.Log = message;
			}

		}



		/// <summary>
		/// add a standard log
		/// </summary>
		/// <param name="log"></param>
		public static void AddLog( string name )
		{
			IndividualLog log = new IndividualLog( name );
			Logs.Add( log );
		}

		public static void AddLog( string name, bool logLastTen )
		{
			IndividualLog log = new IndividualLog( name );
			Logs.Add( log );
			log.LogLastTen = logLastTen;
		}

		public static void AddLog( string name, bool logLastTen, bool logLastTwenty )
		{
			IndividualLog log = new IndividualLog( name );
			Logs.Add( log );
			log.LogLastTen = logLastTen;
			log.LogLastTwenty = logLastTwenty;
		}

		public static void AddLog( string name, bool logLastTen, bool logLastTwenty, bool logLastThirty )
		{
			IndividualLog log = new IndividualLog( name );
			Logs.Add( log );
			log.LogLastTen = logLastTen;
			log.LogLastTwenty = logLastTwenty;
			log.LogLastThirty = logLastThirty;
		}

		public static void AddLog( string name, bool logLastTen, bool logLastTwenty, bool logLastThirty, bool logAll )
		{
			IndividualLog log = new IndividualLog( name );
			Logs.Add( log );
			log.LogLastTen = logLastTen;
			log.LogLastTwenty = logLastTwenty;
			log.LogLastThirty = logLastThirty;
			log.LogAll = logAll;
		}


		/// <summary>
		/// get the last entry in the log
		/// </summary>
		/// <returns></returns>
		public static string GetLatest()
		{
			if( arrayLogs.Count > 0 )
			{
				long lTicks = 0;
				IndividualLog log = null;

				for( int i=0; i<arrayLogs.Count; i++ )
				{
					IndividualLog tempLog = ( IndividualLog )arrayLogs[ i ];

					if( lTicks < tempLog.Ticks )
					{
						lTicks = tempLog.Ticks;
						log = tempLog;
					}
				}

				return ( string )log.Logs[ log.Logs.Count-1 ];
			}
			else
				return null;
		}


		public static void Clear()
		{
			arrayLogs.Clear();
		}

		/// <summary>
		/// Write the log to a text file Note this should be written at the end of the application run
		/// NOTE TO USE APPLICATION PRODUCT NAME FILL IN PRODUCT IN ASSEMBLY
		/// </summary>
		/// <param name="fileName">optional name of the file pass null to use application name</param>
		public static void WriteToTextFile( string fileName )
		{
			StreamWriter streamWriter;

			if( fileName == null )
			{
				if( File.Exists( Application.ProductName + ".txt" ) == false )
				{
					streamWriter = File.CreateText( Application.ProductName + ".txt" );
				}
				else
				{
					File.Delete( Application.ProductName + ".txt" );
					streamWriter = File.CreateText( Application.ProductName + ".txt" );
				}
			}
			else
			{
				StringBuilder strString = new StringBuilder( fileName );
				if( fileName.LastIndexOf( ".txt" ) == -1 )
				{
					strString.Append( ".txt" );
				}

				if( File.Exists( strString.ToString() ) == false )
				{
					streamWriter = File.CreateText( strString.ToString() );
				}
				else
				{
					File.Delete( strString.ToString() );
					streamWriter = File.CreateText( strString.ToString() );
				}
			}

			for( int i=0; i<Logs.Count; i++ )
			{
				( ( IndividualLog )Logs[ i ] ).WriteToTextFile( streamWriter );
			}

			streamWriter.Flush();
			streamWriter.Close();
		}
	}

	public class IndividualLog
	{
		/// <summary>
		/// Number of logs to keep
		/// </summary>
		private int nLogCount = 10;
		/// <summary>
		/// array to hold the progress logs
		/// </summary>
		private ArrayList arrayLogs;
		/// <summary>
		/// log just the last ten
		/// </summary>
		private bool bLogLastTen;
		/// <summary>
		/// log just the last twenty
		/// </summary>
		private bool bLogLastTwenty;
		/// <summary>
		/// log just the last thirty
		/// </summary>
		private bool bLogLastThirty;
		/// <summary>
		/// log all
		/// </summary>
		private bool bLogAll;
		/// <summary>
		/// has the latest progress log been shown?
		/// </summary>
		private bool bLatestShown;
		/// <summary>
		/// name of this log
		/// </summary>
		private string strLogName;
		/// <summary>
		/// ticks for the last message
		/// </summary>
		private long lTicks;

		/// <summary>
		/// get and set the number of logs to retain in the array
		/// </summary>
		public int LogCount
		{
			get
			{
				return nLogCount;
			}
			set
			{
				nLogCount = value;
			}
		}

		/// <summary>
		/// allow access to the log array
		/// </summary>
		public ArrayList Logs
		{
			get
			{
				return arrayLogs;
			}
		}

		/// <summary>
		/// log a message
		/// </summary>
		public string Log
		{
			set
			{
				arrayLogs.Add( value );
				lTicks = DateTime.Now.Ticks;
			}
		}

		public bool LogLastTen
		{
			get
			{
				return bLogLastTen;
			}
			set
			{
				bLogLastTen = value;
				if( bLogLastTen == true )
				{
					nLogCount = 10;
				}
			}
		}

		public bool LogLastTwenty
		{
			get
			{
				return bLogLastTwenty;
			}
			set
			{
				bLogLastTwenty = value;
				if( bLogLastTwenty == true )
				{
					nLogCount = 20;
				}
			}
		}

		public bool LogLastThirty
		{
			get
			{
				return bLogLastThirty;
			}
			set
			{
				bLogLastThirty = value;
				if( bLogLastThirty == true )
				{
					nLogCount = 30;
				}
			}
		}

		public bool LogAll
		{
			get
			{
				return bLogAll;
			}
			set
			{
				bLogAll = value;
			}
		}

		public bool LatestShown
		{
			get
			{
				return bLatestShown;
			}
		}

		public string LogName
		{
			get
			{
				return strLogName;
			}
			set
			{
				strLogName = value;
			}
		}

		public long Ticks
		{
			get
			{
				return lTicks;
			}
		}

		public IndividualLog()
		{
			arrayLogs = new ArrayList();
			LogLastTen = true;
			LogLastTwenty = false;
			LogLastThirty = false;
			LogAll = false;
			bLatestShown = true;
		}

		public IndividualLog( string name ) : this()
		{
			LogName = name;
		}

		public void WriteToTextFile( StreamWriter streamWriter )
		{
			streamWriter.WriteLine();
			streamWriter.WriteLine( "Log For " + strLogName );
			streamWriter.WriteLine();
			streamWriter.WriteLine( "Log Last Ten " + bLogLastTen.ToString() );
			streamWriter.WriteLine( "Log Last Twenty " + bLogLastTwenty.ToString() );
			streamWriter.WriteLine( "Log Last Thirty " + bLogLastThirty.ToString() );
			streamWriter.WriteLine( "Log All " + bLogAll.ToString() );
			streamWriter.WriteLine();
			streamWriter.WriteLine( "Logged Messages" );
			for( int i=0; i<this.Logs.Count; i++ )
			{
				streamWriter.WriteLine( ( string )Logs[ i ] );
			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -