db_logger/lib.rs
1// db_logger
2// Copyright 2022 Julio Merino
3//
4// Licensed under the Apache License, Version 2.0 (the "License"); you may not
5// use this file except in compliance with the License. You may obtain a copy
6// of the License at:
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13// License for the specific language governing permissions and limitations
14// under the License.
15
16//! A database-backed logger for use with the [`log` crate][log-crate-url].
17
18// Keep these in sync with other top-level files.
19#![allow(clippy::await_holding_refcell_ref)]
20#![warn(anonymous_parameters, bad_style, missing_docs)]
21#![warn(unused, unused_extern_crates, unused_import_braces, unused_qualifications)]
22#![warn(unsafe_code)]
23
24use std::sync::Arc;
25
26mod clocks;
27pub(crate) mod logger;
28use crate::logger::LogEntry;
29pub use logger::{init, Handle};
30#[cfg(test)]
31mod testutils;
32
33#[cfg(not(any(feature = "postgres", feature = "sqlite")))]
34compile_error!("one of the features ['postgres', 'sqlite'] must be enabled");
35#[cfg(feature = "postgres")]
36pub mod postgres;
37#[cfg(feature = "sqlite")]
38pub mod sqlite;
39
40/// Opaque type representing a connection to the logging database.
41#[derive(Clone)]
42pub struct Connection(Arc<dyn Db + Send + Sync + 'static>);
43
44impl Connection {
45 /// Initializes the database schema.
46 pub async fn create_schema(&self) -> Result<()> {
47 self.0.create_schema().await
48 }
49}
50
51/// Result type for this library.
52pub(crate) type Result<T> = std::result::Result<T, String>;
53
54/// Abstraction over the database connection.
55#[async_trait::async_trait]
56pub(crate) trait Db {
57 /// Initializes the database schema.
58 async fn create_schema(&self) -> Result<()>;
59
60 /// Returns the sorted list of all log entries in the database.
61 ///
62 /// Given that this is exposed for testing purposes only, this just returns a flat textual
63 /// representation of the log entry and does not try to deserialize it as a `LogEntry`. This
64 /// is for simplicity given that a `LogEntry` keeps references to static strings and we cannot
65 /// obtain those from the database.
66 async fn get_log_entries(&self) -> Result<Vec<String>>;
67
68 /// Appends a series of `entries` to the log.
69 ///
70 /// All entries are inserted at once into the database to avoid unnecessary round trips for each
71 /// log entry, which happen to be very expensive for something as frequent as log messages.
72 ///
73 /// This takes a `Vec` instead of a slice for efficiency, as the writes may have to truncate the
74 /// entries.
75 async fn put_log_entries(&self, entries: Vec<LogEntry<'_, '_>>) -> Result<()>;
76}
77
78/// Fits the string in `input` within the specified `max_len`.
79fn truncate_option_str(input: Option<&str>, max_len: usize) -> Option<String> {
80 match input {
81 Some(s) => {
82 let mut s = s.to_owned();
83 s.truncate(max_len);
84 Some(s)
85 }
86 None => None,
87 }
88}