Console.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright (c) 2020, Emanuele Torre <torreemanuele6@gmail.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/Function.h>
  28. #include <AK/HashMap.h>
  29. #include <AK/Noncopyable.h>
  30. #include <LibJS/Forward.h>
  31. namespace JS {
  32. class ConsoleClient;
  33. class Console {
  34. AK_MAKE_NONCOPYABLE(Console);
  35. AK_MAKE_NONMOVABLE(Console);
  36. public:
  37. Console(Interpreter&);
  38. void set_client(ConsoleClient& client) { m_client = &client; }
  39. Interpreter& interpreter() { return m_interpreter; }
  40. const Interpreter& interpreter() const { return m_interpreter; }
  41. HashMap<String, unsigned>& counters() { return m_counters; }
  42. const HashMap<String, unsigned>& counters() const { return m_counters; }
  43. Value debug();
  44. Value error();
  45. Value info();
  46. Value log();
  47. Value warn();
  48. Value clear();
  49. Value trace();
  50. Value count();
  51. Value count_reset();
  52. unsigned counter_increment(String label);
  53. bool counter_reset(String label);
  54. private:
  55. Interpreter& m_interpreter;
  56. ConsoleClient* m_client { nullptr };
  57. HashMap<String, unsigned> m_counters;
  58. };
  59. class ConsoleClient {
  60. public:
  61. ConsoleClient(Console& console)
  62. : m_console(console)
  63. {
  64. }
  65. virtual Value debug() = 0;
  66. virtual Value error() = 0;
  67. virtual Value info() = 0;
  68. virtual Value log() = 0;
  69. virtual Value warn() = 0;
  70. virtual Value clear() = 0;
  71. virtual Value trace() = 0;
  72. virtual Value count() = 0;
  73. virtual Value count_reset() = 0;
  74. protected:
  75. Interpreter& interpreter() { return m_console.interpreter(); }
  76. const Interpreter& interpreter() const { return m_console.interpreter(); }
  77. Vector<String> get_trace() const;
  78. Console& m_console;
  79. };
  80. }