-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
166 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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,84 @@ | ||
# Custom Sinks | ||
|
||
Under certain circumstances, it is useful to send the log output to a | ||
destination other than a file, `stderr` and/or `stdout`. In case, the library | ||
provides the `#!cpp google::LogSink` interface whose implementations can be used | ||
to write the log output to arbitrary locations. | ||
|
||
## Basic Interface | ||
|
||
The sink interface is defined as follows: | ||
|
||
``` cpp | ||
class LogSink { | ||
public: | ||
virtual void send(LogSeverity severity, const char* full_filename, | ||
const char* base_filename, int line, | ||
const LogMessageTime& time, const char* message, | ||
size_t message_len); | ||
}; | ||
``` | ||
The user must implement `#!cpp google::LogSink::send`, which is called by the | ||
library every time a message is logged. | ||
!!! warning "Possible deadlock due to nested logging" | ||
This method can't use `LOG()` or `CHECK()` as logging system mutex(s) are | ||
held during this call. | ||
## Registering Log Sinks | ||
To use the custom sink and instance of the above interface implementation must | ||
be registered using `google::AddLogSink` which expects a pointer to the | ||
`google::LogSink` instance. To unregister use `google::RemoveLogSink`. Both | ||
functions are thread-safe. | ||
!!! danger "`LogSink` ownership" | ||
The `google::LogSink` instance must not be destroyed until the referencing | ||
pointer is unregistered. | ||
## Direct Logging | ||
Instead of registering the sink, we can directly use to log messages. While `#! | ||
LOG_TO_SINK(sink, severity)` allows to log both to the sink and to a global log | ||
registry, e.g., a file, `#!cpp LOG_TO_SINK_BUT_NOT_TO_LOGFILE(sink, severity)` | ||
will avoid the latter. | ||
!!! example "Using a custom sink" | ||
``` cpp title="custom_sink.cc" | ||
-8<- "examples/custom_sink.cc:33:" | ||
``` | ||
1. `MySink` implements a custom sink that sends log messages to `std::cout`. | ||
2. The custom sink must be registered to for use with existing logging | ||
macros. | ||
3. Once the custom sink is no longer needed we remove it from the registry. | ||
4. A sink does not need to be registered globally. However, then, messages | ||
must be logged using dedicated macros. | ||
Running the above example as `#!bash GLOG_log_dir=. ./custom_sink_example` | ||
will produce | ||
<div class="annotate" markdown> | ||
``` title="Custom sink output" | ||
INFO custom_sink.cc:63 logging to MySink | ||
INFO custom_sink.cc:68 direct logging | ||
INFO custom_sink.cc:69 direct logging but not to file (1) | ||
``` | ||
</div> | ||
1. This line is not present in the log file because we used | ||
`LOG_TO_SINK_BUT_NOT_TO_LOGFILE` to log the message. | ||
and the corresponding log file will contain | ||
``` title="Log file generated with the custom sink" | ||
Log file created at: 2024/06/11 13:24:27 | ||
Running on machine: pc | ||
Running duration (h:mm:ss): 0:00:00 | ||
Log line format: [IWEF]yyyymmdd hh:mm:ss.uuuuuu threadid file:line] msg | ||
I20240611 13:24:27.476620 126237946035776 custom_sink.cc:63] logging to MySink | ||
I20240611 13:24:27.476796 126237946035776 custom_sink.cc:68] direct logging | ||
``` |
This file contains 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,71 @@ | ||
// Copyright (c) 2024, Google Inc. | ||
// All rights reserved. | ||
// | ||
// Redistribution and use in source and binary forms, with or without | ||
// modification, are permitted provided that the following conditions are | ||
// met: | ||
// | ||
// * Redistributions of source code must retain the above copyright | ||
// notice, this list of conditions and the following disclaimer. | ||
// * Redistributions in binary form must reproduce the above | ||
// copyright notice, this list of conditions and the following disclaimer | ||
// in the documentation and/or other materials provided with the | ||
// distribution. | ||
// * Neither the name of Google Inc. nor the names of its | ||
// contributors may be used to endorse or promote products derived from | ||
// this software without specific prior written permission. | ||
// | ||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
// | ||
// Author: Sergiu Deitsch | ||
// | ||
|
||
#include <glog/logging.h> | ||
|
||
#include <algorithm> | ||
#include <cstddef> | ||
#include <iostream> | ||
#include <iterator> | ||
|
||
namespace { | ||
|
||
struct MyLogSink : google::LogSink { // (1)! | ||
void send(google::LogSeverity severity, const char* full_filename, | ||
const char* base_filename, int line, | ||
const google::LogMessageTime& time, const char* message, | ||
std::size_t message_len) override { | ||
std::cout << google::GetLogSeverityName(severity) << ' ' << base_filename | ||
<< ':' << line << ' '; | ||
std::copy_n(message, message_len, | ||
std::ostreambuf_iterator<char>{std::cout}); | ||
std::cout << '\n'; | ||
} | ||
}; | ||
|
||
} // namespace | ||
|
||
int main(int argc, char** argv) { | ||
google::InitGoogleLogging(argv[0]); | ||
|
||
MyLogSink sink; | ||
google::AddLogSink(&sink); // (2)! | ||
|
||
LOG(INFO) << "logging to MySink"; | ||
|
||
google::RemoveLogSink(&sink); // (3)! | ||
|
||
// We can directly log to a sink without registering it | ||
LOG_TO_SINK(&sink, INFO) << "direct logging"; // (4)! | ||
LOG_TO_SINK_BUT_NOT_TO_LOGFILE(&sink, INFO) | ||
<< "direct logging but not to file"; | ||
} |
This file contains 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