From 73b6314ec74b5a59335ace89ad88758931ef9b27 Mon Sep 17 00:00:00 2001 From: Brahim Hadriche Date: Tue, 9 Jan 2024 14:19:54 -0500 Subject: [PATCH] Add app logs to log file Refactor a new functions by adding name spaces Startup times are now logged --- .../BootstrapScene/BootstrapScene.bs | 26 +++---- playlet-app/src/source/Dialog.bs | 75 ++++++++++--------- playlet-app/src/source/Logger.bs | 20 +++++ playlet-app/src/source/Main.bs | 5 +- playlet-app/src/source/Manifest.bs | 13 +++- playlet-app/src/source/Manifest.spec.bs | 2 +- playlet-app/src/source/Registry.bs | 57 +++++++------- playlet-lib/src/components/Logger/Logger.bs | 14 +++- playlet-lib/src/components/Logger/Logger.xml | 1 + 9 files changed, 128 insertions(+), 85 deletions(-) create mode 100644 playlet-app/src/source/Logger.bs diff --git a/playlet-app/src/components/BootstrapScene/BootstrapScene.bs b/playlet-app/src/components/BootstrapScene/BootstrapScene.bs index 3dbdb675..ad47d0b3 100644 --- a/playlet-app/src/components/BootstrapScene/BootstrapScene.bs +++ b/playlet-app/src/components/BootstrapScene/BootstrapScene.bs @@ -1,8 +1,6 @@ import "LoadingMessages.bs" +import "pkg:/source/Logger.bs" -' TODO:P2 Add a "logQueue" field to the scene, and push it to the logger later when playlet-lib is loaded -' Alternatively, just log to the console and the known log file in cachefs -' TODO:P2 log the playlet-lib load status and load speed to the log queue function Init() as void m.scene = m.top.getScene() m.top.backgroundColor = "#242424FF" @@ -24,17 +22,17 @@ end function function GetPlayletLibUrls() as object #if DEBUG - debug = { type: "debug", link: ReadManifestValue("playlet_lib_debug_url") } + debug = { type: "debug", link: Manifest.ReadValue("playlet_lib_debug_url") } return [debug] #else - savedUrls = GetPlayletLibUrlsFromRegistry() + savedUrls = Registry.GetPlayletLibUrlsFromRegistry() if savedUrls <> invalid m.shouldClearRegistryOnLoadFail = true return savedUrls end if - github = { type: "github", link: ReadManifestValue("playlet_lib_remote_url") } - embedded = { type: "embedded", link: ReadManifestValue("playlet_lib_embedded_url") } + github = { type: "github", link: Manifest.ReadValue("playlet_lib_remote_url") } + embedded = { type: "embedded", link: Manifest.ReadValue("playlet_lib_embedded_url") } return [github, embedded] #end if end function @@ -43,7 +41,7 @@ function LoadPlayletLib() as void if m.playletLibUrlIndex >= m.playletLibUrls.Count() m.loadingIndicator.visible = false if m.shouldClearRegistryOnLoadFail = true - DeleteRegistryKey("playlet_lib_urls", "Playlet") + Registry.DeleteKey("playlet_lib_urls", "Playlet") end if ShowPlayletLibLoadErrorDialog() return @@ -58,8 +56,7 @@ function OnLoadStatusChanged() as void libUrl = m.playletLibUrls[m.playletLibUrlIndex] if m.lib.loadStatus = "ready" RemoveComponentLibrary() - ' bs:disable-next-line LINT3012 - ?"Playlet lib loaded from " libUrl.link " in " m.timer.TotalMilliseconds().ToStr() "ms" + Logger.Log(`Playlet lib loaded from ${libUrl.link} in ${m.timer.TotalMilliseconds()} ms`) m.timer = invalid m.scene.playletLibLoadedUrl = libUrl container = m.scene.findNode("MainSceneContainer") @@ -68,8 +65,7 @@ function OnLoadStatusChanged() as void end if if m.lib.loadStatus = "failed" - ' bs:disable-next-line LINT3012 - ?"Playlet lib failed to load from " libUrl.link + Logger.Log(`Playlet lib failed to load from ${libUrl.link}`) ' Try next url m.playletLibUrlIndex += 1 LoadPlayletLib() @@ -93,8 +89,8 @@ function LoadComponentLibrary(uri as string) m.lib = m.scene.createChild("ComponentLibrary") ' bs:disable-next-line NO_OBSERVE_STRING_CALLBACK m.lib.observeField("loadStatus", "OnLoadStatusChanged") - ' bs:disable-next-line LINT3012 - ?"Loading Playlet lib from " uri + + Logger.Log(`Loading Playlet lib from ${uri}`) m.lib.uri = uri end function @@ -113,5 +109,5 @@ function ShowPlayletLibLoadErrorDialog() "If the problem persist, contact Playlet authors." ] - ShowErrorDialog(message, bulletText, bottomText) + Dialog.ShowError(message, bulletText, bottomText) end function diff --git a/playlet-app/src/source/Dialog.bs b/playlet-app/src/source/Dialog.bs index d8e3f2cd..b2110b27 100644 --- a/playlet-app/src/source/Dialog.bs +++ b/playlet-app/src/source/Dialog.bs @@ -1,40 +1,45 @@ -function ShowErrorDialog(message as object, bulletText as object, bottomMessage as object) - ' Note: dialog can be null since StandardMessageDialog is only available on 10.0 and later - dialog = CreateObject("roSGNode", "StandardMessageDialog") - SetPalette(dialog) - dialog.message = message - dialog.bulletText = bulletText - dialog.bottomMessage = bottomMessage - dialog.title = "Error loading Playlet" - dialog.buttons = ["Exit"] +namespace Dialog - ' bs:disable-next-line NO_OBSERVE_STRING_CALLBACK - dialog.observeField("buttonSelected", "OnButtonSelected") - ' bs:disable-next-line NO_OBSERVE_STRING_CALLBACK - dialog.observeField("wasClosed", "OnDialogClosed") + function ShowError(message as object, bulletText as object, bottomMessage as object) + ' Note: dialog can be null since StandardMessageDialog is only available on 10.0 and later + msgDialog = CreateObject("roSGNode", "StandardMessageDialog") + SetPalette(msgDialog) + msgDialog.message = message + msgDialog.bulletText = bulletText + msgDialog.bottomMessage = bottomMessage + msgDialog.title = "Error loading Playlet" + msgDialog.buttons = ["Exit"] - m.top.getScene().dialog = dialog -end function + ' bs:disable-next-line NO_OBSERVE_STRING_CALLBACK + msgDialog.observeField("buttonSelected", "OnButtonSelected") + ' bs:disable-next-line NO_OBSERVE_STRING_CALLBACK + msgDialog.observeField("wasClosed", "OnDialogClosed") -function SetPalette(dialog as object) - palette = createObject("roSGNode", "RSGPalette") - palette.colors = { - DialogBackgroundColor: "#242424FF" - DialogFocusColor: "#CECECEFF" - DialogFocusItemColor: "#202020FF" - DialogSecondaryTextColor: "#DBDBDBFF" - DialogSecondaryItemColor: "#FF1C30FF" - DialogTextColor: "#DBDBDBFF" - } - dialog.palette = palette -end function + m.top.getScene().dialog = msgDialog + end function -function OnButtonSelected() - m.top.getScene().dialog.close = true -end function + function SetPalette(msgDialog as object) + palette = createObject("roSGNode", "RSGPalette") + palette.colors = { + DialogBackgroundColor: "#242424FF" + DialogFocusColor: "#CECECEFF" + DialogFocusItemColor: "#202020FF" + DialogSecondaryTextColor: "#DBDBDBFF" + DialogSecondaryItemColor: "#FF1C30FF" + DialogTextColor: "#DBDBDBFF" + } + msgDialog.palette = palette + end function + + function OnButtonSelected() + m.top.getScene().dialog.close = true + end function + + function OnDialogClosed() + ' TODO:P1 throw an exception to cause a crash report to be generated + ' Should include which playlet lib failed to load + m.top.getScene().exitChannel = true + end function + +end namespace -function OnDialogClosed() - ' TODO:P1 throw an exception to cause a crash report to be generated - ' Should include which playlet lib failed to load - m.top.getScene().exitChannel = true -end function diff --git a/playlet-app/src/source/Logger.bs b/playlet-app/src/source/Logger.bs new file mode 100644 index 00000000..9c2abebb --- /dev/null +++ b/playlet-app/src/source/Logger.bs @@ -0,0 +1,20 @@ +namespace Logger + + ' Put the file at the root directry to avoid needing to create a folder + const LOGS_FILE_PATH = "cachefs:/playlet_startup_logs.txt" + + function Init() + DeleteFile(LOGS_FILE_PATH) + end function + + function Log(msg as string) + msg = "[APP] " + msg + ' bs:disable-next-line LINT3012 + print(msg) + + buffer = CreateObject("roByteArray") + buffer.FromAsciiString(msg + `\n`) + buffer.AppendFile(LOGS_FILE_PATH) + end function + +end namespace diff --git a/playlet-app/src/source/Main.bs b/playlet-app/src/source/Main.bs index 20fc4415..a25408f2 100644 --- a/playlet-app/src/source/Main.bs +++ b/playlet-app/src/source/Main.bs @@ -19,8 +19,9 @@ function Main(args as object) as void port = CreateObject("roMessagePort") screen.setMessagePort(port) m.global = screen.getGlobalNode() + Logger.Init() - ClearPlayletLibUrlsIfNeeded(args) + Registry.ClearPlayletLibUrlsIfNeeded(args) scene = screen.CreateScene("BootstrapScene") screen.show() @@ -77,7 +78,7 @@ function Main(args as object) as void field = msg.getField() data = msg.getData() if field = "exitChannel" and data = true - END + return end if else if msgType = "roInputEvent" scene.inputArgs = msg.getInfo() diff --git a/playlet-app/src/source/Manifest.bs b/playlet-app/src/source/Manifest.bs index 690d6152..cc9767da 100644 --- a/playlet-app/src/source/Manifest.bs +++ b/playlet-app/src/source/Manifest.bs @@ -1,4 +1,9 @@ -function ReadManifestValue(key as string) as string - appInfo = CreateObject("roAppInfo") - return appInfo.GetValue(key) -end function +namespace Manifest + + function ReadValue(key as string) as string + appInfo = CreateObject("roAppInfo") + return appInfo.GetValue(key) + end function + +end namespace + diff --git a/playlet-app/src/source/Manifest.spec.bs b/playlet-app/src/source/Manifest.spec.bs index 84e9d576..8acfb0f3 100644 --- a/playlet-app/src/source/Manifest.spec.bs +++ b/playlet-app/src/source/Manifest.spec.bs @@ -11,7 +11,7 @@ namespace Tests @params("playlet_lib_remote_url", "https://github.com/iBicha/playlet/releases/latest/download/playlet-lib.zip") @params("playlet_lib_embedded_url", "pkg:/lib/playlet-lib.zip") function _(key as string, expectedValue as string) - m.assertEqual(ReadManifestValue(key), expectedValue) + m.assertEqual(Manifest.ReadValue(key), expectedValue) end function end class diff --git a/playlet-app/src/source/Registry.bs b/playlet-app/src/source/Registry.bs index 144b26a6..c6f28632 100644 --- a/playlet-app/src/source/Registry.bs +++ b/playlet-app/src/source/Registry.bs @@ -1,29 +1,34 @@ -function ReadRegistryKey(key as string, section as string) as dynamic - sec = CreateObject("roRegistrySection", section) - if not sec.Exists(key) - return invalid - end if - return sec.Read(key) -end function +namespace Registry -function DeleteRegistryKey(key as string, section as string) - sec = CreateObject("roRegistrySection", section) - sec.Delete(key) - sec.Flush() -end function + function ReadKey(key as string, section as string) as dynamic + sec = CreateObject("roRegistrySection", section) + if not sec.Exists(key) + return invalid + end if + return sec.Read(key) + end function -function GetPlayletLibUrlsFromRegistry() as object - urlsJson = ReadRegistryKey("playlet_lib_urls", "Playlet") - if urlsJson = invalid - return invalid - end if - urls = ParseJson(urlsJson) - return urls -end function + function DeleteKey(key as string, section as string) + sec = CreateObject("roRegistrySection", section) + sec.Delete(key) + sec.Flush() + end function -function ClearPlayletLibUrlsIfNeeded(launchArgs as object) as void - if launchArgs = invalid or launchArgs.clearPlayletLibUrls = invalid - return - end if - DeleteRegistryKey("playlet_lib_urls", "Playlet") -end function + function GetPlayletLibUrlsFromRegistry() as object + urlsJson = ReadKey("playlet_lib_urls", "Playlet") + if urlsJson = invalid + return invalid + end if + urls = ParseJson(urlsJson) + return urls + end function + + function ClearPlayletLibUrlsIfNeeded(launchArgs as object) as void + if launchArgs = invalid or launchArgs.clearPlayletLibUrls = invalid + return + end if + DeleteKey("playlet_lib_urls", "Playlet") + Logger.Log("Cleared key playlet_lib_urls from registry") + end function + +end namespace diff --git a/playlet-lib/src/components/Logger/Logger.bs b/playlet-lib/src/components/Logger/Logger.bs index b6e626f0..bf211762 100644 --- a/playlet-lib/src/components/Logger/Logger.bs +++ b/playlet-lib/src/components/Logger/Logger.bs @@ -1,5 +1,5 @@ import "pkg:/source/utils/Logging.bs" -import "pkg:/source/utils/Types.bs" +import "pkg:/source/utils/StringUtils.bs" function Init() m.top.functionName = "LoggerLoop" @@ -32,13 +32,23 @@ function LoggerLoop() port = m.port buffer = CreateObject("roByteArray") - previousLogsFile = m.top.folder + "/" + m.top.filenamePrevious logsFile = m.top.folder + "/" + m.top.filename + previousLogsFile = m.top.folder + "/" + m.top.filenamePrevious + filenameStartup = m.top.filenameStartup CreateDirectory(m.top.folder) DeleteFile(previousLogsFile) MoveFile(logsFile, previousLogsFile) + startUpLogs = ReadAsciiFile(filenameStartup) + if not StringUtils.IsNullOrEmpty(startUpLogs) + line = `************* Startup Logs *************\n${startUpLogs}*********** End Startup Logs ***********\n` + ' bs:disable-next-line LINT3012 + print line + buffer.FromAsciiString(line) + buffer.AppendFile(logsFile) + end if + while true msg = wait(0, port) msgType = type(msg) diff --git a/playlet-lib/src/components/Logger/Logger.xml b/playlet-lib/src/components/Logger/Logger.xml index 39ff9bbc..aa8e1529 100644 --- a/playlet-lib/src/components/Logger/Logger.xml +++ b/playlet-lib/src/components/Logger/Logger.xml @@ -5,5 +5,6 @@ + \ No newline at end of file