• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #ifndef IPC_IPC_LOGGING_H_
6 #define IPC_IPC_LOGGING_H_
7 
8 #include "ipc/ipc_buildflags.h"
9 
10 #if BUILDFLAG(IPC_MESSAGE_LOG_ENABLED)
11 
12 #include <stdint.h>
13 #include <vector>
14 
15 #include "base/component_export.h"
16 #include "base/containers/hash_tables.h"
17 #include "base/memory/ref_counted.h"
18 #include "base/memory/singleton.h"
19 #include "base/single_thread_task_runner.h"
20 #include "ipc/ipc_message.h"
21 
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 
28 typedef base::hash_map<uint32_t, LogFunction > LogFunctionMap;
29 
30 namespace IPC {
31 
32 class Message;
33 class Sender;
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.
COMPONENT_EXPORT(IPC)38 class COMPONENT_EXPORT(IPC) Logging {
39  public:
40   // Implemented by consumers of log messages.
41   class Consumer {
42    public:
43     virtual void Log(const LogData& data) = 0;
44 
45    protected:
46     virtual ~Consumer() {}
47   };
48 
49   void SetConsumer(Consumer* consumer);
50 
51   ~Logging();
52   static Logging* GetInstance();
53 
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_; }
61 
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);
65 
66   // Called in the browser process when logging data from a child process is
67   // received.
68   void OnReceivedLoggingMessage(const Message& message);
69 
70   void OnSendMessage(Message* message);
71   void OnPreDispatchMessage(const Message& message);
72   void OnPostDispatchMessage(const Message& message);
73 
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.
77   static void GetMessageText(uint32_t type, std::string* name,
78                              const Message* message, std::string* params);
79 
80   static void set_log_function_map(LogFunctionMap* functions) {
81     log_function_map_ = functions;
82   }
83 
84   static LogFunctionMap* log_function_map() {
85     return log_function_map_;
86   }
87 
88  private:
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 
103   friend struct base::DefaultSingletonTraits<Logging>;
104   Logging();
105 
106   void OnSendLogs();
107   void Log(const LogData& data);
108 
109   bool enabled_;
110   bool enabled_on_stderr_;  // only used on POSIX for now
111   bool enabled_color_; // only used on POSIX for now
112 
113   std::vector<LogData> queued_logs_;
114   bool queue_invoke_later_pending_;
115 
116   Sender* sender_;
117   scoped_refptr<base::SingleThreadTaskRunner> main_thread_;
118 
119   Consumer* consumer_;
120 
121   static LogFunctionMap* log_function_map_;
122 };
123 
124 }  // namespace IPC
125 
126 #endif  // BUILDFLAG(IPC_MESSAGE_LOG_ENABLED)
127 
128 #endif  // IPC_IPC_LOGGING_H_
129