1 /** @file 2 3 A brief file description 4 5 @section license License 6 7 Licensed to the Apache Software Foundation (ASF) under one 8 or more contributor license agreements. See the NOTICE file 9 distributed with this work for additional information 10 regarding copyright ownership. The ASF licenses this file 11 to you under the Apache License, Version 2.0 (the 12 "License"); you may not use this file except in compliance 13 with the License. You may obtain a copy of the License at 14 15 http://www.apache.org/licenses/LICENSE-2.0 16 17 Unless required by applicable law or agreed to in writing, software 18 distributed under the License is distributed on an "AS IS" BASIS, 19 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 20 See the License for the specific language governing permissions and 21 limitations under the License. 22 */ 23 24 #pragma once 25 26 #include <cstdarg> 27 #include <cstdio> 28 29 #include "tscore/ink_platform.h" 30 #include "LogBufferSink.h" 31 32 class LogBuffer; 33 struct LogBufferHeader; 34 class LogObject; 35 class BaseLogFile; 36 class BaseMetaInfo; 37 38 /*------------------------------------------------------------------------- 39 LogFile 40 -------------------------------------------------------------------------*/ 41 42 class LogFile : public LogBufferSink, public RefCountObj 43 { 44 public: 45 LogFile(const char *name, const char *header, LogFileFormat format, uint64_t signature, size_t ascii_buffer_size = 4 * 9216, 46 size_t max_line_size = 9216, int pipe_buffer_size = 0); 47 LogFile(const LogFile &); 48 ~LogFile() override; 49 50 enum { 51 LOG_FILE_NO_ERROR = 0, 52 LOG_FILE_NO_PIPE_READERS, 53 LOG_FILE_COULD_NOT_CREATE_PIPE, 54 LOG_FILE_PIPE_MODE_NOT_SUPPORTED, 55 LOG_FILE_COULD_NOT_OPEN_FILE, 56 LOG_FILE_FILESYSTEM_CHECKS_FAILED 57 }; 58 59 int preproc_and_try_delete(LogBuffer *lb) override; 60 61 bool trim_rolled(size_t rolling_max_count); 62 int roll(long interval_start, long interval_end, bool reopen_after_rolling = false); 63 64 /** Check whether the file at the log's filename exists and, if not, close 65 * the current file descriptor and reopen it. 66 * 67 * @return True if the file was re-opened, false otherwise. 68 */ 69 bool reopen_if_moved(); 70 71 const char * get_name()72 get_name() const 73 { 74 return m_name; 75 } 76 77 void change_header(const char *header); 78 void change_name(const char *new_name); 79 80 LogFileFormat get_format()81 get_format() const 82 { 83 return m_file_format; 84 } 85 86 const char * get_format_name()87 get_format_name() const 88 { 89 return (m_file_format == LOG_FILE_BINARY ? "binary" : (m_file_format == LOG_FILE_PIPE ? "ascii_pipe" : "ascii")); 90 } 91 92 static int write_ascii_logbuffer(LogBufferHeader *buffer_header, int fd, const char *path, const char *alt_format = nullptr); 93 int write_ascii_logbuffer3(LogBufferHeader *buffer_header, const char *alt_format = nullptr); 94 static bool rolled_logfile(char *file); 95 static bool exists(const char *pathname); 96 97 void display(FILE *fd = stdout); 98 int open_file(); 99 100 off_t get_size_bytes()101 get_size_bytes() const 102 { 103 if (m_file_format == LOG_FILE_PIPE) 104 return 0; 105 else if (m_log) 106 return m_log->get_size_bytes(); 107 else 108 return 0; 109 } 110 111 public: 112 bool is_open(); 113 void close_file(); 114 void check_fd(); 115 int get_fd(); 116 static int writeln(char *data, int len, int fd, const char *path); 117 118 public: 119 LogFileFormat m_file_format; 120 121 private: 122 char *m_name; 123 124 public: 125 BaseLogFile *m_log; // BaseLogFile backs the actual file on disk 126 char *m_header; 127 uint64_t m_signature; // signature of log object stored 128 size_t m_ascii_buffer_size; // size of ascii buffer 129 size_t m_max_line_size; // size of longest log line (record) 130 int m_pipe_buffer_size; // this is the size of the pipe buffer set by fcntl 131 int m_fd; // this could back m_log or a pipe, depending on the situation 132 133 public: 134 Link<LogFile> link; 135 // noncopyable 136 LogFile &operator=(const LogFile &) = delete; 137 138 private: 139 // -- member functions not allowed -- 140 LogFile(); 141 }; 142