blob: e1f822de66c4c83823ad646693b06a9b5d2a08da [file] [log] [blame]
Thirumurugan7ea0a5e2024-10-16 15:44:031// 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 Bakerf0fd42e2025-03-21 16:47:398#include <stddef.h>
Collin Baker7cbc0d172025-03-19 21:02:019#include <stdint.h>
10
Thirumurugan7ea0a5e2024-10-16 15:44:0311#include "base/base_export.h"
Collin Bakerf0fd42e2025-03-21 16:47:3912#include "base/logging.h"
Lukasz Anforowicz191a4d32024-11-12 01:48:0813#include "base/logging/log_severity.h"
Collin Bakerf0fd42e2025-03-21 16:47:3914#include "third_party/rust/cxx/v1/cxx.h"
Thirumurugan7ea0a5e2024-10-16 15:44:0315
16namespace logging {
17namespace internal {
18
Collin Bakerf0fd42e2025-03-21 16:47:3919// 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.
22struct RustFmtArguments;
23
Lukasz Anforowicz191a4d32024-11-12 01:48:0824// 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 Bakerf0fd42e2025-03-21 16:47:3930void print_rust_log(const RustFmtArguments& msg,
Collin Bakere1e02fde2025-03-26 16:40:1631 const char* file,
Collin Baker7cbc0d172025-03-19 21:02:0132 int32_t line,
33 int32_t severity,
34 bool verbose);
Lukasz Anforowicz191a4d32024-11-12 01:48:0835
Collin Bakerf0fd42e2025-03-21 16:47:3936// Wraps a LogMessage object so that Rust code can write to its ostream.
37class LogMessageRustWrapper {
38 public:
Collin Bakere1e02fde2025-03-26 16:40:1639 LogMessageRustWrapper(const char* file,
Collin Bakerf0fd42e2025-03-21 16:47:3940 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
Thirumurugan7ea0a5e2024-10-16 15:44:0349} // namespace internal
50} // namespace logging
51
52#endif // BASE_LOGGING_RUST_LOG_INTEGRATION_H_