Console.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 <LibGC/CellAllocator.h>
  15. #include <LibJS/Forward.h>
  16. #include <LibJS/Heap/Cell.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. GC_CELL(Console, Cell);
  23. GC_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. Table,
  42. Trace,
  43. Warn,
  44. };
  45. struct Group {
  46. String label;
  47. };
  48. struct Trace {
  49. String label;
  50. Vector<String> stack;
  51. };
  52. void set_client(ConsoleClient& client) { m_client = &client; }
  53. Realm& realm() const { return m_realm; }
  54. GC::MarkedVector<Value> vm_arguments();
  55. HashMap<String, unsigned>& counters() { return m_counters; }
  56. HashMap<String, unsigned> const& counters() const { return m_counters; }
  57. ThrowCompletionOr<Value> assert_();
  58. Value clear();
  59. ThrowCompletionOr<Value> debug();
  60. ThrowCompletionOr<Value> error();
  61. ThrowCompletionOr<Value> info();
  62. ThrowCompletionOr<Value> log();
  63. ThrowCompletionOr<Value> table();
  64. ThrowCompletionOr<Value> trace();
  65. ThrowCompletionOr<Value> warn();
  66. ThrowCompletionOr<Value> dir();
  67. ThrowCompletionOr<Value> count();
  68. ThrowCompletionOr<Value> count_reset();
  69. ThrowCompletionOr<Value> group();
  70. ThrowCompletionOr<Value> group_collapsed();
  71. ThrowCompletionOr<Value> group_end();
  72. ThrowCompletionOr<Value> time();
  73. ThrowCompletionOr<Value> time_log();
  74. ThrowCompletionOr<Value> time_end();
  75. void output_debug_message(LogLevel log_level, String const& output) const;
  76. void report_exception(JS::Error const&, bool) const;
  77. private:
  78. explicit Console(Realm&);
  79. virtual void visit_edges(Visitor&) override;
  80. ThrowCompletionOr<String> value_vector_to_string(GC::MarkedVector<Value> const&);
  81. ThrowCompletionOr<String> format_time_since(Core::ElapsedTimer timer);
  82. GC::Ref<Realm> m_realm;
  83. GC::Ptr<ConsoleClient> m_client;
  84. HashMap<String, unsigned> m_counters;
  85. HashMap<String, Core::ElapsedTimer> m_timer_table;
  86. Vector<Group> m_group_stack;
  87. };
  88. class ConsoleClient : public Cell {
  89. GC_CELL(ConsoleClient, Cell);
  90. GC_DECLARE_ALLOCATOR(ConsoleClient);
  91. public:
  92. using PrinterArguments = Variant<Console::Group, Console::Trace, GC::MarkedVector<Value>>;
  93. ThrowCompletionOr<Value> logger(Console::LogLevel log_level, GC::MarkedVector<Value> const& args);
  94. ThrowCompletionOr<GC::MarkedVector<Value>> formatter(GC::MarkedVector<Value> const& args);
  95. virtual ThrowCompletionOr<Value> printer(Console::LogLevel log_level, PrinterArguments) = 0;
  96. virtual void add_css_style_to_current_message(StringView) { }
  97. virtual void report_exception(JS::Error const&, bool) { }
  98. virtual void clear() = 0;
  99. virtual void end_group() = 0;
  100. ThrowCompletionOr<String> generically_format_values(GC::MarkedVector<Value> const&);
  101. protected:
  102. explicit ConsoleClient(Console&);
  103. virtual ~ConsoleClient() override;
  104. virtual void visit_edges(Visitor& visitor) override;
  105. GC::Ref<Console> m_console;
  106. };
  107. }