Skip to content

Commit

Permalink
Memory warnings logs
Browse files Browse the repository at this point in the history
  • Loading branch information
iBicha committed May 31, 2024
1 parent a8a4915 commit f9bcf1f
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
47 changes: 47 additions & 0 deletions playlet-lib/src/components/Logger/Logger.bs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit f9bcf1f

Please sign in to comment.