Skip to content

Commit

Permalink
AuditLogger no longer requires mutable access to self
Browse files Browse the repository at this point in the history
This because we decided to use the logger from different threads.
  • Loading branch information
Lut99 committed Nov 5, 2024
1 parent acc8cd9 commit 760a8d1
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 43 deletions.
19 changes: 10 additions & 9 deletions lib/loggers/file/src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Created:
// 10 Oct 2024, 14:16:24
// Last edited:
// 17 Oct 2024, 13:17:01
// 05 Nov 2024, 10:43:25
// Auto updated?
// Yes
//
Expand Down Expand Up @@ -124,7 +124,7 @@ pub struct FileLogger {
path: PathBuf,
/// Whether the user has already printed the context or not.
#[cfg(debug_assertions)]
logged_context: bool,
logged_context: std::sync::Arc<std::sync::atomic::AtomicBool>,
}
impl FileLogger {
/// Constructor for the FileLogger that initializes it pointing to the given file.
Expand All @@ -141,7 +141,7 @@ impl FileLogger {
id: id.into(),
path: path.into(),
#[cfg(debug_assertions)]
logged_context: false,
logged_context: std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)),
}
}

Expand Down Expand Up @@ -202,7 +202,7 @@ impl AuditLogger for FileLogger {
type Error = Error;

#[inline]
fn log_context<'a, C>(&'a mut self, context: &'a C) -> impl 'a + Future<Output = Result<(), Self::Error>>
fn log_context<'a, C>(&'a self, context: &'a C) -> impl 'a + Future<Output = Result<(), Self::Error>>
where
C: ?Sized + Context,
{
Expand All @@ -215,14 +215,15 @@ impl AuditLogger for FileLogger {

// Log it
self.log(LogStatement::Context { context }).await?;
self.logged_context = true;
#[cfg(debug_assertions)]
self.logged_context.store(true, std::sync::atomic::Ordering::Relaxed);
Ok(())
}
}

#[inline]
fn log_response<'a, R>(
&'a mut self,
&'a self,
reference: &'a str,
response: &'a ReasonerResponse<R>,
raw: Option<&'a str>,
Expand All @@ -232,7 +233,7 @@ impl AuditLogger for FileLogger {
{
async move {
#[cfg(debug_assertions)]
if !self.logged_context {
if !self.logged_context.load(std::sync::atomic::Ordering::Relaxed) {
tracing::warn!("Logging reasoner response without having logged the reasoner context; please call FileLogger::log_context() first.");
}

Expand All @@ -251,14 +252,14 @@ impl AuditLogger for FileLogger {
}

#[inline]
fn log_question<'a, S, Q>(&'a mut self, reference: &'a str, state: &'a S, question: &'a Q) -> impl 'a + Future<Output = Result<(), Self::Error>>
fn log_question<'a, S, Q>(&'a self, reference: &'a str, state: &'a S, question: &'a Q) -> impl 'a + Future<Output = Result<(), Self::Error>>
where
S: Serialize,
Q: Serialize,
{
async move {
#[cfg(debug_assertions)]
if !self.logged_context {
if !self.logged_context.load(std::sync::atomic::Ordering::Relaxed) {
tracing::warn!("Logging reasoner response without having logged the reasoner context; please call FileLogger::log_context() first.");
}

Expand Down
13 changes: 4 additions & 9 deletions lib/loggers/no-op/src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Created:
// 10 Oct 2024, 14:46:33
// Last edited:
// 17 Oct 2024, 13:14:34
// 05 Nov 2024, 10:23:27
// Auto updated?
// Yes
//
Expand Down Expand Up @@ -41,7 +41,7 @@ impl AuditLogger for MockLogger {
type Error = Infallible;

#[inline]
fn log_context<'a, C>(&'a mut self, _context: &'a C) -> impl 'a + Future<Output = Result<(), Self::Error>>
fn log_context<'a, C>(&'a self, _context: &'a C) -> impl 'a + Future<Output = Result<(), Self::Error>>
where
C: ?Sized + Context,
{
Expand All @@ -53,7 +53,7 @@ impl AuditLogger for MockLogger {

#[inline]
fn log_response<'a, R>(
&'a mut self,
&'a self,
_reference: &'a str,
_response: &'a ReasonerResponse<R>,
_raw: Option<&'a str>,
Expand All @@ -68,12 +68,7 @@ impl AuditLogger for MockLogger {
}

#[inline]
fn log_question<'a, S, Q>(
&'a mut self,
_reference: &'a str,
_state: &'a S,
_question: &'a Q,
) -> impl 'a + Future<Output = Result<(), Self::Error>>
fn log_question<'a, S, Q>(&'a self, _reference: &'a str, _state: &'a S, _question: &'a Q) -> impl 'a + Future<Output = Result<(), Self::Error>>
where
S: Serialize,
Q: Serialize,
Expand Down
4 changes: 2 additions & 2 deletions lib/reasoners/eflint-json/src/reasonerconn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Created:
// 09 Oct 2024, 15:52:06
// Last edited:
// 17 Oct 2024, 13:59:28
// 05 Nov 2024, 10:44:14
// Auto updated?
// Yes
//
Expand Down Expand Up @@ -216,7 +216,7 @@ where
&self,
state: Self::State,
question: Self::Question,
logger: &mut SessionedAuditLogger<L>,
logger: &SessionedAuditLogger<L>,
) -> impl Future<Output = Result<ReasonerResponse<Self::Reason>, Self::Error>>
where
L: AuditLogger,
Expand Down
4 changes: 2 additions & 2 deletions lib/reasoners/no-op/src/reasonerconn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Created:
// 10 Oct 2024, 16:21:09
// Last edited:
// 17 Oct 2024, 13:56:10
// 05 Nov 2024, 10:43:39
// Auto updated?
// Yes
//
Expand Down Expand Up @@ -80,7 +80,7 @@ where
&self,
state: Self::State,
question: Self::Question,
logger: &mut SessionedAuditLogger<L>,
logger: &SessionedAuditLogger<L>,
) -> impl Future<Output = Result<ReasonerResponse<Self::Reason>, Self::Error>>
where
L: AuditLogger,
Expand Down
4 changes: 2 additions & 2 deletions lib/reasoners/posix/src/reasonerconn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Created:
// 11 Oct 2024, 16:54:51
// Last edited:
// 18 Oct 2024, 11:27:13
// 05 Nov 2024, 10:44:07
// Auto updated?
// Yes
//
Expand Down Expand Up @@ -256,7 +256,7 @@ impl ReasonerConnector for PosixReasonerConnector {
&self,
state: Self::State,
_question: Self::Question,
logger: &mut SessionedAuditLogger<L>,
logger: &SessionedAuditLogger<L>,
) -> impl Future<Output = Result<ReasonerResponse<Self::Reason>, Self::Error>>
where
L: AuditLogger,
Expand Down
69 changes: 52 additions & 17 deletions lib/spec/src/auditlogger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Created:
// 09 Oct 2024, 13:38:41
// Last edited:
// 04 Nov 2024, 16:35:48
// 05 Nov 2024, 10:23:16
// Auto updated?
// Yes
//
Expand Down Expand Up @@ -57,57 +57,57 @@ impl<L: AuditLogger> SessionedAuditLogger<L> {
/// - `response`: The [`ReasonerResponse`] that we're logging.
/// - `raw`: The raw response produced by the reasoner, if applicable.
pub fn log_response<'a, R>(
&'a mut self,
&'a self,
response: &'a ReasonerResponse<R>,
raw: Option<&'a str>,
) -> impl 'a + Future<Output = Result<(), <Self as AuditLogger>::Error>>
where
R: Display,
{
L::log_response(&mut self.logger, &self.reference, response, raw)
L::log_response(&self.logger, &self.reference, response, raw)
}

/// Logs that the reasoner is being asked a question.
///
/// # Arguments
/// - `state`: Some serializable state given as input to the reasoner.
/// - `question`: Some serializable question that we're asking.
pub fn log_question<'a, S, Q>(&'a mut self, state: &'a S, question: &'a Q) -> impl 'a + Future<Output = Result<(), <Self as AuditLogger>::Error>>
pub fn log_question<'a, S, Q>(&'a self, state: &'a S, question: &'a Q) -> impl 'a + Future<Output = Result<(), <Self as AuditLogger>::Error>>
where
S: Serialize,
Q: Serialize,
{
L::log_question(&mut self.logger, &self.reference, state, question)
L::log_question(&self.logger, &self.reference, state, question)
}
}
impl<L: AuditLogger> AuditLogger for SessionedAuditLogger<L> {
type Error = L::Error;

fn log_context<'a, C>(&'a mut self, context: &'a C) -> impl 'a + Future<Output = Result<(), Self::Error>>
fn log_context<'a, C>(&'a self, context: &'a C) -> impl 'a + Future<Output = Result<(), Self::Error>>
where
C: ?Sized + Context,
{
L::log_context(&mut self.logger, context)
L::log_context(&self.logger, context)
}

fn log_response<'a, R>(
&'a mut self,
&'a self,
reference: &'a str,
response: &'a ReasonerResponse<R>,
raw: Option<&'a str>,
) -> impl 'a + Future<Output = Result<(), Self::Error>>
where
R: Display,
{
L::log_response(&mut self.logger, reference, response, raw)
L::log_response(&self.logger, reference, response, raw)
}

fn log_question<'a, S, Q>(&'a mut self, reference: &'a str, state: &'a S, question: &'a Q) -> impl 'a + Future<Output = Result<(), Self::Error>>
fn log_question<'a, S, Q>(&'a self, reference: &'a str, state: &'a S, question: &'a Q) -> impl 'a + Future<Output = Result<(), Self::Error>>
where
S: Serialize,
Q: Serialize,
{
L::log_question(&mut self.logger, reference, state, question)
L::log_question(&self.logger, reference, state, question)
}
}

Expand All @@ -117,6 +117,8 @@ impl<L: AuditLogger> AuditLogger for SessionedAuditLogger<L> {

/***** LIBRARY *****/
/// Defines a generic interface to write to an audit trail.
///
/// Note that this logger may be used across threads. As such, any mutability must be inferior.
pub trait AuditLogger {
/// Defines the errors returned by this logger.
type Error: Error;
Expand All @@ -126,7 +128,7 @@ pub trait AuditLogger {
///
/// # Arguments
/// - `context`: Something [`Serialize`]able that we want to write at startup.
fn log_context<'a, C>(&'a mut self, context: &'a C) -> impl 'a + Future<Output = Result<(), Self::Error>>
fn log_context<'a, C>(&'a self, context: &'a C) -> impl 'a + Future<Output = Result<(), Self::Error>>
where
C: ?Sized + Context;

Expand All @@ -137,7 +139,7 @@ pub trait AuditLogger {
/// - `response`: The [`ReasonerResponse`] that we're logging.
/// - `raw`: The raw response produced by the reasoner, if applicable.
fn log_response<'a, R>(
&'a mut self,
&'a self,
reference: &'a str,
response: &'a ReasonerResponse<R>,
raw: Option<&'a str>,
Expand All @@ -151,18 +153,51 @@ pub trait AuditLogger {
/// - `reference`: Some reference that links the response to a particular answer.
/// - `state`: Some serializable state given as input to the reasoner.
/// - `question`: Some serializable question that we're asking.
fn log_question<'a, S, Q>(&'a mut self, reference: &'a str, state: &'a S, question: &'a Q) -> impl 'a + Future<Output = Result<(), Self::Error>>
fn log_question<'a, S, Q>(&'a self, reference: &'a str, state: &'a S, question: &'a Q) -> impl 'a + Future<Output = Result<(), Self::Error>>
where
S: Serialize,
Q: Serialize;
}

// Standard impls
impl<'a, T: AuditLogger> AuditLogger for &'a T {
type Error = T::Error;

#[inline]
fn log_context<'s, C>(&'s self, context: &'s C) -> impl 's + Future<Output = Result<(), Self::Error>>
where
C: ?Sized + Context,
{
<T as AuditLogger>::log_context(self, context)
}

#[inline]
fn log_response<'s, R>(
&'s self,
reference: &'s str,
response: &'s ReasonerResponse<R>,
raw: Option<&'s str>,
) -> impl 's + Future<Output = Result<(), Self::Error>>
where
R: Display,
{
<T as AuditLogger>::log_response(self, reference, response, raw)
}

#[inline]
fn log_question<'s, S, Q>(&'s self, reference: &'s str, state: &'s S, question: &'s Q) -> impl 's + Future<Output = Result<(), Self::Error>>
where
S: Serialize,
Q: Serialize,
{
<T as AuditLogger>::log_question(self, reference, state, question)
}
}
impl<'a, T: AuditLogger> AuditLogger for &'a mut T {
type Error = T::Error;

#[inline]
fn log_context<'s, C>(&'s mut self, context: &'s C) -> impl 's + Future<Output = Result<(), Self::Error>>
fn log_context<'s, C>(&'s self, context: &'s C) -> impl 's + Future<Output = Result<(), Self::Error>>
where
C: ?Sized + Context,
{
Expand All @@ -171,7 +206,7 @@ impl<'a, T: AuditLogger> AuditLogger for &'a mut T {

#[inline]
fn log_response<'s, R>(
&'s mut self,
&'s self,
reference: &'s str,
response: &'s ReasonerResponse<R>,
raw: Option<&'s str>,
Expand All @@ -183,7 +218,7 @@ impl<'a, T: AuditLogger> AuditLogger for &'a mut T {
}

#[inline]
fn log_question<'s, S, Q>(&'s mut self, reference: &'s str, state: &'s S, question: &'s Q) -> impl 's + Future<Output = Result<(), Self::Error>>
fn log_question<'s, S, Q>(&'s self, reference: &'s str, state: &'s S, question: &'s Q) -> impl 's + Future<Output = Result<(), Self::Error>>
where
S: Serialize,
Q: Serialize,
Expand Down
4 changes: 2 additions & 2 deletions lib/spec/src/reasonerconn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Created:
// 09 Oct 2024, 13:35:41
// Last edited:
// 17 Oct 2024, 09:50:17
// 05 Nov 2024, 10:43:44
// Auto updated?
// Yes
//
Expand Down Expand Up @@ -66,7 +66,7 @@ pub trait ReasonerConnector {
&self,
state: Self::State,
question: Self::Question,
logger: &mut SessionedAuditLogger<L>,
logger: &SessionedAuditLogger<L>,
) -> impl Future<Output = Result<ReasonerResponse<Self::Reason>, Self::Error>>
where
L: AuditLogger;
Expand Down

0 comments on commit 760a8d1

Please sign in to comment.