Thirumurugan | 7ea0a5e | 2024-10-16 15:44:03 | [diff] [blame] | 1 | // Copyright 2024 The Chromium Authors |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef BASE_LOGGING_RUST_LOG_INTEGRATION_H_ |
| 6 | #define BASE_LOGGING_RUST_LOG_INTEGRATION_H_ |
| 7 | |
Collin Baker | f0fd42e | 2025-03-21 16:47:39 | [diff] [blame] | 8 | #include <stddef.h> |
Collin Baker | 7cbc0d17 | 2025-03-19 21:02:01 | [diff] [blame] | 9 | #include <stdint.h> |
| 10 | |
Thirumurugan | 7ea0a5e | 2024-10-16 15:44:03 | [diff] [blame] | 11 | #include "base/base_export.h" |
Collin Baker | f0fd42e | 2025-03-21 16:47:39 | [diff] [blame] | 12 | #include "base/logging.h" |
Lukasz Anforowicz | 191a4d3 | 2024-11-12 01:48:08 | [diff] [blame] | 13 | #include "base/logging/log_severity.h" |
Collin Baker | f0fd42e | 2025-03-21 16:47:39 | [diff] [blame] | 14 | #include "third_party/rust/cxx/v1/cxx.h" |
Thirumurugan | 7ea0a5e | 2024-10-16 15:44:03 | [diff] [blame] | 15 | |
| 16 | namespace logging { |
| 17 | namespace internal { |
| 18 | |
Collin Baker | f0fd42e | 2025-03-21 16:47:39 | [diff] [blame] | 19 | // Opaquely wraps a Rust std::fmt::Arguments object, which can be turned into a |
| 20 | // string but must be done so from a Rust stack frame with the help of |
| 21 | // LogMessageRustWrapper below. |
| 22 | struct RustFmtArguments; |
| 23 | |
Lukasz Anforowicz | 191a4d3 | 2024-11-12 01:48:08 | [diff] [blame] | 24 | // Receives a log line from Rust and forwards it to base logging, because |
| 25 | // logging::LogMessage is not accessible from Rust yet with our current interop |
| 26 | // tools. |
| 27 | // |
| 28 | // TODO(danakj): Should this helper function be replaced with C-like apis next |
| 29 | // to logging::LogMessage that Rust uses more directly? |
Collin Baker | f0fd42e | 2025-03-21 16:47:39 | [diff] [blame] | 30 | void print_rust_log(const RustFmtArguments& msg, |
Collin Baker | e1e02fde | 2025-03-26 16:40:16 | [diff] [blame] | 31 | const char* file, |
Collin Baker | 7cbc0d17 | 2025-03-19 21:02:01 | [diff] [blame] | 32 | int32_t line, |
| 33 | int32_t severity, |
| 34 | bool verbose); |
Lukasz Anforowicz | 191a4d3 | 2024-11-12 01:48:08 | [diff] [blame] | 35 | |
Collin Baker | f0fd42e | 2025-03-21 16:47:39 | [diff] [blame] | 36 | // Wraps a LogMessage object so that Rust code can write to its ostream. |
| 37 | class LogMessageRustWrapper { |
| 38 | public: |
Collin Baker | e1e02fde | 2025-03-26 16:40:16 | [diff] [blame] | 39 | LogMessageRustWrapper(const char* file, |
Collin Baker | f0fd42e | 2025-03-21 16:47:39 | [diff] [blame] | 40 | int line, |
| 41 | ::logging::LogSeverity severity); |
| 42 | |
| 43 | void write_to_stream(rust::Str str); |
| 44 | |
| 45 | private: |
| 46 | ::logging::LogMessage log_message; |
| 47 | }; |
| 48 | |
Thirumurugan | 7ea0a5e | 2024-10-16 15:44:03 | [diff] [blame] | 49 | } // namespace internal |
| 50 | } // namespace logging |
| 51 | |
| 52 | #endif // BASE_LOGGING_RUST_LOG_INTEGRATION_H_ |