/src/trafficserver/include/proxy/logging/LogFile.h
Line | Count | Source |
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 "proxy/logging/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 RefCountObjInHeap |
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, LogEscapeType escape_type = LOG_ESCAPE_NONE); |
47 | | LogFile(const LogFile &) = delete; |
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 * |
72 | | get_name() const |
73 | 0 | { |
74 | 0 | return m_name; |
75 | 0 | } |
76 | | |
77 | | void change_header(const char *header); |
78 | | void change_name(const char *new_name); |
79 | | |
80 | | LogFileFormat |
81 | | get_format() const |
82 | 0 | { |
83 | 0 | return m_file_format; |
84 | 0 | } |
85 | | |
86 | | LogEscapeType |
87 | | get_escape_type() const |
88 | 0 | { |
89 | 0 | return m_escape_type; |
90 | 0 | } |
91 | | |
92 | | const char * |
93 | | get_format_name() const |
94 | 0 | { |
95 | 0 | return (m_file_format == LOG_FILE_BINARY ? "binary" : (m_file_format == LOG_FILE_PIPE ? "ascii_pipe" : "ascii")); |
96 | 0 | } |
97 | | |
98 | | static int write_ascii_logbuffer(LogBufferHeader *buffer_header, int fd, const char *path, const char *alt_format = nullptr); |
99 | | int write_ascii_logbuffer3(LogBufferHeader *buffer_header, const char *alt_format = nullptr); |
100 | | static bool rolled_logfile(char *file); |
101 | | static bool exists(const char *pathname); |
102 | | |
103 | | void display(FILE *fd = stdout); |
104 | | int open_file(); |
105 | | |
106 | | off_t |
107 | | get_size_bytes() const |
108 | 0 | { |
109 | 0 | if (m_file_format == LOG_FILE_PIPE) |
110 | 0 | return 0; |
111 | 0 | else if (m_log) |
112 | 0 | return m_log->get_size_bytes(); |
113 | 0 | else |
114 | 0 | return 0; |
115 | 0 | } |
116 | | |
117 | | public: |
118 | | bool is_open(); |
119 | | void close_file(); |
120 | | void check_fd(); |
121 | | int get_fd(); |
122 | | static int writeln(char *data, int len, int fd, const char *path); |
123 | | |
124 | | public: |
125 | | LogFileFormat m_file_format; |
126 | | |
127 | | private: |
128 | | char *m_name; |
129 | | LogEscapeType m_escape_type; |
130 | | |
131 | | public: |
132 | | std::unique_ptr<BaseLogFile> m_log; // BaseLogFile backs the actual file on disk |
133 | | char *m_header; |
134 | | uint64_t m_signature; // signature of log object stored |
135 | | size_t m_ascii_buffer_size; // size of ascii buffer |
136 | | size_t m_max_line_size; // size of longest log line (record) |
137 | | int m_pipe_buffer_size; // this is the size of the pipe buffer set by fcntl |
138 | | int m_fd; // this could back m_log or a pipe, depending on the situation |
139 | | |
140 | | public: |
141 | | Link<LogFile> link; |
142 | | // noncopyable |
143 | | LogFile &operator=(const LogFile &) = delete; |
144 | | |
145 | | private: |
146 | | // -- member functions not allowed -- |
147 | | LogFile(); |
148 | | }; |