]>
Commit | Line | Data |
---|---|---|
82b7abe3 | 1 | /* |
bbc27441 | 2 | * Copyright (C) 1996-2014 The Squid Software Foundation and contributors |
82b7abe3 | 3 | * |
bbc27441 AJ |
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. | |
82b7abe3 AJ |
7 | */ |
8 | ||
bbc27441 AJ |
9 | /* DEBUG: section 50 Log file handling */ |
10 | ||
582c2af2 | 11 | #include "squid.h" |
ed6e9fb9 | 12 | #include "fatal.h" |
82b7abe3 AJ |
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" | |
fb0c2f17 | 19 | #include "log/TcpLogger.h" |
82b7abe3 AJ |
20 | |
21 | CBDATA_TYPE(Logfile); | |
22 | ||
23 | Logfile * | |
24 | logfileOpen(const char *path, size_t bufsz, int fatal_flag) | |
25 | { | |
26 | int ret; | |
27 | const char *patharg; | |
28 | ||
e0236918 | 29 | debugs(50, DBG_IMPORTANT, "Logfile: opening log " << path); |
82b7abe3 AJ |
30 | CBDATA_INIT_TYPE(Logfile); |
31 | ||
32 | Logfile *lf = cbdataAlloc(Logfile); | |
33 | xstrncpy(lf->path, path, MAXPATHLEN); | |
34 | patharg = path; | |
35 | /* need to call the per-logfile-type code */ | |
36 | if (strncmp(path, "stdio:", 6) == 0) { | |
9d65168e A |
37 | patharg = path + 6; |
38 | ret = logfile_mod_stdio_open(lf, patharg, bufsz, fatal_flag); | |
82b7abe3 | 39 | } else if (strncmp(path, "daemon:", 7) == 0) { |
9d65168e A |
40 | patharg = path + 7; |
41 | ret = logfile_mod_daemon_open(lf, patharg, bufsz, fatal_flag); | |
d33e847c AJ |
42 | } else if (strncmp(path, "tcp:", 4) == 0) { |
43 | patharg = path + 4; | |
fb0c2f17 | 44 | ret = Log::TcpLogger::Open(lf, patharg, bufsz, fatal_flag); |
82b7abe3 | 45 | } else if (strncmp(path, "udp:", 4) == 0) { |
9d65168e A |
46 | patharg = path + 4; |
47 | ret = logfile_mod_udp_open(lf, patharg, bufsz, fatal_flag); | |
82b7abe3 AJ |
48 | #if HAVE_SYSLOG |
49 | } else if (strncmp(path, "syslog:", 7) == 0) { | |
9d65168e A |
50 | patharg = path + 7; |
51 | ret = logfile_mod_syslog_open(lf, patharg, bufsz, fatal_flag); | |
82b7abe3 AJ |
52 | #endif |
53 | } else { | |
fb0c2f17 | 54 | debugs(50, DBG_IMPORTANT, "WARNING: log name now starts with a module name. Use 'stdio:" << patharg << "'"); |
8030a2a0 | 55 | snprintf(lf->path, MAXPATHLEN, "stdio:%s", patharg); |
9d65168e | 56 | ret = logfile_mod_stdio_open(lf, patharg, bufsz, fatal_flag); |
82b7abe3 AJ |
57 | } |
58 | if (!ret) { | |
9d65168e | 59 | if (fatal_flag) |
8030a2a0 | 60 | fatalf("logfileOpen: %s: couldn't open!\n", path); |
9d65168e | 61 | else |
e0236918 | 62 | debugs(50, DBG_IMPORTANT, "logfileOpen: " << path << ": couldn't open!"); |
9d65168e A |
63 | lf->f_close(lf); |
64 | cbdataFree(lf); | |
65 | return NULL; | |
82b7abe3 AJ |
66 | } |
67 | assert(lf->data != NULL); | |
68 | ||
69 | if (fatal_flag) | |
70 | lf->flags.fatal = 1; | |
71 | ||
72 | lf->sequence_number = 0; | |
73 | ||
74 | return lf; | |
75 | } | |
76 | ||
77 | void | |
78 | logfileClose(Logfile * lf) | |
79 | { | |
e0236918 | 80 | debugs(50, DBG_IMPORTANT, "Logfile: closing log " << lf->path); |
82b7abe3 AJ |
81 | lf->f_flush(lf); |
82 | lf->f_close(lf); | |
83 | cbdataFree(lf); | |
84 | } | |
85 | ||
86 | void | |
87 | logfileRotate(Logfile * lf) | |
88 | { | |
e0236918 | 89 | debugs(50, DBG_IMPORTANT, "logfileRotate: " << lf->path); |
82b7abe3 AJ |
90 | lf->f_rotate(lf); |
91 | } | |
92 | ||
93 | void | |
94 | logfileWrite(Logfile * lf, char *buf, size_t len) | |
95 | { | |
96 | lf->f_linewrite(lf, buf, len); | |
97 | } | |
98 | ||
99 | void | |
100 | logfilePrintf(Logfile * lf, const char *fmt,...) | |
101 | { | |
102 | va_list args; | |
103 | char buf[8192]; | |
104 | int s; | |
105 | ||
106 | va_start(args, fmt); | |
107 | ||
108 | s = vsnprintf(buf, 8192, fmt, args); | |
109 | ||
110 | if (s > 8192) { | |
111 | s = 8192; | |
112 | ||
113 | if (fmt[strlen(fmt) - 1] == '\n') | |
114 | buf[8191] = '\n'; | |
115 | } | |
116 | ||
117 | logfileWrite(lf, buf, (size_t) s); | |
118 | va_end(args); | |
119 | } | |
120 | ||
121 | void | |
122 | logfileLineStart(Logfile * lf) | |
123 | { | |
124 | lf->f_linestart(lf); | |
125 | } | |
126 | ||
127 | void | |
128 | logfileLineEnd(Logfile * lf) | |
129 | { | |
130 | lf->f_lineend(lf); | |
d7ae3534 | 131 | ++ lf->sequence_number; |
82b7abe3 AJ |
132 | } |
133 | ||
134 | void | |
135 | logfileFlush(Logfile * lf) | |
136 | { | |
137 | lf->f_flush(lf); | |
138 | } | |
f53969cc | 139 |