Skip to content

Commit 9ab0977

Browse files
adopt linux log rotation
1 parent ae86032 commit 9ab0977

File tree

19 files changed

+137
-119
lines changed

19 files changed

+137
-119
lines changed

Emby.Server.Implementations/HttpServer/HttpListenerHost.cs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -527,11 +527,7 @@ protected async Task RequestHandler(IHttpRequest httpReq, string urlString, stri
527527

528528
if (!ValidateSsl(httpReq.RemoteIp, urlString))
529529
{
530-
var httpsUrl = urlString
531-
.Replace("http://", "https://", StringComparison.OrdinalIgnoreCase)
532-
.Replace(":" + _config.Configuration.PublicPort.ToString(CultureInfo.InvariantCulture), ":" + _config.Configuration.PublicHttpsPort.ToString(CultureInfo.InvariantCulture), StringComparison.OrdinalIgnoreCase);
533-
534-
RedirectToUrl(httpRes, httpsUrl);
530+
RedirectToSecureUrl(httpReq, httpRes, urlString);
535531
return;
536532
}
537533

@@ -730,6 +726,30 @@ private void Write(IResponse response, string text)
730726
outputStream.Write(bOutput, 0, bOutput.Length);
731727
}
732728

729+
private void RedirectToSecureUrl(IHttpRequest httpReq, IResponse httpRes, string url)
730+
{
731+
int currentPort;
732+
Uri uri;
733+
if (Uri.TryCreate(url, UriKind.Absolute, out uri))
734+
{
735+
currentPort = uri.Port;
736+
var builder = new UriBuilder(uri);
737+
builder.Port = _config.Configuration.PublicHttpsPort;
738+
builder.Scheme = "https";
739+
url = builder.Uri.ToString();
740+
741+
RedirectToUrl(httpRes, url);
742+
}
743+
else
744+
{
745+
var httpsUrl = url
746+
.Replace("http://", "https://", StringComparison.OrdinalIgnoreCase)
747+
.Replace(":" + _config.Configuration.PublicPort.ToString(CultureInfo.InvariantCulture), ":" + _config.Configuration.PublicHttpsPort.ToString(CultureInfo.InvariantCulture), StringComparison.OrdinalIgnoreCase);
748+
749+
RedirectToUrl(httpRes, url);
750+
}
751+
}
752+
733753
public static void RedirectToUrl(IResponse httpRes, string url)
734754
{
735755
httpRes.StatusCode = 302;

Emby.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpRequest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ public static string GetResponseContentType(IRequest httpReq)
260260
var serverDefaultContentType = "application/json";
261261

262262
var acceptContentTypes = httpReq.AcceptTypes;
263-
var defaultContentType = httpReq.ContentType;
263+
string defaultContentType = null;
264264
if (HasAnyOfContentTypes(httpReq, FormUrlEncoded, MultiPartFormData))
265265
{
266266
defaultContentType = serverDefaultContentType;

Emby.Server.Implementations/IO/ManagedFileSystem.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,13 @@ public string MakeAbsolutePath(string folderPath, string filePath)
161161
return filePath;
162162
}
163163

164-
if (filePath[0] == '/' || filePath[0] == '\\') //relative path
164+
var firstChar = filePath[0];
165+
if (firstChar == '/')
166+
{
167+
// For this we don't really know.
168+
return filePath;
169+
}
170+
if (firstChar == '\\') //relative path
165171
{
166172
filePath = filePath.Substring(1);
167173
}

Emby.Server.Implementations/Library/MediaSourceManager.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ await item.RefreshMetadata(new MediaBrowser.Controller.Providers.MetadataRefresh
150150
{
151151
SetUserProperties(hasMediaSources as BaseItem, source, user);
152152
}
153-
153+
154154
// Validate that this is actually possible
155155
if (source.SupportsDirectStream)
156156
{
@@ -188,10 +188,12 @@ private bool SupportsDirectStream(string path, MediaProtocol protocol)
188188
{
189189
if (path != null)
190190
{
191-
if (path.IndexOf(".m3u", StringComparison.OrdinalIgnoreCase) == -1)
191+
if (path.IndexOf(".m3u", StringComparison.OrdinalIgnoreCase) != -1)
192192
{
193-
//return true;
193+
return false;
194194
}
195+
196+
return true;
195197
}
196198
}
197199

Emby.Server.Implementations/Logging/SimpleLogManager.cs

Lines changed: 71 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,27 +31,31 @@ public ILogger GetLogger(string name)
3131
return new NamedLogger(name, this);
3232
}
3333

34-
public void ReloadLogger(LogSeverity severity)
34+
public async Task ReloadLogger(LogSeverity severity, CancellationToken cancellationToken)
3535
{
3636
LogSeverity = severity;
3737

3838
var logger = _fileLogger;
3939
if (logger != null)
4040
{
4141
logger.Dispose();
42+
await TryMoveToArchive(logger.Path, cancellationToken).ConfigureAwait(false);
4243
}
4344

44-
var path = Path.Combine(LogDirectory, LogFilePrefix + "-" + decimal.Floor(DateTime.Now.Ticks / 10000000) + ".txt");
45+
var newPath = Path.Combine(LogDirectory, LogFilePrefix + ".txt");
4546

46-
_fileLogger = new FileLogger(path);
47+
if (File.Exists(newPath))
48+
{
49+
newPath = await TryMoveToArchive(newPath, cancellationToken).ConfigureAwait(false);
50+
}
51+
52+
_fileLogger = new FileLogger(newPath);
4753

4854
if (LoggerLoaded != null)
4955
{
5056
try
5157
{
52-
5358
LoggerLoaded(this, EventArgs.Empty);
54-
5559
}
5660
catch (Exception ex)
5761
{
@@ -60,6 +64,42 @@ public void ReloadLogger(LogSeverity severity)
6064
}
6165
}
6266

67+
private async Task<string> TryMoveToArchive(string file, CancellationToken cancellationToken, int retryCount = 0)
68+
{
69+
var archivePath = GetArchiveFilePath();
70+
71+
try
72+
{
73+
File.Move(file, archivePath);
74+
75+
return file;
76+
}
77+
catch (FileNotFoundException)
78+
{
79+
return file;
80+
}
81+
catch (DirectoryNotFoundException)
82+
{
83+
return file;
84+
}
85+
catch
86+
{
87+
if (retryCount >= 50)
88+
{
89+
return GetArchiveFilePath();
90+
}
91+
92+
await Task.Delay(100, cancellationToken).ConfigureAwait(false);
93+
94+
return await TryMoveToArchive(file, cancellationToken, retryCount + 1).ConfigureAwait(false);
95+
}
96+
}
97+
98+
private string GetArchiveFilePath()
99+
{
100+
return Path.Combine(LogDirectory, LogFilePrefix + "-" + decimal.Floor(DateTime.Now.Ticks / 10000000) + ".txt");
101+
}
102+
63103
public event EventHandler LoggerLoaded;
64104

65105
public void Flush()
@@ -104,6 +144,9 @@ public void Dispose()
104144
if (logger != null)
105145
{
106146
logger.Dispose();
147+
148+
var task = TryMoveToArchive(logger.Path, CancellationToken.None);
149+
Task.WaitAll(task);
107150
}
108151

109152
_fileLogger = null;
@@ -119,9 +162,13 @@ public class FileLogger : IDisposable
119162
private readonly CancellationTokenSource _cancellationTokenSource;
120163
private readonly BlockingCollection<string> _queue = new BlockingCollection<string>();
121164

165+
public string Path { get; set; }
166+
122167
public FileLogger(string path)
123168
{
124-
Directory.CreateDirectory(Path.GetDirectoryName(path));
169+
Path = path;
170+
171+
Directory.CreateDirectory(System.IO.Path.GetDirectoryName(path));
125172

126173
_fileStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read, 32768);
127174
_cancellationTokenSource = new CancellationTokenSource();
@@ -144,6 +191,10 @@ private void LogInternal()
144191
}
145192

146193
_fileStream.Write(bytes, 0, bytes.Length);
194+
if (_disposed)
195+
{
196+
return;
197+
}
147198

148199
_fileStream.Flush(true);
149200
}
@@ -177,12 +228,23 @@ public void Flush()
177228

178229
public void Dispose()
179230
{
231+
if (_disposed)
232+
{
233+
return;
234+
}
235+
236+
_disposed = true;
180237
_cancellationTokenSource.Cancel();
181238

182-
Flush();
239+
var stream = _fileStream;
240+
if (stream != null)
241+
{
242+
using (stream)
243+
{
244+
stream.Flush(true);
245+
}
246+
}
183247

184-
_disposed = true;
185-
_fileStream.Dispose();
186248
GC.SuppressFinalize(this);
187249
}
188250
}

Emby.Server.Implementations/ScheduledTasks/Tasks/ReloadLoggerFileTask.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,9 @@ public Task Execute(CancellationToken cancellationToken, IProgress<double> progr
5858

5959
progress.Report(0);
6060

61-
LogManager.ReloadLogger(ConfigurationManager.CommonConfiguration.EnableDebugLevelLogging
61+
return LogManager.ReloadLogger(ConfigurationManager.CommonConfiguration.EnableDebugLevelLogging
6262
? LogSeverity.Debug
63-
: LogSeverity.Info);
64-
65-
return Task.FromResult(true);
63+
: LogSeverity.Info, cancellationToken);
6664
}
6765

6866
/// <summary>
@@ -96,7 +94,7 @@ public string Category
9694

9795
public bool IsHidden
9896
{
99-
get { return true; }
97+
get { return false; }
10098
}
10199

102100
public bool IsEnabled

MediaBrowser.Controller/Entities/Folder.cs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -363,22 +363,17 @@ private async Task ValidateChildrenInternal2(IProgress<double> progress, Cancell
363363

364364
if (IsFileProtocol)
365365
{
366-
var isOffline = false;
367366
IEnumerable<BaseItem> nonCachedChildren;
368367

369368
try
370369
{
371370
nonCachedChildren = GetNonCachedChildren(directoryService);
372371
}
373-
catch (IOException ex)
372+
catch (Exception ex)
374373
{
375-
nonCachedChildren = new BaseItem[] { };
376-
377-
isOffline = true;
374+
return;
378375
}
379376

380-
if (nonCachedChildren == null) return; //nothing to validate
381-
382377
progress.Report(5);
383378

384379
if (recursive)
@@ -428,9 +423,6 @@ private async Task ValidateChildrenInternal2(IProgress<double> progress, Cancell
428423
{
429424
}
430425

431-
else if (isOffline)
432-
{
433-
}
434426
else
435427
{
436428
Logger.Debug("Removed item: " + item.Path);
@@ -1481,8 +1473,6 @@ public List<BaseItem> GetLinkedChildren(User user)
14811473
.OfType<Folder>()
14821474
.ToList();
14831475

1484-
//var allUserRootChildren = LibraryManager.GetUserRootFolder().Children.OfType<Folder>().ToList();
1485-
14861476
var collectionFolderIds = allUserRootChildren
14871477
.Select(i => i.Id)
14881478
.ToList();

MediaBrowser.Model/Logging/ILogManager.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System;
2+
using System.Threading;
3+
using System.Threading.Tasks;
24

35
namespace MediaBrowser.Model.Logging
46
{
@@ -29,7 +31,7 @@ public interface ILogManager
2931
/// <summary>
3032
/// Reloads the logger.
3133
/// </summary>
32-
void ReloadLogger(LogSeverity severity);
34+
Task ReloadLogger(LogSeverity severity, CancellationToken cancellationToken);
3335

3436
/// <summary>
3537
/// Occurs when [logger loaded].

MediaBrowser.Model/Providers/RemoteSearchResult.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,12 @@ public class RemoteSearchResult : IHasProviderIds
3535
public string Overview { get; set; }
3636

3737
public RemoteSearchResult AlbumArtist { get; set; }
38+
public RemoteSearchResult[] Artists { get; set; }
3839

3940
public RemoteSearchResult()
4041
{
4142
ProviderIds = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
43+
Artists = new RemoteSearchResult[] { };
4244
}
4345
}
4446
}

MediaBrowser.Providers/MediaBrowser.Providers.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@
8888
<Compile Include="Music\AudioDbExternalIds.cs" />
8989
<Compile Include="Music\AudioMetadataService.cs" />
9090
<Compile Include="Music\Extensions.cs" />
91-
<Compile Include="Music\ImvdbProvider.cs" />
9291
<Compile Include="Music\MovieDbMusicVideoProvider.cs" />
9392
<Compile Include="Music\MusicBrainzArtistProvider.cs" />
9493
<Compile Include="Music\MusicExternalIds.cs" />

0 commit comments

Comments
 (0)