Console.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright (c) 2020, Emanuele Torre <torreemanuele6@gmail.com>
  3. * Copyright (c) 2020, Linus Groh <mail@linusgroh.de>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  24. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <AK/String.h>
  28. #include <AK/StringBuilder.h>
  29. #include <LibJS/Console.h>
  30. #include <LibJS/Interpreter.h>
  31. namespace JS {
  32. Console::Console(Interpreter& interpreter)
  33. : m_interpreter(interpreter)
  34. {
  35. }
  36. void Console::add_message(ConsoleMessageKind kind, String text)
  37. {
  38. ConsoleMessage message = { kind, text };
  39. m_messages.append(message);
  40. if (on_new_message)
  41. on_new_message(message);
  42. }
  43. void Console::debug(String text)
  44. {
  45. add_message(ConsoleMessageKind::Debug, text);
  46. }
  47. void Console::error(String text)
  48. {
  49. add_message(ConsoleMessageKind::Error, text);
  50. }
  51. void Console::info(String text)
  52. {
  53. add_message(ConsoleMessageKind::Info, text);
  54. }
  55. void Console::log(String text)
  56. {
  57. add_message(ConsoleMessageKind::Log, text);
  58. }
  59. void Console::warn(String text)
  60. {
  61. add_message(ConsoleMessageKind::Warn, text);
  62. }
  63. void Console::clear()
  64. {
  65. m_messages.clear();
  66. add_message(ConsoleMessageKind::Clear, {});
  67. }
  68. void Console::trace(String title)
  69. {
  70. StringBuilder message_text;
  71. message_text.append(title);
  72. auto call_stack = m_interpreter.call_stack();
  73. // -2 to skip the console.trace() call frame
  74. for (ssize_t i = call_stack.size() - 2; i >= 0; --i) {
  75. auto function_name = call_stack[i].function_name;
  76. message_text.append("\n\t");
  77. if (String(function_name).is_empty())
  78. function_name = "<anonymous>";
  79. message_text.append(function_name);
  80. }
  81. add_message(ConsoleMessageKind::Trace, message_text.build());
  82. }
  83. unsigned Console::count(String label)
  84. {
  85. auto counter_value = m_counters.get(label);
  86. if (!counter_value.has_value()) {
  87. add_message(ConsoleMessageKind::Count, String::format("%s: 1", label.characters()));
  88. m_counters.set(label, 1);
  89. return 1;
  90. }
  91. auto new_counter_value = counter_value.value() + 1;
  92. add_message(ConsoleMessageKind::Count, String::format("%s: %u", label.characters(), new_counter_value));
  93. m_counters.set(label, new_counter_value);
  94. return new_counter_value;
  95. }
  96. bool Console::count_reset(String label)
  97. {
  98. if (!m_counters.contains(label))
  99. return false;
  100. m_counters.remove(label);
  101. add_message(ConsoleMessageKind::Count, String::format("%s: 0", label.characters()));
  102. return true;
  103. }
  104. }