31 constexpr
const size_t kMaxChildren = 20;
33 std::string ConvertToPrettyString(Handle<JsValue> value,
bool allow_long);
50 std::string ConvertStringToPrettyString(
const std::string&
string) {
52 buffer.push_back(
'"');
53 for (
const char c :
string) {
98 buffer.push_back(
'"');
102 std::string ConvertArrayToLongPrettyString(Handle<JsValue> value) {
103 const LocalVar<JsObject> array = UnsafeJsCast<JsObject>(value);
105 const size_t min_length = std::min(kMaxChildren, array_length);
109 std::string array_as_string =
"[";
111 for (
size_t i = 0; i < min_length; i++) {
114 array_as_string += i ?
", " :
"";
115 array_as_string += ConvertToPrettyString(index_value,
false);
120 if (array_length > min_length) {
121 array_as_string +=
", ...";
124 array_as_string +=
"]";
126 return array_as_string;
129 std::string ConvertObjectToLongPrettyString(Handle<JsObject>
object) {
134 std::sort(member_names.begin(), member_names.end());
136 const size_t min_length = std::min(kMaxChildren, member_names.size());
138 std::string object_as_string =
"{";
140 for (
size_t i = 0; i < min_length; i++) {
141 const std::string& member_name = member_names[i];
142 const LocalVar<JsValue> member_value =
GetMemberRaw(
object, member_name);
144 object_as_string += i ?
", " :
"";
145 object_as_string += member_name;
146 object_as_string +=
":";
147 object_as_string += ConvertToPrettyString(member_value,
false);
152 if (member_names.size() > min_length) {
153 object_as_string +=
", ...";
156 object_as_string +=
"}";
157 return object_as_string;
160 std::string ConvertToPrettyString(Handle<JsValue> value,
bool allow_long) {
171 return "function() {...}";
175 return ConvertStringToPrettyString(
string);
178 return allow_long ? ConvertArrayToLongPrettyString(value) :
"[...]";
187 return "String(" + ConvertStringToPrettyString(
ConvertToString(value)) +
194 LocalVar<JsObject>
object = UnsafeJsCast<JsObject>(value);
197 return allow_long ? ConvertObjectToLongPrettyString(
object) :
"{...}";
205 Console::~Console() {}
210 LogReal(
kError, arguments,
"Assertion failed: ", 1);
216 LogReal(
kError, arguments);
220 LogReal(
kWarn, arguments);
224 LogReal(
kInfo, arguments);
228 LogReal(
kLog, arguments);
232 LogReal(
kDebug, arguments);
236 return shaka::js::ConvertToPrettyString(value,
true);
239 void Console::LogReal(
LogLevel level,
const CallbackArguments& arguments,
240 const char* prefix,
size_t skip_count)
const {
242 std::printf(
"[%s]: ",
to_string(level).c_str());
244 std::printf(
"%s", prefix);
246 for (
size_t i = skip_count; i < length; ++i) {
bool IsObject(Handle< JsValue > value)
std::vector< std::string > GetMemberNames(Handle< JsObject > object)
ReturnVal< JsValue > GetArrayIndexRaw(Handle< JsObject > object, size_t index, LocalVar< JsValue > *exception=nullptr)
size_t ArgumentCount(const CallbackArguments &arguments)
void Info(const CallbackArguments &arguments) const
static std::string GetJsStack()
void Warn(const CallbackArguments &arguments) const
void Log(const CallbackArguments &arguments) const
void Debug(const CallbackArguments &arguments) const
proto::ValueType GetValueType(Handle< JsValue > value)
std::string ConvertToString(Handle< JsValue > value)
void Error(const CallbackArguments &arguments) const
size_t ArrayLength(Handle< JsObject > value)
static std::string ConvertToPrettyString(Handle< JsValue > value)
void Assert(Any cond, const CallbackArguments &arguments) const
ReturnVal< JsValue > GetMemberRaw(Handle< JsObject > object, const std::string &name, LocalVar< JsValue > *exception=nullptr)
bool IsBuiltInObject(Handle< JsObject > object)