3
0
Fork 0

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
This commit is contained in:
Michael Brown 2020-12-16 10:25:37 -05:00
parent 2c2aa3d2b0
commit affdc8a7bb
No known key found for this signature in database
GPG Key ID: 3F02D82B487AD431
5 changed files with 41 additions and 2 deletions

View File

@ -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?
) {

View File

@ -43,6 +43,7 @@ class Site {
String status;
String logFile;
String logVerbosity;
bool logLocalTZ;
// A list of errors encountered while loading the site
List<String> 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,
};
}

View File

@ -105,8 +105,10 @@ class _SiteLogsScreenState extends State<SiteLogsScreen> {
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<SiteLogsScreen> {
}
}
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([]);

View File

@ -28,6 +28,7 @@ class Advanced {
String verbosity;
List<UnsafeRoute> unsafeRoutes;
int mtu;
bool logLocalTZ;
}
class AdvancedScreen extends StatefulWidget {
@ -52,6 +53,7 @@ class _AdvancedScreenState extends State<AdvancedScreen> {
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<AdvancedScreen> {
});
});
}),
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,

View File

@ -218,6 +218,7 @@ class _SiteConfigScreenState extends State<SiteConfigScreen> {
site.lhDuration = settings.lhDuration;
site.port = settings.port;
site.logVerbosity = settings.verbosity;
site.logLocalTZ = settings.logLocalTZ;
site.unsafeRoutes = settings.unsafeRoutes;
site.mtu = settings.mtu;
});