use super::active;
pub const CHANNEL_OPENING: &str = "channel-opening";
pub const CHANNEL_OPENED: &str = "channel-opened";
pub const CHANNEL_OPEN_FAILED: &str = "channel-open-failed";
pub const CHANNEL_CLOSING: &str = "channel-closing";
pub const RESPONSE_VOLUME: &str = "response-volume";
pub const ATTEMPT_RETRYING: &str = "attempt-retrying";
pub const ATTEMPT_PERMANENT: &str = "attempt-permanent";
pub const ATTEMPT_BUDGET_SPENT: &str = "attempt-budget-spent";
pub const BUDGET_EXCEEDED: &str = "budget-exceeded";
pub const BUDGET_ABORT_REQUESTED: &str = "budget-abort-requested";
pub const BUDGET_ABORT_CONFIRMED: &str = "budget-abort-confirmed";
pub const BUDGET_ABORT_UNCONFIRMED: &str = "budget-abort-unconfirmed";
pub const COPY_STARTED: &str = "copy-started";
pub const COPY_TABLE: &str = "copy-table";
pub const COPY_SNAPSHOT_UNAVAILABLE: &str = "copy-snapshot-unavailable";
pub const COPY_FINISHED: &str = "copy-finished";
pub const LOAD_BATCH_COMMITTED: &str = "load-batch-committed";
pub const LOAD_ROW_REJECTED: &str = "load-row-rejected";
pub const LOAD_FINISHED: &str = "load-finished";
pub const STATEMENT_SENDING: &str = "statement-sending";
pub const SAFETY_NO_UNIQUE_KEY: &str = "safety-no-unique-key";
pub const EPHEMERAL_STATE: &str = "ephemeral-state";
pub const TABLE_NO_ROWS: &str = "table-no-rows";
pub const ENVELOPE_GAP: &str = "envelope-gap";
pub const TABLE_ABSENT: &str = "table-absent";
pub const MANUAL_SECTION: &str = "manual-section";
pub const PERMISSIONS_UNAVAILABLE_PROFILES: &str = "permissions-unavailable-profiles";
pub const PERMISSIONS_UNAVAILABLE_SETTINGS: &str = "permissions-unavailable-settings";
pub const ALL: [&str; 28] = [
CHANNEL_OPENING,
CHANNEL_OPENED,
CHANNEL_OPEN_FAILED,
CHANNEL_CLOSING,
RESPONSE_VOLUME,
ATTEMPT_RETRYING,
ATTEMPT_PERMANENT,
ATTEMPT_BUDGET_SPENT,
BUDGET_EXCEEDED,
BUDGET_ABORT_REQUESTED,
BUDGET_ABORT_CONFIRMED,
BUDGET_ABORT_UNCONFIRMED,
COPY_STARTED,
COPY_TABLE,
COPY_SNAPSHOT_UNAVAILABLE,
COPY_FINISHED,
LOAD_BATCH_COMMITTED,
LOAD_ROW_REJECTED,
LOAD_FINISHED,
STATEMENT_SENDING,
SAFETY_NO_UNIQUE_KEY,
EPHEMERAL_STATE,
TABLE_NO_ROWS,
ENVELOPE_GAP,
TABLE_ABSENT,
MANUAL_SECTION,
PERMISSIONS_UNAVAILABLE_PROFILES,
PERMISSIONS_UNAVAILABLE_SETTINGS,
];
#[must_use]
pub fn prose(id: &str, arguments: &[(&str, &str)]) -> String {
active().format(id, arguments)
}
fn say(id: &str, arguments: &[(&str, &str)]) {
tracing::info!(message_id = id, "{}", prose(id, arguments));
}
fn warn(id: &str, arguments: &[(&str, &str)]) {
tracing::warn!(message_id = id, "{}", prose(id, arguments));
}
fn detail(id: &str, arguments: &[(&str, &str)]) {
tracing::debug!(message_id = id, "{}", prose(id, arguments));
}
pub fn permissions_unavailable_profiles(file: &str) {
tracing::warn!(
message_id = PERMISSIONS_UNAVAILABLE_PROFILES,
file,
"{}",
prose(PERMISSIONS_UNAVAILABLE_PROFILES, &[])
);
}
pub fn permissions_unavailable_settings(file: &str) {
tracing::warn!(
message_id = PERMISSIONS_UNAVAILABLE_SETTINGS,
file,
"{}",
prose(PERMISSIONS_UNAVAILABLE_SETTINGS, &[])
);
}
pub fn channel_opening(destination: &str) {
say(CHANNEL_OPENING, &[("destination", destination)]);
}
pub fn channel_opened(version: &str, millis: u128) {
tracing::info!(
message_id = CHANNEL_OPENED,
open_millis = millis,
"{}",
prose(
CHANNEL_OPENED,
&[("version", version), ("millis", &millis.to_string())]
)
);
}
pub fn channel_open_failed(millis: u128) {
tracing::warn!(
message_id = CHANNEL_OPEN_FAILED,
open_millis = millis,
"{}",
prose(CHANNEL_OPEN_FAILED, &[("millis", &millis.to_string())])
);
}
pub fn channel_closing(millis: u128) {
tracing::info!(
message_id = CHANNEL_CLOSING,
command_millis = millis,
"{}",
prose(CHANNEL_CLOSING, &[("millis", &millis.to_string())])
);
}
pub fn response_volume(bytes: u64, lines: u64) {
tracing::info!(
message_id = RESPONSE_VOLUME,
output_bytes = bytes,
output_lines = lines,
"{}",
prose(
RESPONSE_VOLUME,
&[("bytes", &bytes.to_string()), ("lines", &lines.to_string())]
)
);
}
pub fn attempt_retrying(attempt: u32, attempts: u32, wait_millis: u128) {
warn(
ATTEMPT_RETRYING,
&[
("attempt", &attempt.to_string()),
("attempts", &attempts.to_string()),
("wait", &wait_millis.to_string()),
],
);
}
pub fn attempt_permanent() {
detail(ATTEMPT_PERMANENT, &[]);
}
pub fn attempt_budget_spent(spent_millis: u128, budget_millis: u128) {
warn(
ATTEMPT_BUDGET_SPENT,
&[
("spent", &spent_millis.to_string()),
("budget", &budget_millis.to_string()),
],
);
}
pub fn budget_exceeded(phase: &str, seconds: u64) {
warn(
BUDGET_EXCEEDED,
&[("phase", phase), ("seconds", &seconds.to_string())],
);
}
pub fn budget_abort_requested() {
warn(BUDGET_ABORT_REQUESTED, &[]);
}
pub fn budget_abort_confirmed() {
say(BUDGET_ABORT_CONFIRMED, &[]);
}
pub fn budget_abort_unconfirmed() {
warn(BUDGET_ABORT_UNCONFIRMED, &[]);
}
pub fn copy_started(tables: usize, path: &str) {
say(
COPY_STARTED,
&[("tables", &tables.to_string()), ("path", path)],
);
}
pub fn copy_table(table: &str) {
say(COPY_TABLE, &[("table", table)]);
}
pub fn copy_snapshot_unavailable(table: &str) {
warn(COPY_SNAPSHOT_UNAVAILABLE, &[("table", table)]);
}
pub fn copy_finished(rows: u64, bytes: u64) {
say(
COPY_FINISHED,
&[("rows", &rows.to_string()), ("bytes", &bytes.to_string())],
);
}
pub fn load_batch_committed(written: u64, offset: u64) {
say(
LOAD_BATCH_COMMITTED,
&[
("written", &written.to_string()),
("offset", &offset.to_string()),
],
);
}
pub fn load_row_rejected(row: u64, reason: &str) {
warn(
LOAD_ROW_REJECTED,
&[("row", &row.to_string()), ("reason", reason)],
);
}
pub fn load_finished(written: u64, rejected: u64) {
say(
LOAD_FINISHED,
&[
("written", &written.to_string()),
("rejected", &rejected.to_string()),
],
);
}
pub fn statement_sending(statement: &str, values: usize, classification: &str) {
tracing::debug!(
message_id = STATEMENT_SENDING,
statement = statement,
values = values,
classification = classification,
"{}",
prose(STATEMENT_SENDING, &[])
);
}
pub fn safety_no_unique_key(table: &str) {
tracing::warn!(
message_id = SAFETY_NO_UNIQUE_KEY,
table = table,
"{}",
prose(SAFETY_NO_UNIQUE_KEY, &[("table", table)])
);
}
pub fn ephemeral_state(state: &str) {
tracing::warn!(
message_id = EPHEMERAL_STATE,
state = state,
"{}",
prose(EPHEMERAL_STATE, &[("state", state)])
);
}
#[cfg(test)]
mod tests {
use super::*;
use crate::i18n::{negotiate, Messages, SUPPORTED};
#[test]
fn every_message_this_module_emits_is_carried_by_every_language() {
for tag in SUPPORTED {
let language = negotiate::parse(tag).expect("a language");
let messages = Messages::load(language).expect("carried by this build");
for id in ALL {
assert!(messages.carries(id), "`{tag}` is missing `{id}`");
}
}
}
#[test]
fn no_message_is_named_twice() {
let unique: std::collections::BTreeSet<&str> = ALL.iter().copied().collect();
assert_eq!(unique.len(), ALL.len(), "a message is named more than once");
}
#[test]
fn every_message_renders_as_prose_and_never_as_its_own_name() {
for id in ALL {
let rendered = prose(id, &[]);
assert!(!rendered.is_empty(), "`{id}` rendered as nothing");
assert_ne!(rendered, id, "`{id}` has no prose behind it");
}
}
#[test]
fn emitting_into_a_silent_channel_never_stops_the_work() {
channel_opening("a-host");
channel_opened("11.4.0", 12);
channel_open_failed(4_000);
channel_closing(37);
response_volume(2048, 1);
attempt_retrying(1, 3, 250);
attempt_permanent();
budget_exceeded("read", 30);
budget_abort_requested();
budget_abort_confirmed();
budget_abort_unconfirmed();
copy_started(4, "/tmp/out");
copy_table("orders");
copy_snapshot_unavailable("orders");
copy_finished(10, 2048);
load_batch_committed(1000, 1000);
load_row_rejected(7, "the value is not a number");
load_finished(1000, 7);
statement_sending("SELECT ?", 1, "read");
safety_no_unique_key("orders");
ephemeral_state("temporary_table");
}
}