Writes the buffer from stdin to the leading log file. When the number of written bytes exceeds `--max_size`, the leading log file is rotated. When the number of log files exceed `--max_files`, the oldest log file is deleted.
| 193 | // log file is rotated. When the number of log files exceed `--max_files`, |
| 194 | // the oldest log file is deleted. |
| 195 | Try<Nothing> write(size_t readSize) |
| 196 | { |
| 197 | // Rotate the log file if it will grow beyond the `--max_size`. |
| 198 | if (bytesWritten + readSize > flags.max_size.bytes()) { |
| 199 | rotate(); |
| 200 | } |
| 201 | |
| 202 | // If the leading log file is not open, open it. |
| 203 | // NOTE: We open the file in append-mode as `logrotate` may sometimes fail. |
| 204 | if (leading.isNone()) { |
| 205 | Try<int> open = os::open( |
| 206 | flags.log_filename.get(), |
| 207 | O_WRONLY | O_CREAT | O_APPEND | O_CLOEXEC, |
| 208 | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); |
| 209 | |
| 210 | if (open.isError()) { |
| 211 | return Error( |
| 212 | "Failed to open '" + flags.log_filename.get() + |
| 213 | "': " + open.error()); |
| 214 | } |
| 215 | |
| 216 | leading = open.get(); |
| 217 | } |
| 218 | |
| 219 | // Write from stdin to `leading`. |
| 220 | // NOTE: We do not exit on error here since we are prioritizing |
| 221 | // clearing the STDIN pipe (which would otherwise potentially block |
| 222 | // the container on write) over log fidelity. |
| 223 | Try<Nothing> result = |
| 224 | os::write(leading.get(), string(buffer, readSize)); |
| 225 | |
| 226 | if (result.isError()) { |
| 227 | std::cerr << "Failed to write: " << result.error() << std::endl; |
| 228 | } |
| 229 | |
| 230 | bytesWritten += readSize; |
| 231 | |
| 232 | return Nothing(); |
| 233 | } |
| 234 | |
| 235 | // Calls `logrotate` on the leading log file and resets the `bytesWritten`. |
| 236 | void rotate() |
no test coverage detected