1 // Copyright (c) 2009 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_
9 #include "ipc/ipc_message.h" // For IPC_MESSAGE_LOG_ENABLED.
11 #ifdef IPC_MESSAGE_LOG_ENABLED
15 #include "base/message_loop.h"
16 #include "base/scoped_ptr.h"
17 #include "base/singleton.h"
23 // One instance per process. Needs to be created on the main thread (the UI
24 // thread in the browser) but OnPreDispatchMessage/OnPostDispatchMessage
25 // can be called on other threads.
28 // Implemented by consumers of log messages.
31 virtual void Log(const LogData
& data
) = 0;
34 virtual ~Consumer() {}
37 void SetConsumer(Consumer
* consumer
);
40 static Logging
* current();
42 // Enable and Disable are NOT cross-process; they only affect the
43 // current thread/process. If you want to modify the value for all
44 // processes, perhaps your intent is to call
45 // g_browser_process->SetIPCLoggingEnabled().
48 bool Enabled() const { return enabled_
; }
50 // Called by child processes to give the logger object the channel to send
51 // logging data to the browser process.
52 void SetIPCSender(Message::Sender
* sender
);
54 // Called in the browser process when logging data from a child process is
56 void OnReceivedLoggingMessage(const Message
& message
);
58 void OnSendMessage(Message
* message
, const std::string
& channel_id
);
59 void OnPreDispatchMessage(const Message
& message
);
60 void OnPostDispatchMessage(const Message
& message
,
61 const std::string
& channel_id
);
63 // Returns the name of the logging enabled/disabled events so that the
64 // sandbox can add them to to the policy. If true, gets the name of the
65 // enabled event, if false, gets the name of the disabled event.
66 static std::wstring
GetEventName(bool enabled
);
68 // Like the *MsgLog functions declared for each message class, except this
69 // calls the correct one based on the message type automatically. Defined in
71 static void GetMessageText(uint32 type
, std::string
* name
,
72 const Message
* message
, std::string
* params
);
74 // Logging function. |name| is a string in ASCII and |params| is a string in
76 typedef void (*LogFunction
)(uint32 type
,
81 static void SetLoggerFunctions(LogFunction
*functions
);
84 friend struct DefaultSingletonTraits
<Logging
>;
88 void Log(const LogData
& data
);
91 bool enabled_on_stderr_
; // only used on POSIX for now
93 std::vector
<LogData
> queued_logs_
;
94 bool queue_invoke_later_pending_
;
96 Message::Sender
* sender_
;
97 MessageLoop
* main_thread_
;
101 static LogFunction
*log_function_mapping_
;
106 #endif // IPC_MESSAGE_LOG_ENABLED
108 #endif // IPC_IPC_LOGGING_H_