Skip to content

Commit

Permalink
Improve: Improved logger
Browse files Browse the repository at this point in the history
  • Loading branch information
gokadzev committed Dec 28, 2023
1 parent 9350f7e commit d993f85
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 27 deletions.
21 changes: 12 additions & 9 deletions lib/API/musify.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Future<List> fetchSongsList(String searchQuery) async {

return searchResults.map((video) => returnSongLayout(0, video)).toList();
} catch (e, stackTrace) {
logger.log('Error in fetchSongsList:$e\n$stackTrace');
logger.log('Error in fetchSongsList', e, stackTrace);
return [];
}
}
Expand Down Expand Up @@ -92,7 +92,7 @@ Future<List> getRecommendedSongs() async {

return playlistSongs.take(15).toList();
} catch (e, stackTrace) {
logger.log('Error in getRecommendedSongs:$e\n$stackTrace');
logger.log('Error in getRecommendedSongs', e, stackTrace);
return [];
}
}
Expand Down Expand Up @@ -338,7 +338,7 @@ Future<List<Map<String, int>>> getSkipSegments(String id) async {
return [];
}
} catch (e, stack) {
logger.log('Error in getSkipSegments: $e $stack');
logger.log('Error in getSkipSegments', e, stack);
return [];
}
}
Expand Down Expand Up @@ -428,7 +428,7 @@ Future<AudioOnlyStreamInfo> getSongManifest(String songId) async {
final audioStream = manifest.audioOnly.withHighestBitrate();
return audioStream;
} catch (e, stackTrace) {
logger.log('Error while getting song streaming manifest:$e\n$stackTrace');
logger.log('Error while getting song streaming manifest', e, stackTrace);
rethrow; // Rethrow the exception to allow the caller to handle it
}
}
Expand Down Expand Up @@ -457,7 +457,7 @@ Future<String> getSong(String songId, bool isLive) async {
return await getAudioUrl(songId, isQualityChanged, cacheKey);
}
} catch (e, stackTrace) {
logger.log('Error while getting song streaming URL:$e\n$stackTrace');
logger.log('Error while getting song streaming URL', e, stackTrace);
rethrow;
}
}
Expand Down Expand Up @@ -510,7 +510,7 @@ Future<Map<String, dynamic>> getSongDetails(
final song = await yt.videos.get(songId);
return returnSongLayout(songIndex, song);
} catch (e, stackTrace) {
logger.log('Error while getting song details:$e\n$stackTrace');
logger.log('Error while getting song details', e, stackTrace);
rethrow;
}
}
Expand Down Expand Up @@ -589,11 +589,14 @@ Future<File?> _downloadAndSaveArtworkFile(String url, String filePath) async {
await file.writeAsBytes(response.bodyBytes);
return file;
} else {
logger
.log('Failed to download file. Status code: ${response.statusCode}');
logger.log(
'Failed to download file. Status code: ${response.statusCode}',
null,
null,
);
}
} catch (e, stackTrace) {
logger.log('Error downloading and saving file: $e\n$stackTrace');
logger.log('Error downloading and saving file', e, stackTrace);
}

return null;
Expand Down
4 changes: 2 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class _MusifyState extends State<Musify> {
yield LicenseEntryWithLineBreaks(['google_fonts'], license1);
});
} catch (e, stackTrace) {
logger.log('License Registration Error:$e\n$stackTrace');
logger.log('License Registration Error', e, stackTrace);
}
}

Expand Down Expand Up @@ -301,6 +301,6 @@ Future<void> initialisation() async {
tapOpensFile: true,
);
} catch (e, stackTrace) {
logger.log('Initialization Error:$e\n$stackTrace');
logger.log('Initialization Error', e, stackTrace);
}
}
10 changes: 8 additions & 2 deletions lib/screens/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ class _HomePageState extends State<HomePage> {
if (snapshot.connectionState == ConnectionState.waiting) {
return _buildLoadingWidget();
} else if (snapshot.hasError) {
logger.log('Error in _buildSuggestedPlaylistsWidget: ${snapshot.error}');
logger.log(
'Error in _buildSuggestedPlaylistsWidget',
snapshot.error,
snapshot.stackTrace,
);
return _buildErrorWidget(context);
} else if (!snapshot.hasData || snapshot.data!.isEmpty) {
return const SizedBox.shrink();
Expand Down Expand Up @@ -145,7 +149,9 @@ class _HomePageState extends State<HomePage> {
case ConnectionState.done:
if (snapshot.hasError) {
logger.log(
'Error in _buildRecommendedSongsAndArtists: ${snapshot.error}',
'Error in _buildRecommendedSongsAndArtists',
snapshot.error,
snapshot.stackTrace,
);
return _buildErrorWidget(context);
}
Expand Down
6 changes: 5 additions & 1 deletion lib/screens/playlists_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,11 @@ class _PlaylistsPageState extends State<PlaylistsPage> {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Spinner();
} else if (snapshot.hasError) {
logger.log('Error on playlists page: ${snapshot.error}');
logger.log(
'Error on playlists page',
snapshot.error,
snapshot.stackTrace,
);
return Center(
child: Text(context.l10n!.error),
);
Expand Down
2 changes: 1 addition & 1 deletion lib/screens/search_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class _SearchPageState extends State<SearchPage> {
try {
_searchResult = await fetchSongsList(query);
} catch (e, stackTrace) {
logger.log('Error while searching online songs:$e\n$stackTrace');
logger.log('Error while searching online songs', e, stackTrace);
}

if (_fetchingSongs.value) {
Expand Down
7 changes: 5 additions & 2 deletions lib/screens/user_added_playlists_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,11 @@ class _UserPlaylistsPageState extends State<UserPlaylistsPage> {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Spinner();
} else if (snapshot.hasError) {
logger
.log('Error on user playlists page: ${snapshot.error}');
logger.log(
'Error on user playlists page',
snapshot.error,
snapshot.stackTrace,
);
return Center(
child: Text(context.l10n!.error),
);
Expand Down
8 changes: 4 additions & 4 deletions lib/services/audio_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class MusifyAudioHandler extends BaseAudioHandler {
try {
audioPlayer.setAudioSource(_playlist);
} catch (e, stackTrace) {
logger.log('Error in setNewPlaylist:$e\n$stackTrace');
logger.log('Error in setNewPlaylist', e, stackTrace);
}

_initialize();
Expand Down Expand Up @@ -171,7 +171,7 @@ class MusifyAudioHandler extends BaseAudioHandler {
}
});
} catch (e, stackTrace) {
logger.log('Error initializing audio session:$e\n$stackTrace');
logger.log('Error initializing audio session', e, stackTrace);
}
}

Expand Down Expand Up @@ -220,7 +220,7 @@ class MusifyAudioHandler extends BaseAudioHandler {
}
await audioPlayer.play();
} catch (e, stackTrace) {
logger.log('Error playing song:$e\n$stackTrace');
logger.log('Error playing song', e, stackTrace);
}
}

Expand Down Expand Up @@ -307,7 +307,7 @@ class MusifyAudioHandler extends BaseAudioHandler {

await audioPlayer.setAudioSource(_audioSource);
} catch (e, stackTrace) {
logger.log('Error checking sponsor block:$e\n$stackTrace');
logger.log('Error checking sponsor block', e, stackTrace);
}
}

Expand Down
6 changes: 3 additions & 3 deletions lib/services/logger_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ class Logger {
String _logs = '';
int _logCount = 0;

void log(String message) {
void log(String errorLocation, Object? error, StackTrace? stackTrace) {
final timestamp = DateTime.now().toString();
final logMessage = '[$timestamp] $message';
final logMessage = '[$timestamp] $errorLocation:$error\n$stackTrace';
debugPrint(logMessage);
_logs += '$logMessage\n';
_logCount++;
Expand All @@ -23,7 +23,7 @@ class Logger {
return '${context.l10n!.copyLogsNoLogs}.';
}
} catch (e, stackTrace) {
log('Error copying logs:$e\n$stackTrace');
log('Error copying logs', e, stackTrace);
return 'Error: $e';
}
}
Expand Down
8 changes: 5 additions & 3 deletions lib/services/update_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,12 @@ Future<void> checkAppUpdates(BuildContext context) async {
} else {
logger.log(
'Fetch update API call returned status code ${response.statusCode}',
null,
null,
);
}
} catch (e, stackTrace) {
logger.log('Error in checkAppUpdates:$e\n$stackTrace');
logger.log('Error in checkAppUpdates', e, stackTrace);
}
}

Expand All @@ -131,7 +133,7 @@ Future<void> downloadAppUpdates(Map<String, dynamic> map) async {
},
);
} catch (e, stackTrace) {
logger.log('Error in downloadAppUpdates:$e\n$stackTrace');
logger.log('Error in downloadAppUpdates', e, stackTrace);
}
}

Expand Down Expand Up @@ -176,7 +178,7 @@ Future<void> checkNecessaryPermissions(BuildContext context) async {
try {
await Permission.storage.request();
} catch (e, stackTrace) {
logger.log('Error while requesting permissions:$e\n$stackTrace');
logger.log('Error while requesting permissions', e, stackTrace);
showToast(
context,
'${context.l10n!.errorWhileRequestingPerms} + $e',
Expand Down

0 comments on commit d993f85

Please sign in to comment.