Roll src/third_party/WebKit 7d313db:88b90a6 (svn 202266:202267)
[chromium-blink-merge.git] / ipc / ipc_logging.h
blob2d12cb4c6641b50b01c8041273b0b3c68c2789d0
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
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 <stdint.h>
10 #include "ipc/ipc_message.h" // For IPC_MESSAGE_LOG_ENABLED.
12 #ifdef IPC_MESSAGE_LOG_ENABLED
14 #include <vector>
16 #include "base/containers/hash_tables.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/memory/singleton.h"
19 #include "base/message_loop/message_loop.h"
20 #include "ipc/ipc_export.h"
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);
28 typedef base::hash_map<uint32_t, LogFunction > LogFunctionMap;
30 namespace IPC {
32 class Message;
33 class Sender;
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.
38 class IPC_EXPORT Logging {
39 public:
40 // Implemented by consumers of log messages.
41 class Consumer {
42 public:
43 virtual void Log(const LogData& data) = 0;
45 protected:
46 virtual ~Consumer() {}
49 void SetConsumer(Consumer* consumer);
51 ~Logging();
52 static Logging* GetInstance();
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().
58 void Enable();
59 void Disable();
60 bool Enabled() const { return enabled_; }
62 // Called by child processes to give the logger object the channel to send
63 // logging data to the browser process.
64 void SetIPCSender(Sender* sender);
66 // Called in the browser process when logging data from a child process is
67 // received.
68 void OnReceivedLoggingMessage(const Message& message);
70 void OnSendMessage(Message* message, const std::string& channel_id);
71 void OnPreDispatchMessage(const Message& message);
72 void OnPostDispatchMessage(const Message& message,
73 const std::string& channel_id);
75 // Like the *MsgLog functions declared for each message class, except this
76 // calls the correct one based on the message type automatically. Defined in
77 // ipc_logging.cc.
78 static void GetMessageText(uint32_t type, std::string* name,
79 const Message* message, std::string* params);
81 static void set_log_function_map(LogFunctionMap* functions) {
82 log_function_map_ = functions;
85 static LogFunctionMap* log_function_map() {
86 return log_function_map_;
89 private:
90 typedef enum {
91 ANSI_COLOR_RESET = -1,
92 ANSI_COLOR_BLACK,
93 ANSI_COLOR_RED,
94 ANSI_COLOR_GREEN,
95 ANSI_COLOR_YELLOW,
96 ANSI_COLOR_BLUE,
97 ANSI_COLOR_MAGENTA,
98 ANSI_COLOR_CYAN,
99 ANSI_COLOR_WHITE
100 } ANSIColor;
101 const char* ANSIEscape(ANSIColor color);
102 ANSIColor DelayColor(double delay);
104 friend struct base::DefaultSingletonTraits<Logging>;
105 Logging();
107 void OnSendLogs();
108 void Log(const LogData& data);
110 bool enabled_;
111 bool enabled_on_stderr_; // only used on POSIX for now
112 bool enabled_color_; // only used on POSIX for now
114 std::vector<LogData> queued_logs_;
115 bool queue_invoke_later_pending_;
117 Sender* sender_;
118 base::MessageLoop* main_thread_;
120 Consumer* consumer_;
122 static LogFunctionMap* log_function_map_;
125 } // namespace IPC
127 #endif // IPC_MESSAGE_LOG_ENABLED
129 #endif // IPC_IPC_LOGGING_H_