From f9bcf1ff78a059c9ff4beca50a97d9729cdcbe74 Mon Sep 17 00:00:00 2001 From: Brahim Hadriche Date: Fri, 31 May 2024 13:52:20 -0400 Subject: [PATCH] Memory warnings logs --- CHANGELOG.md | 6 +++ playlet-lib/src/components/Logger/Logger.bs | 47 +++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 24c7e040..f259847d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [Unreleased] + +### Added + +- Low memory warnings to the app logs for easier memory issue debugging + ## [0.23.7] - 2024-05-28 ### Added diff --git a/playlet-lib/src/components/Logger/Logger.bs b/playlet-lib/src/components/Logger/Logger.bs index e9c2d9ee..d44ed8b2 100644 --- a/playlet-lib/src/components/Logger/Logger.bs +++ b/playlet-lib/src/components/Logger/Logger.bs @@ -62,6 +62,17 @@ function LoggerLoop() buffer.AppendFile(logsFile) end if + appMemoryMonitor = CreateObject("roAppMemoryMonitor") + if appMemoryMonitor <> invalid and FindMemberFunction(appMemoryMonitor, "EnableMemoryWarningEvent") <> invalid + appMemoryMonitor.SetMessagePort(port) + appMemoryMonitor.EnableMemoryWarningEvent(true) + end if + + deviceInfo = CreateObject("roDeviceInfo") + deviceInfo.SetMessagePort(port) + deviceInfo.EnableLowGeneralMemoryEvent(true) + m.lastDeviceInfoMemoryLevel = "" + while true msg = wait(0, port) msgType = type(msg) @@ -72,6 +83,10 @@ function LoggerLoop() else if field = "systemLogEvent" OnLineSysLog(msg, dateTime, logsFile, buffer) end if + else if msgType = "roAppMemoryNotificationEvent" + OnLineLogMonitorMemoryWarning(msg, dateTime, logsFile, buffer) + else if msgType = "roDeviceInfoEvent" + OnLineLogDeviceInfoMemoryWarning(msg, dateTime, logsFile, buffer) end if end while end function @@ -98,6 +113,38 @@ function OnLineSysLog(event as object, dateTime as object, logsFile as string, b buffer.AppendFile(logsFile) end function +function OnLineLogMonitorMemoryWarning(event as object, dateTime as object, logsFile as string, buffer as object) as void + info = event.getInfo() + line = FormatTime(dateTime) + "[WARN][AppMonitorMemory] " + ToString(info) + + ' bs:disable-next-line LINT3012 + print line + + buffer.FromAsciiString(line + `\n`) + buffer.AppendFile(logsFile) +end function + +function OnLineLogDeviceInfoMemoryWarning(event as object, dateTime as object, logsFile as string, buffer as object) as void + info = event.getInfo() + if not IsString(info.generalMemoryLevel) + return + end if + + ' roDeviceInfoEvent can be a bit spammy, so only log if the memory level has changed + if info.generalMemoryLevel = m.lastDeviceInfoMemoryLevel + return + end if + m.lastDeviceInfoMemoryLevel = info.generalMemoryLevel + + line = FormatTime(dateTime) + "[WARN][DeviceInfoMemory] " + 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