namespace Eulersharp.Output { using System; using System.Collections; using System.Runtime.CompilerServices; /// /// This class manages the output from the euler component /// public class Outputter:ILogger, IProof, IResult { #region Declarations private static object LOCK = new object(); private static Outputter instance = null; /// /// Flag that indicates whether there are log listeners interested in logging /// info. /// public static bool log = false; /// /// This enumeration defines the different log levels /// public enum LOGLEVEL { /// /// SEVERE log level /// SEVERE, /// /// SEVERE log level /// WARNING, /// /// WARNING log level /// INFO, /// /// INFO log level /// CONFIG, /// /// CONFIG log level /// FINE, /// /// FINE log level /// FINER, /// /// FINEST log level /// FINEST }; private ArrayList _logListeners = null; private ArrayList _proofListeners = new ArrayList (); private ArrayList _resultListeners = new ArrayList (); #endregion #region Constructor and Destructor /// /// This method constructs an instance of the outputter class. This /// constructor is private because this class is a singleton. The only /// instance of this class should be retrieved via the getInstance() method. /// private Outputter() { } /// /// This class acts as a singleton. getInstance() returns the only instance of /// this class. /// /// the only instance of this class public static Outputter GetInstance() { lock(LOCK) { if (instance == null) { instance = new Outputter(); } } return instance; } /// /// This method releases the only instance of this class. All internal /// state will be cleared. /// public static void ReleaseInstance() { lock(LOCK) { if (instance != null) { // clear all internal state instance._logListeners = null; instance._proofListeners = null; instance._resultListeners = null; // clear instance instance = null; } } } #endregion #region Public methods /// /// This method registers a log listener. From now on this log listener will /// also receive log messages from the euler component /// /// the logger to be registered. [MethodImpl(MethodImplOptions.Synchronized)] public void AddLogger(ILogger logger) { if (_logListeners == null) { _logListeners = new ArrayList(); log = true; } if (!_logListeners.Contains(logger)) { _logListeners.Add(logger); } } /// /// This method unregisters a log listener. From now on this log listener will /// receive no log messages anymore from the euler component. /// /// the logger to be removed as a listener. [MethodImpl(MethodImplOptions.Synchronized)] public void RemoveLogger(ILogger logger) { if (_logListeners.Contains(logger)) { _logListeners.Remove(logger); } if (_logListeners.Count == 0) { _logListeners = null; log = false; } } /// /// This method registers a result listener. From now on this result listener /// will also receive the results of the different test cases /// /// the result listener to be registered. [MethodImpl(MethodImplOptions.Synchronized)] public void AddResultListener(IResult result) { if (!_resultListeners.Contains(result)) { _resultListeners.Add(result); } } /// /// This method unregisters a result listener. From now on this result /// listener will receive no results anymore from the euler component. /// /// the result listener to be removed. [MethodImpl(MethodImplOptions.Synchronized)] public void RemoveResultListener(IResult result) { if (_resultListeners.Contains(result)) { _resultListeners.Remove(result); } } /// /// This method registers a proof listener. From now on this proof listener /// will also receive the proofs of the different test cases /// /// the proof listener to be registered. [MethodImpl(MethodImplOptions.Synchronized)] public void AddProofListener(IProof proof) { if (!_proofListeners.Contains(proof)) { _proofListeners.Add(proof); } } /// /// This method unregisters a proof listener. From now on this proof listener /// will receive no proofs anymore from the euler component. /// /// the proof listener to be removed. [MethodImpl(MethodImplOptions.Synchronized)] public void RemoveProofListener(IProof proof) { if (_proofListeners.Contains(proof)) { _proofListeners.Remove(proof); } } #endregion #region Implementation of interface ILogger /// /// This method distributes a log message to the different interested log /// /// the name of the class that initiated the message /// the method that initiated the log message /// the log message /// the log level [MethodImpl(MethodImplOptions.Synchronized)] public void Log(string className, string methodName, string message, LOGLEVEL logLevel) { if (_logListeners != null && Enum.IsDefined(typeof(LOGLEVEL), logLevel)) { for (IEnumerator en = _logListeners.GetEnumerator(); en.MoveNext();) { ( (ILogger) en.Current).Log(className, methodName, message, logLevel); } } } #endregion #region Implementation of interface IResult /// /// This method distributes the result of a test case to the different /// interested listeners. /// /// the name of the test case /// the result of the test case [MethodImpl(MethodImplOptions.Synchronized)] public void Result(string testCase, string result) { for (IEnumerator en = _resultListeners.GetEnumerator(); en.MoveNext();) { ((IResult) en.Current).Result(testCase, result); } } #endregion #region Implementation of interface IProof /// /// This method reports the proof for a specific test case /// /// the name of the test case /// the proof (if found) [MethodImpl(MethodImplOptions.Synchronized)] public void Proof(string testCase, string proof) { for (IEnumerator en = _proofListeners.GetEnumerator(); en.MoveNext();) { ((IProof) en.Current).Proof(testCase, proof); } } #endregion } }