Console.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. Value Console::debug()
  37. {
  38. if (m_client)
  39. return m_client->debug();
  40. dbg() << "debug: " << m_interpreter.join_arguments();
  41. return js_undefined();
  42. }
  43. Value Console::error()
  44. {
  45. if (m_client)
  46. return m_client->error();
  47. dbg() << "error: " << m_interpreter.join_arguments();
  48. return js_undefined();
  49. }
  50. Value Console::info()
  51. {
  52. if (m_client)
  53. return m_client->info();
  54. dbg() << "info: " << m_interpreter.join_arguments();
  55. return js_undefined();
  56. }
  57. Value Console::log()
  58. {
  59. if (m_client)
  60. return m_client->log();
  61. dbg() << "log: " << m_interpreter.join_arguments();
  62. return js_undefined();
  63. }
  64. Value Console::warn()
  65. {
  66. if (m_client)
  67. return m_client->warn();
  68. dbg() << "warn: " << m_interpreter.join_arguments();
  69. return js_undefined();
  70. }
  71. Value Console::clear()
  72. {
  73. if (m_client)
  74. return m_client->clear();
  75. dbg() << "clear:";
  76. return js_undefined();
  77. }
  78. Value Console::trace()
  79. {
  80. if (m_client)
  81. return m_client->trace();
  82. StringBuilder message_text;
  83. message_text.append(m_interpreter.join_arguments());
  84. auto trace = m_interpreter.get_trace();
  85. for (auto function_name : trace) {
  86. message_text.append("\n -> ");
  87. if (String(function_name).is_empty())
  88. function_name = "<anonymous>";
  89. message_text.append(function_name);
  90. }
  91. dbg() << "log: " << message_text.build();
  92. return js_undefined();
  93. }
  94. Value Console::count()
  95. {
  96. if (m_client)
  97. return m_client->count();
  98. auto label = m_interpreter.argument_count() ? m_interpreter.argument(0).to_string_without_side_effects() : "default";
  99. auto counter_value = counter_increment(label);
  100. dbg() << "log: " << label << ": " << counter_value;
  101. return js_undefined();
  102. }
  103. Value Console::count_reset()
  104. {
  105. if (m_client)
  106. return m_client->count_reset();
  107. auto label = m_interpreter.argument_count() ? m_interpreter.argument(0).to_string_without_side_effects() : "default";
  108. if (counter_reset(label))
  109. dbg() << "log: " << label << ": 0";
  110. else
  111. dbg() << "warn: \"" << label << "\" doesn't have a count";
  112. return js_undefined();
  113. }
  114. unsigned Console::counter_increment(String label)
  115. {
  116. auto value = m_counters.get(label);
  117. if (!value.has_value()) {
  118. m_counters.set(label, 1);
  119. return 1;
  120. }
  121. auto new_value = value.value() + 1;
  122. m_counters.set(label, new_value);
  123. return new_value;
  124. }
  125. bool Console::counter_reset(String label)
  126. {
  127. if (!m_counters.contains(label))
  128. return false;
  129. m_counters.remove(label);
  130. return true;
  131. }
  132. }