mirror of
				https://github.com/jellyfin/jellyfin.git
				synced 2025-11-03 19:17:24 -05:00 
			
		
		
		
	Added ThreadedLogger
This commit is contained in:
		
							parent
							
								
									614668a447
								
							
						
					
					
						commit
						b1df61f7ce
					
				@ -1,13 +1,13 @@
 | 
				
			|||||||
using MediaBrowser.Controller;
 | 
					using MediaBrowser.Controller;
 | 
				
			||||||
using MediaBrowser.Model.DTO;
 | 
					using MediaBrowser.Model.DTO;
 | 
				
			||||||
using MediaBrowser.Model.Entities;
 | 
					using MediaBrowser.Model.Entities;
 | 
				
			||||||
 | 
					using MediaBrowser.Model.Entities.Movies;
 | 
				
			||||||
using MediaBrowser.Model.Entities.TV;
 | 
					using MediaBrowser.Model.Entities.TV;
 | 
				
			||||||
using System;
 | 
					using System;
 | 
				
			||||||
using System.Collections.Generic;
 | 
					using System.Collections.Generic;
 | 
				
			||||||
using System.Linq;
 | 
					using System.Linq;
 | 
				
			||||||
using System.Net;
 | 
					using System.Net;
 | 
				
			||||||
using System.Threading.Tasks;
 | 
					using System.Threading.Tasks;
 | 
				
			||||||
using MediaBrowser.Model.Entities.Movies;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace MediaBrowser.Api
 | 
					namespace MediaBrowser.Api
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
 | 
				
			|||||||
@ -79,6 +79,10 @@ namespace MediaBrowser.Common.Logging
 | 
				
			|||||||
            LogEntry(row);
 | 
					            LogEntry(row);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        protected virtual void Flush()
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        public virtual void Dispose()
 | 
					        public virtual void Dispose()
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
				
			|||||||
@ -7,7 +7,7 @@ namespace MediaBrowser.Common.Logging
 | 
				
			|||||||
    /// <summary>
 | 
					    /// <summary>
 | 
				
			||||||
    /// Provides a Logger that can write to any Stream
 | 
					    /// Provides a Logger that can write to any Stream
 | 
				
			||||||
    /// </summary>
 | 
					    /// </summary>
 | 
				
			||||||
    public class StreamLogger : BaseLogger
 | 
					    public class StreamLogger : ThreadedLogger
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        private Stream Stream { get; set; }
 | 
					        private Stream Stream { get; set; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -17,7 +17,7 @@ namespace MediaBrowser.Common.Logging
 | 
				
			|||||||
            Stream = stream;
 | 
					            Stream = stream;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        protected override void LogEntry(LogRow row)
 | 
					        protected override void AsyncLogMessage(LogRow row)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            byte[] bytes = new UTF8Encoding().GetBytes(row.ToString() + Environment.NewLine);
 | 
					            byte[] bytes = new UTF8Encoding().GetBytes(row.ToString() + Environment.NewLine);
 | 
				
			||||||
            Stream.Write(bytes, 0, bytes.Length);
 | 
					            Stream.Write(bytes, 0, bytes.Length);
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										75
									
								
								MediaBrowser.Common/Logging/ThreadedLogger.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										75
									
								
								MediaBrowser.Common/Logging/ThreadedLogger.cs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,75 @@
 | 
				
			|||||||
 | 
					using System;
 | 
				
			||||||
 | 
					using System.Collections.Generic;
 | 
				
			||||||
 | 
					using System.Linq;
 | 
				
			||||||
 | 
					using System.Text;
 | 
				
			||||||
 | 
					using System.Threading;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					namespace MediaBrowser.Common.Logging
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    public abstract class ThreadedLogger : BaseLogger
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        Thread loggingThread;
 | 
				
			||||||
 | 
					        Queue<Action> queue = new Queue<Action>();
 | 
				
			||||||
 | 
					        AutoResetEvent hasNewItems = new AutoResetEvent(false);
 | 
				
			||||||
 | 
					        volatile bool terminate = false;
 | 
				
			||||||
 | 
					        bool waiting = false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        public ThreadedLogger()
 | 
				
			||||||
 | 
					            : base()
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            loggingThread = new Thread(new ThreadStart(ProcessQueue));
 | 
				
			||||||
 | 
					            loggingThread.IsBackground = true;
 | 
				
			||||||
 | 
					            loggingThread.Start();
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        void ProcessQueue()
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            while (!terminate)
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                waiting = true;
 | 
				
			||||||
 | 
					                hasNewItems.WaitOne(10000, true);
 | 
				
			||||||
 | 
					                waiting = false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                Queue<Action> queueCopy;
 | 
				
			||||||
 | 
					                lock (queue)
 | 
				
			||||||
 | 
					                {
 | 
				
			||||||
 | 
					                    queueCopy = new Queue<Action>(queue);
 | 
				
			||||||
 | 
					                    queue.Clear();
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                foreach (var log in queueCopy)
 | 
				
			||||||
 | 
					                {
 | 
				
			||||||
 | 
					                    log();
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        protected override void LogEntry(LogRow row)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            lock (queue)
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                queue.Enqueue(() => AsyncLogMessage(row));
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            hasNewItems.Set();
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        protected abstract void AsyncLogMessage(LogRow row);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        protected override void Flush()
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            while (!waiting)
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                Thread.Sleep(1);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        public override void Dispose()
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            Flush();
 | 
				
			||||||
 | 
					            terminate = true;
 | 
				
			||||||
 | 
					            hasNewItems.Set();
 | 
				
			||||||
 | 
					            base.Dispose();
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -83,6 +83,7 @@
 | 
				
			|||||||
  <ItemGroup>
 | 
					  <ItemGroup>
 | 
				
			||||||
    <Compile Include="Kernel\BaseApplicationPaths.cs" />
 | 
					    <Compile Include="Kernel\BaseApplicationPaths.cs" />
 | 
				
			||||||
    <Compile Include="Drawing\DrawingUtils.cs" />
 | 
					    <Compile Include="Drawing\DrawingUtils.cs" />
 | 
				
			||||||
 | 
					    <Compile Include="Logging\ThreadedLogger.cs" />
 | 
				
			||||||
    <Compile Include="Net\Handlers\StaticFileHandler.cs" />
 | 
					    <Compile Include="Net\Handlers\StaticFileHandler.cs" />
 | 
				
			||||||
    <Compile Include="Net\MimeTypes.cs" />
 | 
					    <Compile Include="Net\MimeTypes.cs" />
 | 
				
			||||||
    <Compile Include="Properties\Resources.Designer.cs">
 | 
					    <Compile Include="Properties\Resources.Designer.cs">
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user