//StealthNet
//Copyright (C) 2010 T.Norad

//This program is free software; you can redistribute it and/or
//modify it under the terms of the GNU General Public License
//as published by the Free Software Foundation; either version 2
//of the License, or (at your option) any later version.

//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with this program; if not, write to the Free Software
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

using System;
using System.Collections.Generic;
using System.Text;
using Regensburger.RCollections.ArrayBased;

namespace Regensburger.RShare
{
    public sealed class LogObserver : ILogObserver
    {
        private RList<LogEntry> m_LogEntries = null;

        // singelton instance and lock object
        private static LogObserver instance = null;
        private static readonly object padlock = new object();

        /// <summary>
        /// Private constructor to prevent creating manually an instance of this class.
        /// </summary>
        private LogObserver()
        {
            m_LogEntries = new RList<LogEntry>();
        }

        /// <summary>
        /// Singelton implementation
        /// </summary>
        public static LogObserver Instance
        {
            get
            {
                lock (padlock)
                {
                    if (instance == null)
                    {
                        instance = new LogObserver();
                    }
                    return instance;
                }
            }
        }

        /// <summary>
        /// Method implementation from ILogObserver
        /// </summary>
        delegate void NewLogEntryDelegate(LogEntry logEntry);
        public void NewLogEntry(LogEntry logEntry)
        {
            m_LogEntries.Add(logEntry);
        }

        /// <summary>
        /// Return the list of log entries
        /// </summary>
        public RList<LogEntry> LogEntryList
        {
            get { return m_LogEntries; }
        }

        public void AttachLogObserver()
        {
            Logger.Instance.Attach(this);
        }

        public void DetachLogObserver()
        {
            Logger.Instance.Detach(this);
        }
    }
}
