~ps10gel/ubuntu/xenial/trafficserver/6.2.0

« back to all changes in this revision

Viewing changes to proxy/logging/LogFile.h

  • Committer: Bazaar Package Importer
  • Author(s): Arno Toell
  • Date: 2011-01-13 11:49:18 UTC
  • Revision ID: james.westby@ubuntu.com-20110113114918-vu422h8dknrgkj15
Tags: upstream-2.1.5-unstable
ImportĀ upstreamĀ versionĀ 2.1.5-unstable

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
25
 
 
26
#ifndef LOG_FILE_H
 
27
#define LOG_FILE_H
 
28
 
 
29
#include <stdarg.h>
 
30
#include <stdio.h>
 
31
 
 
32
#include "libts.h"
 
33
#include "LogFormatType.h"
 
34
#include "LogBufferSink.h"
 
35
 
 
36
class LogSock;
 
37
class LogBuffer;
 
38
struct LogBufferHeader;
 
39
class LogObject;
 
40
 
 
41
#define LOGFILE_ROLLED_EXTENSION ".old"
 
42
#define LOGFILE_SEPARATOR_STRING "_"
 
43
 
 
44
/*-------------------------------------------------------------------------
 
45
  MetaInfo
 
46
 
 
47
  Meta information for LogFile
 
48
 
 
49
  -------------------------------------------------------------------------*/
 
50
 
 
51
class MetaInfo
 
52
{
 
53
public:
 
54
  enum
 
55
  {
 
56
    DATA_FROM_METAFILE = 1,     // metadata was read (or attempted to)
 
57
    // from metafile
 
58
    VALID_CREATION_TIME = 2,    // creation time is valid
 
59
    VALID_SIGNATURE = 4,        // signature is valid
 
60
    PRE_PANDA_METAFILE = 8,     // metafile has pre-Panda format
 
61
    // (i.e., creation time only)
 
62
    FILE_OPEN_SUCCESSFUL = 16   // metafile was opened successfully
 
63
  };
 
64
 
 
65
  enum
 
66
  {
 
67
    BUF_SIZE = 640              // size of read/write buffer
 
68
  };
 
69
 
 
70
private:
 
71
  char *_filename;              // the name of the meta file
 
72
  time_t _creation_time;        // file creation time
 
73
  uint64_t _log_object_signature; // log object signature
 
74
  int _flags;                   // metainfo status flags
 
75
  char _buffer[BUF_SIZE];       // read/write buffer
 
76
 
 
77
  void _read_from_file();
 
78
  void _write_to_file();
 
79
  void _build_name(const char *filename);
 
80
 
 
81
public:
 
82
    MetaInfo(char *filename):_flags(0)
 
83
  {
 
84
    _build_name(filename);
 
85
    _read_from_file();
 
86
  };
 
87
 
 
88
  MetaInfo(char *filename, time_t creation, uint64_t signature):_creation_time(creation),
 
89
    _log_object_signature(signature), _flags(VALID_CREATION_TIME | VALID_SIGNATURE)
 
90
  {
 
91
 
 
92
    _build_name(filename);
 
93
    _write_to_file();
 
94
  };
 
95
 
 
96
  ~MetaInfo() {
 
97
    xfree(_filename);
 
98
  }
 
99
 
 
100
  bool get_creation_time(time_t * time)
 
101
  {
 
102
    if (_flags & VALID_CREATION_TIME) {
 
103
      *time = _creation_time;
 
104
      return true;
 
105
    } else {
 
106
      return false;
 
107
    }
 
108
  };
 
109
  bool get_log_object_signature(uint64_t * signature)
 
110
  {
 
111
    if (_flags & VALID_SIGNATURE) {
 
112
      *signature = _log_object_signature;
 
113
      return true;
 
114
    } else {
 
115
      return false;
 
116
    }
 
117
  };
 
118
  bool data_from_metafile()
 
119
  {
 
120
    return (_flags & DATA_FROM_METAFILE ? true : false);
 
121
  };
 
122
  bool pre_panda_metafile()
 
123
  {
 
124
    return (_flags & PRE_PANDA_METAFILE ? true : false);
 
125
  };
 
126
  bool file_open_successful()
 
127
  {
 
128
    return (_flags & FILE_OPEN_SUCCESSFUL ? true : false);
 
129
  };
 
130
};
 
131
 
 
132
/*-------------------------------------------------------------------------
 
133
  LogFile
 
134
  -------------------------------------------------------------------------*/
 
135
 
 
136
class LogFile:public LogBufferSink
 
137
{
 
138
private:
 
139
  LogFile(const LogFile &);
 
140
public:
 
141
  LogFile(const char *name, const char *header, LogFileFormat format, uint64_t signature,
 
142
#ifndef TS_MICRO
 
143
          size_t ascii_buffer_size = 4 * 9216, size_t max_line_size = 9216,
 
144
#else
 
145
          size_t ascii_buffer_size = 1024, size_t max_line_size = 1024);
 
146
#endif
 
147
  size_t overspill_report_count = 1000);
 
148
  ~ LogFile();
 
149
 
 
150
  enum
 
151
  {
 
152
    LOG_FILE_NO_ERROR = 0,
 
153
    LOG_FILE_NO_PIPE_READERS,
 
154
    LOG_FILE_COULD_NOT_CREATE_PIPE,
 
155
    LOG_FILE_PIPE_MODE_NOT_SUPPORTED,
 
156
    LOG_FILE_COULD_NOT_OPEN_FILE,
 
157
    LOG_FILE_FILESYSTEM_CHECKS_FAILED
 
158
  };
 
159
 
 
160
  int write(LogBuffer * lb, size_t * to_disk = 0, size_t * to_net = 0, size_t * to_pipe = 0);
 
161
  int roll(long interval_start, long interval_end);
 
162
  char *get_name()
 
163
  {
 
164
    return m_name;
 
165
  }
 
166
  void change_header(const char *header);
 
167
  void change_name(char *new_name);
 
168
  LogFileFormat get_format()
 
169
  {
 
170
    return m_file_format;
 
171
  };
 
172
  const char *get_format_name()
 
173
  {
 
174
    return (m_file_format == BINARY_LOG ? "binary" : (m_file_format == ASCII_PIPE ? "ascii_pipe" : "ascii"));
 
175
  };
 
176
  static int write_ascii_logbuffer(LogBufferHeader * buffer_header, int fd, const char *path, char *alt_format = NULL);
 
177
  int write_ascii_logbuffer3(LogBufferHeader * buffer_header, char *alt_format = NULL);
 
178
  static bool rolled_logfile(char *file);
 
179
  static bool exists(const char *pathname);
 
180
 
 
181
  void display(FILE * fd = stdout);
 
182
  int open_file();
 
183
  bool size_limit_exceeded()
 
184
  {
 
185
    if (!m_filesystem_checks_done) {
 
186
      do_filesystem_checks();
 
187
    }
 
188
    return (m_has_size_limit ? (uint64_t) m_size_bytes > m_size_limit_bytes : false);
 
189
  };
 
190
  int do_filesystem_checks();
 
191
  off_t get_size_bytes()const
 
192
  {
 
193
    return m_size_bytes;
 
194
  };
 
195
 
 
196
private:
 
197
  void init();
 
198
  bool is_open()
 
199
  {
 
200
    return (m_fd >= 0);
 
201
  };
 
202
  void close_file();
 
203
 
 
204
  void check_fd();
 
205
  static int writeln(char *data, int len, int fd, const char *path);
 
206
  void read_metadata();
 
207
 
 
208
private:
 
209
  LogFileFormat m_file_format;
 
210
  char *m_name;
 
211
  char *m_header;
 
212
  uint64_t m_signature;           // signature of log object stored
 
213
  MetaInfo *m_meta_info;
 
214
 
 
215
  char *m_ascii_buffer;         // buffer for ascii output
 
216
  size_t m_ascii_buffer_size;   // size of ascii buffer
 
217
  size_t m_max_line_size;       // size of longest log line (record)
 
218
  // (including newline)
 
219
  char *m_overspill_buffer;     // buffer for data that did not fit
 
220
  // the pipe buffer
 
221
  size_t m_overspill_bytes;     // bytes in the overspill buffer
 
222
  size_t m_overspill_written;   // bytes in overspill that have been
 
223
  // transferred to pipe buffer
 
224
  size_t m_attempts_to_write_overspill; // times transfer from overspill to
 
225
  // pipe buffer has been attempted
 
226
  size_t m_overspill_report_count;      // number of attempts at which
 
227
  // overspill report is written to
 
228
  // diags log
 
229
 
 
230
  int m_fd;
 
231
  long m_start_time;
 
232
  long m_end_time;
 
233
  uint64_t m_bytes_written;
 
234
  off_t m_size_bytes;           // current size of file in bytes
 
235
  bool m_has_size_limit;        // true if file has a size limit
 
236
  uint64_t m_size_limit_bytes;    // maximum file size in bytes
 
237
  bool m_filesystem_checks_done;        // file system checks have been done
 
238
 
 
239
public:
 
240
  Link<LogFile> link;
 
241
 
 
242
private:
 
243
  // -- member functions not allowed --
 
244
  LogFile();
 
245
  LogFile & operator=(const LogFile &);
 
246
};
 
247
 
 
248
/***************************************************************************
 
249
 LogFileList IS NOT USED
 
250
****************************************************************************/
 
251
 
 
252
#endif