Console.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (c) 2020, Emanuele Torre <torreemanuele6@gmail.com>
  3. * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/Function.h>
  9. #include <AK/HashMap.h>
  10. #include <AK/Noncopyable.h>
  11. #include <AK/String.h>
  12. #include <AK/Vector.h>
  13. #include <LibCore/ElapsedTimer.h>
  14. #include <LibJS/Forward.h>
  15. #include <LibJS/Runtime/Value.h>
  16. namespace JS {
  17. class ConsoleClient;
  18. // https://console.spec.whatwg.org
  19. class Console {
  20. AK_MAKE_NONCOPYABLE(Console);
  21. AK_MAKE_NONMOVABLE(Console);
  22. public:
  23. // These are not really levels, but that's the term used in the spec.
  24. enum class LogLevel {
  25. Assert,
  26. Count,
  27. CountReset,
  28. Debug,
  29. Dir,
  30. DirXML,
  31. Error,
  32. Group,
  33. GroupCollapsed,
  34. Info,
  35. Log,
  36. TimeEnd,
  37. TimeLog,
  38. Trace,
  39. Warn,
  40. };
  41. struct Group {
  42. String label;
  43. };
  44. struct Trace {
  45. String label;
  46. Vector<String> stack;
  47. };
  48. explicit Console(Realm&);
  49. void set_client(ConsoleClient& client) { m_client = &client; }
  50. Realm& realm() const { return m_realm; }
  51. MarkedVector<Value> vm_arguments();
  52. HashMap<String, unsigned>& counters() { return m_counters; }
  53. HashMap<String, unsigned> const& counters() const { return m_counters; }
  54. ThrowCompletionOr<Value> assert_();
  55. Value clear();
  56. ThrowCompletionOr<Value> debug();
  57. ThrowCompletionOr<Value> error();
  58. ThrowCompletionOr<Value> info();
  59. ThrowCompletionOr<Value> log();
  60. ThrowCompletionOr<Value> trace();
  61. ThrowCompletionOr<Value> warn();
  62. ThrowCompletionOr<Value> dir();
  63. ThrowCompletionOr<Value> count();
  64. ThrowCompletionOr<Value> count_reset();
  65. ThrowCompletionOr<Value> group();
  66. ThrowCompletionOr<Value> group_collapsed();
  67. ThrowCompletionOr<Value> group_end();
  68. ThrowCompletionOr<Value> time();
  69. ThrowCompletionOr<Value> time_log();
  70. ThrowCompletionOr<Value> time_end();
  71. void output_debug_message(LogLevel log_level, String const& output) const;
  72. void report_exception(JS::Error const&, bool) const;
  73. private:
  74. ThrowCompletionOr<String> value_vector_to_string(MarkedVector<Value> const&);
  75. ThrowCompletionOr<String> format_time_since(Core::ElapsedTimer timer);
  76. NonnullGCPtr<Realm> m_realm;
  77. ConsoleClient* m_client { nullptr };
  78. HashMap<String, unsigned> m_counters;
  79. HashMap<String, Core::ElapsedTimer> m_timer_table;
  80. Vector<Group> m_group_stack;
  81. };
  82. class ConsoleClient {
  83. public:
  84. explicit ConsoleClient(Console& console)
  85. : m_console(console)
  86. {
  87. }
  88. using PrinterArguments = Variant<Console::Group, Console::Trace, MarkedVector<Value>>;
  89. ThrowCompletionOr<Value> logger(Console::LogLevel log_level, MarkedVector<Value> const& args);
  90. ThrowCompletionOr<MarkedVector<Value>> formatter(MarkedVector<Value> const& args);
  91. virtual ThrowCompletionOr<Value> printer(Console::LogLevel log_level, PrinterArguments) = 0;
  92. virtual void add_css_style_to_current_message(StringView) { }
  93. virtual void report_exception(JS::Error const&, bool) { }
  94. virtual void clear() = 0;
  95. virtual void end_group() = 0;
  96. ThrowCompletionOr<String> generically_format_values(MarkedVector<Value> const&);
  97. protected:
  98. virtual ~ConsoleClient() = default;
  99. Console& m_console;
  100. };
  101. }