1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
use ::safer_ffi::{closure::boxed::*, prelude::*};
use ::std::path::Path;
use ::tokio::sync::oneshot;
#[cfg(doc)]
use crate::error::{CoreApiErrorKind, ErrorKind};
use crate::{error::Result, prelude::*, utils::extension_traits::FfiResultIntoRustResult};
/// Type with free associated functions ("static methods") to customize the logging behavior from
/// Ditto and log messages with the Ditto logging infrastructure.
pub struct DittoLogger(::never_say_never::Never);
impl DittoLogger {
/// Enable or disable logging.
///
/// Logs exported through [`export_to_file`] are not affected by this
/// setting and will also include logs emitted while `enabled` is false.
pub fn set_logging_enabled(enabled: bool) {
ffi_sdk::ditto_logger_enabled(enabled)
}
/// Return true if logging is enabled.
///
/// Logs exported through [`export_to_file`] are not affected by this
/// setting and will also include logs emitted while `enabled` is false.
pub fn get_logging_enabled() -> bool {
ffi_sdk::ditto_logger_enabled_get()
}
/// Represent whether or not emojis should be used as the log level indicator in the logs.
pub fn get_emoji_log_level_headings_enabled() -> bool {
ffi_sdk::ditto_logger_emoji_headings_enabled_get()
}
/// Set whether or not emojis should be used as the log level indicator in the logs.
pub fn set_emoji_log_level_headings_enabled(enabled: bool) {
ffi_sdk::ditto_logger_emoji_headings_enabled(enabled);
}
/// Get the current minimum log level.
///
/// Logs exported through [`export_to_file`] are not affected by this
/// setting and include all logs at [`LogLevel::Warning`] and above.
pub fn get_minimum_log_level() -> LogLevel {
ffi_sdk::ditto_logger_minimum_log_level_get()
}
/// Set the current minimum log level.
///
/// Logs exported through [`export_to_file`] are not affected by this
/// setting and include all logs at [`LogLevel::Warning`] and above.
pub fn set_minimum_log_level(log_level: LogLevel) {
ffi_sdk::ditto_logger_minimum_log_level(log_level);
}
}
impl DittoLogger {
/// Not part of the public API.
#[doc(hidden)]
pub fn __log_error(msg: impl Into<String>) {
::ffi_sdk::ditto_log(CLogLevel::Error, char_p::new(msg.into()).as_ref())
}
}
/// [`DittoLogger::export_to_file()`] API.
impl DittoLogger {
fn rx_export_to_file(file_path: &Path) -> oneshot::Receiver<Result<u64>> {
let (tx, rx) = oneshot::channel();
let mut tx = Some(tx);
ffi_sdk::dittoffi_logger_try_export_to_file_async(
char_p::new(file_path.to_str().expect("path to be UTF-8")).as_ref(),
BoxDynFnMut1::new(Box::new(move |ffi_result: ::ffi_sdk::FfiResult<u64>| {
tx.take()
.expect("completion callback to be called exactly once")
.send(ffi_result.into_rust_result().map_err(DittoError::from))
.ok();
})),
);
rx
}
/// Exports collected logs to a compressed and JSON-encoded file on the
/// local file system.
///
/// `DittoLogger` locally collects a limited amount of logs at the `LogLevel::Debug`
/// level and above, periodically discarding old logs. The internal logger is
/// always enabled and workds independently of the `enabled` setting and the
/// configured `minimum_log_level`. Its logs can be requested and downloaded
/// from any peer that is active in a Ditto app using the portal's device
/// dashboard. This method provides an alternative way of accessing those
/// logs by exporting them to the local filesystem.
///
/// The logs will be written as a gzip compressed file at the URL specified
/// by the `file_path` parameter. When uncompressed, the file contains one
/// JSON value per line with the oldest entry on the first line (JSON lines
/// format).
///
/// Ditto limits the amount of logs it retains on disk to 15 MB and a
/// maximum age of three days. Older logs are periodically discarded once
/// one of these limits is reached.
///
/// This method currently only exports logs from the most recently created
/// Ditto instance, even when multiple instances are running in the same
/// process.
///
/// This method currently only exports logs from the most recently created
/// Ditto instance when multiple instances are running in the same process.
///
/// - **Parameter** `file_path`: the path of the file to write the logs to. The file must not
/// already exist, and the containing directory must exist. **It is recommended for the path
/// to have the `.jsonl.gz` file extension** but Ditto won't enforce it, nor correct it.
///
/// - **Errors**: it can run into I/O errors when the file cannot be written to disk. Prevent
/// this by ensuring that no file exists at the provided path, all parent directories exist,
/// sufficient permissions are granted, and that the disk is not full.
///
/// More precisely, this "throws" a [`DittoError`] whose [`.kind()`][DittoError::kind()] is
/// that of an [`ErrorKind::CoreApi`], such as:
///
/// - [`CoreApiErrorKind::IoNotFound`]
/// - [`CoreApiErrorKind::IoPermissionDenied`]
/// - [`CoreApiErrorKind::IoAlreadyExists`]
/// - [`CoreApiErrorKind::IoOperationFailed`]
///
/// - **Returns**: the number of bytes written to disk.
///
/// # Example
///
/// ```rust ,no_run
/// use ::dittolive_ditto::{
/// error::{CoreApiErrorKind, ErrorKind},
/// prelude::*,
/// };
/// # use eprintln as error;
///
/// # _ = async {
/// let ditto: Ditto = /* construct at least one ditto instance first */
/// # todo!();
/// match DittoLogger::export_to_file("/some/path.jsonl.gz").await {
/// Ok(_bytes_written) => { /* … */ },
/// Err(err) => match err.kind() {
/// ErrorKind::CoreApi(CoreApiErrorKind::IoNotFound) => { /* … */ },
/// ErrorKind::CoreApi(CoreApiErrorKind::IoPermissionDenied) => { /* … */ },
/// ErrorKind::CoreApi(CoreApiErrorKind::IoAlreadyExists) => { /* … */ },
/// ErrorKind::CoreApi(CoreApiErrorKind::IoOperationFailed) => { /* … */ },
/// _ => error!("{err}"),
/// },
/// }
/// # };
/// ```
pub async fn export_to_file(file_path: &(impl ?Sized + AsRef<Path>)) -> Result<u64> {
Self::rx_export_to_file(file_path.as_ref())
.await
.expect("channel to be used by the FFI")
}
#[cfg(any(test, doctest))] // NOT exported yet until we settle on whether to Townhousify this API.
/// Convenience function around [`Self::export_to_file()`], to be used when outside
/// of `async`hronous contexts, by falling back to a blocking call.
///
/// ### Panics
///
/// This function may panic if called from within an asynchronous context.
pub fn blocking_export_to_file(file_path: &(impl ?Sized + AsRef<Path>)) -> Result<u64> {
Self::rx_export_to_file(file_path.as_ref())
.blocking_recv()
.expect("channel to be used by the FFI")
}
}
#[cfg(any(test, doctest))]
#[path = "logger_tests.rs"]
mod tests;