Skip to content

Add new --grep option to journalctl - #7881

Merged
poettering merged 4 commits into
systemd:masterfrom
keszybz:pcre
Jan 28, 2018
Merged

poettering merged 4 commits into
systemd:masterfrom
keszybz:pcre

Conversation

@keszybz

@keszybz keszybz commented Jan 15, 2018 •

Copy link
Copy Markdown
Member

This uses the pcre2 libary, which is, as least on systems on which systemd is built with selinux support, essentially free, since selinux already links to it.

Using --grep instead of piping to grep is nice because our coloring and pager support is retained. Also, only the MESSAGE field is grepped, not the metadata.

@keszybz keszybz changed the title Pcre Add new --grep option to journalctl Jan 15, 2018
@poettering

Copy link
Copy Markdown
Member

urks. regexes. yuck.

Are we sure we want this? also, if we do, are we sure we want perl-compatible ones? why not stick to normal glibc regexes?

Comment thread src/journal/journalctl.c Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Maybe instead use -G for case-senstivie grepping, and -g for non-case-sensitive grepping?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I though about that... that can always be added. But a separate option is still needed, because there are three cases: sensitive, insensitive, automatic. So g/G is not enough.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

hmm, indenting is different from other lines...

@poettering

Copy link
Copy Markdown
Member

also, grep does coloring here, this patch does not, are we sure we want this in our code?

Moreover, if we apply grepping to MESSAGES= only then using normal grep is exactly as good as our stuff...

@keszybz

keszybz commented Jan 15, 2018

Copy link
Copy Markdown
Member Author

urks. regexes. yuck.

Well, it's a useful tool. We do all kinds of filtering, but there are many cases where that filtering is not enough. Of course it's not always applicable, but there are many many cases where it is. As random examples: finding all messages about firmware from the kernel and services and printing them, without destroying multiline messages; or finding all sshd messages referring to a certain user name.

Are we sure we want this?

I think it's quite useful.

also, if we do, are we sure we want perl-compatible ones? why not stick to normal glibc regexes?

Because they are the most powerful and the reason to use those older syntaxes is backwards compat which doesn't apply here.

also, grep does coloring here, this patch does not, are we sure we want this in our code?

I didn't think this would be particularly useful. I'd let people start playing with this, and maybe implement later if there's demand.

Moreover, if we apply grepping to MESSAGES= only then using normal grep is exactly as good as our stuff...

No, not really. I list a bunch of reasons in the initial message. In addition, doing this internally is faster, and this patch works with --output and other niceties that we have. I did the grepping on MESSAGE because I think that seems the most excpected use case. I don't you'd grep on the other fields which tend to be structured or contain a single value which can be matched exactly. MESSAGE fields tends to be freeform and vary the most.

@boucman

boucman commented Jan 15, 2018

Copy link
Copy Markdown
Contributor

hmm, small call from the embedded people to have this optional...

libpcre is not that big (~100k from a quick look at a yocto package) but it's still an increase of the actual space taken by systemd on minimalistic systems...

systemd being a fundamental brick, any systemd dependency is a mandatory system dependancy for all practical purpose...

@boucman

boucman commented Jan 15, 2018

Copy link
Copy Markdown
Contributor

my bad... it is optional

@vcaputo

vcaputo commented Jan 17, 2018

Copy link
Copy Markdown
Contributor

Wouldn't it make more sense to just add a flag for enabling regexp in all the existing matching machinery? So you'd just use journalctl exactly as you do now, but what used to only be literal match fields now support regexp because you added a flag to enable the behavior.

@keszybz

keszybz commented Jan 17, 2018

Copy link
Copy Markdown
Member Author

That would be a huge reworking. Right now when you ask for MESSAGE=foobar or MESSAGE_ID=something, there's no searching going on, because fields and field-value pairs are indexed. With regexp (or any other pattern matching) this all becomes impossible and the only way is to first find all the occurrences and them filter them. What you are suggesting would be interesting, but it's massively more work compared to this simple patch.

@keszybz

keszybz commented Jan 27, 2018

Copy link
Copy Markdown
Member Author

Updated with some small fixes and one new feature: the matched pattern is now highlighted, similarly to what grep does.

Comment thread src/journal/journalctl.c Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

please do --case-sensitive as --case-sensitive=

Comment thread src/journal/journalctl.c Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

this is indented unlike the other lines

Case sensitive or case insensitive matching can be requested using
--case-sensitive[=yes|no].

Unless specified, matching is case sensitive if the pattern contains any
uppercase letters, and case insensitive otherwise. This matches what
forward-search does in emacs, and recently also --ignore-case in less.  This
works surprisingly well, because usually when one is wants to do case-sensitive
matching, the pattern is usually camel-cased. In the less frequent case when
case-sensitive matching is required with an all-lowercase pattern,
--case-sensitive can be used to override the automatic logic.
Red is used for highligting, the same as grep does. Except when the line is
highlighted red already, because it has high priority, in which case plain ansi
highlight is used for the matched substring.

Coloring is implemented for short and cat outputs, and not for other types.
I guess we could also add it for verbose output in the future.
@keszybz

keszybz commented Jan 28, 2018

Copy link
Copy Markdown
Member Author

Updated.

@poettering
poettering merged commit 7755083 into systemd:master Jan 28, 2018
@keszybz
keszybz deleted the pcre branch January 28, 2018 15:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

4 participants