Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add xsyslog_ev function and related #5186

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

mjdominus-cyrus
Copy link

Per https://linear.app/fastmail/issue/CYR-1407/produce-new-xsyslog-ev-function

Also:

  • Adds a new test suite, util.testc
  • Tests pass and valgrind is clean
  • Includes some unrelated minor documentation fixes and other corrections

mjdominus and others added 9 commits December 23, 2024 15:22
Also, more comments

The history of these two commits is complicated:
* MJD wrote an initial draft
* Ken revised it to use struct buf instead of C strings
* MJD came back and restored his original comments and replaced
  the printf hackery with smaller printf hackery

NB: The diff for this commit is a mess ---  *unless* you use Git's --patience option.
@mjdominus-cyrus mjdominus-cyrus force-pushed the cyr-1407-xsyslog_ev-1223 branch from c471a74 to e5a4647 Compare December 23, 2024 21:10
@rsto rsto self-requested a review December 24, 2024 06:04
Copy link
Member

@rsto rsto left a comment

Choose a reason for hiding this comment

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

I like improving the log format! It's also great we got proper unit tests for this.

Apart from the few items I found in the code, I wonder:
Is there a canonical specification for logfmt somewhere? I see our tests don't assert how empty strings in the key or value are handled, would that be the empty string or ""?

I'm a bit wary of the magic printf formatter guessing, but it looks well thought out so let's see how it turns out in practice.

One other thing that stands out to me are the commit message: in Cyrus, we typically prefix the commit message with the filename that mostly got changed in this commit (e.g. util.c). Also, our commit messages almost always start with a verb.

// in place of
// xsyslog_ev(..., key, "foo", DUMMY, ...)
// but that seems morelikely to be confusing than otherwise
case -1:
Copy link
Member

Choose a reason for hiding this comment

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

This does not compile on my machine which runs on aarch64:

  2360      // but that seems morelikely to be confusing than otherwise
E 2361     case -1:     <-- error: Overflow converting case value to switch condition type (-1 to 255)
  2362     default:

The reason for that is that on aarch64 the char type is unsigned. Luckily, in this case the fix just looks to be to remove that case -1 altogether, as it would fall through to the default label anyway.

There's also a nit in the line above: split "morelikely" into "more likely"

Copy link
Author

Choose a reason for hiding this comment

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

I can do this. Would it also work to declare the function as returning signed char?

while (*s && *s != '%') s++; // seek to first % sign or to end of string
if (*s == 0) return 0; // String has no percent signs at all
s++;
if (*s == '%') { s++; goto TOP; } // it's "%%", skip it and continue
Copy link
Member

Choose a reason for hiding this comment

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

nit: I know you asked before writing this code about when to and when to not use goto, so I won't request to change this now. But I find this goto here not only unnecessary but confusing. A while loop with a break or continue statement would make that code likely more simple to read.

As a rule of thumb: goto in the Cyrus codebase is perfectly fine for early function exits that require cleanup (e.g. the done label). In all other cases, they are the least preferred option.

@rjbs
Copy link
Collaborator

rjbs commented Dec 24, 2024

@rsto logfmt is, in my experience, insufficiently documented in the wild. The most cited source is https://brandur.org/logfmt which I find useful but … well, it wouldn't make a good I-D.

I thought we'd improved the clarity of this in the Fastmail internal spec, but I'm wrong. We just use Perl's Log::Fmt, which I wrote. In that, the value must have length, so x="" and not x=. (It serializes Perl's undef value as ~missing~.)

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.

5 participants