blob: 43a13946dced6db1151cee5073022884a65ffbdb [file] [log] [blame]
[email protected]3b63f8f42011-03-28 01:54:151// Copyright (c) 2011 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]946d1b22009-07-22 23:57:215#ifndef IPC_IPC_LOGGING_H_
6#define IPC_IPC_LOGGING_H_
[email protected]32b76ef2010-07-26 23:08:247#pragma once
initial.commit09911bf2008-07-26 23:55:298
[email protected]946d1b22009-07-22 23:57:219#include "ipc/ipc_message.h" // For IPC_MESSAGE_LOG_ENABLED.
initial.commit09911bf2008-07-26 23:55:2910
11#ifdef IPC_MESSAGE_LOG_ENABLED
12
[email protected]7bf730952009-05-29 09:31:1513#include <vector>
14
[email protected]21fa3a12010-12-08 23:34:1615#include "base/hash_tables.h"
[email protected]3b63f8f42011-03-28 01:54:1516#include "base/memory/scoped_ptr.h"
17#include "base/memory/singleton.h"
[email protected]100897a2009-02-26 02:27:1118#include "base/message_loop.h"
[email protected]7c854372011-08-15 20:41:4619#include "ipc/ipc_export.h"
[email protected]cee1dfa2008-08-14 09:21:5020
[email protected]21fa3a12010-12-08 23:34:1621// Logging function. |name| is a string in ASCII and |params| is a string in
22// UTF-8.
23typedef void (*LogFunction)(std::string* name,
24 const IPC::Message* msg,
25 std::string* params);
26
27typedef base::hash_map<uint32, LogFunction > LogFunctionMap;
28
initial.commit09911bf2008-07-26 23:55:2929namespace IPC {
30
31class Message;
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.
[email protected]7c854372011-08-15 20:41:4636class IPC_EXPORT Logging {
initial.commit09911bf2008-07-26 23:55:2937 public:
38 // Implemented by consumers of log messages.
39 class Consumer {
40 public:
[email protected]f91cb992009-02-04 20:10:1241 virtual void Log(const LogData& data) = 0;
[email protected]20cb5f482009-12-16 01:01:2542
43 protected:
44 virtual ~Consumer() {}
initial.commit09911bf2008-07-26 23:55:2945 };
46
47 void SetConsumer(Consumer* consumer);
48
49 ~Logging();
[email protected]8e8bb6d2010-12-13 08:18:5550 static Logging* GetInstance();
initial.commit09911bf2008-07-26 23:55:2951
[email protected]d55aaa132009-09-28 21:08:0452 // 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().
initial.commit09911bf2008-07-26 23:55:2956 void Enable();
57 void Disable();
[email protected]e707d5e62009-02-12 04:00:0858 bool Enabled() const { return enabled_; }
initial.commit09911bf2008-07-26 23:55:2959
60 // Called by child processes to give the logger object the channel to send
61 // logging data to the browser process.
[email protected]57319ce2012-06-11 22:35:2662 void SetIPCSender(Sender* sender);
initial.commit09911bf2008-07-26 23:55:2963
64 // Called in the browser process when logging data from a child process is
65 // received.
66 void OnReceivedLoggingMessage(const Message& message);
67
[email protected]9a3a293b2009-06-04 22:28:1668 void OnSendMessage(Message* message, const std::string& channel_id);
initial.commit09911bf2008-07-26 23:55:2969 void OnPreDispatchMessage(const Message& message);
70 void OnPostDispatchMessage(const Message& message,
[email protected]9a3a293b2009-06-04 22:28:1671 const std::string& channel_id);
initial.commit09911bf2008-07-26 23:55:2972
initial.commit09911bf2008-07-26 23:55:2973 // 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.
[email protected]252cad62010-08-18 18:33:5776 static void GetMessageText(uint32 type, std::string* name,
77 const Message* message, std::string* params);
initial.commit09911bf2008-07-26 23:55:2978
[email protected]21fa3a12010-12-08 23:34:1679 static void set_log_function_map(LogFunctionMap* functions) {
80 log_function_map_ = functions;
81 }
[email protected]3335d872009-02-11 05:38:4182
[email protected]21fa3a12010-12-08 23:34:1683 static LogFunctionMap* log_function_map() {
84 return log_function_map_;
85 }
[email protected]d5dfa5a2009-02-10 19:38:5586
initial.commit09911bf2008-07-26 23:55:2987 private:
[email protected]ea7744a2011-10-20 19:34:4388 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
[email protected]f91cb992009-02-04 20:10:12102 friend struct DefaultSingletonTraits<Logging>;
initial.commit09911bf2008-07-26 23:55:29103 Logging();
104
initial.commit09911bf2008-07-26 23:55:29105 void OnSendLogs();
106 void Log(const LogData& data);
107
initial.commit09911bf2008-07-26 23:55:29108 bool enabled_;
[email protected]d55aaa132009-09-28 21:08:04109 bool enabled_on_stderr_; // only used on POSIX for now
[email protected]ea7744a2011-10-20 19:34:43110 bool enabled_color_; // only used on POSIX for now
initial.commit09911bf2008-07-26 23:55:29111
112 std::vector<LogData> queued_logs_;
113 bool queue_invoke_later_pending_;
114
[email protected]57319ce2012-06-11 22:35:26115 Sender* sender_;
initial.commit09911bf2008-07-26 23:55:29116 MessageLoop* main_thread_;
117
118 Consumer* consumer_;
[email protected]d5dfa5a2009-02-10 19:38:55119
[email protected]21fa3a12010-12-08 23:34:16120 static LogFunctionMap* log_function_map_;
initial.commit09911bf2008-07-26 23:55:29121};
122
[email protected]3178f4e22008-08-05 21:20:41123} // namespace IPC
initial.commit09911bf2008-07-26 23:55:29124
125#endif // IPC_MESSAGE_LOG_ENABLED
126
[email protected]946d1b22009-07-22 23:57:21127#endif // IPC_IPC_LOGGING_H_