Exemplo n.º 1
0
        // getCurrent
        /// Gets the current log client from the top of the setack
        ///////////////////////////////////////////////////
        public static LogClient getCurrent()
        {               //Enter lock
            m_threadlock.AcquireReaderLock(Timeout.Infinite);

            //Get the current logclient
            logstack clientstack = null;
            int      threadid    = Thread.CurrentThread.ManagedThreadId;

            try
            {                   //The list may be modified, so we want to remain in sync
                if (!m_threads.TryGetValue(threadid, out clientstack))
                {               //Failed to find anything related to the thread
                                //Let's assume that the stack just hasn't been created
                    return(null);
                }

                //Take a look
                if (clientstack.Count == 0)
                {
                    return(null);
                }
                return(clientstack.Peek());
            }
            finally
            {                   //Release again
                m_threadlock.ReleaseReaderLock();
            }
        }
Exemplo n.º 2
0
        internal static void write(TLog type, string Message, Thread thread, bool bCautious)
        {               //Get a reader lock
            m_threadlock.AcquireReaderLock(Timeout.Infinite);

            //We need to release the sync object afterwards
            LogClient client = null;

            try
            {                   //Get the current logclient
                logstack clientstack = null;
                int      threadid    = thread.ManagedThreadId;

                //The list may be modified, so we want to remain in sync
                if (!m_threads.TryGetValue(threadid, out clientstack))
                {                       //Failed to find it; make a note
                    lock (m_warning)
                    {
                        m_warning.WriteLine("[" + DateTime.Now.ToLongTimeString() + "][Log] Failed to find associated clientstack for thread. (write)");
                        m_warning.WriteLine("[" + DateTime.Now.ToLongTimeString() + "][Log] Message: " + Message);
                        m_warning.Flush();
                    }
                    return;
                }

                //If we don't have any logclients, don't bother
                if (clientstack.Count == 0)
                {                       //Notify them
                    lock (m_warning)
                    {
                        m_warning.WriteLine("[" + DateTime.Now.ToLongTimeString() + "][Log] Attempted to write to log when no LogClients were active.");
                        m_warning.Flush();
                    }
                    return;
                }

                //Got it!
                client = clientstack.Peek();
            }
            finally
            {                   //Release our lock
                m_threadlock.ReleaseReaderLock();
            }

            //Redirect
            write(type, Message, client, bCautious);
        }