ipc_logging.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. #ifndef IPC_IPC_LOGGING_H_
  5. #define IPC_IPC_LOGGING_H_
  6. #include "ipc/ipc_buildflags.h"
  7. #if BUILDFLAG(IPC_MESSAGE_LOG_ENABLED)
  8. #include <stdint.h>
  9. #include <unordered_map>
  10. #include <vector>
  11. #include "base/component_export.h"
  12. #include "base/memory/ref_counted.h"
  13. #include "base/memory/singleton.h"
  14. #include "base/task/single_thread_task_runner.h"
  15. #include "ipc/ipc_message.h"
  16. // Logging function. |name| is a string in ASCII and |params| is a string in
  17. // UTF-8.
  18. typedef void (*LogFunction)(std::string* name,
  19. const IPC::Message* msg,
  20. std::string* params);
  21. typedef std::unordered_map<uint32_t, LogFunction> LogFunctionMap;
  22. namespace IPC {
  23. class Message;
  24. class Sender;
  25. // One instance per process. Needs to be created on the main thread (the UI
  26. // thread in the browser) but OnPreDispatchMessage/OnPostDispatchMessage
  27. // can be called on other threads.
  28. class COMPONENT_EXPORT(IPC) Logging {
  29. public:
  30. // Implemented by consumers of log messages.
  31. class Consumer {
  32. public:
  33. virtual void Log(const LogData& data) = 0;
  34. protected:
  35. virtual ~Consumer() {}
  36. };
  37. void SetConsumer(Consumer* consumer);
  38. ~Logging();
  39. static Logging* GetInstance();
  40. // Enable and Disable are NOT cross-process; they only affect the
  41. // current thread/process. If you want to modify the value for all
  42. // processes, perhaps your intent is to call
  43. // g_browser_process->SetIPCLoggingEnabled().
  44. void Enable();
  45. void Disable();
  46. bool Enabled() const { return enabled_; }
  47. // Called by child processes to give the logger object the channel to send
  48. // logging data to the browser process.
  49. void SetIPCSender(Sender* sender);
  50. // Called in the browser process when logging data from a child process is
  51. // received.
  52. void OnReceivedLoggingMessage(const Message& message);
  53. void OnSendMessage(Message* message);
  54. void OnPreDispatchMessage(const Message& message);
  55. void OnPostDispatchMessage(const Message& message);
  56. // Like the *MsgLog functions declared for each message class, except this
  57. // calls the correct one based on the message type automatically. Defined in
  58. // ipc_logging.cc.
  59. static void GetMessageText(uint32_t type, std::string* name,
  60. const Message* message, std::string* params);
  61. static void set_log_function_map(LogFunctionMap* functions) {
  62. log_function_map_ = functions;
  63. }
  64. static LogFunctionMap* log_function_map() {
  65. return log_function_map_;
  66. }
  67. private:
  68. typedef enum {
  69. ANSI_COLOR_RESET = -1,
  70. ANSI_COLOR_BLACK,
  71. ANSI_COLOR_RED,
  72. ANSI_COLOR_GREEN,
  73. ANSI_COLOR_YELLOW,
  74. ANSI_COLOR_BLUE,
  75. ANSI_COLOR_MAGENTA,
  76. ANSI_COLOR_CYAN,
  77. ANSI_COLOR_WHITE
  78. } ANSIColor;
  79. const char* ANSIEscape(ANSIColor color);
  80. ANSIColor DelayColor(double delay);
  81. friend struct base::DefaultSingletonTraits<Logging>;
  82. Logging();
  83. void OnSendLogs();
  84. void Log(const LogData& data);
  85. bool enabled_;
  86. bool enabled_on_stderr_; // only used on POSIX for now
  87. bool enabled_color_; // only used on POSIX for now
  88. std::vector<LogData> queued_logs_;
  89. bool queue_invoke_later_pending_;
  90. Sender* sender_;
  91. scoped_refptr<base::SingleThreadTaskRunner> main_thread_;
  92. Consumer* consumer_;
  93. static LogFunctionMap* log_function_map_;
  94. };
  95. } // namespace IPC
  96. #endif // BUILDFLAG(IPC_MESSAGE_LOG_ENABLED)
  97. #endif // IPC_IPC_LOGGING_H_