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.
5 #ifndef IPC_IPC_LOGGING_H_
6 #define IPC_IPC_LOGGING_H_
8 #include "base/task/single_thread_task_runner.h"
9 #include "ipc/ipc_buildflags.h"
11 #if BUILDFLAG(IPC_MESSAGE_LOG_ENABLED)
14 #include <unordered_map>
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"
24 // Logging function. |name| is a string in ASCII and |params| is a string in
26 typedef void (*LogFunction)(std::string* name,
27 const IPC::Message* msg,
30 typedef std::unordered_map<uint32_t, LogFunction> LogFunctionMap;
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.
40 class COMPONENT_EXPORT(IPC) Logging {
42 // Implemented by consumers of log messages.
45 virtual void Log(const LogData& data) = 0;
48 virtual ~Consumer() {}
51 void SetConsumer(Consumer* consumer);
54 static Logging* GetInstance();
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().
62 bool Enabled() const { return enabled_; }
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);
68 // Called in the browser process when logging data from a child process is
70 void OnReceivedLoggingMessage(const Message& message);
72 void OnSendMessage(Message* message);
73 void OnPreDispatchMessage(const Message& message);
74 void OnPostDispatchMessage(const Message& message);
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
79 static void GetMessageText(uint32_t type, std::string* name,
80 const Message* message, std::string* params);
82 static void set_log_function_map(LogFunctionMap* functions) {
83 log_function_map_ = functions;
86 static LogFunctionMap* log_function_map() {
87 return log_function_map_;
92 ANSI_COLOR_RESET = -1,
102 const char* ANSIEscape(ANSIColor color);
103 ANSIColor DelayColor(double delay);
105 friend struct base::DefaultSingletonTraits<Logging>;
109 void Log(const LogData& data);
112 bool enabled_on_stderr_; // only used on POSIX for now
113 bool enabled_color_; // only used on POSIX for now
115 std::vector<LogData> queued_logs_;
116 bool queue_invoke_later_pending_;
118 raw_ptr<Sender> sender_;
119 scoped_refptr<base::SingleThreadTaskRunner> main_thread_;
121 raw_ptr<Consumer> consumer_;
123 static LogFunctionMap* log_function_map_;
128 #endif // BUILDFLAG(IPC_MESSAGE_LOG_ENABLED)
130 #endif // IPC_IPC_LOGGING_H_