Skip to content

Commit c7e77e9

Browse files
committed
add
1 parent 0a734ef commit c7e77e9

File tree

4 files changed

+148
-26
lines changed

4 files changed

+148
-26
lines changed

Linux/install_bins_curl.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,7 @@ install_all_bins() {
421421
"tok"
422422
"trufflehog"
423423
"trurl"
424+
"tss"
424425
"ulexec"
425426
"unfurl"
426427
"upx"

Linux/tss/README.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
### ℹ️ About
2+
Timestamp each line of Input Stream like [ts(1)](https://linux.die.net/man/1/ts).<br>
3+
4+
### 🧰 Usage
5+
```mathematica
6+
7+
❯ tss --help
8+
9+
tss - timestamp each line of input
10+
11+
Usage: tss [OPTIONS]
12+
13+
Options:
14+
--buffered Use buffered output (default is unbuffered)
15+
--color Colorize timestamps
16+
--delta Show time delta between lines
17+
-e, --epoch Show seconds since Unix epoch
18+
-f, --format FORMAT Date format (default: %Y-%m-%d %H:%M:%S)
19+
--force-overwrite Overwrite output file instead of appending
20+
-i, --iso Use ISO 8601 format (2025-07-03T14:30:45.123+05:45)
21+
-h, --help Show this help
22+
--microseconds Show microseconds precision
23+
-m, --monotonic Use monotonic clock for relative timestamps
24+
--nanoseconds Show nanoseconds precision
25+
-o, --output FILE Write timestamped output to file (appends by default)
26+
--prefix-only Only show timestamp prefix (no input lines)
27+
-r, --relative Show relative timestamps from start
28+
-s, --separator SEP Separator between timestamp and line (default: " ")
29+
--timezone TZ Use specific timezone (e.g., UTC, EST, PST)
30+
-u, --utc Use UTC time instead of local time
31+
32+
Format specifiers (strftime compatible):
33+
%Y 4-digit year %m Month (01-12) %d Day (01-31)
34+
%H Hour (00-23) %M Minute (00-59) %S Second (00-59)
35+
%3f Milliseconds %6f Microseconds %9f Nanoseconds
36+
%z Timezone offset %Z Timezone name %% Literal %
37+
38+
Examples:
39+
ls -la | tss # Basic timestamping
40+
tail -f /var/log/messages | tss -r # Relative timestamps
41+
ping google.com | tss -f "[%H:%M:%S.%3f]➜ " # Custom format
42+
dmesg | tss -i # ISO format
43+
make 2>&1 | tss -e # Epoch timestamps
44+
tail -f app.log | tss -r -m # Relative monotonic
45+
cat file.txt | tss --delta # Show time between lines
46+
ping host | tss --color --microseconds # Colored with microseconds
47+
command | tss --prefix-only # Only timestamps
48+
make 2>&1 | tss -o build.log # Append to file
49+
tail -f app.log | tss -o logs/app.log --force-overwrite # Overwrite file
50+
ping host | tss -o network.log # Append to network.log
51+
52+
Note: --relative and --delta are mutually exclusive
53+
Output files are appended to by default, use --force-overwrite to replace
54+
55+
```
56+
57+
### 🛠️ Building
58+
```bash
59+
RUST_TARGET="$(uname -m)-unknown-linux-musl"
60+
RUSTFLAGS="-C target-feature=+crt-static \
61+
-C link-self-contained=yes \
62+
-C default-linker-libraries=yes \
63+
-C prefer-dynamic=no \
64+
-C lto=yes \
65+
-C debuginfo=none \
66+
-C strip=symbols \
67+
-C link-arg=-Wl,--build-id=none \
68+
-C link-arg=-Wl,--discard-all \
69+
-C link-arg=-Wl,--strip-all"
70+
71+
export RUST_TARGET RUSTFLAGS
72+
rustup target add "${RUST_TARGET}"
73+
74+
cargo build --target "${RUST_TARGET}" \
75+
--all-features \
76+
--jobs="$(($(nproc)+1))" \
77+
--release
78+
79+
"./target/${RUST_TARGET}/release/tss" --help
80+
```

Linux/tss/src/Cargo.toml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[package]
2+
name = "tss"
3+
version = "0.1.0"
4+
edition = "2021"
5+
license = "MIT"
6+
readme = "README.md"
7+
8+
description = "Timestamp each line of Input Stream"
9+
10+
[dependencies]
11+
chrono = { version = "0.4", features = ["clock", "std"] }
12+
13+
[profile.release]
14+
codegen-units = 1
15+
debug = false
16+
lto = true
17+
opt-level = "z"
18+
panic = "abort"
19+
strip = true

Linux/tss/src/main.rs

Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::env;
22
use std::io::{self, BufRead, BufReader, Write, BufWriter};
33
use std::path::Path;
44
use std::time::{SystemTime, UNIX_EPOCH, Instant};
5-
use std::fs::{File, create_dir_all};
5+
use std::fs::{create_dir_all, OpenOptions};
66
use chrono::{DateTime, Local, Utc, TimeZone, Timelike, Datelike};
77

88
struct Config {
@@ -21,6 +21,7 @@ struct Config {
2121
buffered: bool,
2222
timezone: Option<String>,
2323
output_file: Option<String>,
24+
force_overwrite: bool,
2425
}
2526

2627
impl Config {
@@ -41,6 +42,7 @@ impl Config {
4142
buffered: false, // Default to unbuffered for real-time output
4243
timezone: None,
4344
output_file: None,
45+
force_overwrite: false,
4446
};
4547

4648
let args: Vec<String> = env::args().collect();
@@ -67,6 +69,7 @@ impl Config {
6769
"--prefix-only" => config.prefix_only = true,
6870
"--color" => config.color = true,
6971
"--buffered" => config.buffered = true,
72+
"--force-overwrite" => config.force_overwrite = true,
7073
"-s" | "--separator" => {
7174
i += 1;
7275
if i >= args.len() {
@@ -134,27 +137,28 @@ impl Config {
134137

135138
fn print_help(program_name: &str) {
136139
println!(
137-
"{} - timestamp each line of input
140+
"{} - timestamp each line of input stream
138141
139142
Usage: {} [OPTIONS]
140143
141144
Options:
145+
--buffered Use buffered output (default is unbuffered)
146+
--color Colorize timestamps
147+
--delta Show time delta between lines
148+
-e, --epoch Show seconds since Unix epoch
142149
-f, --format FORMAT Date format (default: %Y-%m-%d %H:%M:%S)
143-
-s, --separator SEP Separator between timestamp and line (default: \" \")
144-
-r, --relative Show relative timestamps from start
145-
-m, --monotonic Use monotonic clock for relative timestamps
146-
-u, --utc Use UTC time instead of local time
150+
--force-overwrite Overwrite output file instead of appending
147151
-i, --iso Use ISO 8601 format (2025-07-03T14:30:45.123+05:45)
148-
-e, --epoch Show seconds since Unix epoch
152+
-h, --help Show this help
149153
--microseconds Show microseconds precision
154+
-m, --monotonic Use monotonic clock for relative timestamps
150155
--nanoseconds Show nanoseconds precision
151-
--delta Show time delta between lines
156+
-o, --output FILE Write timestamped output to file (appends by default)
152157
--prefix-only Only show timestamp prefix (no input lines)
153-
--color Colorize timestamps
154-
--buffered Use buffered output (default is unbuffered)
158+
-r, --relative Show relative timestamps from start
159+
-s, --separator SEP Separator between timestamp and line (default: \" \")
155160
--timezone TZ Use specific timezone (e.g., UTC, EST, PST)
156-
-o, --output FILE Write timestamped output to file (creates directories)
157-
-h, --help Show this help
161+
-u, --utc Use UTC time instead of local time
158162
159163
Format specifiers (strftime compatible):
160164
%Y 4-digit year %m Month (01-12) %d Day (01-31)
@@ -163,22 +167,24 @@ Format specifiers (strftime compatible):
163167
%z Timezone offset %Z Timezone name %% Literal %
164168
165169
Examples:
166-
ls -la | {} # Basic timestamping
167-
tail -f /var/log/messages | {} -r # Relative timestamps
168-
ping google.com | {} -f \"[%H:%M:%S.%3f]\" # Custom format
169-
dmesg | {} -i # ISO format
170-
make 2>&1 | {} -e # Epoch timestamps
171-
tail -f app.log | {} -r -m # Relative monotonic
172-
cat file.txt | {} --delta # Show time between lines
173-
ping host | {} --color --microseconds # Colored with microseconds
174-
command | {} --prefix-only # Only timestamps
175-
make 2>&1 | {} -o build.log # Stream to console and file
176-
tail -f app.log | {} -o logs/app-timestamped.log # Save to file with directories
170+
ls -la | {} # Basic timestamping
171+
tail -f /var/log/messages | {} -r # Relative timestamps
172+
ping google.com | {} -f \"[%H:%M:%S.%3f]➜ \" # Custom format
173+
dmesg | {} -i # ISO format
174+
make 2>&1 | {} -e # Epoch timestamps
175+
tail -f app.log | {} -r -m # Relative monotonic
176+
cat file.txt | {} --delta # Show time between lines
177+
ping host | {} --color --microseconds # Colored with microseconds
178+
command | {} --prefix-only # Only timestamps
179+
make 2>&1 | {} -o build.log # Append to file
180+
tail -f app.log | {} -o logs/app.log --force-overwrite # Overwrite file
181+
ping host | {} -o network.log # Append to network.log
177182
178-
Note: --relative and --delta are mutually exclusive",
183+
Note: --relative and --delta are mutually exclusive
184+
Output files are appended to by default, use --force-overwrite to replace\n",
179185
program_name, program_name, program_name, program_name, program_name,
180186
program_name, program_name, program_name, program_name, program_name,
181-
program_name, program_name, program_name
187+
program_name, program_name, program_name, program_name
182188
);
183189
}
184190
}
@@ -528,7 +534,23 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
528534
if let Some(parent) = Path::new(output_path).parent() {
529535
create_dir_all(parent)?;
530536
}
531-
Some(BufWriter::new(File::create(output_path)?))
537+
538+
// Open file for writing (append by default, create/truncate if force_overwrite)
539+
let file = if config.force_overwrite {
540+
OpenOptions::new()
541+
.create(true)
542+
.write(true)
543+
.truncate(true)
544+
.open(output_path)?
545+
} else {
546+
OpenOptions::new()
547+
.create(true)
548+
.write(true)
549+
.append(true)
550+
.open(output_path)?
551+
};
552+
553+
Some(BufWriter::new(file))
532554
} else {
533555
None
534556
};

0 commit comments

Comments
 (0)