-- Compiled with roblox-ts v2.3.0-dev-30dae68 local TS = _G[script] --[[ * * @license * Copyright 2024 Daymon Littrell-Reyes * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. ]] local Object = TS.import(script, TS.getModule(script, "@rbxts", "object-utils")) local _common = TS.import(script, script.Parent.Parent, "common") local LogLevel = _common.LogLevel local sink = _common.sink -- Context that had a warning sent through it, and will need to have their pending messages sent local flaggedContext = {} -- Messages to send only if the context is flagged local pendingMessages = {} -- Messages to send whenever the context is closed and the context is not flagged local promisedMessages = {} local function getMessages(correlation) local _correlation = correlation if flaggedContext[_correlation] ~= nil then local _correlation_1 = correlation local _condition = pendingMessages[_correlation_1] if _condition == nil then _condition = {} end return _condition else local _correlation_1 = correlation if promisedMessages[_correlation_1] ~= nil then local _correlation_2 = correlation local _condition = promisedMessages[_correlation_2] if _condition == nil then _condition = {} end return _condition end end return nil end --* @internal local LogContextManager = {} do local _container = LogContextManager --[[ * * Save a message to be sent later if the context is flagged. ]] local function save(message, context) local _correlation_id = context.correlation_id local _condition = pendingMessages[_correlation_id] if _condition == nil then _condition = {} end local messages = _condition local _exp = context.correlation_id local _array = {} local _length = #_array local _messagesLength = #messages table.move(messages, 1, _messagesLength, _length + 1, _array) _length += _messagesLength _array[_length + 1] = message pendingMessages[_exp] = _array if message.level >= LogLevel.WARNING then local _correlation_id_1 = context.correlation_id flaggedContext[_correlation_id_1] = true end end _container.save = save --[[ * * Save a message to be sent later, regardless if the context is flagged. ]] local function push(message, context) save(message, context) local _correlation_id = context.correlation_id local _condition = promisedMessages[_correlation_id] if _condition == nil then _condition = {} end local messages = _condition local _exp = context.correlation_id local _array = {} local _length = #_array local _messagesLength = #messages table.move(messages, 1, _messagesLength, _length + 1, _array) _length += _messagesLength _array[_length + 1] = message promisedMessages[_exp] = _array end _container.push = push --[[ * * Mark a context as "flagged", meaning a warning message was sent through it. * * So whenever the context is closed all pending messages should be sent. ]] local function flag(message) if message.context then local _correlation_id = message.context.correlation_id flaggedContext[_correlation_id] = true end end _container.flag = flag --[[ * * Send all the pending messages for a context. * * To be called when the context is stopped. ]] local function flush(context) local _context = context local id = if type(_context) == "string" then context else context.correlation_id local entries = getMessages(id) if entries then if #entries == 0 then warn("rLog Context Manager doesn't have any messages for a context. This shouldn't happen.", "\nCorrelation ID:", id) end for _, pending in entries do sink(pending, pending.config.sinks or {}) end end flaggedContext[id] = nil pendingMessages[id] = nil promisedMessages[id] = nil end _container.flush = flush local function clear() table.clear(flaggedContext) table.clear(pendingMessages) end _container.clear = clear --[[ * * Flag all current contexts, and flush them. * * Intended to be called before a game closes to avoid losing logs. ]] local function forceFlush() local contexts = Object.keys(pendingMessages) for _, context in contexts do flaggedContext[context] = true flush(context) end table.clear(pendingMessages) end _container.forceFlush = forceFlush end return { LogContextManager = LogContextManager, }