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

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors