Skip to content

Commit

Permalink
Merge pull request #85 from Syriiin/add-logging
Browse files Browse the repository at this point in the history
Add simple logging for beatmap downloads
  • Loading branch information
Syriiin authored Oct 23, 2024
2 parents 68e2fd8 + 758ccfb commit 0dd27d2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
9 changes: 9 additions & 0 deletions Difficalcy/DifficalcyStartup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;
using StackExchange.Redis;

Expand Down Expand Up @@ -31,6 +32,14 @@ public void ConfigureServices(IServiceCollection services)
c.SwaggerDoc("v1", new OpenApiInfo { Title = OpenApiTitle, Version = OpenApiVersion });
});

services.AddLogging(options =>
{
options.AddSimpleConsole(console =>
{
console.TimestampFormat = "[HH:mm:ss] ";
});
});

var redisConfig = Configuration["REDIS_CONFIGURATION"];
ICache cache;
if (redisConfig == null)
Expand Down
12 changes: 11 additions & 1 deletion Difficalcy/Services/WebBeatmapProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace Difficalcy.Services
{
public class WebBeatmapProvider(IConfiguration configuration) : IBeatmapProvider
public class WebBeatmapProvider(IConfiguration configuration, ILogger<WebBeatmapProvider> logger) : IBeatmapProvider
{
private readonly string _beatmapDirectory = configuration["BEATMAP_DIRECTORY"];
private readonly string _downloadMissingBeatmaps = configuration["DOWNLOAD_MISSING_BEATMAPS"];
Expand All @@ -17,16 +18,25 @@ public async Task EnsureBeatmap(string beatmapId)
if (!File.Exists(beatmapPath))
{
if (_downloadMissingBeatmaps != "true")
{
logger.LogWarning("Beatmap {BeatmapId} not found and downloading is disabled", beatmapId);
throw new BeatmapNotFoundException(beatmapId);
}

logger.LogInformation("Downloading beatmap {BeatmapId}", beatmapId);

using var response = await _httpClient.GetAsync($"https://osu.ppy.sh/osu/{beatmapId}");
if (!response.IsSuccessStatusCode || response.Content.Headers.ContentLength == 0)
{
logger.LogWarning("Failed to download beatmap {BeatmapId}", beatmapId);
throw new BeatmapNotFoundException(beatmapId);
}

using var fs = new FileStream(beatmapPath, FileMode.CreateNew);
await response.Content.CopyToAsync(fs);
if (fs.Length == 0)
{
logger.LogWarning("Downloaded beatmap {BeatmapId} was empty, deleting", beatmapId);
fs.Close();
File.Delete(beatmapPath);
throw new BeatmapNotFoundException(beatmapId);
Expand Down

0 comments on commit 0dd27d2

Please sign in to comment.