From fd60d2c13776198a6068ed740fae6a2f9897f240 Mon Sep 17 00:00:00 2001 From: jscampucci Date: Thu, 15 Aug 2024 10:26:42 +0200 Subject: [PATCH] [log] clean file logger --- lib/dlForm/formComponents/submit_button.dart | 3 +- lib/services/converter.dart | 11 ++-- lib/services/download.dart | 46 ++++++++-------- lib/services/file_logger.dart | 3 ++ lib/videoList/cubit/videos_cubit.dart | 53 +++++++++++-------- windows/runner/Runner.aps | Bin 0 -> 3764 bytes 6 files changed, 65 insertions(+), 51 deletions(-) create mode 100644 windows/runner/Runner.aps diff --git a/lib/dlForm/formComponents/submit_button.dart b/lib/dlForm/formComponents/submit_button.dart index aba200c..dc45b7c 100644 --- a/lib/dlForm/formComponents/submit_button.dart +++ b/lib/dlForm/formComponents/submit_button.dart @@ -7,6 +7,7 @@ import 'package:notube/constants.dart'; import 'package:notube/models/video.dart'; import 'package:notube/videoList/cubit/videos_cubit.dart'; import 'package:notube/dlForm/cubit/dl_form_cubit.dart'; +import 'package:notube/services/file_logger.dart'; class SubmitButton extends StatefulWidget { const SubmitButton({super.key}); @@ -22,7 +23,7 @@ class _SubmitButtonState extends State { if (state.isParsed) { context.read().setGlobalStatus('loaded'); for (Video video in state.videos) { - debugPrint('Adding video: $video'); + FileLogger().d('Adding video: $video'); context.read().addVideo(video); } context.read().clearForm(); diff --git a/lib/services/converter.dart b/lib/services/converter.dart index 876594f..b481fa0 100644 --- a/lib/services/converter.dart +++ b/lib/services/converter.dart @@ -7,6 +7,7 @@ import 'package:flutter/foundation.dart'; import 'package:process_run/process_run.dart'; import 'package:notube/models/video.dart'; import 'package:path/path.dart' as p; +import 'package:notube/services/file_logger.dart'; class ConverterService { late Directory tempDir; @@ -15,7 +16,7 @@ class ConverterService { ConverterService._(); static Future init() async { - debugPrint('Initializing DLServices'); + FileLogger().d('Initializing DLServices'); var dlService = ConverterService._(); await dlService._init(); return dlService; @@ -50,8 +51,8 @@ class ConverterService { final List files = directory.listSync(); for (FileSystemEntity file in files) { final String dirFilename = p.basenameWithoutExtension(file.path); - debugPrint('dirFilename $dirFilename'); - debugPrint('filename $filename'); + FileLogger().d('dirFilename $dirFilename'); + FileLogger().d('filename $filename'); if (dirFilename == '${filename}_tmp') { return File(file.path); } @@ -60,7 +61,7 @@ class ConverterService { } Future convertFile(Video video) async { - debugPrint( + FileLogger().d( '____Converting ${video.title} to ${video.format.format} format_____'); File tmpFile = File('temp/${video.filename}_tmp.mp4'); @@ -72,7 +73,7 @@ class ConverterService { File doneFile = File('temp/${video.filename}_done.${video.format.extension}'); if (doneFile.existsSync()) { - debugPrint('File already converted'); + FileLogger().d('File already converted'); return Stream.fromIterable(['progress=end']); } diff --git a/lib/services/download.dart b/lib/services/download.dart index 253d614..28ef51a 100644 --- a/lib/services/download.dart +++ b/lib/services/download.dart @@ -30,7 +30,7 @@ class DLServices { DLServices._(); static Future init() async { - debugPrint('Initializing DLServices'); + FileLogger().d('Initializing DLServices'); var dlService = DLServices._(); await dlService._init(); return dlService; @@ -57,7 +57,7 @@ class DLServices { ByteData data = await rootBundle.load('assets/executable/$assetName'); await tempFile.exists().then((value) { if (!value) { - debugPrint('Copying $assetName to ${tempFile.path}'); + FileLogger().d('Copying $assetName to ${tempFile.path}'); tempFile.writeAsBytes( data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes)); } @@ -82,10 +82,10 @@ class DLServices { print('FFmpeg is already installed.'); completer.complete(true); result.map((e) { - debugPrint('Analyse result: $e'); + FileLogger().d('Analyse result: $e'); FileLogger().d('Analyse result: ${e.toString()}'); - FileLogger().d('stdout: ${e.stdout}'); - FileLogger().d('stderr: ${e.stderr}'); + FileLogger().w('stdout: ${e.stdout}'); + FileLogger().e('stderr: ${e.stderr}'); }); }).catchError(( error, @@ -100,7 +100,7 @@ class DLServices { Future checkFFmpeg() async { var result = await futureShell('ffmpeg -version'); - debugPrint('$result'); + FileLogger().d('$result'); try { var result = await Process.run('ffmpeg', ['-version']); @@ -109,7 +109,7 @@ class DLServices { return; } - debugPrint('RESULTS: $result'); + FileLogger().d('RESULTS: $result'); print('Installing FFmpeg...'); if (Platform.isLinux) { @@ -142,7 +142,7 @@ class DLServices { } Future downloadFile(Video video) async { - debugPrint( + FileLogger().d( 'Downloading $url in ${video.format.format} format (${video.filename})'); /* @@ -153,11 +153,11 @@ class DLServices { // get the format code from the Format enum var formatCmd = Format.values.firstWhere((e) => e.format == video.format.format).ytCmd; - debugPrint('Format code: $formatCmd'); + FileLogger().d('Format code: $formatCmd'); File doneFile = File('temp/${video.filename}_tmp.mp4'); if (doneFile.existsSync()) { - debugPrint('File already downloaded'); + FileLogger().d('File already downloaded'); return Stream.fromIterable(['[EmbedThumbnail]']); } @@ -171,24 +171,24 @@ class DLServices { var shell = Shell( stdout: shellLinesController.sink, stderr: shellErrorController.sink); - debugPrint('Running $ytDlpPath $command'); + FileLogger().d('Running $ytDlpPath $command'); shell.run('$ytDlpPath $command').then((result) { result.map((e) { - debugPrint('Analyse result: $e'); + FileLogger().d('Analyse result: $e'); FileLogger().d('Analyse result: ${e.toString()}'); - FileLogger().d('stdout: ${e.stdout}'); - FileLogger().d('stderr: ${e.stderr}'); + FileLogger().w('stdout: ${e.stdout}'); + FileLogger().e('stderr: ${e.stderr}'); }); }).catchError(( error, stackTrace, ) { if (error is ShellException) { - debugPrint('ShellException: ${error.message}'); + FileLogger().d('ShellException: ${error.message}'); FileLogger().e('ShellException error: ${error.result?.stderr}'); FileLogger().e('ShellException out: ${error.result?.stdout}'); } else { - debugPrint('Error: $error'); + FileLogger().d('Error: $error'); FileLogger().e('Error: $error'); } }); @@ -197,7 +197,7 @@ class DLServices { } Future analyseUrl(String url) async { - debugPrint('Analyse $url'); + FileLogger().d('Analyse $url'); var command = '${url.trim()} -q --flat-playlist -J'; @@ -205,7 +205,7 @@ class DLServices { var shellErrorController = ShellLinesController(); shellErrorController.stream.listen((event) { - debugPrint('Analyse error: $event'); + FileLogger().d('Analyse error: $event'); FileLogger().e('Analyse error: $event'); }); @@ -218,21 +218,21 @@ class DLServices { $ytDlpPath $command ''').then((result) { result.map((e) { - debugPrint('Analyse result: $e'); + FileLogger().d('Analyse result: $e'); FileLogger().d('Analyse result: ${e.toString()}'); - FileLogger().d('stdout: ${e.stdout}'); - FileLogger().d('stderr: ${e.stderr}'); + FileLogger().w('stdout: ${e.stdout}'); + FileLogger().e('stderr: ${e.stderr}'); }); }).catchError(( error, stackTrace, ) { if (error is ShellException) { - debugPrint('ShellException: ${error.message}'); + FileLogger().d('ShellException: ${error.message}'); FileLogger().e('ShellException error: ${error.result?.stderr}'); FileLogger().e('ShellException out: ${error.result?.stdout}'); } else { - debugPrint('Error: $error'); + FileLogger().d('Error: $error'); FileLogger().e('Error: $error'); } }); diff --git a/lib/services/file_logger.dart b/lib/services/file_logger.dart index 08057e9..fba4ca6 100644 --- a/lib/services/file_logger.dart +++ b/lib/services/file_logger.dart @@ -18,12 +18,14 @@ class FileLogger { _logFile = File('apps_logs.txt'); _logger = Logger( + filter: ProductionFilter(), printer: PrettyPrinter(), output: FileOutput(file: _logFile), ); } void d(String message) { + FileLogger().d(message); _logger.d(message); } @@ -32,6 +34,7 @@ class FileLogger { } void w(String message) { + FileLogger().d(message); _logger.w(message); } diff --git a/lib/videoList/cubit/videos_cubit.dart b/lib/videoList/cubit/videos_cubit.dart index 3fab52e..f1d8670 100644 --- a/lib/videoList/cubit/videos_cubit.dart +++ b/lib/videoList/cubit/videos_cubit.dart @@ -10,6 +10,7 @@ import 'package:notube/constants.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:downloadsfolder/downloadsfolder.dart'; import 'package:path/path.dart' as p; +import 'package:notube/services/file_logger.dart'; part 'videos_state.dart'; @@ -40,7 +41,7 @@ class VideosCubit extends Cubit { emit(VideosState(videoList: tmpVideoList)); completer.complete(); } else { - debugPrint('Video not found in the list'); + FileLogger().d('Video not found in the list'); completer.completeError('Video not found in the list'); } return completer.future; @@ -60,12 +61,12 @@ class VideosCubit extends Cubit { } Future archiveVideo(Video video) async { - debugPrint('Archiving video: ${video.title} ${video.format.format}'); + FileLogger().d('Archiving video: ${video.title} ${video.format.format}'); var nVideoList = state.videoList.where((v) => v.id != video.id).toList(); var archivedVideos = state.archiveList.toList(); - debugPrint('Archived videos before: ${archivedVideos.length}'); + FileLogger().d('Archived videos before: ${archivedVideos.length}'); archivedVideos.add(video); - debugPrint('Archived videos after: $archivedVideos'); + FileLogger().d('Archived videos after: $archivedVideos'); emit(state.copyWith(videoList: nVideoList, archiveList: archivedVideos)); } @@ -76,7 +77,8 @@ class VideosCubit extends Cubit { for (Video video in videosToConvert) { await convertVideo(video); } - debugPrint('All videos converted'); + FileLogger().d('All videos converted'); + FileLogger().d('All videos converted'); } Future startDownloader() async { @@ -86,9 +88,9 @@ class VideosCubit extends Cubit { state.videoList.where((video) => video.status == 'pending').take(3); while (downloadingVid.isNotEmpty && runningTasks < maxConcurrentTasks) { - debugPrint('Videos to download: ${downloadingVid.length}'); + FileLogger().d('Videos to download: ${downloadingVid.length}'); runningTasks++; - debugPrint('Concurrent workers: $runningTasks'); + FileLogger().d('Concurrent workers: $runningTasks'); await downloadVideo(downloadingVid.first); downloadingVid = @@ -108,11 +110,12 @@ class VideosCubit extends Cubit { } } - debugPrint('All videos downloaded'); + FileLogger().d('All videos downloaded'); + FileLogger().d('All videos downloaded'); } Future moveVideos() async { - debugPrint('Moving videos to download folder'); + FileLogger().d('Moving videos to download folder'); final prefs = await SharedPreferences.getInstance(); Directory defaultDownloadDirectory = await getDownloadDirectory(); prefs.get('downloadFolder') ?? @@ -130,8 +133,8 @@ class VideosCubit extends Cubit { var playlistTitle = video.playlistTitle; await Directory('${downloadDirectory.path}/$playlistTitle') .create(recursive: true) - .catchError((e) => debugPrint('Error creating directory: $e')); - debugPrint('Playlist title: $playlistTitle'); + .catchError((e) => FileLogger().e('Error creating directory: $e')); + FileLogger().d('Playlist title: $playlistTitle'); if (playlistTitle.isNotEmpty && playlistTitle != '') { cleanTitle = '$playlistTitle/$cleanTitle'; } @@ -145,19 +148,20 @@ class VideosCubit extends Cubit { await tmpFile.rename(newFile.path); isMoved = true; } on FileSystemException catch (e) { - debugPrint('Error moving file: $e'); + FileLogger().e('Error moving file: $e'); await Future.delayed(Duration(seconds: 1)); } } await changeStatus(video, 'done'); await archiveVideo(video.copyWith(status: 'done')); - debugPrint('File moved to ${newFile.path}'); + FileLogger().d('File moved to ${newFile.path}'); } } Future convertVideo(Video video) async { converterService = await ConverterService.init(); - debugPrint('Converting ${video.title} to ${video.format.format}'); + FileLogger().d('Converting ${video.title} to ${video.format.format}'); + FileLogger().d('Converting ${video.title} to ${video.format.format}'); changeStatus(video, 'converting'); final shellStream = await converterService.convertFile(video); var duration = '0.0'; @@ -165,12 +169,13 @@ class VideosCubit extends Cubit { shellStream.listen( (line) { - debugPrint(line); + FileLogger().d(line); //FFmpeg doesn't return any output for audio conversion if (video.format.extension == "mp3") { changeStatus(video, 'converted'); if (!completer.isCompleted) { - debugPrint('____Conversion audio completed___'); + FileLogger().d('____Conversion audio completed___'); + FileLogger().d('Conversion audio completed'); runningTasks--; completer.complete(); } @@ -179,18 +184,18 @@ class VideosCubit extends Cubit { var durationString = line.split('Duration: ')[1].split(',')[0].trim(); duration = durationString; - debugPrint('Duration: $duration'); + FileLogger().d('Duration: $duration'); } if (line.contains('out_time_ms=')) { var timeString = line.split('out_time_ms=')[1]; var time = timeString; - debugPrint('Time: $time'); + FileLogger().d('Time: $time'); changeStatus(video, 'converting $time on $duration'); } if (line.contains('progress=end')) { changeStatus(video, 'converted'); if (!completer.isCompleted) { - debugPrint('____Conversion completed___'); + FileLogger().d('____Conversion completed___'); runningTasks--; completer.complete(); } @@ -208,7 +213,10 @@ class VideosCubit extends Cubit { } Future downloadVideo(Video video) async { - debugPrint( + FileLogger().d( + '___Downloading ${video.title} in ${video.format} format ${video.filename}____'); + + FileLogger().d( '___Downloading ${video.title} in ${video.format} format ${video.filename}____'); final shellStream = await dlService.downloadFile(video); var completer = Completer(); @@ -228,12 +236,13 @@ class VideosCubit extends Cubit { changeStatus(video, 'downloaded'); if (!completer.isCompleted) { - debugPrint('____Download completed___'); + FileLogger().d('____Download completed___'); + FileLogger().d('Download completed'); runningTasks--; completer.complete(); } } - //debugPrint(line); + //FileLogger().d(line); }, onError: (error) { if (!completer.isCompleted) { diff --git a/windows/runner/Runner.aps b/windows/runner/Runner.aps new file mode 100644 index 0000000000000000000000000000000000000000..69bdb9857b8779e394a7af90be608b19ca11a0d9 GIT binary patch literal 3764 zcmd5e?Q~)>FTFR^4s22V5w7q#mp+er&aX(gKBDv@6omE`sRWx$hAYU~Ny&=^wo> z_r3e(&dmMI+mGFXduIVeE_3ebTRDJ~2`@EgZqHQFo=tHCH(4-o#n@*c3{Z#NJN-@Sot1rs+=q86Cjs4vc68=nj~9zvUkt)2DZOg>?Dw zI(WR#z_T4-`0CZJ;HPc?+^wo?pBdKa5!CMQ_h@8z_-5Pijq9B;3qA+m(Sd={{<{x) zdWJ4GeSPd`|GqtUUVY{EZ#MO6R9(MJ>5_^+Taq|@_1a+V2VZV`t#3U5!e*OnCoJ4Yx(~FCeg3uCXB2&5Ouo`22Rl08eZ;+ zgT@E%`E2KFzDoLMwV-L1u44FMUF^#l{rAhB%UiuV>}h`c*|kX@53c?-@Uvs{J#`E1 zal76xD|%`Fe`a`<9)9-llOqrPkH1kL#5I*39W0r3CE%OlElc+Y`9#cWnEtzBf0cLN z(#G1+g!*e?;Z;XU5A3ooy2k@SeaoliCU0fS935@kdN%2tr}XyUvVZlgcYh2BX8%i4 z(gWXWU+jjacanF7Etvh{rxj((?e?MggZYc98VoyAj(^m2e(<7JovuxUQyTa&cUR~g4d>Sk-A1X@*bwMatj7#c0 zS6)yg@V@d4Ssv{0kk(AwCn{U!{nvA?)tBSTc1X@U{@g3=J~R|pT6L)lPVP}&u4~wR zLbYziExRE4=i!Yxhbn?gM%uo3Mf&u_%lV>+rIndyGB#?(7iYXh^Pc=BQylZe zvdD&QTTh>U^MY>PA1hu8KK-X=U1s^*d4nb5SmSkdYfRyCWlDBbOUr&_VIYs4nt^Nttx9toV;_xD?m{0`i1 zj;r0(!F{ASY3WZ)ax`|7Tg~V ze|w$~3d2GrF<#{dA}$55-1lXD*0InfxexTc*Yzu!Zx&yEw`3B;1E^o0tU+wBGkj)5 zU~)q@S@$1cA@VDbH;>^YRu!T>#4CBQ9<|_q=oA_s<|8#4cP}1SjYqHW(Gud1$yN(+ zMvMqd_;l&CF>l0vV#_AH=b$8M%kf{9gZs+zlAR&=ojxOPte4&GVAo`Q&)Tm-c?;@v zAS%0?^?>>&`c}yJb|U{|54-zUiWoTjs>~@)Ru2Mj z;&fIg=p0VEMxQQIYvgH4yRvNpEk3+}?<{f=#ude61OtOM2iKx-PuPqhz^^wRb;KiuWDpARY{WZ@oyF)x z#%6Xu?quY|HsZTN%***q9x$UWc0@LTH)U@=p0YPu3}CrZqR>e(aGBY;=nHph{Qsr? zVw@^UV7`b6pU96{{U>64(X?n0!kZl6KjtzoFw2mo;P+Gf#%2fim?q~Sl9OC+C>|2-@$InR+H6=aFKRdE9w~)D>4S%RR2HATSwtLyIRKmk*t}!^J~lcmN&g;&H~Xi|2owSMLh| literal 0 HcmV?d00001