Apache Mesos
logrotate.hpp
Go to the documentation of this file.
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, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 
17 #ifndef __SLAVE_CONTAINER_LOGGER_LOGROTATE_HPP__
18 #define __SLAVE_CONTAINER_LOGGER_LOGROTATE_HPP__
19 
20 #include <stdio.h>
21 #include <unistd.h>
22 
23 #include <stout/bytes.hpp>
24 #include <stout/error.hpp>
25 #include <stout/flags.hpp>
26 #include <stout/option.hpp>
27 #include <stout/path.hpp>
28 
29 #include <stout/os/constants.hpp>
30 #include <stout/os/pagesize.hpp>
31 
32 
33 namespace mesos {
34 namespace internal {
35 namespace logger {
36 namespace rotate {
37 
38 const std::string NAME = "mesos-logrotate-logger";
39 const std::string CONF_SUFFIX = ".logrotate.conf";
40 const std::string STATE_SUFFIX = ".logrotate.state";
41 
42 struct Flags : public virtual flags::FlagsBase
43 {
45  {
47  "Usage: " + NAME + " [options]\n"
48  "\n"
49  "This command pipes from STDIN to the given leading log file.\n"
50  "When the leading log file reaches '--max_size', the command.\n"
51  "uses 'logrotate' to rotate the logs. All 'logrotate' options\n"
52  "are supported. See '--logrotate_options'.\n"
53  "\n");
54 
56  "max_size",
57  "Maximum size, in bytes, of a single log file.\n"
58  "Defaults to 10 MB. Must be at least 1 (memory) page.",
59  Megabytes(10),
60  [](const Bytes& value) -> Option<Error> {
61  if (value.bytes() < os::pagesize()) {
62  return Error(
63  "Expected --max_size of at least " +
64  stringify(os::pagesize()) + " bytes");
65  }
66  return None();
67  });
68 
70  "logrotate_options",
71  "Additional config options to pass into 'logrotate'.\n"
72  "This string will be inserted into a 'logrotate' configuration file.\n"
73  "i.e.\n"
74  " /path/to/<log_filename> {\n"
75  " <logrotate_options>\n"
76  " size <max_size>\n"
77  " }\n"
78  "NOTE: The 'size' option will be overridden by this command.");
79 
81  "log_filename",
82  "Absolute path to the leading log file.\n"
83  "NOTE: This command will also create two files by appending\n"
84  "'" + CONF_SUFFIX + "' and '" + STATE_SUFFIX + "' to the end of\n"
85  "'--log_filename'. These files are used by 'logrotate'.",
86  [](const Option<std::string>& value) -> Option<Error> {
87  if (value.isNone()) {
88  return Error("Missing required option --log_filename");
89  }
90 
91  if (!path::absolute(value.get())) {
92  return Error("Expected --log_filename to be an absolute path");
93  }
94 
95  return None();
96  });
97 
99  "logrotate_path",
100  "If specified, this command will use the specified\n"
101  "'logrotate' instead of the system's 'logrotate'.",
102  "logrotate",
103  [](const std::string& value) -> Option<Error> {
104  // Check if `logrotate` exists via the help command.
105  // TODO(josephw): Consider a more comprehensive check.
106  Try<std::string> helpCommand =
107  os::shell(value + " --help > " + os::DEV_NULL);
108 
109  if (helpCommand.isError()) {
110  return Error(
111  "Failed to check logrotate: " + helpCommand.error());
112  }
113 
114  return None();
115  });
116 
117  add(&Flags::user,
118  "user",
119  "The user this command should run as.");
120  }
121 
125  std::string logrotate_path;
127 };
128 
129 } // namespace rotate {
130 } // namespace logger {
131 } // namespace internal {
132 } // namespace mesos {
133 
134 #endif // __SLAVE_CONTAINER_LOGGER_LOGROTATE_HPP__
Definition: errorbase.hpp:36
Definition: check.hpp:33
Definition: logrotate.hpp:42
size_t pagesize()
Definition: pagesize.hpp:24
constexpr Bytes Megabytes(uint64_t value)
Definition: bytes.hpp:123
const std::string STATE_SUFFIX
Definition: logrotate.hpp:40
Bytes max_size
Definition: logrotate.hpp:122
const std::string NAME
Definition: logrotate.hpp:38
const std::string CONF_SUFFIX
Definition: logrotate.hpp:39
bool absolute(const std::string &path)
Returns whether the given path is an absolute path.
Definition: path.hpp:96
Option< std::string > user
Definition: logrotate.hpp:126
std::string logrotate_path
Definition: logrotate.hpp:125
Definition: flags.hpp:44
Definition: spec.hpp:30
void setUsageMessage(const std::string &message)
Definition: flags.hpp:164
const T & get() const &
Definition: option.hpp:118
Flags()
Definition: logrotate.hpp:44
static Try error(const E &e)
Definition: try.hpp:42
Try< std::string > shell(const std::string &fmt, const T &...t)
Runs a shell command with optional arguments.
Definition: shell.hpp:73
Option< std::string > log_filename
Definition: logrotate.hpp:124
Definition: none.hpp:27
Definition: attributes.hpp:24
bool isError() const
Definition: try.hpp:71
Option< std::string > logrotate_options
Definition: logrotate.hpp:123
constexpr char DEV_NULL[]
Definition: constants.hpp:30
uint64_t bytes() const
Definition: bytes.hpp:79
void add(T1 Flags::*t1, const Name &name, const Option< Name > &alias, const std::string &help, const T2 *t2, F validate)
Definition: flags.hpp:333
bool isNone() const
Definition: option.hpp:116
Definition: bytes.hpp:30
std::string stringify(int flags)