Refactor logger and minor code polish
This commit is contained in:
@@ -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