Add app logs to log file

Refactor a new functions by adding name spaces
Startup times are now logged
This commit is contained in:
Brahim Hadriche committed 2024-01-09 14:19:54 -05:00
1 parent 29969db3e4
commit 73b6314ec7
9 files changed
+81 -38

No files matched your search

@@ -1,8 +1,6 @@
import "LoadingMessages.bs" 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 function Init() as void
m.scene = m.top.getScene() m.scene = m.top.getScene()
m.top.backgroundColor = "#242424FF" m.top.backgroundColor = "#242424FF"
@@ -24,17 +22,17 @@ end function
function GetPlayletLibUrls() as object function GetPlayletLibUrls() as object
#if DEBUG #if DEBUG
debug = { type: "debug", link: ReadManifestValue("playlet_lib_debug_url") } debug = { type: "debug", link: Manifest.ReadValue("playlet_lib_debug_url") }
return [debug] return [debug]
#else #else
savedUrls = GetPlayletLibUrlsFromRegistry() savedUrls = Registry.GetPlayletLibUrlsFromRegistry()
if savedUrls <> invalid if savedUrls <> invalid
m.shouldClearRegistryOnLoadFail = true m.shouldClearRegistryOnLoadFail = true
return savedUrls return savedUrls
end if end if
github = { type: "github", link: ReadManifestValue("playlet_lib_remote_url") } github = { type: "github", link: Manifest.ReadValue("playlet_lib_remote_url") }
embedded = { type: "embedded", link: ReadManifestValue("playlet_lib_embedded_url") } embedded = { type: "embedded", link: Manifest.ReadValue("playlet_lib_embedded_url") }
return [github, embedded] return [github, embedded]
#end if #end if
end function end function
@@ -43,7 +41,7 @@ function LoadPlayletLib() as void
if m.playletLibUrlIndex >= m.playletLibUrls.Count() if m.playletLibUrlIndex >= m.playletLibUrls.Count()
m.loadingIndicator.visible = false m.loadingIndicator.visible = false
if m.shouldClearRegistryOnLoadFail = true if m.shouldClearRegistryOnLoadFail = true
DeleteRegistryKey("playlet_lib_urls", "Playlet") Registry.DeleteKey("playlet_lib_urls", "Playlet")
end if end if
ShowPlayletLibLoadErrorDialog() ShowPlayletLibLoadErrorDialog()
return return
@@ -58,8 +56,7 @@ function OnLoadStatusChanged() as void
libUrl = m.playletLibUrls[m.playletLibUrlIndex] libUrl = m.playletLibUrls[m.playletLibUrlIndex]
if m.lib.loadStatus = "ready" if m.lib.loadStatus = "ready"
RemoveComponentLibrary() RemoveComponentLibrary()
' bs:disable-next-line LINT3012 Logger.Log(`Playlet lib loaded from ${libUrl.link} in ${m.timer.TotalMilliseconds()} ms`)
?"Playlet lib loaded from " libUrl.link " in " m.timer.TotalMilliseconds().ToStr() "ms"
m.timer = invalid m.timer = invalid
m.scene.playletLibLoadedUrl = libUrl m.scene.playletLibLoadedUrl = libUrl
container = m.scene.findNode("MainSceneContainer") container = m.scene.findNode("MainSceneContainer")
@@ -68,8 +65,7 @@ function OnLoadStatusChanged() as void
end if end if
if m.lib.loadStatus = "failed" if m.lib.loadStatus = "failed"
' bs:disable-next-line LINT3012 Logger.Log(`Playlet lib failed to load from ${libUrl.link}`)
?"Playlet lib failed to load from " libUrl.link
' Try next url ' Try next url
m.playletLibUrlIndex += 1 m.playletLibUrlIndex += 1
LoadPlayletLib() LoadPlayletLib()
@@ -93,8 +89,8 @@ function LoadComponentLibrary(uri as string)
m.lib = m.scene.createChild("ComponentLibrary") m.lib = m.scene.createChild("ComponentLibrary")
' bs:disable-next-line NO_OBSERVE_STRING_CALLBACK ' bs:disable-next-line NO_OBSERVE_STRING_CALLBACK
m.lib.observeField("loadStatus", "OnLoadStatusChanged") 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 m.lib.uri = uri
end function end function
@@ -113,5 +109,5 @@ function ShowPlayletLibLoadErrorDialog()
"If the problem persist, contact Playlet authors." "If the problem persist, contact Playlet authors."
] ]
ShowErrorDialog(message, bulletText, bottomText) Dialog.ShowError(message, bulletText, bottomText)
end function end function
+18 -13
View File
@@ -1,22 +1,24 @@
function ShowErrorDialog(message as object, bulletText as object, bottomMessage as object) namespace Dialog
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 ' Note: dialog can be null since StandardMessageDialog is only available on 10.0 and later
dialog = CreateObject("roSGNode", "StandardMessageDialog") msgDialog = CreateObject("roSGNode", "StandardMessageDialog")
SetPalette(dialog) SetPalette(msgDialog)
dialog.message = message msgDialog.message = message
dialog.bulletText = bulletText msgDialog.bulletText = bulletText
dialog.bottomMessage = bottomMessage msgDialog.bottomMessage = bottomMessage
dialog.title = "Error loading Playlet" msgDialog.title = "Error loading Playlet"
dialog.buttons = ["Exit"] msgDialog.buttons = ["Exit"]
' bs:disable-next-line NO_OBSERVE_STRING_CALLBACK ' bs:disable-next-line NO_OBSERVE_STRING_CALLBACK
dialog.observeField("buttonSelected", "OnButtonSelected") msgDialog.observeField("buttonSelected", "OnButtonSelected")
' bs:disable-next-line NO_OBSERVE_STRING_CALLBACK ' bs:disable-next-line NO_OBSERVE_STRING_CALLBACK
dialog.observeField("wasClosed", "OnDialogClosed") msgDialog.observeField("wasClosed", "OnDialogClosed")
m.top.getScene().dialog = dialog m.top.getScene().dialog = msgDialog
end function end function
function SetPalette(dialog as object) function SetPalette(msgDialog as object)
palette = createObject("roSGNode", "RSGPalette") palette = createObject("roSGNode", "RSGPalette")
palette.colors = { palette.colors = {
DialogBackgroundColor: "#242424FF" DialogBackgroundColor: "#242424FF"
@@ -26,7 +28,7 @@ function SetPalette(dialog as object)
DialogSecondaryItemColor: "#FF1C30FF" DialogSecondaryItemColor: "#FF1C30FF"
DialogTextColor: "#DBDBDBFF" DialogTextColor: "#DBDBDBFF"
} }
dialog.palette = palette msgDialog.palette = palette
end function end function
function OnButtonSelected() function OnButtonSelected()
@@ -38,3 +40,6 @@ function OnDialogClosed()
' Should include which playlet lib failed to load ' Should include which playlet lib failed to load
m.top.getScene().exitChannel = true m.top.getScene().exitChannel = true
end function end function
end namespace
+20
View File
@@ -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
+3 -2
View File
@@ -19,8 +19,9 @@ function Main(args as object) as void
port = CreateObject("roMessagePort") port = CreateObject("roMessagePort")
screen.setMessagePort(port) screen.setMessagePort(port)
m.global = screen.getGlobalNode() m.global = screen.getGlobalNode()
Logger.Init()
ClearPlayletLibUrlsIfNeeded(args) Registry.ClearPlayletLibUrlsIfNeeded(args)
scene = screen.CreateScene("BootstrapScene") scene = screen.CreateScene("BootstrapScene")
screen.show() screen.show()
@@ -77,7 +78,7 @@ function Main(args as object) as void
field = msg.getField() field = msg.getField()
data = msg.getData() data = msg.getData()
if field = "exitChannel" and data = true if field = "exitChannel" and data = true
END return
end if end if
else if msgType = "roInputEvent" else if msgType = "roInputEvent"
scene.inputArgs = msg.getInfo() scene.inputArgs = msg.getInfo()
+6 -1
View File
@@ -1,4 +1,9 @@
function ReadManifestValue(key as string) as string namespace Manifest
function ReadValue(key as string) as string
appInfo = CreateObject("roAppInfo") appInfo = CreateObject("roAppInfo")
return appInfo.GetValue(key) return appInfo.GetValue(key)
end function end function
end namespace
+1 -1
View File
@@ -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_remote_url", "https://github.com/iBicha/playlet/releases/latest/download/playlet-lib.zip")
@params("playlet_lib_embedded_url", "pkg:/lib/playlet-lib.zip") @params("playlet_lib_embedded_url", "pkg:/lib/playlet-lib.zip")
function _(key as string, expectedValue as string) function _(key as string, expectedValue as string)
m.assertEqual(ReadManifestValue(key), expectedValue) m.assertEqual(Manifest.ReadValue(key), expectedValue)
end function end function
end class end class
+9 -4
View File
@@ -1,4 +1,6 @@
function ReadRegistryKey(key as string, section as string) as dynamic namespace Registry
function ReadKey(key as string, section as string) as dynamic
sec = CreateObject("roRegistrySection", section) sec = CreateObject("roRegistrySection", section)
if not sec.Exists(key) if not sec.Exists(key)
return invalid return invalid
@@ -6,14 +8,14 @@ function ReadRegistryKey(key as string, section as string) as dynamic
return sec.Read(key) return sec.Read(key)
end function end function
function DeleteRegistryKey(key as string, section as string) function DeleteKey(key as string, section as string)
sec = CreateObject("roRegistrySection", section) sec = CreateObject("roRegistrySection", section)
sec.Delete(key) sec.Delete(key)
sec.Flush() sec.Flush()
end function end function
function GetPlayletLibUrlsFromRegistry() as object function GetPlayletLibUrlsFromRegistry() as object
urlsJson = ReadRegistryKey("playlet_lib_urls", "Playlet") urlsJson = ReadKey("playlet_lib_urls", "Playlet")
if urlsJson = invalid if urlsJson = invalid
return invalid return invalid
end if end if
@@ -25,5 +27,8 @@ function ClearPlayletLibUrlsIfNeeded(launchArgs as object) as void
if launchArgs = invalid or launchArgs.clearPlayletLibUrls = invalid if launchArgs = invalid or launchArgs.clearPlayletLibUrls = invalid
return return
end if end if
DeleteRegistryKey("playlet_lib_urls", "Playlet") DeleteKey("playlet_lib_urls", "Playlet")
Logger.Log("Cleared key playlet_lib_urls from registry")
end function end function
end namespace
+12 -2
View File
@@ -1,5 +1,5 @@
import "pkg:/source/utils/Logging.bs" import "pkg:/source/utils/Logging.bs"
import "pkg:/source/utils/Types.bs" import "pkg:/source/utils/StringUtils.bs"
function Init() function Init()
m.top.functionName = "LoggerLoop" m.top.functionName = "LoggerLoop"
@@ -32,13 +32,23 @@ function LoggerLoop()
port = m.port port = m.port
buffer = CreateObject("roByteArray") buffer = CreateObject("roByteArray")
previousLogsFile = m.top.folder + "/" + m.top.filenamePrevious
logsFile = m.top.folder + "/" + m.top.filename logsFile = m.top.folder + "/" + m.top.filename
previousLogsFile = m.top.folder + "/" + m.top.filenamePrevious
filenameStartup = m.top.filenameStartup
CreateDirectory(m.top.folder) CreateDirectory(m.top.folder)
DeleteFile(previousLogsFile) DeleteFile(previousLogsFile)
MoveFile(logsFile, 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 while true
msg = wait(0, port) msg = wait(0, port)
msgType = type(msg) msgType = type(msg)
@@ -5,5 +5,6 @@
<field id="folder" type="string" value="cachefs:/logs" /> <field id="folder" type="string" value="cachefs:/logs" />
<field id="filename" type="string" value="app_logs.txt" /> <field id="filename" type="string" value="app_logs.txt" />
<field id="filenamePrevious" type="string" value="app_logs_previous.txt" /> <field id="filenamePrevious" type="string" value="app_logs_previous.txt" />
<field id="filenameStartup" type="string" value="cachefs:/playlet_startup_logs.txt" />
</interface> </interface>
</component> </component>