move getMapIdByValue to FieldMask.h
[hiphop-php.git] / hphp / util / struct-log.cpp
blob79b403e5029eb5edd910258821f196e998033a24
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
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 <sstream>
21 #include <folly/Random.h>
22 #include <folly/json.h>
24 #include "hphp/util/assertions.h"
25 #include "hphp/util/stack-trace.h"
27 namespace HPHP {
29 StructuredLogEntry::StructuredLogEntry()
30 : ints(folly::dynamic::object())
31 , strs(folly::dynamic::object())
32 , sets(folly::dynamic::object())
33 , vecs(folly::dynamic::object())
37 void StructuredLogEntry::setInt(folly::StringPiece key, int64_t value) {
38 ints[key] = value;
41 void StructuredLogEntry::setStr(folly::StringPiece key,
42 folly::StringPiece value) {
43 strs[key] = value;
46 void StructuredLogEntry::setSet(folly::StringPiece key,
47 const std::set<folly::StringPiece>& value) {
48 sets[key] = folly::dynamic::object();
49 for (auto const& v : value) sets[key][v] = 1;
52 void StructuredLogEntry::setVec(folly::StringPiece key,
53 const std::vector<folly::StringPiece>& value) {
54 folly::dynamic arr = folly::dynamic::array();
55 arr.resize(value.size());
56 for (int i = 0; i < value.size(); ++i) {
57 arr[i] = value[i];
59 vecs[key] = arr;
62 void StructuredLogEntry::setStackTrace(folly::StringPiece key,
63 const StackTrace& st) {
64 std::vector<folly::StringPiece> stackFrames;
65 folly::split("\n", st.toString(), stackFrames);
66 for (auto& frame : stackFrames) {
67 const char* p = frame.begin();
68 while (*p == '#' || *p == ' ' || (*p >= '0' && *p <= '9')) ++p;
69 frame = folly::StringPiece(p, frame.end());
71 setVec(key, stackFrames);
74 void StructuredLogEntry::clear() {
75 ints = folly::dynamic::object();
76 strs = folly::dynamic::object();
77 sets = folly::dynamic::object();
78 vecs = folly::dynamic::object();
81 namespace StructuredLog {
82 namespace {
83 LogFn s_log = nullptr;
84 RecordGlobalsFn s_recordGlobals = nullptr;
87 bool enabled() {
88 return s_log != nullptr;
91 bool coinflip(uint32_t rate) {
92 return enabled() && rate > 0 && folly::Random::rand32(rate) == 0;
95 void enable(LogFn log, RecordGlobalsFn globals) {
96 assertx(log && globals);
97 s_log = log;
98 s_recordGlobals = globals;
101 void log(const std::string& tableName, const StructuredLogEntry& cols) {
102 if (enabled()) s_log(tableName, cols);
105 void recordRequestGlobals(StructuredLogEntry& cols) {
106 if (enabled()) s_recordGlobals(cols);
110 std::string show(const StructuredLogEntry& cols) {
111 folly::dynamic out = cols.strs;
112 out["ints"] = cols.ints;
113 out["sets"] = cols.sets;
114 out["vecs"] = cols.vecs;
115 return folly::toJson(out);