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

errors: Maintain caller location in to_log #1994

Merged
merged 3 commits into from
Dec 7, 2022

Conversation

har7an
Copy link
Contributor

@har7an har7an commented Dec 6, 2022

The errors::LoggableError trait provides the to_log method as convenience function to log error messages easily. It is increasingly used throughout the zellij codebase, either directly or indirectly from the non_fatal function.

Previously, the log messages generated by to_log would look something like this:

ERROR  |zellij_utils::errors::not| 2022-12-05 21:22:07.662 [stdin_handler] [zellij-utils/src/errors.rs:130]:  a non-fatal error occured

Caused by:
[ ... ]

The log output tells us that the error occured at zellij-utils/src/errors.rs:130, which is in fact wrong: This is only the location where to_log calls log::error! to log the actual message content. It loses information about where the error originated.

This PR changes the way the messages are logged, and provides log entries like the following instead:

ERROR  |???                      | 2022-12-06 10:04:57.527 [screen    ] [zellij-server/src/screen.rs:1931]: a non-fatal error occured

Caused by:
[ ... ]

Most notably, now the file and line number of the real caller are preserved. Unfortunately, this leaves the module empty ("???"), but this is unavoidable (and wasn't correct previously, either). That is because log by default resolves the module name with the std::module_path!() macro, which is replaced at compile time in the location it is written. In our case, this would still be the to_log function. Hence, we leave the module empty.

@har7an har7an temporarily deployed to cachix December 6, 2022 09:54 Inactive
har7an added a commit to har7an/zellij that referenced this pull request Dec 7, 2022
errors: Maintain caller location in `to_log`
by building the log record manually and filling in the callers file and
line manually. It is currently not possible to determine a callers
module, hence the module is now set to "???" in all log entries logged
this way. Nonetheless, the file and line number are sufficient to find
the logs source.
default implementation in the `LoggableError` trait.
errors: Maintain caller location in `to_log`
@har7an har7an force-pushed the errors/caller-location-in-to-log branch from 06b2b74 to 8866f9f Compare December 7, 2022 07:14
@har7an har7an temporarily deployed to cachix December 7, 2022 07:15 — with GitHub Actions Inactive
@har7an har7an merged commit 81287a2 into zellij-org:main Dec 7, 2022
@har7an har7an deleted the errors/caller-location-in-to-log branch December 7, 2022 07:51
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.

None yet

1 participant