[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 5 | #ifndef IPC_IPC_LOGGING_H_ |
| 6 | #define IPC_IPC_LOGGING_H_ |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 7 | |
davidsz | 041528a | 2017-05-12 09:19:23 | [diff] [blame] | 8 | #include "ipc/ipc_features.h" |
| 9 | |
| 10 | #if BUILDFLAG(IPC_MESSAGE_LOG_ENABLED) |
| 11 | |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 12 | #include <stdint.h> |
[email protected] | 7bf73095 | 2009-05-29 09:31:15 | [diff] [blame] | 13 | #include <vector> |
| 14 | |
[email protected] | 14c1c23 | 2013-06-11 17:52:44 | [diff] [blame] | 15 | #include "base/containers/hash_tables.h" |
fdoray | bcb9b32 | 2016-06-20 20:23:32 | [diff] [blame] | 16 | #include "base/memory/ref_counted.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 17 | #include "base/memory/singleton.h" |
fdoray | bcb9b32 | 2016-06-20 20:23:32 | [diff] [blame] | 18 | #include "base/single_thread_task_runner.h" |
[email protected] | 7c85437 | 2011-08-15 20:41:46 | [diff] [blame] | 19 | #include "ipc/ipc_export.h" |
davidsz | 041528a | 2017-05-12 09:19:23 | [diff] [blame] | 20 | #include "ipc/ipc_message.h" |
[email protected] | cee1dfa | 2008-08-14 09:21:50 | [diff] [blame] | 21 | |
[email protected] | 21fa3a1 | 2010-12-08 23:34:16 | [diff] [blame] | 22 | // Logging function. |name| is a string in ASCII and |params| is a string in |
| 23 | // UTF-8. |
| 24 | typedef void (*LogFunction)(std::string* name, |
| 25 | const IPC::Message* msg, |
| 26 | std::string* params); |
| 27 | |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 28 | typedef base::hash_map<uint32_t, LogFunction > LogFunctionMap; |
[email protected] | 21fa3a1 | 2010-12-08 23:34:16 | [diff] [blame] | 29 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 30 | namespace IPC { |
| 31 | |
| 32 | class Message; |
[email protected] | 510344fc | 2012-12-14 22:56:28 | [diff] [blame] | 33 | class Sender; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 34 | |
| 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. |
[email protected] | 7c85437 | 2011-08-15 20:41:46 | [diff] [blame] | 38 | class IPC_EXPORT Logging { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 39 | public: |
| 40 | // Implemented by consumers of log messages. |
| 41 | class Consumer { |
| 42 | public: |
[email protected] | f91cb99 | 2009-02-04 20:10:12 | [diff] [blame] | 43 | virtual void Log(const LogData& data) = 0; |
[email protected] | 20cb5f48 | 2009-12-16 01:01:25 | [diff] [blame] | 44 | |
| 45 | protected: |
| 46 | virtual ~Consumer() {} |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | void SetConsumer(Consumer* consumer); |
| 50 | |
| 51 | ~Logging(); |
[email protected] | 8e8bb6d | 2010-12-13 08:18:55 | [diff] [blame] | 52 | static Logging* GetInstance(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 53 | |
[email protected] | d55aaa13 | 2009-09-28 21:08:04 | [diff] [blame] | 54 | // 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(). |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 58 | void Enable(); |
| 59 | void Disable(); |
[email protected] | e707d5e6 | 2009-02-12 04:00:08 | [diff] [blame] | 60 | bool Enabled() const { return enabled_; } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 61 | |
| 62 | // Called by child processes to give the logger object the channel to send |
| 63 | // logging data to the browser process. |
[email protected] | 57319ce | 2012-06-11 22:35:26 | [diff] [blame] | 64 | void SetIPCSender(Sender* sender); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 65 | |
| 66 | // Called in the browser process when logging data from a child process is |
| 67 | // received. |
| 68 | void OnReceivedLoggingMessage(const Message& message); |
| 69 | |
sammc | a1b3b4d | 2016-11-15 00:34:36 | [diff] [blame] | 70 | void OnSendMessage(Message* message); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 71 | void OnPreDispatchMessage(const Message& message); |
sammc | a1b3b4d | 2016-11-15 00:34:36 | [diff] [blame] | 72 | void OnPostDispatchMessage(const Message& message); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 73 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 74 | // 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. |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 77 | static void GetMessageText(uint32_t type, std::string* name, |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 78 | const Message* message, std::string* params); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 79 | |
[email protected] | 21fa3a1 | 2010-12-08 23:34:16 | [diff] [blame] | 80 | static void set_log_function_map(LogFunctionMap* functions) { |
| 81 | log_function_map_ = functions; |
| 82 | } |
[email protected] | 3335d87 | 2009-02-11 05:38:41 | [diff] [blame] | 83 | |
[email protected] | 21fa3a1 | 2010-12-08 23:34:16 | [diff] [blame] | 84 | static LogFunctionMap* log_function_map() { |
| 85 | return log_function_map_; |
| 86 | } |
[email protected] | d5dfa5a | 2009-02-10 19:38:55 | [diff] [blame] | 87 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 88 | private: |
[email protected] | ea7744a | 2011-10-20 19:34:43 | [diff] [blame] | 89 | 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.raula | 36aa8be | 2015-09-10 11:14:22 | [diff] [blame] | 103 | friend struct base::DefaultSingletonTraits<Logging>; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 104 | Logging(); |
| 105 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 106 | void OnSendLogs(); |
| 107 | void Log(const LogData& data); |
| 108 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 109 | bool enabled_; |
[email protected] | d55aaa13 | 2009-09-28 21:08:04 | [diff] [blame] | 110 | bool enabled_on_stderr_; // only used on POSIX for now |
[email protected] | ea7744a | 2011-10-20 19:34:43 | [diff] [blame] | 111 | bool enabled_color_; // only used on POSIX for now |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 112 | |
| 113 | std::vector<LogData> queued_logs_; |
| 114 | bool queue_invoke_later_pending_; |
| 115 | |
[email protected] | 57319ce | 2012-06-11 22:35:26 | [diff] [blame] | 116 | Sender* sender_; |
fdoray | bcb9b32 | 2016-06-20 20:23:32 | [diff] [blame] | 117 | scoped_refptr<base::SingleThreadTaskRunner> main_thread_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 118 | |
| 119 | Consumer* consumer_; |
[email protected] | d5dfa5a | 2009-02-10 19:38:55 | [diff] [blame] | 120 | |
[email protected] | 21fa3a1 | 2010-12-08 23:34:16 | [diff] [blame] | 121 | static LogFunctionMap* log_function_map_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 122 | }; |
| 123 | |
[email protected] | 3178f4e2 | 2008-08-05 21:20:41 | [diff] [blame] | 124 | } // namespace IPC |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 125 | |
davidsz | 041528a | 2017-05-12 09:19:23 | [diff] [blame] | 126 | #endif // BUILDFLAG(IPC_MESSAGE_LOG_ENABLED) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 127 | |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 128 | #endif // IPC_IPC_LOGGING_H_ |