From 305ddaa7373a05276cdfab87084a3d720acf02c8 Mon Sep 17 00:00:00 2001 From: Brahim Hadriche Date: Fri, 29 Mar 2024 11:30:54 -0400 Subject: [PATCH] Add some timestamps to logs --- playlet-lib/src/components/Logger/Logger.bs | 44 ++++++++++++++++--- playlet-lib/src/source/services/HttpClient.bs | 2 +- 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/playlet-lib/src/components/Logger/Logger.bs b/playlet-lib/src/components/Logger/Logger.bs index 45f55436..15189264 100644 --- a/playlet-lib/src/components/Logger/Logger.bs +++ b/playlet-lib/src/components/Logger/Logger.bs @@ -39,6 +39,7 @@ function LoggerLoop() logsFile = m.top.folder + "/" + m.top.filename previousLogsFile = m.top.folder + "/" + m.top.filenamePrevious filenameStartup = m.top.filenameStartup + dateTime = CreateObject("roDateTime") CreateDirectory(m.top.folder) DeleteFile(previousLogsFile) @@ -59,16 +60,16 @@ function LoggerLoop() if msgType = "roSGNodeEvent" field = msg.getField() if field = "logLine" - OnLineLog(msg, logsFile, buffer) + OnLineLog(msg, dateTime, logsFile, buffer) else if field = "systemLogEvent" - OnLineSysLog(msg, logsFile, buffer) + OnLineSysLog(msg, dateTime, logsFile, buffer) end if end if end while end function -function OnLineLog(event as object, logsFile as string, buffer as object) - line = event.getData() +function OnLineLog(event as object, dateTime as object, logsFile as string, buffer as object) + line = FormatTime(dateTime) + event.getData() ' bs:disable-next-line LINT3012 print line @@ -76,15 +77,46 @@ function OnLineLog(event as object, logsFile as string, buffer as object) buffer.AppendFile(logsFile) end function -function OnLineSysLog(event as object, logsFile as string, buffer as object) as void +function OnLineSysLog(event as object, dateTime as object, logsFile as string, buffer as object) as void info = event.getData() if info.LogType <> "http.error" return end if - line = "[ERROR][SysLog] " + ToString(info) + line = FormatTime(dateTime) + "[ERROR][SysLog] " + ToString(info) ' bs:disable-next-line LINT3012 print line buffer.FromAsciiString(line + `\n`) buffer.AppendFile(logsFile) end function + +' Note: although the timestamp should be captured in the caller, not the logger, +' it is done here to avoid adding overhead to the caller thread. +function FormatTime(dateTime as object) as string + dateTime.Mark() + dateTime.ToLocalTime() + + hours = dateTime.GetHours().ToStr() + if hours.Len() = 1 + hours = "0" + hours + end if + + minutes = dateTime.GetMinutes().ToStr() + if minutes.Len() = 1 + minutes = "0" + minutes + end if + + seconds = dateTime.GetSeconds().ToStr() + if seconds.Len() = 1 + seconds = "0" + seconds + end if + + milliseconds = dateTime.GetMilliseconds().ToStr() + if milliseconds.Len() = 1 + milliseconds = "00" + milliseconds + else if milliseconds.Len() = 2 + milliseconds = "0" + milliseconds + end if + + return "[" + hours + ":" + minutes + ":" + seconds + "." + milliseconds + "]" +end function diff --git a/playlet-lib/src/source/services/HttpClient.bs b/playlet-lib/src/source/services/HttpClient.bs index bfadeec2..47f97568 100644 --- a/playlet-lib/src/source/services/HttpClient.bs +++ b/playlet-lib/src/source/services/HttpClient.bs @@ -486,7 +486,7 @@ namespace HttpClient else eventType = type(msg) if eventType = "roUrlEvent" - LogInfo("Finished", "ID:", m.urlTransfer.GetIdentity()) + LogInfo("Finished ID:", m.urlTransfer.GetIdentity(), "after", m._timer.TotalMilliseconds(), "ms") else if eventType = "roSGNodeEvent" node = msg.getRoSGNode() field = msg.getField()