blob: 929f55e50a9c8e1a3f5c01c4e2fd59089a8eb136 [file] [log] [blame]
levin@chromium.org5c528682011-03-28 10:54:15 +09001// Copyright (c) 2011 The Chromium Authors. All rights reserved.
agl@chromium.org1c6dcf22009-07-23 08:57:21 +09002// 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
davidsz3bf38eb2017-05-12 18:19:23 +09008#include "ipc/ipc_features.h"
9
10#if BUILDFLAG(IPC_MESSAGE_LOG_ENABLED)
11
tfarina1cbfa082015-09-05 03:47:57 +090012#include <stdint.h>
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090013#include <vector>
14
brettw@chromium.org30230a82013-06-12 02:52:44 +090015#include "base/containers/hash_tables.h"
fdorayc10eab42016-06-21 05:23:32 +090016#include "base/memory/ref_counted.h"
levin@chromium.org5c528682011-03-28 10:54:15 +090017#include "base/memory/singleton.h"
fdorayc10eab42016-06-21 05:23:32 +090018#include "base/single_thread_task_runner.h"
darin@chromium.org80e4c5e2011-08-16 05:41:46 +090019#include "ipc/ipc_export.h"
davidsz3bf38eb2017-05-12 18:19:23 +090020#include "ipc/ipc_message.h"
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090021
jam@chromium.org86a8de12010-12-09 08:34:16 +090022// Logging function. |name| is a string in ASCII and |params| is a string in
23// UTF-8.
24typedef void (*LogFunction)(std::string* name,
25 const IPC::Message* msg,
26 std::string* params);
27
tfarina1cbfa082015-09-05 03:47:57 +090028typedef base::hash_map<uint32_t, LogFunction > LogFunctionMap;
jam@chromium.org86a8de12010-12-09 08:34:16 +090029
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090030namespace IPC {
31
32class Message;
viettrungluu@chromium.org5254ca02012-12-15 07:56:28 +090033class Sender;
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090034
35// One instance per process. Needs to be created on the main thread (the UI
36// thread in the browser) but OnPreDispatchMessage/OnPostDispatchMessage
37// can be called on other threads.
darin@chromium.org80e4c5e2011-08-16 05:41:46 +090038class IPC_EXPORT Logging {
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090039 public:
40 // Implemented by consumers of log messages.
41 class Consumer {
42 public:
43 virtual void Log(const LogData& data) = 0;
jamesr@chromium.orgab4c65e2009-12-16 10:01:25 +090044
45 protected:
46 virtual ~Consumer() {}
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090047 };
48
49 void SetConsumer(Consumer* consumer);
50
51 ~Logging();
satish@chromium.orgaa870602010-12-13 17:18:55 +090052 static Logging* GetInstance();
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090053
jrg@chromium.orgf2b21be2009-09-29 06:08:04 +090054 // Enable and Disable are NOT cross-process; they only affect the
55 // current thread/process. If you want to modify the value for all
56 // processes, perhaps your intent is to call
57 // g_browser_process->SetIPCLoggingEnabled().
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090058 void Enable();
59 void Disable();
60 bool Enabled() const { return enabled_; }
61
62 // Called by child processes to give the logger object the channel to send
63 // logging data to the browser process.
brettw@chromium.orgf947ed02012-06-12 07:35:26 +090064 void SetIPCSender(Sender* sender);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090065
66 // Called in the browser process when logging data from a child process is
67 // received.
68 void OnReceivedLoggingMessage(const Message& message);
69
sammc65f302f2016-11-15 09:34:36 +090070 void OnSendMessage(Message* message);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090071 void OnPreDispatchMessage(const Message& message);
sammc65f302f2016-11-15 09:34:36 +090072 void OnPostDispatchMessage(const Message& message);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090073
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090074 // Like the *MsgLog functions declared for each message class, except this
75 // calls the correct one based on the message type automatically. Defined in
76 // ipc_logging.cc.
tfarina1cbfa082015-09-05 03:47:57 +090077 static void GetMessageText(uint32_t type, std::string* name,
erg@google.com8aca7272010-08-19 03:33:57 +090078 const Message* message, std::string* params);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090079
jam@chromium.org86a8de12010-12-09 08:34:16 +090080 static void set_log_function_map(LogFunctionMap* functions) {
81 log_function_map_ = functions;
82 }
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090083
jam@chromium.org86a8de12010-12-09 08:34:16 +090084 static LogFunctionMap* log_function_map() {
85 return log_function_map_;
86 }
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090087
88 private:
scottbyer@chromium.orgf0cf0b72011-10-21 04:34:43 +090089 typedef enum {
90 ANSI_COLOR_RESET = -1,
91 ANSI_COLOR_BLACK,
92 ANSI_COLOR_RED,
93 ANSI_COLOR_GREEN,
94 ANSI_COLOR_YELLOW,
95 ANSI_COLOR_BLUE,
96 ANSI_COLOR_MAGENTA,
97 ANSI_COLOR_CYAN,
98 ANSI_COLOR_WHITE
99 } ANSIColor;
100 const char* ANSIEscape(ANSIColor color);
101 ANSIColor DelayColor(double delay);
102
olli.raula5ba94892015-09-10 20:14:22 +0900103 friend struct base::DefaultSingletonTraits<Logging>;
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900104 Logging();
105
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900106 void OnSendLogs();
107 void Log(const LogData& data);
108
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900109 bool enabled_;
jrg@chromium.orgf2b21be2009-09-29 06:08:04 +0900110 bool enabled_on_stderr_; // only used on POSIX for now
scottbyer@chromium.orgf0cf0b72011-10-21 04:34:43 +0900111 bool enabled_color_; // only used on POSIX for now
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900112
113 std::vector<LogData> queued_logs_;
114 bool queue_invoke_later_pending_;
115
brettw@chromium.orgf947ed02012-06-12 07:35:26 +0900116 Sender* sender_;
fdorayc10eab42016-06-21 05:23:32 +0900117 scoped_refptr<base::SingleThreadTaskRunner> main_thread_;
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900118
119 Consumer* consumer_;
120
jam@chromium.org86a8de12010-12-09 08:34:16 +0900121 static LogFunctionMap* log_function_map_;
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900122};
123
124} // namespace IPC
125
davidsz3bf38eb2017-05-12 18:19:23 +0900126#endif // BUILDFLAG(IPC_MESSAGE_LOG_ENABLED)
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900127
128#endif // IPC_IPC_LOGGING_H_