Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/media/doctor/DDLogValue.cpp
Line
Count
Source (jump to first uncovered line)
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
{
15
  nsCString& mString;
16
17
0
  void match(const DDNoValue&) const {}
18
0
  void match(const DDLogObject& a) const { a.AppendPrintf(mString); }
19
0
  void match(const char* a) const { mString.AppendPrintf(R"("%s")", a); }
20
  void match(const nsCString& a) const
21
0
  {
22
0
    mString.AppendPrintf(R"(nsCString("%s"))", a.Data());
23
0
  }
24
0
  void match(bool a) const { mString.AppendPrintf(a ? "true" : "false"); }
25
0
  void match(int8_t a) const { mString.AppendPrintf("int8_t(%" PRIi8 ")", a); }
26
  void match(uint8_t a) const
27
0
  {
28
0
    mString.AppendPrintf("uint8_t(%" PRIu8 ")", a);
29
0
  }
30
  void match(int16_t a) const
31
0
  {
32
0
    mString.AppendPrintf("int16_t(%" PRIi16 ")", a);
33
0
  }
34
  void match(uint16_t a) const
35
0
  {
36
0
    mString.AppendPrintf("uint16_t(%" PRIu16 ")", a);
37
0
  }
38
  void match(int32_t a) const
39
0
  {
40
0
    mString.AppendPrintf("int32_t(%" PRIi32 ")", a);
41
0
  }
42
  void match(uint32_t a) const
43
0
  {
44
0
    mString.AppendPrintf("uint32_t(%" PRIu32 ")", a);
45
0
  }
46
  void match(int64_t a) const
47
0
  {
48
0
    mString.AppendPrintf("int64_t(%" PRIi64 ")", a);
49
0
  }
50
  void match(uint64_t a) const
51
0
  {
52
0
    mString.AppendPrintf("uint64_t(%" PRIu64 ")", a);
53
0
  }
54
0
  void match(double a) const { mString.AppendPrintf("double(%f)", a); }
55
  void match(const DDRange& a) const
56
0
  {
57
0
    mString.AppendPrintf("%" PRIi64 "<=(%" PRIi64 "B)<%" PRIi64 "",
58
0
                         a.mOffset,
59
0
                         a.mBytes,
60
0
                         a.mOffset + a.mBytes);
61
0
  }
62
  void match(const nsresult& a) const
63
0
  {
64
0
    nsCString name;
65
0
    GetErrorName(a, name);
66
0
    mString.AppendPrintf(
67
0
      "nsresult(%s =0x%08" PRIx32 ")", name.get(), static_cast<uint32_t>(a));
68
0
  }
69
  void match(const MediaResult& a) const
70
0
  {
71
0
    nsCString name;
72
0
    GetErrorName(a.Code(), name);
73
0
    mString.AppendPrintf("MediaResult(%s =0x%08" PRIx32 ", \"%s\")",
74
0
                         name.get(),
75
0
                         static_cast<uint32_t>(a.Code()),
76
0
                         a.Message().get());
77
0
  }
78
};
79
80
void
81
AppendToString(const DDLogValue& aValue, nsCString& aString)
82
0
{
83
0
  aValue.match(LogValueMatcher{ aString });
84
0
}
85
86
struct LogValueMatcherJson
87
{
88
  JSONWriter& mJW;
89
  const char* mPropertyName;
90
91
0
  void match(const DDNoValue&) const { mJW.NullProperty(mPropertyName); }
92
  void match(const DDLogObject& a) const
93
0
  {
94
0
    mJW.StringProperty(
95
0
      mPropertyName,
96
0
      nsPrintfCString(R"("%s[%p]")", a.TypeName(), a.Pointer()).get());
97
0
  }
98
0
  void match(const char* a) const { mJW.StringProperty(mPropertyName, a); }
99
  void match(const nsCString& a) const
100
0
  {
101
0
    mJW.StringProperty(mPropertyName, a.Data());
102
0
  }
103
0
  void match(bool a) const { mJW.BoolProperty(mPropertyName, a); }
104
0
  void match(int8_t a) const { mJW.IntProperty(mPropertyName, a); }
105
0
  void match(uint8_t a) const { mJW.IntProperty(mPropertyName, a); }
106
0
  void match(int16_t a) const { mJW.IntProperty(mPropertyName, a); }
107
0
  void match(uint16_t a) const { mJW.IntProperty(mPropertyName, a); }
108
0
  void match(int32_t a) const { mJW.IntProperty(mPropertyName, a); }
109
0
  void match(uint32_t a) const { mJW.IntProperty(mPropertyName, a); }
110
0
  void match(int64_t a) const { mJW.IntProperty(mPropertyName, a); }
111
0
  void match(uint64_t a) const { mJW.DoubleProperty(mPropertyName, a); }
112
0
  void match(double a) const { mJW.DoubleProperty(mPropertyName, a); }
113
  void match(const DDRange& a) const
114
0
  {
115
0
    mJW.StartArrayProperty(mPropertyName);
116
0
    mJW.IntElement(a.mOffset);
117
0
    mJW.IntElement(a.mOffset + a.mBytes);
118
0
    mJW.EndArray();
119
0
  }
120
  void match(const nsresult& a) const
121
0
  {
122
0
    nsCString name;
123
0
    GetErrorName(a, name);
124
0
    mJW.StringProperty(mPropertyName, name.get());
125
0
  }
126
  void match(const MediaResult& a) const
127
0
  {
128
0
    nsCString name;
129
0
    GetErrorName(a.Code(), name);
130
0
    mJW.StringProperty(mPropertyName,
131
0
                       nsPrintfCString(R"lit("MediaResult(%s, %s)")lit",
132
0
                                       name.get(),
133
0
                                       a.Message().get())
134
0
                         .get());
135
0
  }
136
};
137
138
void
139
ToJSON(const DDLogValue& aValue,
140
       JSONWriter& aJSONWriter,
141
       const char* aPropertyName)
142
0
{
143
0
  aValue.match(LogValueMatcherJson{ aJSONWriter, aPropertyName });
144
0
}
145
146
} // namespace mozilla