Vaibhav_Feature-#768 #59
@ -1,18 +1,14 @@
|
|||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
import 'package:logger/logger.dart';
|
import 'package:logger/logger.dart';
|
||||||
import 'package:intl/intl.dart';
|
import 'package:intl/intl.dart';
|
||||||
import 'package:permission_handler/permission_handler.dart';
|
import 'package:path_provider/path_provider.dart';
|
||||||
|
|
||||||
/// Global logger instance
|
/// Global logger instance
|
||||||
late final Logger appLogger;
|
late final Logger appLogger;
|
||||||
|
|
||||||
/// Log file output handler
|
|
||||||
late final FileLogOutput fileLogOutput;
|
late final FileLogOutput fileLogOutput;
|
||||||
|
|
||||||
/// Initialize logging (call once in `main()`)
|
/// Initialize logging
|
||||||
Future<void> initLogging() async {
|
Future<void> initLogging() async {
|
||||||
await requestStoragePermission();
|
|
||||||
|
|
||||||
fileLogOutput = FileLogOutput();
|
fileLogOutput = FileLogOutput();
|
||||||
|
|
||||||
appLogger = Logger(
|
appLogger = Logger(
|
||||||
@ -23,21 +19,13 @@ Future<void> initLogging() async {
|
|||||||
printEmojis: true,
|
printEmojis: true,
|
||||||
),
|
),
|
||||||
output: MultiOutput([
|
output: MultiOutput([
|
||||||
ConsoleOutput(), // ✅ Console will use the top-level PrettyPrinter
|
ConsoleOutput(),
|
||||||
fileLogOutput, // ✅ File will still use the SimpleFileLogPrinter
|
fileLogOutput,
|
||||||
]),
|
]),
|
||||||
level: Level.debug,
|
level: Level.debug,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Request storage permission (for Android 11+)
|
|
||||||
Future<void> requestStoragePermission() async {
|
|
||||||
final status = await Permission.manageExternalStorage.status;
|
|
||||||
if (!status.isGranted) {
|
|
||||||
await Permission.manageExternalStorage.request();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Safe logger wrapper
|
/// Safe logger wrapper
|
||||||
void logSafe(
|
void logSafe(
|
||||||
String message, {
|
String message, {
|
||||||
@ -66,15 +54,15 @@ void logSafe(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Custom log output that writes to a local `.txt` file
|
/// Log output to file (safe path, no permission required)
|
||||||
class FileLogOutput extends LogOutput {
|
class FileLogOutput extends LogOutput {
|
||||||
File? _logFile;
|
File? _logFile;
|
||||||
|
|
||||||
/// Initialize log file in Downloads/marco_logs/log_YYYY-MM-DD.txt
|
|
||||||
Future<void> _init() async {
|
Future<void> _init() async {
|
||||||
if (_logFile != null) return;
|
if (_logFile != null) return;
|
||||||
|
|
||||||
final directory = Directory('/storage/emulated/0/Download/marco_logs');
|
final baseDir = await getExternalStorageDirectory();
|
||||||
|
final directory = Directory('${baseDir!.path}/marco_logs');
|
||||||
if (!await directory.exists()) {
|
if (!await directory.exists()) {
|
||||||
await directory.create(recursive: true);
|
await directory.create(recursive: true);
|
||||||
}
|
}
|
||||||
@ -119,7 +107,6 @@ class FileLogOutput extends LogOutput {
|
|||||||
return _logFile!.readAsString();
|
return _logFile!.readAsString();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Delete logs older than 3 days
|
|
||||||
Future<void> _cleanOldLogs(Directory directory) async {
|
Future<void> _cleanOldLogs(Directory directory) async {
|
||||||
final files = directory.listSync();
|
final files = directory.listSync();
|
||||||
final now = DateTime.now();
|
final now = DateTime.now();
|
||||||
@ -135,7 +122,7 @@ class FileLogOutput extends LogOutput {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A simple, readable log printer for file output
|
/// Simple log printer for file output
|
||||||
class SimpleFileLogPrinter extends LogPrinter {
|
class SimpleFileLogPrinter extends LogPrinter {
|
||||||
@override
|
@override
|
||||||
List<String> log(LogEvent event) {
|
List<String> log(LogEvent event) {
|
||||||
@ -152,5 +139,5 @@ class SimpleFileLogPrinter extends LogPrinter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Optional log level enum for better type safety
|
/// Optional enum for log levels
|
||||||
enum LogLevel { debug, info, warning, error, verbose }
|
enum LogLevel { debug, info, warning, error, verbose }
|
||||||
|
Loading…
x
Reference in New Issue
Block a user