Console.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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/Heap/Cell.h>
  16. #include <LibJS/Heap/CellAllocator.h>
  17. #include <LibJS/Runtime/Value.h>
  18. namespace JS {
  19. class ConsoleClient;
  20. // https://console.spec.whatwg.org
  21. class Console : public Cell {
  22. JS_CELL(Console, Cell);
  23. JS_DECLARE_ALLOCATOR(Console);
  24. public:
  25. virtual ~Console() override;
  26. // These are not really levels, but that's the term used in the spec.
  27. enum class LogLevel {
  28. Assert,
  29. Count,
  30. CountReset,
  31. Debug,
  32. Dir,
  33. DirXML,
  34. Error,
  35. Group,
  36. GroupCollapsed,
  37. Info,
  38. Log,
  39. TimeEnd,
  40. TimeLog,
  41. Trace,
  42. Warn,
  43. };
  44. struct Group {
  45. String label;
  46. };
  47. struct Trace {
  48. String label;
  49. Vector<String> stack;
  50. };
  51. void set_client(ConsoleClient& client) { m_client = &client; }
  52. Realm& realm() const { return m_realm; }
  53. MarkedVector<Value> vm_arguments();
  54. HashMap<String, unsigned>& counters() { return m_counters; }
  55. HashMap<String, unsigned> const& counters() const { return m_counters; }
  56. ThrowCompletionOr<Value> assert_();
  57. Value clear();
  58. ThrowCompletionOr<Value> debug();
  59. ThrowCompletionOr<Value> error();
  60. ThrowCompletionOr<Value> info();
  61. ThrowCompletionOr<Value> log();
  62. ThrowCompletionOr<Value> trace();
  63. ThrowCompletionOr<Value> warn();
  64. ThrowCompletionOr<Value> dir();
  65. ThrowCompletionOr<Value> count();
  66. ThrowCompletionOr<Value> count_reset();
  67. ThrowCompletionOr<Value> group();
  68. ThrowCompletionOr<Value> group_collapsed();
  69. ThrowCompletionOr<Value> group_end();
  70. ThrowCompletionOr<Value> time();
  71. ThrowCompletionOr<Value> time_log();
  72. ThrowCompletionOr<Value> time_end();
  73. void output_debug_message(LogLevel log_level, String const& output) const;
  74. void report_exception(JS::Error const&, bool) const;
  75. private:
  76. explicit Console(Realm&);
  77. virtual void visit_edges(Visitor&) override;
  78. ThrowCompletionOr<String> value_vector_to_string(MarkedVector<Value> const&);
  79. ThrowCompletionOr<String> format_time_since(Core::ElapsedTimer timer);
  80. NonnullGCPtr<Realm> m_realm;
  81. GCPtr<ConsoleClient> m_client;
  82. HashMap<String, unsigned> m_counters;
  83. HashMap<String, Core::ElapsedTimer> m_timer_table;
  84. Vector<Group> m_group_stack;
  85. };
  86. class ConsoleClient : public Cell {
  87. JS_CELL(ConsoleClient, Cell);
  88. JS_DECLARE_ALLOCATOR(ConsoleClient);
  89. public:
  90. using PrinterArguments = Variant<Console::Group, Console::Trace, MarkedVector<Value>>;
  91. ThrowCompletionOr<Value> logger(Console::LogLevel log_level, MarkedVector<Value> const& args);
  92. ThrowCompletionOr<MarkedVector<Value>> formatter(MarkedVector<Value> const& args);
  93. virtual ThrowCompletionOr<Value> printer(Console::LogLevel log_level, PrinterArguments) = 0;
  94. virtual void add_css_style_to_current_message(StringView) { }
  95. virtual void report_exception(JS::Error const&, bool) { }
  96. virtual void clear() = 0;
  97. virtual void end_group() = 0;
  98. ThrowCompletionOr<String> generically_format_values(MarkedVector<Value> const&);
  99. protected:
  100. explicit ConsoleClient(Console&);
  101. virtual ~ConsoleClient() override;
  102. virtual void visit_edges(Visitor& visitor) override;
  103. NonnullGCPtr<Console> m_console;
  104. };
  105. }