Apache Mesos
exit.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 __STOUT_EXIT_HPP__
14 #define __STOUT_EXIT_HPP__
15 
16 #include <stdlib.h>
17 
18 #include <ostream>
19 
20 #include <glog/logging.h>
21 #include <glog/raw_logging.h>
22 
23 #include <stout/attributes.hpp>
24 
25 
26 // Exit takes an exit status and provides a glog stream for output
27 // prior to exiting. This is like glog's LOG(FATAL) or CHECK, except
28 // that it does _not_ print a stack trace.
29 //
30 // Ex: EXIT(EXIT_FAILURE) << "Cgroups are not present in this system.";
31 #define EXIT(status) __Exit(__FILE__, __LINE__, status).stream()
32 
33 // Async-signal safe exit which prints a message.
34 //
35 // NOTE: We use RAW_LOG instead of LOG because RAW_LOG doesn't
36 // allocate any memory or grab locks. And according to
37 // https://code.google.com/p/google-glog/issues/detail?id=161
38 // it should work in 'most' cases in signal handlers.
39 //
40 // NOTE: We expect that compiler supports `,##__VA_ARGS__`, see:
41 // https://stackoverflow.com/questions/5588855
42 #define SAFE_EXIT(status, fmt, ...) \
43  do { \
44  if (status) { \
45  RAW_LOG(ERROR, "EXIT with status %d: " fmt, status, ##__VA_ARGS__); \
46  } else { \
47  RAW_LOG(INFO, "EXIT with status %d: " fmt, status, ##__VA_ARGS__); \
48  } \
49  ::_exit(status); \
50  } while (0)
51 
52 
53 struct __Exit
54 {
55  __Exit(const char* file, int line, int _status)
56  : status(_status),
57  message(
58  file,
59  line,
60  _status == EXIT_SUCCESS ? google::GLOG_INFO : google::GLOG_ERROR)
61  {
62  stream() << "EXIT with status " << _status << ": ";
63  }
64 
66  {
67  message.Flush();
68  exit(status);
69  }
70 
71  std::ostream& stream()
72  {
73  return message.stream();
74  }
75 
76  const int status;
77  google::LogMessage message;
78 };
79 
80 
81 #endif // __STOUT_EXIT_HPP__
NORETURN ~__Exit()
Definition: exit.hpp:65
std::ostream & stream()
Definition: exit.hpp:71
Definition: exit.hpp:53
const int status
Definition: exit.hpp:76
URI file(const std::string &path)
Creates a file URI with the given path on the local host.
Definition: file.hpp:33
google::LogMessage message
Definition: exit.hpp:77
__Exit(const char *file, int line, int _status)
Definition: exit.hpp:55
Definition: protobuf.hpp:68
#define NORETURN
Definition: attributes.hpp:20