xref: /aosp_15_r20/external/cronet/ipc/ipc_logging.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2011 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 IPC_IPC_LOGGING_H_
6 #define IPC_IPC_LOGGING_H_
7 
8 #include "base/task/single_thread_task_runner.h"
9 #include "ipc/ipc_buildflags.h"
10 
11 #if BUILDFLAG(IPC_MESSAGE_LOG_ENABLED)
12 
13 #include <stdint.h>
14 #include <unordered_map>
15 #include <vector>
16 
17 #include "base/component_export.h"
18 #include "base/memory/raw_ptr.h"
19 #include "base/memory/ref_counted.h"
20 #include "base/memory/singleton.h"
21 #include "base/task/single_thread_task_runner.h"
22 #include "ipc/ipc_message.h"
23 
24 // Logging function. |name| is a string in ASCII and |params| is a string in
25 // UTF-8.
26 typedef void (*LogFunction)(std::string* name,
27                             const IPC::Message* msg,
28                             std::string* params);
29 
30 typedef std::unordered_map<uint32_t, LogFunction> LogFunctionMap;
31 
32 namespace IPC {
33 
34 class Message;
35 class Sender;
36 
37 // One instance per process.  Needs to be created on the main thread (the UI
38 // thread in the browser) but OnPreDispatchMessage/OnPostDispatchMessage
39 // can be called on other threads.
COMPONENT_EXPORT(IPC)40 class COMPONENT_EXPORT(IPC) Logging {
41  public:
42   // Implemented by consumers of log messages.
43   class Consumer {
44    public:
45     virtual void Log(const LogData& data) = 0;
46 
47    protected:
48     virtual ~Consumer() {}
49   };
50 
51   void SetConsumer(Consumer* consumer);
52 
53   ~Logging();
54   static Logging* GetInstance();
55 
56   // Enable and Disable are NOT cross-process; they only affect the
57   // current thread/process.  If you want to modify the value for all
58   // processes, perhaps your intent is to call
59   // g_browser_process->SetIPCLoggingEnabled().
60   void Enable();
61   void Disable();
62   bool Enabled() const { return enabled_; }
63 
64   // Called by child processes to give the logger object the channel to send
65   // logging data to the browser process.
66   void SetIPCSender(Sender* sender);
67 
68   // Called in the browser process when logging data from a child process is
69   // received.
70   void OnReceivedLoggingMessage(const Message& message);
71 
72   void OnSendMessage(Message* message);
73   void OnPreDispatchMessage(const Message& message);
74   void OnPostDispatchMessage(const Message& message);
75 
76   // Like the *MsgLog functions declared for each message class, except this
77   // calls the correct one based on the message type automatically.  Defined in
78   // ipc_logging.cc.
79   static void GetMessageText(uint32_t type, std::string* name,
80                              const Message* message, std::string* params);
81 
82   static void set_log_function_map(LogFunctionMap* functions) {
83     log_function_map_ = functions;
84   }
85 
86   static LogFunctionMap* log_function_map() {
87     return log_function_map_;
88   }
89 
90  private:
91   typedef enum {
92     ANSI_COLOR_RESET = -1,
93     ANSI_COLOR_BLACK,
94     ANSI_COLOR_RED,
95     ANSI_COLOR_GREEN,
96     ANSI_COLOR_YELLOW,
97     ANSI_COLOR_BLUE,
98     ANSI_COLOR_MAGENTA,
99     ANSI_COLOR_CYAN,
100     ANSI_COLOR_WHITE
101   } ANSIColor;
102   const char* ANSIEscape(ANSIColor color);
103   ANSIColor DelayColor(double delay);
104 
105   friend struct base::DefaultSingletonTraits<Logging>;
106   Logging();
107 
108   void OnSendLogs();
109   void Log(const LogData& data);
110 
111   bool enabled_;
112   bool enabled_on_stderr_;  // only used on POSIX for now
113   bool enabled_color_; // only used on POSIX for now
114 
115   std::vector<LogData> queued_logs_;
116   bool queue_invoke_later_pending_;
117 
118   raw_ptr<Sender> sender_;
119   scoped_refptr<base::SingleThreadTaskRunner> main_thread_;
120 
121   raw_ptr<Consumer> consumer_;
122 
123   static LogFunctionMap* log_function_map_;
124 };
125 
126 }  // namespace IPC
127 
128 #endif  // BUILDFLAG(IPC_MESSAGE_LOG_ENABLED)
129 
130 #endif  // IPC_IPC_LOGGING_H_
131