Struct cwe_checker_lib::utils::log::LogThread
source · pub struct LogThread { /* private fields */ }
Expand description
A type for managing threads for collecting log messages.
With LogThread::spawn()
one can create a new log thread
whose handle is contained in the returned LogThread
struct.
By calling the collect()
method
one can tell the log thread to shut down
and return the logs collected to this point.
If the LogThread
object gets dropped before calling collect()
,
the corresponding logging thread will be stopped
and all collected logs will be discarded.
If one deliberately wants to discard all logging messages,
one can simply create a sender to a disconnected channel
via LogThread::create_disconnected_sender()
.
Implementations§
source§impl LogThread
impl LogThread
sourcepub fn spawn<F>(collector_func: F) -> LogThread
pub fn spawn<F>(collector_func: F) -> LogThread
Create a new LogThread
object with a handle to a freshly spawned logging collector thread.
The parameter is the function containing the actual log collection logic.
I.e. the function should receive messages through the given receiver until the channel disconnects
or until it receives a LogThreadMsg::Terminate
message.
After that it should return the logs collected up to that point.
See LogThread::collect_and_deduplicate
for a standard collector function that can be used here.
sourcepub fn create_disconnected_sender() -> Sender<LogThreadMsg>
pub fn create_disconnected_sender() -> Sender<LogThreadMsg>
Just create a disconnected sender to a (non-existing) logging thread. Can be used like a sender to a channel that deliberately discards all messages sent to it.
sourcepub fn get_msg_sender(&self) -> Sender<LogThreadMsg>
pub fn get_msg_sender(&self) -> Sender<LogThreadMsg>
Get a sender that can be used to send messages to the logging thread corresponding to this LogThread
instance.
sourcepub fn collect(self) -> (Vec<LogMessage>, Vec<CweWarning>)
pub fn collect(self) -> (Vec<LogMessage>, Vec<CweWarning>)
Stop the logging thread by sending it the Terminate
signal
and then return all logs collected until that point.
sourcepub fn collect_and_deduplicate(
receiver: Receiver<LogThreadMsg>
) -> (Vec<LogMessage>, Vec<CweWarning>)
pub fn collect_and_deduplicate( receiver: Receiver<LogThreadMsg> ) -> (Vec<LogMessage>, Vec<CweWarning>)
This function is collects logs from the given receiver until a LogThreadMsg::Terminate
signal is received.
All collected logs are deduplicated before being returned.
CWE warnings and log messages are deduplicated if two messages share the same address of origin. In such a case only the last message received is kept. If a CWE message has more than one address only the first address is considered when deduplicating. Note that this may lead to information loss if log messages with the same origin address that are not duplicates are generated.
This function can be used as a standard collector function for LogThread::spawn
.