YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/util/logging_test_util.h
Line
Count
Source (jump to first uncovered line)
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
//
18
// The following only applies to changes made to this file as part of YugaByte development.
19
//
20
// Portions Copyright (c) YugaByte, Inc.
21
//
22
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
23
// in compliance with the License.  You may obtain a copy of the License at
24
//
25
// http://www.apache.org/licenses/LICENSE-2.0
26
//
27
// Unless required by applicable law or agreed to in writing, software distributed under the License
28
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
29
// or implied.  See the License for the specific language governing permissions and limitations
30
// under the License.
31
//
32
33
#ifndef YB_UTIL_LOGGING_TEST_UTIL_H
34
#define YB_UTIL_LOGGING_TEST_UTIL_H
35
36
#include <atomic>
37
#include <chrono>
38
#include <string>
39
#include <vector>
40
41
#include <glog/logging.h>
42
43
#include "yb/util/monotime.h"
44
#include "yb/util/status_fwd.h"
45
46
namespace yb {
47
48
using namespace std::literals;
49
50
// GLog sink that keeps an internal buffer of messages that have been logged.
51
class StringVectorSink : public google::LogSink {
52
 public:
53
  void send(google::LogSeverity severity, const char* full_filename,
54
            const char* base_filename, int line,
55
            const struct ::tm* tm_time,
56
3
            const char* message, size_t message_len) override {
57
3
    logged_msgs_.push_back(ToString(severity, base_filename, line,
58
3
                                    tm_time, message, message_len));
59
3
  }
60
61
773
  const std::vector<std::string>& logged_msgs() const {
62
773
    return logged_msgs_;
63
773
  }
64
65
 private:
66
  std::vector<std::string> logged_msgs_;
67
};
68
69
// GLog sink that waits for specified string to appear in log.
70
class StringWaiterLogSink : public google::LogSink {
71
 public:
72
  explicit StringWaiterLogSink(const std::string& string_to_wait)
73
6
      : string_to_wait_(string_to_wait) {
74
6
    google::AddLogSink(this);
75
6
  }
76
77
  // Wait for string_to_wait to occur in log.
78
  CHECKED_STATUS WaitFor(MonoDelta timeout);
79
80
  void send(
81
      google::LogSeverity severity, const char* full_filename, const char* base_filename, int line,
82
      const struct ::tm* tm_time, const char* message, size_t message_len) override;
83
84
0
  bool IsEventOccurred() { return event_occurred_; }
85
86
6
  ~StringWaiterLogSink() { google::RemoveLogSink(this); }
87
88
 private:
89
  static const char* kWaitingMessage;
90
  std::string string_to_wait_;
91
  std::atomic<bool> event_occurred_{false};
92
};
93
94
// RAII wrapper around registering a LogSink with GLog.
95
struct ScopedRegisterSink {
96
2
  explicit ScopedRegisterSink(google::LogSink* s) : s_(s) {
97
2
    google::AddLogSink(s_);
98
2
  }
99
2
  ~ScopedRegisterSink() {
100
2
    google::RemoveLogSink(s_);
101
2
  }
102
103
  google::LogSink* s_;
104
};
105
106
} // namespace yb
107
108
#endif  // YB_UTIL_LOGGING_TEST_UTIL_H