TcpLogger.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2022 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 
17 #include <list>
18 
19 class MemBlob;
21 
22 namespace Log
23 {
24 
29 class TcpLogger : public AsyncJob
30 {
32 
33 public:
35 
36  /* Logfile API */
37  static int Open(Logfile *lf, const char *path, size_t bufSz, int fatalFlag);
38 
39 protected:
40  TcpLogger(size_t, bool, Ip::Address);
41  virtual ~TcpLogger();
42 
47  void endGracefully();
48 
50  void logRecord(const char *buf, size_t len);
51 
53  void flush();
54 
55  /* AsyncJob API */
56  virtual void start();
57  virtual bool doneAll() const;
58  virtual void swanSong();
59 
60 private:
61  /* Logfile API. Map c-style Logfile calls to TcpLogger method calls. */
62  static void Flush(Logfile *lf);
63  static void WriteLine(Logfile *lf, const char *buf, size_t len);
64  static void StartLine(Logfile *lf);
65  static void EndLine(Logfile *lf);
66  static void Rotate(Logfile *lf, const int16_t);
67  static void Close(Logfile *lf);
68 
69  static TcpLogger *StillLogging(Logfile *lf);
70 
71  static void DelayedReconnect(void *data);
72  void delayedReconnect();
73 
74  bool canFit(const size_t len) const;
75  void appendRecord(const char *buf, size_t len);
76  void appendChunk(const char *chunk, const size_t len);
77  void writeIfNeeded();
78  void writeIfPossible();
79  void doConnect();
80  void disconnect();
81 
82  /* comm callbacks */
84  void writeDone(const CommIoCbParams &io);
85  void handleClosure(const CommCloseCbParams &io);
86 
87  static const size_t IoBufSize;
88  static const size_t BufferCapacityMin;
89 
93  bool dieOnError;
94 
95  std::list<MemBlobPointer> buffers;
96  size_t bufferCapacity;
97  size_t bufferedSize;
98  size_t flushDebt;
99 
100  bool quitOnEmpty;
103 
107 
110 
111  uint64_t connectFailures;
112  uint64_t drops;
113 };
114 
115 } // namespace Log
116 
117 #endif /* _SQUID_SRC_LOG_TCPLOGGER_H */
118 
RefCount< MemBlob > MemBlobPointer
Definition: TcpLogger.h:19
virtual ~TcpLogger()
Definition: TcpLogger.cc:60
bool reconnectScheduled
we are sleeping before the next connection attempt
Definition: TcpLogger.h:101
JobWait< Comm::ConnOpener > connWait
waits for a connection to the remote logger to be established/opened
Definition: TcpLogger.h:109
void writeDone(const CommIoCbParams &io)
Comm::Write callback.
Definition: TcpLogger.cc:331
virtual void start()
called by AsyncStart; do not call directly
Definition: TcpLogger.cc:67
void writeIfNeeded()
starts writing if and only if it is time to write accumulated records
Definition: TcpLogger.cc:126
void disconnect()
close our connection now, without flushing
Definition: TcpLogger.cc:381
void logRecord(const char *buf, size_t len)
buffers record and possibly writes it to the remote logger
Definition: TcpLogger.cc:118
bool quitOnEmpty
whether this job should quit when buffers are empty
Definition: TcpLogger.h:100
void writeIfPossible()
starts writing if possible
Definition: TcpLogger.cc:135
bool writeScheduled
we are waiting for the latest write() results
Definition: TcpLogger.h:102
static void EndLine(Logfile *lf)
Definition: TcpLogger.cc:423
bool canFit(const size_t len) const
whether len more bytes can be buffered
Definition: TcpLogger.cc:158
void appendChunk(const char *chunk, const size_t len)
buffer a record chunk without splitting it across buffers
Definition: TcpLogger.cc:220
static void Flush(Logfile *lf)
Definition: TcpLogger.cc:404
void delayedReconnect()
"sleep a little before trying to connect again" event callback
Definition: TcpLogger.cc:321
size_t flushDebt
how many record bytes we still need to write ASAP
Definition: TcpLogger.h:98
static void DelayedReconnect(void *data)
Log::TcpLogger::delayedReconnect() wrapper.
Definition: TcpLogger.cc:304
uint64_t connectFailures
number of sequential connection failures
Definition: TcpLogger.h:111
TcpLogger(size_t, bool, Ip::Address)
Definition: TcpLogger.cc:36
virtual bool doneAll() const
whether positive goal has been reached
Definition: TcpLogger.cc:73
static TcpLogger * StillLogging(Logfile *lf)
Definition: TcpLogger.cc:396
void handleClosure(const CommCloseCbParams &io)
Definition: TcpLogger.cc:367
Ip::Address remote
where the remote logger expects our records
Definition: TcpLogger.h:105
CbcPointer< TcpLogger > Pointer
Definition: TcpLogger.h:34
AsyncCall::Pointer closer
handles unexpected/external conn closures
Definition: TcpLogger.h:106
static void WriteLine(Logfile *lf, const char *buf, size_t len)
Definition: TcpLogger.cc:411
Comm::ConnectionPointer conn
opened connection to the remote logger
Definition: TcpLogger.h:104
static void Close(Logfile *lf)
Definition: TcpLogger.cc:435
void endGracefully()
Definition: TcpLogger.cc:102
void doConnect()
starts [re]connecting to the remote logger
Definition: TcpLogger.cc:241
static const size_t IoBufSize
fixed I/O buffer size
Definition: TcpLogger.h:87
CBDATA_CLASS(TcpLogger)
virtual void swanSong()
Definition: TcpLogger.cc:95
static const size_t BufferCapacityMin
minimum bufferCapacity value
Definition: TcpLogger.h:88
void connectDone(const CommConnectCbParams &conn)
Comm::ConnOpener callback.
Definition: TcpLogger.cc:263
size_t bufferedSize
number of log record bytes stored in RAM now
Definition: TcpLogger.h:97
void appendRecord(const char *buf, size_t len)
buffer a record that might exceed IoBufSize
Definition: TcpLogger.cc:200
uint64_t drops
number of records dropped during the current outage
Definition: TcpLogger.h:112
std::list< MemBlobPointer > buffers
I/O buffers.
Definition: TcpLogger.h:95
static int Open(Logfile *lf, const char *path, size_t bufSz, int fatalFlag)
Definition: TcpLogger.cc:452
bool dieOnError
Definition: TcpLogger.h:93
void flush()
write all currently buffered records ASAP
Definition: TcpLogger.cc:111
size_t bufferCapacity
bufferedSize limit
Definition: TcpLogger.h:96
static void Rotate(Logfile *lf, const int16_t)
Definition: TcpLogger.cc:430
static void StartLine(Logfile *lf)
Definition: TcpLogger.cc:418
Definition: File.h:39
Definition: Config.h:18

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors