|
| 1 | +/** |
| 2 | + * Copyright (C) 2019-present MongoDB, Inc. |
| 3 | + * |
| 4 | + * This program is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the Server Side Public License, version 1, |
| 6 | + * as published by MongoDB, Inc. |
| 7 | + * |
| 8 | + * This program is distributed in the hope that it will be useful, |
| 9 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | + * Server Side Public License for more details. |
| 12 | + * |
| 13 | + * You should have received a copy of the Server Side Public License |
| 14 | + * along with this program. If not, see |
| 15 | + * <http://www.mongodb.com/licensing/server-side-public-license>. |
| 16 | + * |
| 17 | + * As a special exception, the copyright holders give permission to link the |
| 18 | + * code of portions of this program with the OpenSSL library under certain |
| 19 | + * conditions as described in each individual source file and distribute |
| 20 | + * linked combinations including the program with the OpenSSL library. You |
| 21 | + * must comply with the Server Side Public License in all respects for |
| 22 | + * all of the code used other than as permitted herein. If you modify file(s) |
| 23 | + * with this exception, you may extend this exception to your version of the |
| 24 | + * file(s), but you are not obligated to do so. If you do not wish to do so, |
| 25 | + * delete this exception statement from your version. If you delete this |
| 26 | + * exception statement from all source files in the program, then also delete |
| 27 | + * it in the license file. |
| 28 | + */ |
| 29 | + |
| 30 | +#pragma once |
| 31 | + |
| 32 | +#include <array> |
| 33 | +#include <ostream> |
| 34 | + |
| 35 | +#include "mongo/base/string_data.h" |
| 36 | +#include "mongo/bson/bsonelement.h" |
| 37 | +#include "mongo/util/stacktrace.h" |
| 38 | + |
| 39 | +namespace mongo::stacktrace_detail { |
| 40 | + |
| 41 | +/** |
| 42 | + * A utility for uint64_t <=> uppercase hex string conversions. It |
| 43 | + * can be used to produce a StringData. |
| 44 | + * |
| 45 | + * sink << Hex(x).str(); // as a temporary |
| 46 | + * |
| 47 | + * Hex hx(x); |
| 48 | + * StringData sd = hx.str() // sd storage is in `hx`. |
| 49 | + */ |
| 50 | +class Hex { |
| 51 | +public: |
| 52 | + using Buf = std::array<char, 16>; |
| 53 | + |
| 54 | + static StringData toHex(uint64_t x, Buf& buf); |
| 55 | + |
| 56 | + static uint64_t fromHex(StringData s); |
| 57 | + |
| 58 | + explicit Hex(uint64_t x) : _str(toHex(x, _buf)) {} |
| 59 | + |
| 60 | + StringData str() const { |
| 61 | + return _str; |
| 62 | + } |
| 63 | + |
| 64 | +private: |
| 65 | + Buf _buf; |
| 66 | + StringData _str; |
| 67 | +}; |
| 68 | + |
| 69 | +/** An append-only, async-safe, malloc-free Json emitter. */ |
| 70 | +class CheapJson { |
| 71 | +public: |
| 72 | + class Value; |
| 73 | + |
| 74 | + explicit CheapJson(StackTraceSink& sink); |
| 75 | + |
| 76 | + // Create an empty JSON document. |
| 77 | + Value doc(); |
| 78 | + |
| 79 | +private: |
| 80 | + StackTraceSink& _sink; |
| 81 | +}; |
| 82 | + |
| 83 | +/** |
| 84 | + * A Json Value node being emitted. Emits {}/[] braces, keyval ":" separators, commas, |
| 85 | + * and quotes. To use this, make a Value for the root document and call `append*` |
| 86 | + * members, adding a nested structure of objects, arrays, and scalars to the active |
| 87 | + * Value. |
| 88 | + * |
| 89 | + * The constructor emits any appropriate opening brace, and the destructor emits any |
| 90 | + * appropriate closing brace. Keeps an internal state so that a comma is emitted on the |
| 91 | + * second and subsequent append call. |
| 92 | + */ |
| 93 | +class CheapJson::Value { |
| 94 | +public: |
| 95 | + /** The empty root document, which emits no braces. */ |
| 96 | + explicit Value(CheapJson* env) : Value(env, kNop) {} |
| 97 | + |
| 98 | + /** Emit the closing brace if any. */ |
| 99 | + ~Value(); |
| 100 | + |
| 101 | + /** Begin a Json Array. Returns a Value that can be used to append elements to it. */ |
| 102 | + Value appendArr(); |
| 103 | + |
| 104 | + /** Begin a Json Object. Returns a Value that can be used to append elements to it. */ |
| 105 | + Value appendObj(); |
| 106 | + |
| 107 | + /** Append key `k` to this Json Object. Returns the empty Value mapped to `k`. */ |
| 108 | + Value appendKey(StringData k); |
| 109 | + |
| 110 | + /** Append string `v`, surrounded by doublequote characters. */ |
| 111 | + void append(StringData v); |
| 112 | + |
| 113 | + /** Append integer `v`, in decimal. */ |
| 114 | + void append(uint64_t v); |
| 115 | + |
| 116 | + /** |
| 117 | + * Append the elements of `be` to this Object or Array. |
| 118 | + * Behavior depends on the kind of Value this is. |
| 119 | + * - If object: append `be` keys and values. |
| 120 | + * - If array: append `be` values only. |
| 121 | + */ |
| 122 | + void append(const BSONElement& be); |
| 123 | + |
| 124 | +private: |
| 125 | + enum Kind { |
| 126 | + kNop, // A blank Value, not an aggregate, emits no punctuation. Can emit one element. |
| 127 | + kObj, // Object: can emit multiple key-value pairs: {k1:v1, k2:v2, ...} |
| 128 | + kArr, // Array: can emit multiple scalars [v1, v2, ...] |
| 129 | + }; |
| 130 | + |
| 131 | + /* Emit the opening brace corresponding to the specified `k`. */ |
| 132 | + Value(CheapJson* env, Kind k); |
| 133 | + void _copyBsonElementValue(const BSONElement& be); |
| 134 | + void _next(); |
| 135 | + |
| 136 | + CheapJson* _env; |
| 137 | + Kind _kind; |
| 138 | + StringData _sep; // Emitted upon append. Starts empty, then set to ",". |
| 139 | +}; |
| 140 | + |
| 141 | +} // namespace mongo::stacktrace_detail |
0 commit comments