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
+128 -85

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
+40 -35
View File
@@ -1,40 +1,45 @@
function ShowErrorDialog(message as object, bulletText as object, bottomMessage as object) namespace Dialog
' 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"]
' bs:disable-next-line NO_OBSERVE_STRING_CALLBACK function ShowError(message as object, bulletText as object, bottomMessage as object)
dialog.observeField("buttonSelected", "OnButtonSelected") ' Note: dialog can be null since StandardMessageDialog is only available on 10.0 and later
' bs:disable-next-line NO_OBSERVE_STRING_CALLBACK msgDialog = CreateObject("roSGNode", "StandardMessageDialog")
dialog.observeField("wasClosed", "OnDialogClosed") SetPalette(msgDialog)
msgDialog.message = message
msgDialog.bulletText = bulletText
msgDialog.bottomMessage = bottomMessage
msgDialog.title = "Error loading Playlet"
msgDialog.buttons = ["Exit"]
m.top.getScene().dialog = dialog ' bs:disable-next-line NO_OBSERVE_STRING_CALLBACK
end function msgDialog.observeField("buttonSelected", "OnButtonSelected")
' bs:disable-next-line NO_OBSERVE_STRING_CALLBACK
msgDialog.observeField("wasClosed", "OnDialogClosed")
function SetPalette(dialog as object) m.top.getScene().dialog = msgDialog
palette = createObject("roSGNode", "RSGPalette") end function
palette.colors = {
DialogBackgroundColor: "#242424FF"
DialogFocusColor: "#CECECEFF"
DialogFocusItemColor: "#202020FF"
DialogSecondaryTextColor: "#DBDBDBFF"
DialogSecondaryItemColor: "#FF1C30FF"
DialogTextColor: "#DBDBDBFF"
}
dialog.palette = palette
end function
function OnButtonSelected() function SetPalette(msgDialog as object)
m.top.getScene().dialog.close = true palette = createObject("roSGNode", "RSGPalette")
end function 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
+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()
+9 -4
View File
@@ -1,4 +1,9 @@
function ReadManifestValue(key as string) as string namespace Manifest
appInfo = CreateObject("roAppInfo")
return appInfo.GetValue(key) function ReadValue(key as string) as string
end function appInfo = CreateObject("roAppInfo")
return appInfo.GetValue(key)
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
+31 -26
View File
@@ -1,29 +1,34 @@
function ReadRegistryKey(key as string, section as string) as dynamic namespace Registry
sec = CreateObject("roRegistrySection", section)
if not sec.Exists(key)
return invalid
end if
return sec.Read(key)
end function
function DeleteRegistryKey(key as string, section as string) function ReadKey(key as string, section as string) as dynamic
sec = CreateObject("roRegistrySection", section) sec = CreateObject("roRegistrySection", section)
sec.Delete(key) if not sec.Exists(key)
sec.Flush() return invalid
end function end if
return sec.Read(key)
end function
function GetPlayletLibUrlsFromRegistry() as object function DeleteKey(key as string, section as string)
urlsJson = ReadRegistryKey("playlet_lib_urls", "Playlet") sec = CreateObject("roRegistrySection", section)
if urlsJson = invalid sec.Delete(key)
return invalid sec.Flush()
end if end function
urls = ParseJson(urlsJson)
return urls
end function
function ClearPlayletLibUrlsIfNeeded(launchArgs as object) as void function GetPlayletLibUrlsFromRegistry() as object
if launchArgs = invalid or launchArgs.clearPlayletLibUrls = invalid urlsJson = ReadKey("playlet_lib_urls", "Playlet")
return if urlsJson = invalid
end if return invalid
DeleteRegistryKey("playlet_lib_urls", "Playlet") end if
end function 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
+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>