From 8a3a61aeec55b132b72b58da4dff7a68f33a0b1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Pedersen?= Date: Wed, 1 Jan 2025 19:10:17 +0100 Subject: [PATCH] Implemented logging framework (#358) * Implemented logging framework * Bump v0.10.2+52 --- lib/core/database.dart | 24 ++++++++++++++---------- lib/tabs/visualization.dart | 12 ++++++++---- lib/widgets/records.dart | 6 +++++- pubspec.yaml | 5 ++++- 4 files changed, 31 insertions(+), 16 deletions(-) diff --git a/lib/core/database.dart b/lib/core/database.dart index 786ff96..5a3f77e 100644 --- a/lib/core/database.dart +++ b/lib/core/database.dart @@ -5,6 +5,10 @@ import 'dart:io'; import 'package:sqflite/sqflite.dart'; import 'package:path/path.dart'; import '../common/constants.dart'; +import 'package:logging/logging.dart'; + +// Initialize the logger +final Logger _logger = Logger('DatabaseLogger'); class DatabaseHelper { static final DatabaseHelper _instance = DatabaseHelper._internal(); @@ -25,7 +29,7 @@ class DatabaseHelper { Future _initDatabase() async { String path = await _databasePath(); - print("Database is located at: $path"); + _logger.info("Database is located at: $path"); return await openDatabase( path, version: _databaseVersion, @@ -115,7 +119,7 @@ class DatabaseHelper { await db.execute( 'CREATE TABLE fitness(id INTEGER PRIMARY KEY AUTOINCREMENT, weight TEXT, height INTEGER, age INTEGER, timestamp TEXT)', ); - print('Fitness table recreated successfully'); + _logger.info('Fitness table recreated successfully'); } else { await db.delete(table); } @@ -581,15 +585,15 @@ class DatabaseHelper { String backupPath = '$selectedDirectory/backup_database.db'; await databaseFile.copy(backupPath); - print('Database backed up successfully to $backupPath'); + _logger.info('Database backed up successfully to $backupPath'); } else { - print('Backup operation cancelled'); + _logger.info('Backup operation cancelled'); } } else { - print('Storage permission denied'); + _logger.severe('Storage permission denied'); } } catch (e) { - print('Failed to back up database: $e'); + _logger.severe('Failed to back up database: $e'); } } @@ -612,15 +616,15 @@ class DatabaseHelper { File selectedFile = File(selectedFilePath); await selectedFile.copy(dbPath); - print('Database restored successfully from $selectedFilePath'); + _logger.info('Database restored successfully from $selectedFilePath'); } else { - print('Restore operation cancelled'); + _logger.info('Restore operation cancelled'); } } else { - print('Storage permission denied'); + _logger.severe('Storage permission denied'); } } catch (e) { - print('Failed to restore database: $e'); + _logger.severe('Failed to restore database: $e'); } } } diff --git a/lib/tabs/visualization.dart b/lib/tabs/visualization.dart index 403600a..3efd3aa 100644 --- a/lib/tabs/visualization.dart +++ b/lib/tabs/visualization.dart @@ -3,6 +3,10 @@ import 'package:fl_chart/fl_chart.dart'; import 'package:intl/intl.dart'; import '../core/database.dart'; import '../core/theme.dart'; +import 'package:logging/logging.dart'; + +// Initialize the logger +final Logger _logger = Logger('VisualizationLogger'); class VisualizationTab extends StatefulWidget { final bool isKg; @@ -71,7 +75,7 @@ class VisualizationTabState extends State { } Future _fetchExerciseNames() async { - print('Fetching names from $_selectedTable table...'); + _logger.info('Fetching names from $_selectedTable table...'); try { List names; if (_selectedTable == 'Exercise') { @@ -99,7 +103,7 @@ class VisualizationTabState extends State { } }); } catch (e) { - print('Error fetching names: $e'); + _logger.severe('Error fetching names: $e'); } // render last recorded exercise by default if any @@ -114,7 +118,7 @@ class VisualizationTabState extends State { }); } } catch (e) { - print('Error fetching last recorded exercise: $e'); + _logger.severe('Error fetching last recorded exercise: $e'); } } @@ -129,7 +133,7 @@ class VisualizationTabState extends State { } Future _fetchDataPoints(String? exerciseName) async { - print('Fetching data points for: $exerciseName'); + _logger.info('Fetching data points for: $exerciseName'); String currAggregationMethod = _aggregationMethod; List> records; if (_selectedTable == 'Exercise') { diff --git a/lib/widgets/records.dart b/lib/widgets/records.dart index 7e12b53..ea54071 100644 --- a/lib/widgets/records.dart +++ b/lib/widgets/records.dart @@ -1,5 +1,9 @@ import 'package:flutter/material.dart'; import '../core/database.dart'; +import 'package:logging/logging.dart'; + +// Initialize the logger +final Logger _logger = Logger('RecordsLogger'); class RecordsTab extends StatefulWidget { final bool isKg; @@ -43,7 +47,7 @@ class RecordsTabState extends State { _errorMessage = 'Failed to load data. Please try again later.'; // Set error message }); - print('Error fetching records: $e'); // Log error for debugging + _logger.severe('Error fetching records: $e'); } } diff --git a/pubspec.yaml b/pubspec.yaml index 79cb3d7..f171234 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -16,7 +16,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev # https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html # In Windows, build-name is used as the major, minor, and patch parts # of the product and file versions while build-number is used as the build suffix. -version: 0.10.1+51 +version: 0.10.2+52 environment: sdk: '>=3.4.3 <4.0.0' @@ -59,6 +59,9 @@ dependencies: # Use with the CupertinoIcons class for iOS style icons. cupertino_icons: ^1.0.6 + # logging framework + logging: ^1.0.2 + dev_dependencies: flutter_test: sdk: flutter