File.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2021 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 /* DEBUG: section 50 Log file handling */
10 
11 #include "squid.h"
12 #include "DebugMessages.h"
13 #include "fatal.h"
14 #include "fde.h"
15 #include "log/File.h"
16 #include "log/ModDaemon.h"
17 #include "log/ModStdio.h"
18 #include "log/ModSyslog.h"
19 #include "log/ModUdp.h"
20 #include "log/TcpLogger.h"
21 #include "sbuf/SBuf.h"
22 
24 
25 Logfile::Logfile(const char *aPath) :
26  sequence_number(0),
27  data(NULL),
28  f_linestart(NULL),
29  f_linewrite(NULL),
30  f_lineend(NULL),
31  f_flush(NULL),
32  f_rotate(NULL),
33  f_close(NULL)
34 {
35  xstrncpy(path, aPath, sizeof(path));
36  flags.fatal = 0;
37 }
38 
39 Logfile *
40 logfileOpen(const char *path, size_t bufsz, int fatal_flag)
41 {
42  int ret;
43  const char *patharg;
44 
45  debugs(50, Important(26), "Logfile: opening log " << path);
46 
47  Logfile *lf = new Logfile(path);
48  patharg = path;
49  /* need to call the per-logfile-type code */
50  if (strncmp(path, "stdio:", 6) == 0) {
51  patharg = path + 6;
52  ret = logfile_mod_stdio_open(lf, patharg, bufsz, fatal_flag);
53  } else if (strncmp(path, "daemon:", 7) == 0) {
54  patharg = path + 7;
55  ret = logfile_mod_daemon_open(lf, patharg, bufsz, fatal_flag);
56  } else if (strncmp(path, "tcp:", 4) == 0) {
57  patharg = path + 4;
58  ret = Log::TcpLogger::Open(lf, patharg, bufsz, fatal_flag);
59  } else if (strncmp(path, "udp:", 4) == 0) {
60  patharg = path + 4;
61  ret = logfile_mod_udp_open(lf, patharg, bufsz, fatal_flag);
62 #if HAVE_SYSLOG
63  } else if (strncmp(path, "syslog:", 7) == 0) {
64  patharg = path + 7;
65  ret = logfile_mod_syslog_open(lf, patharg, bufsz, fatal_flag);
66 #endif
67  } else {
68  debugs(50, DBG_IMPORTANT, "WARNING: log name now starts with a module name. Use 'stdio:" << patharg << "'");
69  snprintf(lf->path, MAXPATHLEN, "stdio:%s", patharg);
70  ret = logfile_mod_stdio_open(lf, patharg, bufsz, fatal_flag);
71  }
72  if (!ret) {
73  if (fatal_flag)
74  fatalf("logfileOpen: %s: couldn't open!\n", path);
75  else
76  debugs(50, DBG_IMPORTANT, "logfileOpen: " << path << ": couldn't open!");
77  lf->f_close(lf);
78  delete lf;
79  return NULL;
80  }
81  assert(lf->data != NULL);
82 
83  if (fatal_flag)
84  lf->flags.fatal = 1;
85 
86  lf->sequence_number = 0;
87 
88  return lf;
89 }
90 
91 void
93 {
94  debugs(50, Important(27), "Logfile: closing log " << lf->path);
95  lf->f_flush(lf);
96  lf->f_close(lf);
97  delete lf;
98 }
99 
100 void
101 logfileRotate(Logfile * lf, int16_t rotateCount)
102 {
103  debugs(50, DBG_IMPORTANT, "logfileRotate: " << lf->path);
104  lf->f_rotate(lf, rotateCount);
105 }
106 
107 void
108 logfileWrite(Logfile * lf, const char *buf, size_t len)
109 {
110  lf->f_linewrite(lf, buf, len);
111 }
112 
113 void
114 logfilePrintf(Logfile * lf, const char *fmt,...)
115 {
116  va_list args;
117  va_start(args, fmt);
118  static SBuf sbuf;
119  sbuf.clear();
120  sbuf.vappendf(fmt, args); // Throws on overflow. TODO: handle that better
121  logfileWrite(lf, sbuf.c_str(), sbuf.length());
122  va_end(args);
123 }
124 
125 void
127 {
128  lf->f_linestart(lf);
129 }
130 
131 void
133 {
134  lf->f_lineend(lf);
135  ++ lf->sequence_number;
136 }
137 
138 void
140 {
141  lf->f_flush(lf);
142 }
143 
void logfileClose(Logfile *lf)
Definition: File.cc:92
CBDATA_CLASS_INIT(Logfile)
void logfileWrite(Logfile *lf, const char *buf, size_t len)
Definition: File.cc:108
SBuf & vappendf(const char *fmt, va_list vargs)
Definition: SBuf.cc:239
Definition: SBuf.h:87
int logfile_mod_stdio_open(Logfile *lf, const char *path, size_t bufsz, int fatal_flag)
Definition: ModStdio.cc:177
char * xstrncpy(char *dst, const char *src, size_t n)
Definition: xstring.cc:37
LOGWRITE * f_linewrite
Definition: File.h:58
struct Logfile::@83 flags
unsigned int fatal
Definition: File.h:49
#define DBG_IMPORTANT
Definition: Debug.h:41
void clear()
Definition: SBuf.cc:175
static int Open(Logfile *lf, const char *path, size_t bufSz, int fatalFlag)
Definition: TcpLogger.cc:454
void logfilePrintf(Logfile *lf, const char *fmt,...)
Definition: File.cc:114
int logfile_mod_syslog_open(Logfile *lf, const char *path, size_t bufsz, int fatal_flag)
void * data
Definition: File.h:55
void logfileFlush(Logfile *lf)
Definition: File.cc:139
#define NULL
Definition: types.h:166
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Debug.h:123
int logfile_mod_udp_open(Logfile *lf, const char *path, size_t bufsz, int fatal_flag)
Definition: ModUdp.cc:133
void logfileLineStart(Logfile *lf)
Definition: File.cc:126
LOGLINEEND * f_lineend
Definition: File.h:59
Logfile * logfileOpen(const char *path, size_t bufsz, int fatal_flag)
Definition: File.cc:40
#define assert(EX)
Definition: assert.h:19
void fatalf(const char *fmt,...)
Definition: fatal.cc:68
void logfileRotate(Logfile *lf, int16_t rotateCount)
Definition: File.cc:101
LOGCLOSE * f_close
Definition: File.h:62
const char * c_str()
Definition: SBuf.cc:516
LOGROTATE * f_rotate
Definition: File.h:61
size_type length() const
Returns the number of bytes stored in SBuf.
Definition: SBuf.h:408
Logfile(const char *aPath)
Definition: File.cc:25
Definition: File.h:39
int64_t sequence_number
Unique sequence number per log line.
Definition: File.h:52
int logfile_mod_daemon_open(Logfile *lf, const char *path, size_t, int)
Definition: ModDaemon.cc:211
#define Important(id)
Definition: DebugMessages.h:91
LOGFLUSH * f_flush
Definition: File.h:60
#define MAXPATHLEN
Definition: stdio.h:62
void logfileLineEnd(Logfile *lf)
Definition: File.cc:132
LOGLINESTART * f_linestart
Definition: File.h:57
char path[MAXPATHLEN]
Definition: File.h:46

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors