import "pkg:/source/utils/Logging.bs" import "pkg:/source/utils/MessagePortUtils.bs" import "pkg:/source/utils/StringUtils.bs" import "pkg:/source/utils/Types.bs" const FOLDER = "cachefs:/logs" const FILENAME = "app_logs.txt" const FILENAME_PREVIOUS = "app_logs_previous.txt" const FILENAME_STARTUP = "cachefs:/playlet_startup_logs.txt" function Init() m.top.functionName = "LoggerLoop" port = CreateObject("roMessagePort") m.top.ObserveField("logLine", port) scene = m.top.getScene() if scene.hasField("systemLogEvent") MessagePortUtils.ObserveFieldScopedExSafe(scene, "systemLogEvent", port) if scene.hasField("enableSystemLogType") scene.enableSystemLogType = "http.error" #if DEBUG scene.enableSystemLogType = "http.connect" scene.enableSystemLogType = "http.complete" scene.enableSystemLogType = "bandwidth.minute" #end if end if end if m.port = port #if DEBUG StartLogger(m.top, LogLevel.Debug) #else StartLogger(m.top, LogLevel.Info) #end if end function function StartLogger(logger as object, logLevel = LogLevel.Info as LogLevel) as void if m.global.logger <> invalid return end if logger.logLevel = logLevel m.global.addFields({ logger: logger }) logger.control = "run" end function function LoggerLoop() port = m.port buffer = CreateObject("roByteArray") logsFile = FOLDER + "/" + FILENAME previousLogsFile = FOLDER + "/" + FILENAME_PREVIOUS dateTime = CreateObject("roDateTime") CreateDirectory(FOLDER) DeleteFile(previousLogsFile) MoveFile(logsFile, previousLogsFile) ' Sometimes lastExitInfo.console_log (which would be contained in startUpLogs) ' would contain 'AppExitComplete', which is used to detect test runner exits. ' So we don't log it in test mode. #if TEST_MODE #else startUpLogs = ReadAsciiFile(FILENAME_STARTUP) if not StringUtils.IsNullOrEmpty(startUpLogs) line = `************* Startup Logs *************\n${startUpLogs}*********** End Startup Logs ***********\n` print line buffer.FromAsciiString(line) buffer.AppendFile(logsFile) end if #end if m.eventThrottleTimer = CreateObject("roTimespan") 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) if msgType = "roSGNodeEvent" field = msg.getField() if field = "logLine" OnLineLog(msg, dateTime, logsFile, buffer) 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 function OnLineLog(event as object, dateTime as object, logsFile as string, buffer as object) line = FormatTime(dateTime) + event.getData() print line buffer.FromAsciiString(line + `\n`) buffer.AppendFile(logsFile) end function function OnLineSysLog(event as object, dateTime as object, logsFile as string, buffer as object) as void info = event.getData() if info.LogType <> "http.error" return end if line = FormatTime(dateTime) + "[ERROR][SysLog] " + ToString(info) print line buffer.FromAsciiString(line + `\n`) buffer.AppendFile(logsFile) fingerprint = `SysLog${info.Status}${info.LogType}${info.HttpCode}${info.Method}` if ThrottleEvent("SysLogError", fingerprint, 60) return end if LogTelemetryEvent({ level: "error" message: line + `\nCurrentTime: ${m.eventThrottleTimer.TotalSeconds()}` fingerprint: [fingerprint] }) end function function OnLineLogMonitorMemoryWarning(event as object, dateTime as object, logsFile as string, buffer as object) as void info = event.getInfo() message = "[WARN][AppMonitorMemory] " + ToString(info) line = FormatTime(dateTime) + message print line buffer.FromAsciiString(line + `\n`) buffer.AppendFile(logsFile) if ThrottleEvent("AppMonitorMemoryWarning", message, 60) return end if LogTelemetryEvent({ level: "warning" message: line + `\nCurrentTime: ${m.eventThrottleTimer.TotalSeconds()}` fingerprint: [message] }) 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 if ThrottleEvent("DeviceInfoMemoryWarning", info.generalMemoryLevel, 60) return end if message = "[WARN][DeviceInfoMemory] " + ToString(info) line = FormatTime(dateTime) + message print line buffer.FromAsciiString(line + `\n`) buffer.AppendFile(logsFile) LogTelemetryEvent({ level: "warning" message: line + `\nCurrentTime: ${m.eventThrottleTimer.TotalSeconds()}` fingerprint: [message] }) end function ' Returns true if the event should be throttled function ThrottleEvent(key as string, fingerprint as string, throttleSeconds as integer) as boolean lastSentTime = ValidInt(m[key]) currentTime = m.eventThrottleTimer.TotalSeconds() if ((currentTime - lastSentTime) < throttleSeconds) and (m[key + "Fingerprint"] = fingerprint) return true end if m[key] = currentTime m[key + "Fingerprint"] = fingerprint return false 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 dateTime.Mark() dateTime.ToLocalTime() hours = dateTime.GetHours().ToStr("%02d") minutes = dateTime.GetMinutes().ToStr("%02d") seconds = dateTime.GetSeconds().ToStr("%02d") milliseconds = dateTime.GetMilliseconds().ToStr("%03d") return "[" + hours + ":" + minutes + ":" + seconds + "." + milliseconds + "]" end function