TcpLogger.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
3  *
4  * Squid software is distributed under GPLv2+ license and includes
5  * contributions from numerous individuals and organizations.
6  * Please see the COPYING and CONTRIBUTORS files for details.
7  */
8 
9 #ifndef SQUID_SRC_LOG_TCPLOGGER_H
10 #define SQUID_SRC_LOG_TCPLOGGER_H
11 
12 #include "base/AsyncJob.h"
13 #include "base/JobWait.h"
14 #include "comm/forward.h"
15 #include "ip/Address.h"
16 #include "log/forward.h"
17 
18 #include <list>
19 
20 class CommCloseCbParams;
22 class CommIoCbParams;
23 class MemBlob;
25 
26 namespace Log
27 {
28 
33 class TcpLogger : public AsyncJob
34 {
36 
37 public:
39 
40  /* Logfile API */
41  static int Open(Logfile *lf, const char *path, size_t bufSz, int fatalFlag);
42 
43 protected:
44  TcpLogger(size_t, bool, Ip::Address);
45  ~TcpLogger() override;
46 
51  void endGracefully();
52 
54  void logRecord(const char *buf, size_t len);
55 
57  void flush();
58 
59  /* AsyncJob API */
60  void start() override;
61  bool doneAll() const override;
62  void swanSong() override;
63 
64 private:
65  /* Logfile API. Map c-style Logfile calls to TcpLogger method calls. */
66  static void Flush(Logfile *lf);
67  static void WriteLine(Logfile *lf, const char *buf, size_t len);
68  static void StartLine(Logfile *lf);
69  static void EndLine(Logfile *lf);
70  static void Rotate(Logfile *lf, const int16_t);
71  static void Close(Logfile *lf);
72 
73  static TcpLogger *StillLogging(Logfile *lf);
74 
75  static void DelayedReconnect(void *data);
76  void delayedReconnect();
77 
78  bool canFit(const size_t len) const;
79  void appendRecord(const char *buf, size_t len);
80  void appendChunk(const char *chunk, const size_t len);
81  void writeIfNeeded();
82  void writeIfPossible();
83  void doConnect();
84  void disconnect();
85 
86  /* comm callbacks */
88  void writeDone(const CommIoCbParams &io);
89  void handleClosure(const CommCloseCbParams &io);
90 
91  static const size_t IoBufSize;
92  static const size_t BufferCapacityMin;
93 
97  bool dieOnError;
98 
99  std::list<MemBlobPointer> buffers;
100  size_t bufferCapacity;
101  size_t bufferedSize;
102  size_t flushDebt;
103 
104  bool quitOnEmpty;
107 
111 
114 
115  uint64_t connectFailures;
116  uint64_t drops;
117 };
118 
119 } // namespace Log
120 
121 #endif /* SQUID_SRC_LOG_TCPLOGGER_H */
122 
void logRecord(const char *buf, size_t len)
buffers record and possibly writes it to the remote logger
Definition: TcpLogger.cc:118
bool reconnectScheduled
we are sleeping before the next connection attempt
Definition: TcpLogger.h:105
uint64_t connectFailures
number of sequential connection failures
Definition: TcpLogger.h:115
static TcpLogger * StillLogging(Logfile *lf)
Definition: TcpLogger.cc:396
void endGracefully()
Definition: TcpLogger.cc:102
void start() override
called by AsyncStart; do not call directly
Definition: TcpLogger.cc:67
JobWait< Comm::ConnOpener > connWait
waits for a connection to the remote logger to be established/opened
Definition: TcpLogger.h:113
void handleClosure(const CommCloseCbParams &io)
Definition: TcpLogger.cc:367
void connectDone(const CommConnectCbParams &conn)
Comm::ConnOpener callback.
Definition: TcpLogger.cc:263
uint64_t drops
number of records dropped during the current outage
Definition: TcpLogger.h:116
void writeIfNeeded()
starts writing if and only if it is time to write accumulated records
Definition: TcpLogger.cc:126
std::list< MemBlobPointer > buffers
I/O buffers.
Definition: TcpLogger.h:99
bool canFit(const size_t len) const
whether len more bytes can be buffered
Definition: TcpLogger.cc:158
bool writeScheduled
we are waiting for the latest write() results
Definition: TcpLogger.h:106
static void EndLine(Logfile *lf)
Definition: TcpLogger.cc:423
static void Flush(Logfile *lf)
Definition: TcpLogger.cc:404
void disconnect()
close our connection now, without flushing
Definition: TcpLogger.cc:381
static void StartLine(Logfile *lf)
Definition: TcpLogger.cc:418
static int Open(Logfile *lf, const char *path, size_t bufSz, int fatalFlag)
Definition: TcpLogger.cc:452
size_t bufferedSize
number of log record bytes stored in RAM now
Definition: TcpLogger.h:101
static void Close(Logfile *lf)
Definition: TcpLogger.cc:435
Comm::ConnectionPointer conn
opened connection to the remote logger
Definition: TcpLogger.h:108
static void WriteLine(Logfile *lf, const char *buf, size_t len)
Definition: TcpLogger.cc:411
static void DelayedReconnect(void *data)
Log::TcpLogger::delayedReconnect() wrapper.
Definition: TcpLogger.cc:304
CBDATA_CHILD(TcpLogger)
void swanSong() override
Definition: TcpLogger.cc:95
static const size_t BufferCapacityMin
minimum bufferCapacity value
Definition: TcpLogger.h:92
Definition: File.h:38
Definition: Config.h:17
Ip::Address remote
where the remote logger expects our records
Definition: TcpLogger.h:109
void appendChunk(const char *chunk, const size_t len)
buffer a record chunk without splitting it across buffers
Definition: TcpLogger.cc:220
void flush()
write all currently buffered records ASAP
Definition: TcpLogger.cc:111
static const size_t IoBufSize
fixed I/O buffer size
Definition: TcpLogger.h:91
size_t flushDebt
how many record bytes we still need to write ASAP
Definition: TcpLogger.h:102
void delayedReconnect()
"sleep a little before trying to connect again" event callback
Definition: TcpLogger.cc:321
static void Rotate(Logfile *lf, const int16_t)
Definition: TcpLogger.cc:430
CbcPointer< TcpLogger > Pointer
Definition: TcpLogger.h:38
AsyncCall::Pointer closer
handles unexpected/external conn closures
Definition: TcpLogger.h:110
void writeDone(const CommIoCbParams &io)
Comm::Write callback.
Definition: TcpLogger.cc:331
TcpLogger(size_t, bool, Ip::Address)
Definition: TcpLogger.cc:36
void appendRecord(const char *buf, size_t len)
buffer a record that might exceed IoBufSize
Definition: TcpLogger.cc:200
void writeIfPossible()
starts writing if possible
Definition: TcpLogger.cc:135
RefCount< MemBlob > MemBlobPointer
Definition: TcpLogger.h:23
bool doneAll() const override
whether positive goal has been reached
Definition: TcpLogger.cc:73
bool quitOnEmpty
whether this job should quit when buffers are empty
Definition: TcpLogger.h:104
size_t bufferCapacity
bufferedSize limit
Definition: TcpLogger.h:100
bool dieOnError
Definition: TcpLogger.h:97
~TcpLogger() override
Definition: TcpLogger.cc:60
void doConnect()
starts [re]connecting to the remote logger
Definition: TcpLogger.cc:241

 

Introduction

Documentation

Support

Miscellaneous