levin@chromium.org | 5c52868 | 2011-03-28 10:54:15 +0900 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 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 "ipc/ipc_message.h" // For IPC_MESSAGE_LOG_ENABLED. |
| 9 | |
| 10 | #ifdef IPC_MESSAGE_LOG_ENABLED |
| 11 | |
| 12 | #include <vector> |
| 13 | |
brettw@chromium.org | 30230a8 | 2013-06-12 02:52:44 +0900 | [diff] [blame] | 14 | #include "base/containers/hash_tables.h" |
levin@chromium.org | 5c52868 | 2011-03-28 10:54:15 +0900 | [diff] [blame] | 15 | #include "base/memory/scoped_ptr.h" |
| 16 | #include "base/memory/singleton.h" |
avi@chromium.org | a29af56 | 2013-07-18 08:00:30 +0900 | [diff] [blame] | 17 | #include "base/message_loop/message_loop.h" |
darin@chromium.org | 80e4c5e | 2011-08-16 05:41:46 +0900 | [diff] [blame] | 18 | #include "ipc/ipc_export.h" |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 19 | |
jam@chromium.org | 86a8de1 | 2010-12-09 08:34:16 +0900 | [diff] [blame] | 20 | // Logging function. |name| is a string in ASCII and |params| is a string in |
| 21 | // UTF-8. |
| 22 | typedef void (*LogFunction)(std::string* name, |
| 23 | const IPC::Message* msg, |
| 24 | std::string* params); |
| 25 | |
| 26 | typedef base::hash_map<uint32, LogFunction > LogFunctionMap; |
| 27 | |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 28 | namespace IPC { |
| 29 | |
| 30 | class Message; |
viettrungluu@chromium.org | 5254ca0 | 2012-12-15 07:56:28 +0900 | [diff] [blame] | 31 | class Sender; |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 32 | |
| 33 | // One instance per process. Needs to be created on the main thread (the UI |
| 34 | // thread in the browser) but OnPreDispatchMessage/OnPostDispatchMessage |
| 35 | // can be called on other threads. |
darin@chromium.org | 80e4c5e | 2011-08-16 05:41:46 +0900 | [diff] [blame] | 36 | class IPC_EXPORT Logging { |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 37 | public: |
| 38 | // Implemented by consumers of log messages. |
| 39 | class Consumer { |
| 40 | public: |
| 41 | virtual void Log(const LogData& data) = 0; |
jamesr@chromium.org | ab4c65e | 2009-12-16 10:01:25 +0900 | [diff] [blame] | 42 | |
| 43 | protected: |
| 44 | virtual ~Consumer() {} |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | void SetConsumer(Consumer* consumer); |
| 48 | |
| 49 | ~Logging(); |
satish@chromium.org | aa87060 | 2010-12-13 17:18:55 +0900 | [diff] [blame] | 50 | static Logging* GetInstance(); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 51 | |
jrg@chromium.org | f2b21be | 2009-09-29 06:08:04 +0900 | [diff] [blame] | 52 | // Enable and Disable are NOT cross-process; they only affect the |
| 53 | // current thread/process. If you want to modify the value for all |
| 54 | // processes, perhaps your intent is to call |
| 55 | // g_browser_process->SetIPCLoggingEnabled(). |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 56 | void Enable(); |
| 57 | void Disable(); |
| 58 | bool Enabled() const { return enabled_; } |
| 59 | |
| 60 | // Called by child processes to give the logger object the channel to send |
| 61 | // logging data to the browser process. |
brettw@chromium.org | f947ed0 | 2012-06-12 07:35:26 +0900 | [diff] [blame] | 62 | void SetIPCSender(Sender* sender); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 63 | |
| 64 | // Called in the browser process when logging data from a child process is |
| 65 | // received. |
| 66 | void OnReceivedLoggingMessage(const Message& message); |
| 67 | |
| 68 | void OnSendMessage(Message* message, const std::string& channel_id); |
| 69 | void OnPreDispatchMessage(const Message& message); |
| 70 | void OnPostDispatchMessage(const Message& message, |
| 71 | const std::string& channel_id); |
| 72 | |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 73 | // Like the *MsgLog functions declared for each message class, except this |
| 74 | // calls the correct one based on the message type automatically. Defined in |
| 75 | // ipc_logging.cc. |
erg@google.com | 8aca727 | 2010-08-19 03:33:57 +0900 | [diff] [blame] | 76 | static void GetMessageText(uint32 type, std::string* name, |
| 77 | const Message* message, std::string* params); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 78 | |
jam@chromium.org | 86a8de1 | 2010-12-09 08:34:16 +0900 | [diff] [blame] | 79 | static void set_log_function_map(LogFunctionMap* functions) { |
| 80 | log_function_map_ = functions; |
| 81 | } |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 82 | |
jam@chromium.org | 86a8de1 | 2010-12-09 08:34:16 +0900 | [diff] [blame] | 83 | static LogFunctionMap* log_function_map() { |
| 84 | return log_function_map_; |
| 85 | } |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 86 | |
| 87 | private: |
scottbyer@chromium.org | f0cf0b7 | 2011-10-21 04:34:43 +0900 | [diff] [blame] | 88 | typedef enum { |
| 89 | ANSI_COLOR_RESET = -1, |
| 90 | ANSI_COLOR_BLACK, |
| 91 | ANSI_COLOR_RED, |
| 92 | ANSI_COLOR_GREEN, |
| 93 | ANSI_COLOR_YELLOW, |
| 94 | ANSI_COLOR_BLUE, |
| 95 | ANSI_COLOR_MAGENTA, |
| 96 | ANSI_COLOR_CYAN, |
| 97 | ANSI_COLOR_WHITE |
| 98 | } ANSIColor; |
| 99 | const char* ANSIEscape(ANSIColor color); |
| 100 | ANSIColor DelayColor(double delay); |
| 101 | |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 102 | friend struct DefaultSingletonTraits<Logging>; |
| 103 | Logging(); |
| 104 | |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 105 | void OnSendLogs(); |
| 106 | void Log(const LogData& data); |
| 107 | |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 108 | bool enabled_; |
jrg@chromium.org | f2b21be | 2009-09-29 06:08:04 +0900 | [diff] [blame] | 109 | bool enabled_on_stderr_; // only used on POSIX for now |
scottbyer@chromium.org | f0cf0b7 | 2011-10-21 04:34:43 +0900 | [diff] [blame] | 110 | bool enabled_color_; // only used on POSIX for now |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 111 | |
| 112 | std::vector<LogData> queued_logs_; |
| 113 | bool queue_invoke_later_pending_; |
| 114 | |
brettw@chromium.org | f947ed0 | 2012-06-12 07:35:26 +0900 | [diff] [blame] | 115 | Sender* sender_; |
xhwang@chromium.org | 0b2c2a5 | 2013-05-01 05:55:03 +0900 | [diff] [blame] | 116 | base::MessageLoop* main_thread_; |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 117 | |
| 118 | Consumer* consumer_; |
| 119 | |
jam@chromium.org | 86a8de1 | 2010-12-09 08:34:16 +0900 | [diff] [blame] | 120 | static LogFunctionMap* log_function_map_; |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 121 | }; |
| 122 | |
| 123 | } // namespace IPC |
| 124 | |
| 125 | #endif // IPC_MESSAGE_LOG_ENABLED |
| 126 | |
| 127 | #endif // IPC_IPC_LOGGING_H_ |