Skip to content

Commit

Permalink
Implemented logging framework (#358)
Browse files Browse the repository at this point in the history
* Implemented logging framework

* Bump v0.10.2+52
  • Loading branch information
andreped authored Jan 1, 2025
1 parent 70f11b2 commit 8a3a61a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 16 deletions.
24 changes: 14 additions & 10 deletions lib/core/database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -25,7 +29,7 @@ class DatabaseHelper {

Future<Database> _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,
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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');
}
}

Expand All @@ -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');
}
}
}
12 changes: 8 additions & 4 deletions lib/tabs/visualization.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -71,7 +75,7 @@ class VisualizationTabState extends State<VisualizationTab> {
}

Future<void> _fetchExerciseNames() async {
print('Fetching names from $_selectedTable table...');
_logger.info('Fetching names from $_selectedTable table...');
try {
List<String> names;
if (_selectedTable == 'Exercise') {
Expand Down Expand Up @@ -99,7 +103,7 @@ class VisualizationTabState extends State<VisualizationTab> {
}
});
} catch (e) {
print('Error fetching names: $e');
_logger.severe('Error fetching names: $e');
}

// render last recorded exercise by default if any
Expand All @@ -114,7 +118,7 @@ class VisualizationTabState extends State<VisualizationTab> {
});
}
} catch (e) {
print('Error fetching last recorded exercise: $e');
_logger.severe('Error fetching last recorded exercise: $e');
}
}

Expand All @@ -129,7 +133,7 @@ class VisualizationTabState extends State<VisualizationTab> {
}

Future<void> _fetchDataPoints(String? exerciseName) async {
print('Fetching data points for: $exerciseName');
_logger.info('Fetching data points for: $exerciseName');
String currAggregationMethod = _aggregationMethod;
List<Map<String, dynamic>> records;
if (_selectedTable == 'Exercise') {
Expand Down
6 changes: 5 additions & 1 deletion lib/widgets/records.dart
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -43,7 +47,7 @@ class RecordsTabState extends State<RecordsTab> {
_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');
}
}

Expand Down
5 changes: 4 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 8a3a61a

Please sign in to comment.