Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add sane default tracing setup
Browse files Browse the repository at this point in the history
OliverNChalk committed Aug 28, 2024
0 parents commit b4cc4f5
Showing 11 changed files with 668 additions and 0 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/actions/rust-toolchain/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: rust-toolchain
inputs:
toolchain:
description: "Which rust toolchain to use"
required: false

runs:
using: "composite"
steps:
- id: get-toolchain
shell: bash
run: |
USER_OVERRIDE=${{ inputs.toolchain }}
DEFAULT_TOOLCHAIN=$(grep channel rust-toolchain.toml | awk '{print $3}' | sed 's/"//g')
TOOLCHAIN=${USER_OVERRIDE:-$DEFAULT_TOOLCHAIN}
echo "toolchain=$TOOLCHAIN" >> $GITHUB_OUTPUT
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: ${{ steps.get-toolchain.outputs.toolchain }}
override: true
- uses: Swatinem/rust-cache@v2.0.0
with:
shared-key: "enderman"
81 changes: 81 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
name: ci-rust-push

on:
push:
branches:
- 'main'

jobs:
skip-duplicates:
runs-on: ubuntu-latest
outputs:
should_skip: ${{ steps.skip_check.outputs.should_skip }}
steps:
- id: skip_check
uses: fkirc/skip-duplicate-actions@v5
with:
concurrent_skipping: "same_content_newer"

check:
needs: skip-duplicates
if: needs.skip-duplicates.outputs.should_skip != 'true'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
token: ${{ secrets.CI_READ }}
submodules: recursive
- uses: ./.github/workflows/actions/rust-toolchain
- run: cargo check --all-features

test:
needs: skip-duplicates
if: needs.skip-duplicates.outputs.should_skip != 'true'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
token: ${{ secrets.CI_READ }}
submodules: recursive
- uses: ./.github/workflows/actions/rust-toolchain
- run: cargo test --all-features

fmt:
needs: skip-duplicates
if: needs.skip-duplicates.outputs.should_skip != 'true'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
token: ${{ secrets.CI_READ }}
submodules: recursive
- uses: ./.github/workflows/actions/rust-toolchain
with:
toolchain: nightly
- run: rustup component add rustfmt
- run: cargo fmt --all --check

clippy:
needs: skip-duplicates
if: needs.skip-duplicates.outputs.should_skip != 'true'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
token: ${{ secrets.CI_READ }}
submodules: recursive
- uses: ./.github/workflows/actions/rust-toolchain
- run: rustup component add clippy
- run: cargo clippy --all-features -- --deny warnings

doc:
needs: skip-duplicates
if: needs.skip-duplicates.outputs.should_skip != 'true'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
token: ${{ secrets.CI_READ }}
submodules: recursive
- uses: ./.github/workflows/actions/rust-toolchain
- run: cargo doc --all-features --no-deps
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.DS_Store
/target
/log
458 changes: 458 additions & 0 deletions Cargo.lock

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "toolbox"
version = "0.1.0"
edition = "2021"

[features]
default = []
tracing = ["dep:const_format", "dep:tracing", "dep:tracing-appender", "dep:tracing-subscriber"]

[dependencies]
const_format = { version = "0.2.32", optional = true }
tracing = { version = "0.1.40", optional = true }
tracing-appender = { version = "0.2.3", optional = true }
tracing-subscriber = { version = "0.3.18", features = ["env-filter", "json"], optional = true }
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Oliver's Toolbox

This repo contains simple & re-usable rust boilerplate/helpers.

## Features

By default nothing is enabled (to not bloat your dependency tree). Below is a
list of features:

| Feature | Description |
| --------- | ----------------------------------------------- |
| `tracing` | Tracing setup function with rolling log support |
2 changes: 2 additions & 0 deletions rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[toolchain]
channel = "1.80.0"
10 changes: 10 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
edition = "2021"
error_on_line_overflow = true
wrap_comments = true
use_field_init_shorthand = true
imports_granularity = "Module"
condense_wildcard_suffixes = true
format_strings = true
group_imports = "StdExternalCrate"
use_small_heuristics = "Max"
chain_width = 60
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#[cfg(feature = "tracing")]
pub mod tracing;
52 changes: 52 additions & 0 deletions src/tracing.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use std::path::Path;

use const_format::formatcp;
use tracing::level_filters::LevelFilter;
use tracing_appender::non_blocking::WorkerGuard;
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt;
use tracing_subscriber::{EnvFilter, Layer};

const DEFAULT_FILE_DIRECTIVE: &str = formatcp!("info,{}=debug", env!("CARGO_PKG_NAME"));
const LOG_FILE_NAME: &str = formatcp!("{}.log", env!("CARGO_PKG_NAME"));

#[must_use]
pub fn setup_tracing(log_directory: Option<&Path>) -> Option<WorkerGuard> {
// Setup stdout layer.
let stdout_filter = EnvFilter::builder()
.with_env_var("RUST_LOG")
.with_default_directive(LevelFilter::INFO.into())
.from_env()
.unwrap();
let stdout_layer = tracing_subscriber::fmt::layer()
.with_writer(std::io::stderr)
.with_filter(stdout_filter);

// Setup file layer (if requested).
let (file_layer, file_guard) = log_directory
.map(|directory| {
let file_appender = tracing_appender::rolling::hourly(directory, LOG_FILE_NAME);
let (file_writer, file_guard) = tracing_appender::non_blocking(file_appender);

// Load the user's file filter else fallback to the default.
let file_filter = std::env::var("RUST_FILE_LOG").ok();
let file_filter = file_filter.as_deref().unwrap_or(DEFAULT_FILE_DIRECTIVE);
let file_filter: EnvFilter = file_filter.parse().unwrap();

let file_layer = tracing_subscriber::fmt::layer()
.json()
.with_writer(file_writer)
.with_filter(file_filter);

(Some(file_layer), Some(file_guard))
})
.unwrap_or((None, None));

// Combine stdout & file layer.
tracing_subscriber::registry()
.with(stdout_layer)
.with(file_layer)
.init();

file_guard
}
10 changes: 10 additions & 0 deletions taplo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[formatting]
column_width = 120
array_auto_expand = true
allowed_blank_lines = 1

[[rule]]
keys = ["dependencies", "dev-dependencies", "build-dependencies", "toolchain", "workspace.dependencies"]

[rule.formatting]
reorder_keys = true

0 comments on commit b4cc4f5

Please sign in to comment.