Console.cpp 3.7 KB

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