From 101cbde1cc1283fb8d81084a07fd5e2a345cb1e5 Mon Sep 17 00:00:00 2001 From: Rene Floor Date: Sun, 28 Nov 2021 11:59:26 +0100 Subject: [PATCH 1/2] Improve logging options --- .../lib/flutter_cache_manager.dart | 1 + .../lib/src/cache_manager.dart | 16 +++++++++------- flutter_cache_manager/lib/src/cache_store.dart | 5 +++++ flutter_cache_manager/lib/src/logger.dart | 18 ++++++++++++++++++ .../cache_info_repository.dart | 6 +++++- .../lib/src/web/web_helper.dart | 4 ++++ 6 files changed, 42 insertions(+), 8 deletions(-) create mode 100644 flutter_cache_manager/lib/src/logger.dart diff --git a/flutter_cache_manager/lib/flutter_cache_manager.dart b/flutter_cache_manager/lib/flutter_cache_manager.dart index fbdcee72..6d436c45 100644 --- a/flutter_cache_manager/lib/flutter_cache_manager.dart +++ b/flutter_cache_manager/lib/flutter_cache_manager.dart @@ -2,6 +2,7 @@ export 'src/cache_manager.dart'; export 'src/cache_managers/cache_managers.dart'; export 'src/compat/file_fetcher.dart'; export 'src/config/config.dart'; +export 'src/logger.dart'; export 'src/result/result.dart'; export 'src/storage/cache_info_repositories/cache_info_repositories.dart'; export 'src/web/file_service.dart'; diff --git a/flutter_cache_manager/lib/src/cache_manager.dart b/flutter_cache_manager/lib/src/cache_manager.dart index 0c958e72..f0ca48a0 100644 --- a/flutter_cache_manager/lib/src/cache_manager.dart +++ b/flutter_cache_manager/lib/src/cache_manager.dart @@ -6,6 +6,7 @@ import 'package:flutter/foundation.dart'; import 'package:flutter_cache_manager/flutter_cache_manager.dart'; import 'package:flutter_cache_manager/src/cache_managers/base_cache_manager.dart'; import 'package:flutter_cache_manager/src/cache_store.dart'; +import 'package:flutter_cache_manager/src/logger.dart'; import 'package:flutter_cache_manager/src/result/download_progress.dart'; import 'package:flutter_cache_manager/src/result/file_info.dart'; import 'package:flutter_cache_manager/src/result/file_response.dart'; @@ -23,6 +24,8 @@ import 'config/config.dart'; /// Basic cache manager implementation, which should be used as a single /// instance. class CacheManager implements BaseCacheManager { + static CacheManagerLogLevel logLevel = CacheManagerLogLevel.none; + /// Creates a new instance of a cache manager. This can be used to retrieve /// files from the cache or download them online. The http headers are used /// for the maximum age of the files. The BaseCacheManager should only be @@ -130,8 +133,9 @@ class CacheManager implements BaseCacheManager { withProgress = false; } } catch (e) { - print( - 'CacheManager: Failed to load cached file for $url with error:\n$e'); + cacheLogger.log( + 'CacheManager: Failed to load cached file for $url with error:\n$e', + CacheManagerLogLevel.debug); } if (cacheFile == null || cacheFile.validTill.isBefore(DateTime.now())) { try { @@ -145,11 +149,9 @@ class CacheManager implements BaseCacheManager { } } } catch (e) { - assert(() { - print( - 'CacheManager: Failed to download file from $url with error:\n$e'); - return true; - }()); + cacheLogger.log( + 'CacheManager: Failed to download file from $url with error:\n$e', + CacheManagerLogLevel.debug); if (cacheFile == null && streamController.hasListener) { streamController.addError(e); } diff --git a/flutter_cache_manager/lib/src/cache_store.dart b/flutter_cache_manager/lib/src/cache_store.dart index b1e79d41..d3fc2db9 100644 --- a/flutter_cache_manager/lib/src/cache_store.dart +++ b/flutter_cache_manager/lib/src/cache_store.dart @@ -4,6 +4,8 @@ import 'package:flutter_cache_manager/src/config/config.dart'; import 'package:flutter_cache_manager/src/storage/file_system/file_system.dart'; import 'package:pedantic/pedantic.dart'; +import '../flutter_cache_manager.dart'; +import 'logger.dart'; import 'result/file_info.dart'; import 'storage/cache_info_repositories/cache_info_repository.dart'; import 'storage/cache_object.dart'; @@ -41,6 +43,9 @@ class CacheStore { return null; } final file = await fileSystem.createFile(cacheObject.relativePath); + cacheLogger.log( + 'CacheManager: Loaded $key from cache', CacheManagerLogLevel.verbose); + return FileInfo( file, FileSource.Cache, diff --git a/flutter_cache_manager/lib/src/logger.dart b/flutter_cache_manager/lib/src/logger.dart new file mode 100644 index 00000000..b232cbdc --- /dev/null +++ b/flutter_cache_manager/lib/src/logger.dart @@ -0,0 +1,18 @@ +import '../flutter_cache_manager.dart'; + +CacheLogger cacheLogger = CacheLogger(); + +enum CacheManagerLogLevel { + none, + warning, + debug, + verbose, +} + +class CacheLogger { + void log(String message, CacheManagerLogLevel level) { + if (CacheManager.logLevel.index >= level.index) { + print(message); + } + } +} diff --git a/flutter_cache_manager/lib/src/storage/cache_info_repositories/cache_info_repository.dart b/flutter_cache_manager/lib/src/storage/cache_info_repositories/cache_info_repository.dart index 7b737174..8a122503 100644 --- a/flutter_cache_manager/lib/src/storage/cache_info_repositories/cache_info_repository.dart +++ b/flutter_cache_manager/lib/src/storage/cache_info_repositories/cache_info_repository.dart @@ -1,5 +1,6 @@ import 'dart:async'; +import 'package:flutter_cache_manager/src/logger.dart'; import 'package:flutter_cache_manager/src/storage/cache_object.dart'; /// Base class for cache info repositories @@ -58,7 +59,10 @@ extension MigrationExtension on CacheInfoRepository { var cacheObjects = await previousRepository.getAllObjects(); await _putAll(cacheObjects); var isClosed = await previousRepository.close(); - if (!isClosed) print('Deleting an open repository while migrating.'); + if (!isClosed) { + cacheLogger.log('Deleting an open repository while migrating.', + CacheManagerLogLevel.warning); + } await previousRepository.deleteDataFile(); } diff --git a/flutter_cache_manager/lib/src/web/web_helper.dart b/flutter_cache_manager/lib/src/web/web_helper.dart index 011e7f63..4c4e36fd 100644 --- a/flutter_cache_manager/lib/src/web/web_helper.dart +++ b/flutter_cache_manager/lib/src/web/web_helper.dart @@ -16,6 +16,8 @@ import 'package:pedantic/pedantic.dart'; import 'package:rxdart/rxdart.dart'; import 'package:uuid/uuid.dart'; +import '../logger.dart'; + ///Flutter Cache Manager ///Copyright (c) 2019 Rene Floor ///Released under MIT License. @@ -60,6 +62,8 @@ class WebHelper { _queue.add(QueueItem(url, key, authHeaders)); return; } + cacheLogger.log( + 'CacheManager: Downloading $url', CacheManagerLogLevel.verbose); concurrentCalls++; var subject = _memCache[key]!; From d14bfa3122c69b434ac2e174bc0025ccaf458340 Mon Sep 17 00:00:00 2001 From: Rene Floor Date: Mon, 29 Nov 2021 19:37:37 +0100 Subject: [PATCH 2/2] add documentation --- flutter_cache_manager/CHANGELOG.md | 6 ++++++ flutter_cache_manager/example/lib/main.dart | 1 + flutter_cache_manager/lib/src/logger.dart | 5 +++++ flutter_cache_manager/pubspec.yaml | 2 +- 4 files changed, 13 insertions(+), 1 deletion(-) diff --git a/flutter_cache_manager/CHANGELOG.md b/flutter_cache_manager/CHANGELOG.md index 327c2047..f6b26e89 100644 --- a/flutter_cache_manager/CHANGELOG.md +++ b/flutter_cache_manager/CHANGELOG.md @@ -1,3 +1,9 @@ +## [3.3.0] - 2021-11-29 +* Added option to manage the log level. Doesn't print failed downloads by default anymore. You can set it like this: +```dart +CacheManager.logLevel = CacheManagerLogLevel.verbose; +``` + ## [3.2.0] - 2021-11-27 * [Bugfix] getSingleFile now downloads a new file before completing as the outdated file might have been deleted. diff --git a/flutter_cache_manager/example/lib/main.dart b/flutter_cache_manager/example/lib/main.dart index 6af27563..e2a655d3 100644 --- a/flutter_cache_manager/example/lib/main.dart +++ b/flutter_cache_manager/example/lib/main.dart @@ -12,6 +12,7 @@ void main() { pubDevURL: 'https://pub.dev/packages/flutter_cache_manager', pages: [CacheManagerPage.createPage()], )); + CacheManager.logLevel = CacheManagerLogLevel.verbose; } const url = 'https://blurha.sh/assets/images/img1.jpg'; diff --git a/flutter_cache_manager/lib/src/logger.dart b/flutter_cache_manager/lib/src/logger.dart index b232cbdc..312b16de 100644 --- a/flutter_cache_manager/lib/src/logger.dart +++ b/flutter_cache_manager/lib/src/logger.dart @@ -1,7 +1,10 @@ import '../flutter_cache_manager.dart'; +/// Instance of the cache manager. Can be set to a custom one if preferred. CacheLogger cacheLogger = CacheLogger(); +/// Log levels of the cache manager. Debug shows failed downloads and verbose +/// also shows successful downloads and cache retrievals. enum CacheManagerLogLevel { none, warning, @@ -9,7 +12,9 @@ enum CacheManagerLogLevel { verbose, } +/// [CacheLogger] which is used by the cache manager to log useful information class CacheLogger { + /// Function to log a message on a certain loglevel void log(String message, CacheManagerLogLevel level) { if (CacheManager.logLevel.index >= level.index) { print(message); diff --git a/flutter_cache_manager/pubspec.yaml b/flutter_cache_manager/pubspec.yaml index 20e0f8d8..0d895bb6 100644 --- a/flutter_cache_manager/pubspec.yaml +++ b/flutter_cache_manager/pubspec.yaml @@ -1,6 +1,6 @@ name: flutter_cache_manager description: Generic cache manager for flutter. Saves web files on the storages of the device and saves the cache info using sqflite. -version: 3.2.0 +version: 3.3.0 homepage: https://github.com/Baseflow/flutter_cache_manager/tree/master/flutter_cache_manager environment: