• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 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_FUCHSIA_TEST_LOG_LISTENER_SAFE_H_
6 #define BASE_FUCHSIA_TEST_LOG_LISTENER_SAFE_H_
7 
8 #include <fidl/fuchsia.logger/cpp/fidl.h>
9 
10 #include <lib/async/default.h>
11 #include <lib/fidl/cpp/binding.h>
12 #include <lib/zx/time.h>
13 
14 #include <memory>
15 #include <string>
16 #include <vector>
17 
18 #include "base/containers/circular_deque.h"
19 #include "base/functional/callback.h"
20 #include "base/strings/string_piece.h"
21 #include "third_party/abseil-cpp/absl/types/optional.h"
22 
23 namespace base {
24 
25 // LogListenerSafe implementation that invokes a caller-supplied callback for
26 // each received message.
27 // Note that messages will be delivered in order of receipt from the system
28 // logger, starting with any recent messages that the logging service had
29 // cached, i.e. including messages that may pre-date this log-listener being
30 // created.
31 class TestLogListenerSafe final
32     : public fidl::Server<fuchsia_logger::LogListenerSafe> {
33  public:
34   using OnLogMessageCallback =
35       base::RepeatingCallback<void(const fuchsia_logger::LogMessage&)>;
36 
37   TestLogListenerSafe();
38   ~TestLogListenerSafe() override;
39 
40   TestLogListenerSafe(const TestLogListenerSafe&) = delete;
41   TestLogListenerSafe& operator=(const TestLogListenerSafe&) = delete;
42 
43   // Sets a callback to be invoked with every message received via Log().
44   void set_on_log_message(OnLogMessageCallback callback);
45 
46  private:
47   // LogListenerSafe implementation.
48   void Log(LogRequest& request, LogCompleter::Sync& completer) override;
49   void LogMany(LogManyRequest& request,
50                LogManyCompleter::Sync& completer) override;
51   void Done(DoneCompleter::Sync& completer) override;
52 
53   OnLogMessageCallback on_log_message_;
54 };
55 
56 // Helper that manages a TestLogListenerSafe to simplify running the message
57 // loop until specific messages are received.
58 // Messages received prior to ListenToLog() being called will be silently
59 // ignored.
60 class SimpleTestLogListener {
61  public:
62   SimpleTestLogListener();
63   ~SimpleTestLogListener();
64 
65   SimpleTestLogListener(const SimpleTestLogListener&) = delete;
66   SimpleTestLogListener& operator=(const SimpleTestLogListener&) = delete;
67 
68   // Attaches this instance to receive data matching `options`, from `log`.
69   void ListenToLog(const fidl::Client<fuchsia_logger::Log>& log,
70                    std::unique_ptr<fuchsia_logger::LogFilterOptions> options);
71 
72   // Runs the message loop until a log message containing `expected_string` is
73   // received, and returns it. Returns `absl::nullopt` if `binding_` disconnects
74   // without the `expected_string` having been logged.
75   absl::optional<fuchsia_logger::LogMessage> RunUntilMessageReceived(
76       base::StringPiece expected_string);
77 
78  private:
79   // Pushes `message` to the `logged_messages_` queue, or to `on_log_message_`.
80   void PushLoggedMessage(const fuchsia_logger::LogMessage& message);
81 
82   // Used to ignore messages with timestamps prior to this listener's creation.
83   zx::time ignore_before_;
84 
85   TestLogListenerSafe listener_;
86   absl::optional<fidl::ServerBinding<fuchsia_logger::LogListenerSafe>> binding_;
87 
88   base::circular_deque<fuchsia_logger::LogMessage> logged_messages_;
89   TestLogListenerSafe::OnLogMessageCallback on_log_message_;
90 };
91 
92 // Configures `listener` to listen for messages from the current process.
93 void ListenFilteredByCurrentProcessId(SimpleTestLogListener& listener);
94 
95 }  // namespace base
96 
97 #endif  // BASE_FUCHSIA_TEST_LOG_LISTENER_SAFE_H_
98