2 +----------------------------------------------------------------------+
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2016 Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
17 #include "hphp/util/struct-log.h"
19 #include <folly/Random.h>
21 #include "hphp/util/stack-trace.h"
25 StructuredLogEntry::StructuredLogEntry()
26 : ints(folly::dynamic::object())
27 , strs(folly::dynamic::object())
28 , sets(folly::dynamic::object())
29 , vecs(folly::dynamic::object())
33 void StructuredLogEntry::setInt(folly::StringPiece key
, int64_t value
) {
37 void StructuredLogEntry::setStr(folly::StringPiece key
,
38 folly::StringPiece value
) {
42 void StructuredLogEntry::setSet(folly::StringPiece key
,
43 const std::set
<folly::StringPiece
>& value
) {
44 sets
[key
] = folly::dynamic::object();
45 for (auto const& v
: value
) sets
[key
][v
] = 1;
48 void StructuredLogEntry::setVec(folly::StringPiece key
,
49 const std::vector
<folly::StringPiece
>& value
) {
50 folly::dynamic arr
= folly::dynamic::array();
51 arr
.resize(value
.size());
52 for (int i
= 0; i
< value
.size(); ++i
) {
58 void StructuredLogEntry::setStackTrace(folly::StringPiece key
,
59 const StackTrace
& st
) {
60 std::vector
<folly::StringPiece
> stackFrames
;
61 folly::split("\n", st
.toString(), stackFrames
);
62 for (auto& frame
: stackFrames
) {
63 const char* p
= frame
.begin();
64 while (*p
== '#' || *p
== ' ' || (*p
>= '0' && *p
<= '9')) ++p
;
65 frame
= folly::StringPiece(p
, frame
.end());
67 setVec(key
, stackFrames
);
70 void StructuredLogEntry::clear() {
71 ints
= folly::dynamic::object();
72 strs
= folly::dynamic::object();
73 sets
= folly::dynamic::object();
74 vecs
= folly::dynamic::object();
77 namespace StructuredLog
{
79 StructuredLogImpl s_impl
= nullptr;
83 return s_impl
!= nullptr;
86 bool coinflip(uint32_t rate
) {
87 return enabled() && rate
> 0 && folly::Random::rand32(rate
) == 0;
90 void enable(StructuredLogImpl impl
) {
94 void log(const std::string
& tableName
, const StructuredLogEntry
& cols
) {
96 s_impl(tableName
, cols
);
101 std::string
show(const StructuredLogEntry
& cols
) {
102 folly::dynamic out
= cols
.strs
;
103 out
["ints"] = cols
.ints
;
104 out
["sets"] = cols
.sets
;
105 out
["vecs"] = cols
.vecs
;
106 std::ostringstream oss
;