Console.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 <LibJS/Console.h>
  28. #include <LibJS/Runtime/GlobalObject.h>
  29. namespace JS {
  30. Console::Console(GlobalObject& global_object)
  31. : m_global_object(global_object)
  32. {
  33. }
  34. VM& Console::vm()
  35. {
  36. return m_global_object.vm();
  37. }
  38. Value Console::debug()
  39. {
  40. #ifdef __serenity__
  41. dbgln("\033[32;1m(js debug)\033[0m {}", vm().join_arguments());
  42. #endif
  43. if (m_client)
  44. return m_client->debug();
  45. return js_undefined();
  46. }
  47. Value Console::error()
  48. {
  49. #ifdef __serenity__
  50. dbgln("\033[32;1m(js error)\033[0m {}", vm().join_arguments());
  51. #endif
  52. if (m_client)
  53. return m_client->error();
  54. return js_undefined();
  55. }
  56. Value Console::info()
  57. {
  58. #ifdef __serenity__
  59. dbgln("\033[32;1m(js info)\033[0m {}", vm().join_arguments());
  60. #endif
  61. if (m_client)
  62. return m_client->info();
  63. return js_undefined();
  64. }
  65. Value Console::log()
  66. {
  67. #ifdef __serenity__
  68. dbgln("\033[32;1m(js log)\033[0m {}", vm().join_arguments());
  69. #endif
  70. if (m_client)
  71. return m_client->log();
  72. return js_undefined();
  73. }
  74. Value Console::warn()
  75. {
  76. #ifdef __serenity__
  77. dbgln("\033[32;1m(js warn)\033[0m {}", vm().join_arguments());
  78. #endif
  79. if (m_client)
  80. return m_client->warn();
  81. return js_undefined();
  82. }
  83. Value Console::clear()
  84. {
  85. if (m_client)
  86. return m_client->clear();
  87. return js_undefined();
  88. }
  89. Value Console::trace()
  90. {
  91. if (m_client)
  92. return m_client->trace();
  93. return js_undefined();
  94. }
  95. Value Console::count()
  96. {
  97. if (m_client)
  98. return m_client->count();
  99. return js_undefined();
  100. }
  101. Value Console::count_reset()
  102. {
  103. if (m_client)
  104. return m_client->count_reset();
  105. return js_undefined();
  106. }
  107. unsigned Console::counter_increment(String label)
  108. {
  109. auto value = m_counters.get(label);
  110. if (!value.has_value()) {
  111. m_counters.set(label, 1);
  112. return 1;
  113. }
  114. auto new_value = value.value() + 1;
  115. m_counters.set(label, new_value);
  116. return new_value;
  117. }
  118. bool Console::counter_reset(String label)
  119. {
  120. if (!m_counters.contains(label))
  121. return false;
  122. m_counters.remove(label);
  123. return true;
  124. }
  125. VM& ConsoleClient::vm()
  126. {
  127. return global_object().vm();
  128. }
  129. Vector<String> ConsoleClient::get_trace() const
  130. {
  131. Vector<String> trace;
  132. auto& call_stack = m_console.global_object().vm().call_stack();
  133. // -2 to skip the console.trace() call frame
  134. for (ssize_t i = call_stack.size() - 2; i >= 0; --i)
  135. trace.append(call_stack[i]->function_name);
  136. return trace;
  137. }
  138. }