Apache Mesos
logging.hpp
Go to the documentation of this file.
1 // Licensed under the Apache License, Version 2.0 (the "License");
2 // you may not use this file except in compliance with the License.
3 // You may obtain a copy of the License at
4 //
5 // http://www.apache.org/licenses/LICENSE-2.0
6 //
7 // Unless required by applicable law or agreed to in writing, software
8 // distributed under the License is distributed on an "AS IS" BASIS,
9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 // See the License for the specific language governing permissions and
11 // limitations under the License
12 
13 #ifndef __PROCESS_LOGGING_HPP__
14 #define __PROCESS_LOGGING_HPP__
15 
16 #include <glog/logging.h>
17 
18 #include <process/future.hpp>
19 #include <process/http.hpp>
20 #include <process/process.hpp>
21 #include <process/timeout.hpp>
22 
23 namespace process {
24 
25 class Logging : public Process<Logging>
26 {
27 public:
28  Logging(Option<std::string> _authenticationRealm)
29  : ProcessBase("logging"),
30  original(FLAGS_v),
31  authenticationRealm(_authenticationRealm)
32  {
33  // Make sure all reads/writes can be done atomically (i.e., to
34  // make sure VLOG(*) statements don't read partial writes).
35  // TODO(benh): Use "atomics" primitives for doing reads/writes of
36  // FLAGS_v anyway to account for proper memory barriers.
37  CHECK(sizeof(FLAGS_v) == sizeof(int32_t));
38  }
39 
40  virtual ~Logging() {}
41 
42  Future<Nothing> set_level(int level, const Duration& duration);
43 
44 protected:
45  virtual void initialize()
46  {
47  if (authenticationRealm.isSome()) {
48  route("/toggle", authenticationRealm.get(), TOGGLE_HELP(), &This::toggle);
49  } else {
50  route("/toggle",
51  TOGGLE_HELP(),
52  [this](const http::Request& request) {
53  return This::toggle(request, None());
54  });
55  }
56  }
57 
58 private:
60  const http::Request& request,
62 
63  void set(int v)
64  {
65  if (FLAGS_v != v) {
66  VLOG(FLAGS_v) << "Setting verbose logging level to " << v;
67  FLAGS_v = v;
68 
69  // Ensure 'FLAGS_v' visible in other threads.
70 #ifdef __WINDOWS__
71  MemoryBarrier();
72 #else
73  __sync_synchronize();
74 #endif // __WINDOWS__
75  }
76  }
77 
78  void revert()
79  {
80  if (timeout.remaining() == Seconds(0)) {
81  set(original);
82  }
83  }
84 
85  static const std::string TOGGLE_HELP();
86 
87  Timeout timeout;
88 
89  const int32_t original; // Original value of FLAGS_v.
90 
91  // The authentication realm that the `/logging/toggle` endpoint will be
92  // installed into.
93  const Option<std::string> authenticationRealm;
94 };
95 
96 } // namespace process {
97 
98 #endif // __PROCESS_LOGGING_HPP__
virtual void initialize()
Invoked when a process gets spawned.
Definition: logging.hpp:45
Future< Response > request(const Request &request, bool streamedResponse=false)
Asynchronously sends an HTTP request to the process and returns the HTTP response once the entire res...
Logging(Option< std::string > _authenticationRealm)
Definition: logging.hpp:28
Definition: process.hpp:72
void route(const std::string &name, const Option< std::string > &help, const HttpRequestHandler &handler, const RouteOptions &options=RouteOptions())
Sets up a handler for HTTP requests with the specified name.
Definition: duration.hpp:32
bool isSome() const
Definition: option.hpp:115
Definition: http.hpp:518
Definition: logging.hpp:25
Definition: duration.hpp:259
const T & get() const &
Definition: option.hpp:118
Result< Process > process(pid_t pid)
Definition: freebsd.hpp:30
Definition: none.hpp:27
Future< Nothing > set_level(int level, const Duration &duration)
Definition: process.hpp:493
virtual ~Logging()
Definition: logging.hpp:40
Duration remaining() const
Definition: timeout.hpp:77