blob: 929f55e50a9c8e1a3f5c01c4e2fd59089a8eb136 [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_
initial.commit09911bf2008-07-26 23:55:297
davidsz041528a2017-05-12 09:19:238#include "ipc/ipc_features.h"
9
10#if BUILDFLAG(IPC_MESSAGE_LOG_ENABLED)
11
tfarina10a5c062015-09-04 18:47:5712#include <stdint.h>
[email protected]7bf730952009-05-29 09:31:1513#include <vector>
14
[email protected]14c1c232013-06-11 17:52:4415#include "base/containers/hash_tables.h"
fdoraybcb9b322016-06-20 20:23:3216#include "base/memory/ref_counted.h"
[email protected]3b63f8f42011-03-28 01:54:1517#include "base/memory/singleton.h"
fdoraybcb9b322016-06-20 20:23:3218#include "base/single_thread_task_runner.h"
[email protected]7c854372011-08-15 20:41:4619#include "ipc/ipc_export.h"
davidsz041528a2017-05-12 09:19:2320#include "ipc/ipc_message.h"
[email protected]cee1dfa2008-08-14 09:21:5021
[email protected]21fa3a12010-12-08 23:34:1622// 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
tfarina10a5c062015-09-04 18:47:5728typedef base::hash_map<uint32_t, LogFunction > LogFunctionMap;
[email protected]21fa3a12010-12-08 23:34:1629
initial.commit09911bf2008-07-26 23:55:2930namespace IPC {
31
32class Message;
[email protected]510344fc2012-12-14 22:56:2833class Sender;
initial.commit09911bf2008-07-26 23:55:2934
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]7c854372011-08-15 20:41:4638class IPC_EXPORT Logging {
initial.commit09911bf2008-07-26 23:55:2939 public:
40 // Implemented by consumers of log messages.
41 class Consumer {
42 public:
[email protected]f91cb992009-02-04 20:10:1243 virtual void Log(const LogData& data) = 0;
[email protected]20cb5f482009-12-16 01:01:2544
45 protected:
46 virtual ~Consumer() {}
initial.commit09911bf2008-07-26 23:55:2947 };
48
49 void SetConsumer(Consumer* consumer);
50
51 ~Logging();
[email protected]8e8bb6d2010-12-13 08:18:5552 static Logging* GetInstance();
initial.commit09911bf2008-07-26 23:55:2953
[email protected]d55aaa132009-09-28 21:08:0454 // 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.commit09911bf2008-07-26 23:55:2958 void Enable();
59 void Disable();
[email protected]e707d5e62009-02-12 04:00:0860 bool Enabled() const { return enabled_; }
initial.commit09911bf2008-07-26 23:55:2961
62 // Called by child processes to give the logger object the channel to send
63 // logging data to the browser process.
[email protected]57319ce2012-06-11 22:35:2664 void SetIPCSender(Sender* sender);
initial.commit09911bf2008-07-26 23:55:2965
66 // Called in the browser process when logging data from a child process is
67 // received.
68 void OnReceivedLoggingMessage(const Message& message);
69
sammca1b3b4d2016-11-15 00:34:3670 void OnSendMessage(Message* message);
initial.commit09911bf2008-07-26 23:55:2971 void OnPreDispatchMessage(const Message& message);
sammca1b3b4d2016-11-15 00:34:3672 void OnPostDispatchMessage(const Message& message);
initial.commit09911bf2008-07-26 23:55:2973
initial.commit09911bf2008-07-26 23:55:2974 // 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.
tfarina10a5c062015-09-04 18:47:5777 static void GetMessageText(uint32_t type, std::string* name,
[email protected]252cad62010-08-18 18:33:5778 const Message* message, std::string* params);
initial.commit09911bf2008-07-26 23:55:2979
[email protected]21fa3a12010-12-08 23:34:1680 static void set_log_function_map(LogFunctionMap* functions) {
81 log_function_map_ = functions;
82 }
[email protected]3335d872009-02-11 05:38:4183
[email protected]21fa3a12010-12-08 23:34:1684 static LogFunctionMap* log_function_map() {
85 return log_function_map_;
86 }
[email protected]d5dfa5a2009-02-10 19:38:5587
initial.commit09911bf2008-07-26 23:55:2988 private:
[email protected]ea7744a2011-10-20 19:34:4389 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.raula36aa8be2015-09-10 11:14:22103 friend struct base::DefaultSingletonTraits<Logging>;
initial.commit09911bf2008-07-26 23:55:29104 Logging();
105
initial.commit09911bf2008-07-26 23:55:29106 void OnSendLogs();
107 void Log(const LogData& data);
108
initial.commit09911bf2008-07-26 23:55:29109 bool enabled_;
[email protected]d55aaa132009-09-28 21:08:04110 bool enabled_on_stderr_; // only used on POSIX for now
[email protected]ea7744a2011-10-20 19:34:43111 bool enabled_color_; // only used on POSIX for now
initial.commit09911bf2008-07-26 23:55:29112
113 std::vector<LogData> queued_logs_;
114 bool queue_invoke_later_pending_;
115
[email protected]57319ce2012-06-11 22:35:26116 Sender* sender_;
fdoraybcb9b322016-06-20 20:23:32117 scoped_refptr<base::SingleThreadTaskRunner> main_thread_;
initial.commit09911bf2008-07-26 23:55:29118
119 Consumer* consumer_;
[email protected]d5dfa5a2009-02-10 19:38:55120
[email protected]21fa3a12010-12-08 23:34:16121 static LogFunctionMap* log_function_map_;
initial.commit09911bf2008-07-26 23:55:29122};
123
[email protected]3178f4e22008-08-05 21:20:41124} // namespace IPC
initial.commit09911bf2008-07-26 23:55:29125
davidsz041528a2017-05-12 09:19:23126#endif // BUILDFLAG(IPC_MESSAGE_LOG_ENABLED)
initial.commit09911bf2008-07-26 23:55:29127
[email protected]946d1b22009-07-22 23:57:21128#endif // IPC_IPC_LOGGING_H_