Coverage Report

Created: 2026-04-07 06:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/trafficserver/include/tscore/BaseLogFile.h
Line
Count
Source
1
/** @file
2
3
  Base class for log files
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
#pragma once
26
27
#include <cstdarg>
28
#include <cstdio>
29
#include <sys/time.h>
30
#include <cstdint>
31
#include <sys/types.h>
32
#include <sys/stat.h>
33
#include <fcntl.h>
34
35
#include "tscore/ink_memory.h"
36
#include "tscore/ink_string.h"
37
#include "tscore/ink_file.h"
38
#include "tscore/ink_cap.h"
39
#include "tscore/ink_time.h"
40
#include "tscore/SimpleTokenizer.h"
41
42
0
#define LOGFILE_ROLLED_EXTENSION ".old"
43
0
#define LOGFILE_SEPARATOR_STRING "_"
44
0
#define LOGFILE_DEFAULT_PERMS    (0644)
45
0
#define LOGFILE_ROLL_MAXPATHLEN  4096
46
#define BASELOGFILE_DEBUG_MODE \
47
127k
  0 // change this to 1 to enable debug messages
48
    // TODO find a way to enable this from autotools
49
50
enum LogLogPriorityLevel {
51
  LL_Debug = 0, // process does not die
52
  LL_Note,      // process does not die
53
  LL_Warning,   // process does not die
54
  LL_Error,     // process does not die
55
  LL_Fatal,     // causes process termination
56
};
57
58
#define log_log_trace(...)                         \
59
127k
  do {                                             \
60
127k
    if (BASELOGFILE_DEBUG_MODE)                    \
61
127k
      BaseLogFile::log_log(LL_Debug, __VA_ARGS__); \
62
127k
  } while (0)
63
64
#define log_log_error(...)                         \
65
0
  do {                                             \
66
0
    if (BASELOGFILE_DEBUG_MODE)                    \
67
0
      BaseLogFile::log_log(LL_Error, __VA_ARGS__); \
68
0
  } while (0)
69
70
/*
71
 *
72
 * BaseMetaInfo class
73
 *
74
 * Used to store persistent information between ATS instances
75
 *
76
 */
77
class BaseMetaInfo
78
{
79
public:
80
  enum {
81
    DATA_FROM_METAFILE = 1, // metadata was read (or attempted to)
82
    // from metafile
83
    VALID_CREATION_TIME = 2, // creation time is valid
84
    VALID_SIGNATURE     = 4, // signature is valid
85
    // (i.e., creation time only)
86
    FILE_OPEN_SUCCESSFUL = 8 // metafile was opened successfully
87
  };
88
89
  enum {
90
    BUF_SIZE = 640 // size of read/write buffer
91
  };
92
93
private:
94
  char    *_filename{nullptr};       // the name of the meta file
95
  time_t   _creation_time{0};        // file creation time
96
  uint64_t _log_object_signature{0}; // log object signature
97
  int      _flags{0};                // metainfo status flags
98
  char     _buffer[BUF_SIZE];        // read/write buffer
99
100
  void _read_from_file();
101
  void _write_to_file();
102
  void _build_name(const char *filename);
103
104
public:
105
  BaseMetaInfo(const char *filename)
106
0
  {
107
0
    _build_name(filename);
108
0
    _read_from_file();
109
0
  }
110
111
0
  BaseMetaInfo(char *filename, time_t creation) : _creation_time(creation), _log_object_signature(0), _flags(VALID_CREATION_TIME)
112
0
  {
113
0
    _build_name(filename);
114
0
    _write_to_file();
115
0
  }
116
117
  BaseMetaInfo(char *filename, time_t creation, uint64_t signature)
118
0
    : _creation_time(creation), _log_object_signature(signature), _flags(VALID_CREATION_TIME | VALID_SIGNATURE)
119
0
  {
120
0
    _build_name(filename);
121
0
    _write_to_file();
122
0
  }
123
124
0
  ~BaseMetaInfo() { ats_free(_filename); }
125
  bool
126
  get_creation_time(time_t *time)
127
0
  {
128
0
    if (_flags & VALID_CREATION_TIME) {
129
0
      *time = _creation_time;
130
0
      return true;
131
0
    } else {
132
0
      return false;
133
0
    }
134
0
  }
135
136
  bool
137
  get_log_object_signature(uint64_t *signature)
138
0
  {
139
0
    if (_flags & VALID_SIGNATURE) {
140
0
      *signature = _log_object_signature;
141
0
      return true;
142
0
    } else {
143
0
      return false;
144
0
    }
145
0
  }
146
147
  bool
148
  data_from_metafile() const
149
0
  {
150
0
    return (_flags & DATA_FROM_METAFILE ? true : false);
151
0
  }
152
153
  bool
154
  file_open_successful()
155
0
  {
156
0
    return (_flags & FILE_OPEN_SUCCESSFUL ? true : false);
157
0
  }
158
};
159
160
/*
161
 *
162
 * BaseLogFile Class
163
 *
164
 */
165
class BaseLogFile
166
{
167
public:
168
  // member functions
169
  BaseLogFile()                               = delete;
170
  BaseLogFile &operator=(const BaseLogFile &) = delete;
171
  BaseLogFile(const char *name);
172
  BaseLogFile(const char *name, uint64_t sig);
173
  BaseLogFile(const BaseLogFile &);
174
  ~BaseLogFile();
175
  int         roll();
176
  int         roll(long interval_start, long interval_end);
177
  static bool rolled_logfile(char *path);
178
  static bool exists(const char *pathname);
179
  int         open_file(int perm = -1);
180
  int         close_file();
181
  void        change_name(const char *new_name);
182
  void        display(FILE *fd = stdout);
183
  const char *
184
  get_name() const
185
0
  {
186
0
    return m_name;
187
0
  }
188
  bool
189
  is_open()
190
21.2k
  {
191
21.2k
    return (m_fp != nullptr);
192
21.2k
  }
193
  off_t
194
  get_size_bytes() const
195
0
  {
196
0
    return m_bytes_written;
197
0
  }
198
  bool
199
  is_init()
200
0
  {
201
0
    return m_is_init;
202
0
  }
203
  const char *
204
  get_hostname() const
205
0
  {
206
0
    return m_hostname;
207
0
  }
208
  void
209
  set_hostname(const char *hn)
210
0
  {
211
0
    m_hostname = ats_strdup(hn);
212
0
  }
213
214
  static void log_log(LogLogPriorityLevel priority, const char *format, ...);
215
216
  // member variables
217
  enum {
218
    LOG_FILE_NO_ERROR = 0,
219
    LOG_FILE_COULD_NOT_OPEN_FILE,
220
  };
221
222
  FILE    *m_fp            = nullptr;
223
  long     m_start_time    = time(nullptr);
224
  long     m_end_time      = 0L;
225
  uint64_t m_bytes_written = 0;
226
227
private:
228
  // member functions
229
  int timestamp_to_str(long timestamp, char *buf, int size);
230
231
  // member variables
232
  ats_scoped_str                m_name;
233
  ats_scoped_str                m_hostname;
234
  bool                          m_is_regfile    = false;
235
  bool                          m_is_init       = false;
236
  std::unique_ptr<BaseMetaInfo> m_meta_info     = nullptr;
237
  uint64_t                      m_signature     = 0;
238
  bool                          m_has_signature = false;
239
};