Skip to content

Support colors and buffer writes#32

Merged
KodrAus merged 3 commits intomasterfrom
feat/termcolor
Nov 1, 2017
Merged

Support colors and buffer writes#32
KodrAus merged 3 commits intomasterfrom
feat/termcolor

Conversation

@KodrAus
Copy link
Collaborator

@KodrAus KodrAus commented Oct 31, 2017

Closes #31 and #2 by using termcolor to do the hard work.

Colours

The default format now includes some colours:

colours

The colour API is still private because I'm not sure how we want to expose it.

Buffering

This API goes back to buffering log records before printing them to the terminal, but we use a single buffer per thread rather than allocating for each log. I did some benchmarking and performance looks good overall.

src/lib.rs Outdated
/// It also supports terminal colors, but this is currently private.
///
/// [`Write`]: https://doc.rust-lang.org/stable/std/io/trait.Write.html
pub struct Formatter {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'll move this into a new fmt module, since when we do make colouring public there will probably be some more types to go along with it.

If you just create your closure as part of the .format call then you won't need to name the Formatter type directly.

src/lib.rs Outdated
// The format is guaranteed to be `Some` by this point
let mut formatter = tl_buf.as_mut().unwrap();

let _ = (self.format)(&mut formatter, record)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should clarify its error conditions a bit better:

  • We only print the record to the terminal if the record is successfully formatted (which it pretty much always should)
  • We always clear the buffer even if the record wasn't printed. Seems like it might not be ideal, but I'm not really sure what would cause a print to fail.

I think it needs a little more thought and should be refactored to clarify the intent a bit better.

src/lib.rs Outdated
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.buf.set_color(&self.spec)?;
let write = self.buf.write(buf);
self.buf.reset()?;
Copy link
Collaborator Author

@KodrAus KodrAus Oct 31, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not returning the bytes written if the terminal fails to reset seems like a bad thing to do. Maybe.

@KodrAus
Copy link
Collaborator Author

KodrAus commented Oct 31, 2017

Ok I'm happy. Docs can be dealt with down the track.

@BurntSushi if you've got a few minutes sometime I'd be grateful if you could give this a quick scan and let me know if anything looks totally insane :)

@BurntSushi
Copy link
Contributor

@KodrAus LGTM! Nice work. :-)

@KodrAus KodrAus merged commit 1508458 into master Nov 1, 2017
@KodrAus KodrAus deleted the feat/termcolor branch November 1, 2017 21:20
epage added a commit to epage/env_logger that referenced this pull request Jan 27, 2026
> [!NOTE]
> Mend has cancelled [the proposed
renaming](https://redirect.github.com/renovatebot/renovate/discussions/37842)
of the Renovate GitHub app being renamed to `mend[bot]`.
> 
> This notice will be removed on 2025-10-07.

<hr>

This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
|
[actions/setup-python](https://redirect.github.com/actions/setup-python)
| action | major | `v5` -> `v6` |

---

### Release Notes

<details>
<summary>actions/setup-python (actions/setup-python)</summary>

###
[`v6`](https://redirect.github.com/actions/setup-python/compare/v5...v6)

[Compare
Source](https://redirect.github.com/actions/setup-python/compare/v5...v6)

</details>

---

### Configuration

📅 **Schedule**: Branch creation - "before 5am on the first day of the
month" (UTC), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you
are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR was generated by [Mend Renovate](https://mend.io/renovate/).
View the [repository job
log](https://developer.mend.io/github/epage/_rust).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0MS4xMzEuOSIsInVwZGF0ZWRJblZlciI6IjQxLjEzMS45IiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6W119-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants