Console.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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/Vector.h>
  12. #include <LibJS/Forward.h>
  13. #include <LibJS/Runtime/Value.h>
  14. namespace JS {
  15. class ConsoleClient;
  16. // https://console.spec.whatwg.org
  17. class Console {
  18. AK_MAKE_NONCOPYABLE(Console);
  19. AK_MAKE_NONMOVABLE(Console);
  20. public:
  21. // These are not really levels, but that's the term used in the spec.
  22. enum class LogLevel {
  23. Assert,
  24. Count,
  25. CountReset,
  26. Debug,
  27. Dir,
  28. DirXML,
  29. Error,
  30. Group,
  31. GroupCollapsed,
  32. Info,
  33. Log,
  34. TimeEnd,
  35. TimeLog,
  36. Trace,
  37. Warn,
  38. };
  39. explicit Console(GlobalObject&);
  40. void set_client(ConsoleClient& client) { m_client = &client; }
  41. GlobalObject& global_object() { return m_global_object; }
  42. const GlobalObject& global_object() const { return m_global_object; }
  43. VM& vm();
  44. Vector<Value> vm_arguments();
  45. HashMap<String, unsigned>& counters() { return m_counters; }
  46. const HashMap<String, unsigned>& counters() const { return m_counters; }
  47. ThrowCompletionOr<Value> debug();
  48. ThrowCompletionOr<Value> error();
  49. ThrowCompletionOr<Value> info();
  50. ThrowCompletionOr<Value> log();
  51. ThrowCompletionOr<Value> warn();
  52. Value clear();
  53. Value trace();
  54. ThrowCompletionOr<Value> count();
  55. ThrowCompletionOr<Value> count_reset();
  56. Value assert_();
  57. void output_debug_message(LogLevel log_level, String output) const;
  58. private:
  59. GlobalObject& m_global_object;
  60. ConsoleClient* m_client { nullptr };
  61. HashMap<String, unsigned> m_counters;
  62. };
  63. class ConsoleClient {
  64. public:
  65. explicit ConsoleClient(Console& console)
  66. : m_console(console)
  67. {
  68. }
  69. ThrowCompletionOr<Value> logger(Console::LogLevel log_level, Vector<Value>& args);
  70. ThrowCompletionOr<Vector<Value>> formatter(Vector<Value>& args);
  71. virtual ThrowCompletionOr<Value> printer(Console::LogLevel log_level, Vector<Value>&) = 0;
  72. virtual Value clear() = 0;
  73. virtual Value trace() = 0;
  74. virtual Value assert_() = 0;
  75. protected:
  76. virtual ~ConsoleClient() = default;
  77. VM& vm();
  78. GlobalObject& global_object() { return m_console.global_object(); }
  79. const GlobalObject& global_object() const { return m_console.global_object(); }
  80. Vector<String> get_trace() const;
  81. Console& m_console;
  82. };
  83. }