From 5f8ce8f7a19371d5c8dccf9b1eaf9769dd6ea7bc Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Sun, 16 Mar 2025 00:54:27 +0100 Subject: [PATCH] Refactor logger and minor code polish --- lib/logger.dart | 70 ++++++++++++++++++++++++++++++++++++++++++++ lib/main.dart | 10 +++---- lib/modloader.dart | 73 ++-------------------------------------------- 3 files changed, 77 insertions(+), 76 deletions(-) create mode 100644 lib/logger.dart diff --git a/lib/logger.dart b/lib/logger.dart new file mode 100644 index 0000000..bf5385b --- /dev/null +++ b/lib/logger.dart @@ -0,0 +1,70 @@ +import 'dart:io'; + +// Logger class for writing logs to console and file +class Logger { + static final Logger _instance = Logger._internal(); + static Logger get instance => _instance; + + File? _logFile; + IOSink? _logSink; + + Logger._internal() { + _initLogFile(); + } + + void _initLogFile() { + try { + // Use system temp directory + final tempDir = Directory.systemTemp; + final logFileName = 'rimworld_modman.log'; + + _logFile = File('${tempDir.path}${Platform.pathSeparator}$logFileName'); + _logSink = _logFile!.openWrite(mode: FileMode.writeOnly); + + info('Logging initialized. Log file: ${_logFile!.path}'); + } catch (e) { + print('Failed to initialize log file: $e'); + } + } + + void _log(String message, String level) { + final timestamp = DateTime.now().toIso8601String(); + final formattedMessage = '[$timestamp] [$level] $message'; + + // Always print to console + print(formattedMessage); + + // Write to file if initialized + if (_logSink != null) { + try { + _logSink!.writeln(formattedMessage); + } catch (e) { + print('Error writing to log file: $e'); + } + } + } + + void info(String message) { + _log(message, 'INFO'); + } + + void warning(String message) { + _log(message, 'WARN'); + } + + void error(String message) { + _log(message, 'ERROR'); + } + + void close() { + if (_logSink != null) { + try { + _logSink!.flush(); + _logSink!.close(); + } catch (e) { + print('Error closing log file: $e'); + } + _logSink = null; + } + } +} diff --git a/lib/main.dart b/lib/main.dart index c0e2ba8..4e15541 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -395,7 +395,7 @@ class _ModManagerPageState extends State { child: Icon( Icons.home, color: Colors.blue, - size: 16, + size: 24, ), ), if (mod.isExpansion) @@ -404,7 +404,7 @@ class _ModManagerPageState extends State { child: Icon( Icons.star, color: Colors.yellow, - size: 16, + size: 24, ), ), const SizedBox(width: 4), @@ -415,7 +415,7 @@ class _ModManagerPageState extends State { child: const Icon( Icons.link, color: Colors.orange, - size: 16, + size: 24, ), ), if (mod.loadAfter.isNotEmpty) @@ -425,7 +425,7 @@ class _ModManagerPageState extends State { child: const Icon( Icons.arrow_downward, color: Colors.blue, - size: 16, + size: 24, ), ), if (mod.loadBefore.isNotEmpty) @@ -435,7 +435,7 @@ class _ModManagerPageState extends State { child: const Icon( Icons.arrow_upward, color: Colors.green, - size: 16, + size: 24, ), ), ], diff --git a/lib/modloader.dart b/lib/modloader.dart index 863fe1f..9711ed4 100644 --- a/lib/modloader.dart +++ b/lib/modloader.dart @@ -1,5 +1,6 @@ import 'dart:io'; import 'dart:async'; +import 'package:rimworld_modman/logger.dart'; import 'package:xml/xml.dart'; const root = r'C:/Users/Administrator/Seafile/Games-Rimworld'; @@ -8,75 +9,6 @@ const configRoot = '$root/AppData/RimWorld by Ludeon Studios/Config'; const configPath = '$configRoot/ModsConfig.xml'; const logsPath = '$root/ModManager'; -// Logger class for writing logs to console and file -class Logger { - static final Logger _instance = Logger._internal(); - static Logger get instance => _instance; - - File? _logFile; - IOSink? _logSink; - - Logger._internal() { - _initLogFile(); - } - - void _initLogFile() { - try { - // Use system temp directory - final tempDir = Directory.systemTemp; - final logFileName = 'rimworld_modman.log'; - - _logFile = File('${tempDir.path}${Platform.pathSeparator}$logFileName'); - _logSink = _logFile!.openWrite(mode: FileMode.writeOnly); - - info('Logging initialized. Log file: ${_logFile!.path}'); - } catch (e) { - print('Failed to initialize log file: $e'); - } - } - - void _log(String message, String level) { - final timestamp = DateTime.now().toIso8601String(); - final formattedMessage = '[$timestamp] [$level] $message'; - - // Always print to console - print(formattedMessage); - - // Write to file if initialized - if (_logSink != null) { - try { - _logSink!.writeln(formattedMessage); - } catch (e) { - print('Error writing to log file: $e'); - } - } - } - - void info(String message) { - _log(message, 'INFO'); - } - - void warning(String message) { - _log(message, 'WARN'); - } - - void error(String message) { - _log(message, 'ERROR'); - } - - void close() { - if (_logSink != null) { - try { - _logSink!.flush(); - _logSink!.close(); - } catch (e) { - print('Error closing log file: $e'); - } - _logSink = null; - } - } -} - XmlElement findCaseInsensitive(XmlElement element, String name) { return element.childElements.firstWhere( (e) => e.name.local.toLowerCase() == name, @@ -342,7 +274,6 @@ class ModList { ModList({required this.path}); - // Simplified loading with config file first Future loadWithConfig({bool skipFileCount = false}) async { final logger = Logger.instance; @@ -492,7 +423,7 @@ class ModList { if (loadedModsCount % 50 == 0 || loadedModsCount == totalModsFound) { logger.info( - 'Progress: Loaded $loadedModsCount mods (${stopwatch.elapsedMilliseconds}ms)', + 'Progress: Loaded $loadedModsCount mods (${modTime}ms)', ); } } catch (e) {