-
Notifications
You must be signed in to change notification settings - Fork 0
Logging the host can read, no reflection, and a lighter build #2
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
817ab1e
refactor: drop gRPC reflection, and stand the tests up without it
devgianlu 3198421
feat: write the log format the host can read
devgianlu 43a76e0
build: stop installing gtest for the host, and make tests opt-in
devgianlu 246fb88
chore: 0.2.0
devgianlu 6129501
fix: let a backend's record through whatever level is set here
devgianlu d68f563
fix: address review, and trim the comments
devgianlu b2eb3f9
test: stop the locale test leaking its locale, or passing without one
devgianlu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| #pragma once | ||
|
|
||
| #include <chrono> | ||
| #include <functional> | ||
| #include <initializer_list> | ||
| #include <string> | ||
| #include <string_view> | ||
|
|
||
| namespace go_plugin::log { | ||
|
|
||
| /** | ||
| * The only five severities go-plugin understands. Anything else leaves the host | ||
| * unable to tell the level and it files the line at its own. | ||
| */ | ||
| enum class Level { Trace, Debug, Info, Warn, Error }; | ||
|
|
||
| class Field { | ||
| public: | ||
| Field(std::string_view key, std::string_view value); | ||
| Field(std::string_view key, const char* value); | ||
| Field(std::string_view key, const std::string& value); | ||
| Field(std::string_view key, bool value); | ||
| Field(std::string_view key, int value); | ||
| Field(std::string_view key, long long value); | ||
| Field(std::string_view key, unsigned long long value); | ||
| Field(std::string_view key, double value); | ||
|
|
||
| const std::string& key() const { return key_; } | ||
| const std::string& value() const { return value_; } | ||
|
|
||
| /** Whether the value is written as a JSON literal rather than a string. */ | ||
| bool literal() const { return literal_; } | ||
|
|
||
| private: | ||
| Field(std::string_view key, std::string value, bool literal); | ||
|
|
||
| std::string key_; | ||
| std::string value_; | ||
| bool literal_ = false; | ||
| }; | ||
|
|
||
| struct Record { | ||
| Level level = Level::Info; | ||
| std::string_view message; | ||
| const Field* fields = nullptr; | ||
| std::size_t field_count = 0; | ||
| std::chrono::system_clock::time_point timestamp; | ||
| }; | ||
|
|
||
| /** Receives every record. The default encodes it and writes it to stderr. */ | ||
| using Sink = std::function<void(const Record&)>; | ||
|
|
||
| /** Pass nullptr to restore the default. */ | ||
| void SetSink(Sink sink); | ||
|
|
||
| /** Governs Write only, not Submit. Info by default. */ | ||
| void SetLevel(Level min); | ||
| Level GetLevel(); | ||
| bool Enabled(Level level); | ||
|
|
||
| void Write(Level level, std::string_view message, std::initializer_list<Field> fields = {}); | ||
|
|
||
| inline void Trace(std::string_view message, std::initializer_list<Field> fields = {}) { | ||
| Write(Level::Trace, message, fields); | ||
| } | ||
| inline void Debug(std::string_view message, std::initializer_list<Field> fields = {}) { | ||
| Write(Level::Debug, message, fields); | ||
| } | ||
| inline void Info(std::string_view message, std::initializer_list<Field> fields = {}) { | ||
| Write(Level::Info, message, fields); | ||
| } | ||
| inline void Warn(std::string_view message, std::initializer_list<Field> fields = {}) { | ||
| Write(Level::Warn, message, fields); | ||
| } | ||
| inline void Error(std::string_view message, std::initializer_list<Field> fields = {}) { | ||
| Write(Level::Error, message, fields); | ||
| } | ||
|
|
||
| /** | ||
| * The seam a backend adapter sits on, so a library that already knows a line's | ||
| * time, severity and origin does not lose them to a second timestamp. | ||
| * | ||
| * SetLevel is deliberately not applied: the record comes from a library that | ||
| * has already decided to emit it, and dropping it again here would lose what a | ||
| * plugin meant to say. | ||
| */ | ||
| void Submit(const Record& record); | ||
|
|
||
| /** Renders a record in the host's format, without the trailing newline. */ | ||
| std::string Encode(const Record& record); | ||
|
|
||
| /** | ||
| * Exactly six fractional digits, and an offset written either as "Z" or with a | ||
| * colon. A timestamp in any other shape makes the host reject the whole line | ||
| * and report it as unparsed text at its own level. | ||
| */ | ||
| std::string FormatTimestamp(std::chrono::system_clock::time_point tp); | ||
|
|
||
| std::string_view LevelName(Level level); | ||
|
|
||
| } // namespace go_plugin::log |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| #pragma once | ||
|
|
||
| namespace go_plugin::log { | ||
|
|
||
| struct AbslBridgeOptions { | ||
| /** | ||
| * Abseil has no debug or trace severity of its own — they exist only as | ||
| * VLOG verbosities — so the mapping has to be stated. VLOG at or above this | ||
| * verbosity is reported as trace, below it as debug. | ||
| */ | ||
| int trace_from_verbosity = 2; | ||
|
|
||
| /** Carry Abseil's source file and line as a `caller` field. */ | ||
| bool include_caller = true; | ||
|
|
||
| /** | ||
| * Stop Abseil writing its own copy of every line. Left on, each line | ||
| * reaches the host twice: once as unparsed text, once in the format it can | ||
| * read. | ||
| */ | ||
| bool silence_absl_stderr = true; | ||
| }; | ||
|
|
||
| /** | ||
| * Routes Abseil's LOG() and VLOG() through the format the host parses, so a | ||
| * plugin keeps its own severity without touching a call site. | ||
| * | ||
| * Call after absl::InitializeLog(). Only the first call installs a sink. | ||
| */ | ||
| void InstallAbslBridge(const AbslBridgeOptions& options = {}); | ||
|
|
||
| } // namespace go_plugin::log |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.