From affdc8a7bbab020f6c00b70549af0ad387300dec Mon Sep 17 00:00:00 2001 From: Michael Brown Date: Wed, 16 Dec 2020 10:25:37 -0500 Subject: [PATCH] Add configuration setting for using local TZ The default log timestamp is in UTC, this commit allows the user to convert the log timestamps to their own timezone. When approaching this problem I originally considered allowing the user to set any timezone offset of their choice. This would require some alignment between the device and client that was feeling cumbersome. In the end we decided to keep things relatively simple and just allow a simple toggle to convert log entries upon reading the logfile on the device. The default log timestamp emitted by the client remains UTC/Zulu Advances #2 --- .../kotlin/net/defined/mobile_nebula/Sites.kt | 3 +++ lib/models/Site.dart | 4 ++++ lib/screens/SiteLogsScreen.dart | 17 +++++++++++++++-- lib/screens/siteConfig/AdvancedScreen.dart | 18 ++++++++++++++++++ lib/screens/siteConfig/SiteConfigScreen.dart | 1 + 5 files changed, 41 insertions(+), 2 deletions(-) diff --git a/android/app/src/main/kotlin/net/defined/mobile_nebula/Sites.kt b/android/app/src/main/kotlin/net/defined/mobile_nebula/Sites.kt index 5046744..5fa5cf3 100644 --- a/android/app/src/main/kotlin/net/defined/mobile_nebula/Sites.kt +++ b/android/app/src/main/kotlin/net/defined/mobile_nebula/Sites.kt @@ -142,6 +142,7 @@ class Site { val cipher: String val sortKey: Int var logVerbosity: String + var logLocalTZ: Boolean? var connected: Boolean? var status: String? val logFile: String? @@ -172,6 +173,7 @@ class Site { sortKey = incomingSite.sortKey ?: 0 logFile = siteDir.resolve("log").absolutePath logVerbosity = incomingSite.logVerbosity ?: "info" + logLocalTZ = incomingSite.logLocalTZ ?: false connected = false status = "Disconnected" @@ -251,6 +253,7 @@ class IncomingSite( val cipher: String, val sortKey: Int?, var logVerbosity: String?, + val logLocalTZ: Boolean?, @Expose(serialize = false) var key: String? ) { diff --git a/lib/models/Site.dart b/lib/models/Site.dart index 06432a3..60321be 100644 --- a/lib/models/Site.dart +++ b/lib/models/Site.dart @@ -43,6 +43,7 @@ class Site { String status; String logFile; String logVerbosity; + bool logLocalTZ; // A list of errors encountered while loading the site List errors; @@ -62,6 +63,7 @@ class Site { this.status, this.logFile, this.logVerbosity = 'info', + this.logLocalTZ, errors, unsafeRoutes}) : staticHostmap = staticHostmap ?? {}, @@ -105,6 +107,7 @@ class Site { sortKey = json['sortKey']; logFile = json['logFile']; logVerbosity = json['logVerbosity']; + logLocalTZ = json['logLocalTZ']; connected = json['connected'] ?? false; status = json['status'] ?? ""; @@ -154,6 +157,7 @@ class Site { 'cipher': cipher, 'sortKey': sortKey, 'logVerbosity': logVerbosity, + 'logLocalTZ': logLocalTZ, }; } diff --git a/lib/screens/SiteLogsScreen.dart b/lib/screens/SiteLogsScreen.dart index b40774b..1d28e57 100644 --- a/lib/screens/SiteLogsScreen.dart +++ b/lib/screens/SiteLogsScreen.dart @@ -105,8 +105,10 @@ class _SiteLogsScreenState extends State { loadLogs() async { var file = File(widget.site.logFile); try { - final v = await file.readAsString(); - + String v = await file.readAsString(); + if(widget.site.logLocalTZ) { + v = convertToLocalTZ(v); + } setState(() { logs = v; }); @@ -115,6 +117,17 @@ class _SiteLogsScreenState extends State { } } +convertToLocalTZ(String rawLog) { + // Regex to extract time value + rawLog = rawLog.replaceAllMapped(RegExp('time="(.*?)"'), (match){ + // Convert to DateTime with the inner match from above + DateTime userDate = DateTime.parse(match.group(1)); + // Return timestamp in format that matches default + return 'time="${userDate.toLocal().toIso8601String()}"'; + }); + return rawLog; +} + deleteLogs() async { var file = File(widget.site.logFile); await file.writeAsBytes([]); diff --git a/lib/screens/siteConfig/AdvancedScreen.dart b/lib/screens/siteConfig/AdvancedScreen.dart index 8911f0b..5ebfa69 100644 --- a/lib/screens/siteConfig/AdvancedScreen.dart +++ b/lib/screens/siteConfig/AdvancedScreen.dart @@ -28,6 +28,7 @@ class Advanced { String verbosity; List unsafeRoutes; int mtu; + bool logLocalTZ; } class AdvancedScreen extends StatefulWidget { @@ -52,6 +53,7 @@ class _AdvancedScreenState extends State { settings.verbosity = widget.site.logVerbosity; settings.unsafeRoutes = widget.site.unsafeRoutes; settings.mtu = widget.site.mtu; + settings.logLocalTZ = widget.site.logLocalTZ; super.initState(); } @@ -131,6 +133,22 @@ class _AdvancedScreenState extends State { }); }); }), + ConfigItem( + label: Text('Use Local Time Zone'), + labelWidth: 200, + content: Align( + alignment: Alignment.centerRight, + child: Switch.adaptive( + materialTapTargetSize: MaterialTapTargetSize.shrinkWrap, + value: settings.logLocalTZ, + onChanged: (value) { + setState(() { + settings.logLocalTZ = value; + changed = true; + }); + }, + )), + ), ConfigPageItem( label: Text('Log verbosity'), labelWidth: 150, diff --git a/lib/screens/siteConfig/SiteConfigScreen.dart b/lib/screens/siteConfig/SiteConfigScreen.dart index a819468..61b1f32 100644 --- a/lib/screens/siteConfig/SiteConfigScreen.dart +++ b/lib/screens/siteConfig/SiteConfigScreen.dart @@ -218,6 +218,7 @@ class _SiteConfigScreenState extends State { site.lhDuration = settings.lhDuration; site.port = settings.port; site.logVerbosity = settings.verbosity; + site.logLocalTZ = settings.logLocalTZ; site.unsafeRoutes = settings.unsafeRoutes; site.mtu = settings.mtu; });