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  ~Logging() override {}
41 
42  Future<Nothing> set_level(int level, const Duration& duration);
43 
44 protected:
45  void initialize() override
46  {
47  route("/toggle", authenticationRealm, TOGGLE_HELP(), &This::toggle);
48  }
49 
50 private:
52  const http::Request& request,
54 
55  void set(int v)
56  {
57  if (FLAGS_v != v) {
58  VLOG(FLAGS_v) << "Setting verbose logging level to " << v;
59  FLAGS_v = v;
60 
61  // Ensure 'FLAGS_v' visible in other threads.
62 #ifdef __WINDOWS__
63  MemoryBarrier();
64 #else
65  __sync_synchronize();
66 #endif // __WINDOWS__
67  }
68  }
69 
70  void revert()
71  {
72  if (timeout.remaining() == Seconds(0)) {
73  set(original);
74  }
75  }
76 
77  static const std::string TOGGLE_HELP();
78 
79  Timeout timeout;
80 
81  const int32_t original; // Original value of FLAGS_v.
82 
83  // The authentication realm that the `/logging/toggle` endpoint will be
84  // installed into.
85  const Option<std::string> authenticationRealm;
86 };
87 
88 } // namespace process {
89 
90 #endif // __PROCESS_LOGGING_HPP__
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.
~Logging() override
Definition: logging.hpp:40
Definition: duration.hpp:32
Definition: http.hpp:533
Definition: logging.hpp:25
Definition: duration.hpp:207
Future< Nothing > set_level(int level, const Duration &duration)
Definition: timeout.hpp:24
Definition: executor.hpp:48
void initialize() override
Invoked when a process gets spawned.
Definition: logging.hpp:45
Definition: process.hpp:505
Duration remaining() const
Definition: timeout.hpp:93