Skip to content

Commit f46e818

Browse files
authored
Bug Fixes (#7)
new release
1 parent 3935cc3 commit f46e818

File tree

6 files changed

+41
-18
lines changed

6 files changed

+41
-18
lines changed

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
## 0.3.0
2+
- Pull Requests
3+
- https://github.com/gsquire/topngx/pull/7
4+
- Change `--no-follow` to `--follow` allowing users to explicitly opt in for tailing log files.
5+
- Change `-t` to `-i` for the interval argument.
6+
- Bug Fixes
7+
- Only restore the cursor when running in tail mode.
8+
- Return an error if a user tries to tail standard input.
9+
10+
## 0.2.0
11+
- Pull Requests
12+
- https://github.com/gsquire/topngx/pull/6
13+
- Implement the first cut of log tailing.

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "topngx"
3-
version = "0.2.0"
3+
version = "0.3.0"
44
authors = ["Garrett Squire <github@garrettsquire.com>"]
55
edition = "2018"
66
description = "Top for NGINX"

README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,26 +31,29 @@ brew install sqlite
3131
sudo apt-get update && sudo apt-get install libsqlite3-dev
3232
```
3333

34+
## CHANGELOG
35+
[See here](CHANGELOG.md)
36+
3437
## Usage
3538
```sh
36-
topngx 0.1.0
39+
topngx 0.3.0
3740
Garrett Squire <github@garrettsquire.com>
3841
top for NGINX
3942

4043
USAGE:
4144
topngx [FLAGS] [OPTIONS] [SUBCOMMAND]
4245

4346
FLAGS:
44-
-h, --help Prints help information
45-
-n, --no-follow Do not tail the log file and only report what is currently there
46-
-V, --version Prints version information
47+
-t, --follow Tail the specified log file. You cannot tail standard input
48+
-h, --help Prints help information
49+
-V, --version Prints version information
4750

4851
OPTIONS:
4952
-a, --access-log <access-log> The access log to parse
5053
-f, --format <format> The specific log format with which to parse [default: combined]
5154
-g, --group-by <group-by> Group by this variable [default: request_path]
5255
-w, --having <having> Having clause [default: 1]
53-
-t, --interval <interval> Refresh the statistics using this interval which is given in seconds [default: 2]
56+
-i, --interval <interval> Refresh the statistics using this interval which is given in seconds [default: 2]
5457
-l, --limit <limit> The number of records to limit for each query [default: 10]
5558
-o, --order-by <order-by> Order of output for the default queries [default: count]
5659

src/main.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,17 @@ struct Options {
5252
having: u64,
5353

5454
/// Refresh the statistics using this interval which is given in seconds.
55-
#[structopt(short = "t", long, conflicts_with = "no_follow", default_value = "2")]
55+
#[structopt(short, long, default_value = "2")]
5656
interval: u64,
5757

58+
/// Tail the specified log file. You cannot tail standard input.
59+
#[structopt(short = "t", long)]
60+
follow: bool,
61+
5862
/// The number of records to limit for each query.
5963
#[structopt(short, long, default_value = "10")]
6064
limit: u64,
6165

62-
/// Do not tail the log file and only report what is currently there.
63-
#[structopt(short, long)]
64-
no_follow: bool,
65-
6666
/// Order of output for the default queries.
6767
#[structopt(short, long, default_value = "count")]
6868
order_by: String,
@@ -165,7 +165,7 @@ fn tail(
165165
}
166166
recv(ticker) -> _ => {
167167
execute!(io::stdout(), Clear(ClearType::All))?;
168-
processor.report()?;
168+
processor.report(opts.follow)?;
169169
}
170170
}
171171
}
@@ -193,8 +193,13 @@ fn run(opts: &Options, fields: Option<Vec<String>>, queries: Option<Vec<String>>
193193
info!("access log: {}", access_log);
194194
info!("access log format: {}", opts.format);
195195

196+
// We cannot tail STDIN.
197+
if opts.follow && access_log == STDIN {
198+
return Err(anyhow!("cannot tail STDIN"));
199+
}
200+
196201
// We need to tail the log file.
197-
if !opts.no_follow {
202+
if opts.follow {
198203
return tail(opts, access_log, fields, queries);
199204
}
200205

@@ -206,7 +211,7 @@ fn run(opts: &Options, fields: Option<Vec<String>>, queries: Option<Vec<String>>
206211
let pattern = format_to_pattern(&opts.format)?;
207212
let processor = generate_processor(opts, fields, queries)?;
208213
parse_input(&lines, &pattern, &processor)?;
209-
processor.report()
214+
processor.report(opts.follow)
210215
}
211216

212217
fn parse_input(lines: &[String], pattern: &Regex, processor: &Processor) -> Result<()> {

src/processor.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ impl Processor {
8181
}
8282

8383
/// Run the queries as specified by the user.
84-
pub(crate) fn report(&self) -> Result<()> {
84+
pub(crate) fn report(&self, save_cursor: bool) -> Result<()> {
8585
for query in &self.queries {
8686
debug!("report query: {}", query);
8787

@@ -127,8 +127,10 @@ impl Processor {
127127
tw.flush()?;
128128
}
129129

130-
// Restore our original cursor position.
131-
execute!(io::stdout(), RestorePosition)?;
130+
// Restore our original cursor position only in tail mode.
131+
if save_cursor {
132+
execute!(io::stdout(), RestorePosition)?;
133+
}
132134

133135
Ok(())
134136
}

0 commit comments

Comments
 (0)