DDLogValue.cpp (4555B)
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim:set ts=2 sw=2 sts=2 et cindent: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include "DDLogValue.h" 8 9 #include "mozilla/JSONWriter.h" 10 11 namespace mozilla { 12 13 struct LogValueMatcher { 14 nsCString& mString; 15 16 void operator()(const DDNoValue&) const {} 17 void operator()(const DDLogObject& a) const { a.AppendPrintf(mString); } 18 void operator()(const char* a) const { mString.AppendPrintf(R"("%s")", a); } 19 void operator()(const nsCString& a) const { 20 mString.AppendPrintf(R"(nsCString("%s"))", a.Data()); 21 } 22 void operator()(bool a) const { mString.AppendPrintf(a ? "true" : "false"); } 23 void operator()(int8_t a) const { 24 mString.AppendPrintf("int8_t(%" PRIi8 ")", a); 25 } 26 void operator()(uint8_t a) const { 27 mString.AppendPrintf("uint8_t(%" PRIu8 ")", a); 28 } 29 void operator()(int16_t a) const { 30 mString.AppendPrintf("int16_t(%" PRIi16 ")", a); 31 } 32 void operator()(uint16_t a) const { 33 mString.AppendPrintf("uint16_t(%" PRIu16 ")", a); 34 } 35 void operator()(int32_t a) const { 36 mString.AppendPrintf("int32_t(%" PRIi32 ")", a); 37 } 38 void operator()(uint32_t a) const { 39 mString.AppendPrintf("uint32_t(%" PRIu32 ")", a); 40 } 41 void operator()(int64_t a) const { 42 mString.AppendPrintf("int64_t(%" PRIi64 ")", a); 43 } 44 void operator()(uint64_t a) const { 45 mString.AppendPrintf("uint64_t(%" PRIu64 ")", a); 46 } 47 void operator()(double a) const { mString.AppendPrintf("double(%f)", a); } 48 void operator()(const DDRange& a) const { 49 mString.AppendPrintf("%" PRIi64 "<=(%" PRIi64 "B)<%" PRIi64 "", a.mOffset, 50 a.mBytes, a.mOffset + a.mBytes); 51 } 52 void operator()(const nsresult& a) const { 53 nsCString name; 54 GetErrorName(a, name); 55 mString.AppendPrintf("nsresult(%s =0x%08" PRIx32 ")", name.get(), 56 static_cast<uint32_t>(a)); 57 } 58 void operator()(const MediaResult& a) const { 59 nsCString name; 60 GetErrorName(a.Code(), name); 61 mString.AppendPrintf("MediaResult(%s =0x%08" PRIx32 ", \"%s\")", name.get(), 62 static_cast<uint32_t>(a.Code()), a.Message().get()); 63 } 64 }; 65 66 void AppendToString(const DDLogValue& aValue, nsCString& aString) { 67 aValue.match(LogValueMatcher{aString}); 68 } 69 70 struct LogValueMatcherJson { 71 JSONWriter& mJW; 72 const Span<const char> mPropertyName; 73 74 void operator()(const DDNoValue&) const { mJW.NullProperty(mPropertyName); } 75 void operator()(const DDLogObject& a) const { 76 nsPrintfCString s(R"("%s[%p]")", a.TypeName(), a.Pointer()); 77 mJW.StringProperty(mPropertyName, s); 78 } 79 void operator()(const char* a) const { 80 mJW.StringProperty(mPropertyName, MakeStringSpan(a)); 81 } 82 void operator()(const nsCString& a) const { 83 mJW.StringProperty(mPropertyName, a); 84 } 85 void operator()(bool a) const { mJW.BoolProperty(mPropertyName, a); } 86 void operator()(int8_t a) const { mJW.IntProperty(mPropertyName, a); } 87 void operator()(uint8_t a) const { mJW.IntProperty(mPropertyName, a); } 88 void operator()(int16_t a) const { mJW.IntProperty(mPropertyName, a); } 89 void operator()(uint16_t a) const { mJW.IntProperty(mPropertyName, a); } 90 void operator()(int32_t a) const { mJW.IntProperty(mPropertyName, a); } 91 void operator()(uint32_t a) const { mJW.IntProperty(mPropertyName, a); } 92 void operator()(int64_t a) const { mJW.IntProperty(mPropertyName, a); } 93 void operator()(uint64_t a) const { mJW.DoubleProperty(mPropertyName, a); } 94 void operator()(double a) const { mJW.DoubleProperty(mPropertyName, a); } 95 void operator()(const DDRange& a) const { 96 mJW.StartArrayProperty(mPropertyName); 97 mJW.IntElement(a.mOffset); 98 mJW.IntElement(a.mOffset + a.mBytes); 99 mJW.EndArray(); 100 } 101 void operator()(const nsresult& a) const { 102 nsCString name; 103 GetErrorName(a, name); 104 mJW.StringProperty(mPropertyName, name); 105 } 106 void operator()(const MediaResult& a) const { 107 nsCString name; 108 GetErrorName(a.Code(), name); 109 mJW.StringProperty(mPropertyName, 110 nsPrintfCString(R"lit("MediaResult(%s, %s)")lit", 111 name.get(), a.Message().get())); 112 } 113 }; 114 115 void ToJSON(const DDLogValue& aValue, JSONWriter& aJSONWriter, 116 const char* aPropertyName) { 117 aValue.match(LogValueMatcherJson{aJSONWriter, MakeStringSpan(aPropertyName)}); 118 } 119 120 } // namespace mozilla