File: log_message_handler.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (75 lines) | stat: -rw-r--r-- 2,834 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef REMOTING_HOST_NATIVE_MESSAGING_LOG_MESSAGE_HANDLER_H_
#define REMOTING_HOST_NATIVE_MESSAGING_LOG_MESSAGE_HANDLER_H_

#include <stddef.h>

#include <memory>

#include "base/functional/callback.h"
#include "base/logging.h"
#include "base/memory/weak_ptr.h"
#include "base/values.h"

namespace base {
class SingleThreadTaskRunner;
}

namespace remoting {

// Helper class for logging::SetLogMessageHandler to deliver log messages to
// a consistent thread in a thread-safe way and in a format suitable for sending
// over a Native Messaging channel.
class LogMessageHandler {
 public:
  using Delegate = base::RepeatingCallback<void(base::Value::Dict message)>;

  explicit LogMessageHandler(const Delegate& delegate);
  ~LogMessageHandler();

  // When set to true, if a message is logged on the caller thread, the message
  // will be synchronously sent to the delegate; otherwise a task will always
  // be posted to the caller thread to handle the message. Defaults to false to
  // prevent blocking LOG calls. Set this to false if you want to make sure a
  // log gets handled when the caller sequence is about to be terminated.
  void set_log_synchronously_if_possible(bool log_synchronously_if_possible) {
    log_synchronously_if_possible_ = log_synchronously_if_possible;
  }

  static const char* kDebugMessageTypeName;

 private:
  // TODO(yuweih): Reimplement this class using using a message queue which is
  // protected by |g_log_message_handler_lock|.
  static bool OnLogMessage(int severity,
                           const char* file,
                           int line,
                           size_t message_start,
                           const std::string& str);
  void PostLogMessageToCorrectThread(int severity,
                                     const char* file,
                                     int line,
                                     size_t message_start,
                                     const std::string& str);
  void SendLogMessageToClient(int severity,
                              const char* file,
                              int line,
                              size_t message_start,
                              const std::string& str);

  Delegate delegate_;
  bool suppress_logging_;
  bool log_synchronously_if_possible_ = false;
  // TODO(yuweih): Replace all "thread" references in this class with
  // "sequence".
  scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner_;
  logging::LogMessageHandlerFunction previous_log_message_handler_;
  base::WeakPtrFactory<LogMessageHandler> weak_ptr_factory_{this};
};

}  // namespace remoting

#endif  // REMOTING_HOST_NATIVE_MESSAGING_LOG_MESSAGE_HANDLER_H_