Initialize Tizen 2.3
[external/chromium.git] / ipc / ipc_logging.h
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 #pragma once
8
9 #include "ipc/ipc_message.h"  // For IPC_MESSAGE_LOG_ENABLED.
10
11 #ifdef IPC_MESSAGE_LOG_ENABLED
12
13 #include <vector>
14
15 #include "base/hash_tables.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "base/memory/singleton.h"
18 #include "base/message_loop.h"
19 #include "ipc/ipc_export.h"
20
21 // Logging function. |name| is a string in ASCII and |params| is a string in
22 // UTF-8.
23 typedef void (*LogFunction)(std::string* name,
24                             const IPC::Message* msg,
25                             std::string* params);
26
27 typedef base::hash_map<uint32, LogFunction > LogFunctionMap;
28
29 namespace IPC {
30
31 class 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.
36 class IPC_EXPORT Logging {
37  public:
38   // Implemented by consumers of log messages.
39   class Consumer {
40    public:
41     virtual void Log(const LogData& data) = 0;
42
43    protected:
44     virtual ~Consumer() {}
45   };
46
47   void SetConsumer(Consumer* consumer);
48
49   ~Logging();
50   static Logging* GetInstance();
51
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().
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.
62   void SetIPCSender(Message::Sender* sender);
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
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.
76   static void GetMessageText(uint32 type, std::string* name,
77                              const Message* message, std::string* params);
78
79   static void set_log_function_map(LogFunctionMap* functions) {
80     log_function_map_ = functions;
81   }
82
83   static LogFunctionMap* log_function_map() {
84     return log_function_map_;
85   }
86
87  private:
88   friend struct DefaultSingletonTraits<Logging>;
89   Logging();
90
91   void OnSendLogs();
92   void Log(const LogData& data);
93
94   bool enabled_;
95   bool enabled_on_stderr_;  // only used on POSIX for now
96
97   std::vector<LogData> queued_logs_;
98   bool queue_invoke_later_pending_;
99
100   Message::Sender* sender_;
101   MessageLoop* main_thread_;
102
103   Consumer* consumer_;
104
105   static LogFunctionMap* log_function_map_;
106 };
107
108 }  // namespace IPC
109
110 #endif // IPC_MESSAGE_LOG_ENABLED
111
112 #endif  // IPC_IPC_LOGGING_H_