Refactor logger and minor code polish
This commit is contained in:
70
lib/logger.dart
Normal file
70
lib/logger.dart
Normal file
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
@@ -395,7 +395,7 @@ class _ModManagerPageState extends State<ModManagerPage> {
|
||||
child: Icon(
|
||||
Icons.home,
|
||||
color: Colors.blue,
|
||||
size: 16,
|
||||
size: 24,
|
||||
),
|
||||
),
|
||||
if (mod.isExpansion)
|
||||
@@ -404,7 +404,7 @@ class _ModManagerPageState extends State<ModManagerPage> {
|
||||
child: Icon(
|
||||
Icons.star,
|
||||
color: Colors.yellow,
|
||||
size: 16,
|
||||
size: 24,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 4),
|
||||
@@ -415,7 +415,7 @@ class _ModManagerPageState extends State<ModManagerPage> {
|
||||
child: const Icon(
|
||||
Icons.link,
|
||||
color: Colors.orange,
|
||||
size: 16,
|
||||
size: 24,
|
||||
),
|
||||
),
|
||||
if (mod.loadAfter.isNotEmpty)
|
||||
@@ -425,7 +425,7 @@ class _ModManagerPageState extends State<ModManagerPage> {
|
||||
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<ModManagerPage> {
|
||||
child: const Icon(
|
||||
Icons.arrow_upward,
|
||||
color: Colors.green,
|
||||
size: 16,
|
||||
size: 24,
|
||||
),
|
||||
),
|
||||
],
|
||||
|
@@ -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<void> 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) {
|
||||
|
Reference in New Issue
Block a user