]> git.ipfire.org Git - thirdparty/squid.git/blobdiff - src/log/File.cc
Source Format Enforcement (#763)
[thirdparty/squid.git] / src / log / File.cc
index a6e6fc642d871066ba6e1f0e3d699e2c67386fb8..af185b4a4094b31957da71d7bc21962f5ad3419b 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
+ * Copyright (C) 1996-2021 The Squid Software Foundation and contributors
  *
  * Squid software is distributed under GPLv2+ license and includes
  * contributions from numerous individuals and organizations.
 #include "log/ModSyslog.h"
 #include "log/ModUdp.h"
 #include "log/TcpLogger.h"
-
-CBDATA_TYPE(Logfile);
+#include "sbuf/SBuf.h"
+
+CBDATA_CLASS_INIT(Logfile);
+
+Logfile::Logfile(const char *aPath) :
+    sequence_number(0),
+    data(NULL),
+    f_linestart(NULL),
+    f_linewrite(NULL),
+    f_lineend(NULL),
+    f_flush(NULL),
+    f_rotate(NULL),
+    f_close(NULL)
+{
+    xstrncpy(path, aPath, sizeof(path));
+    flags.fatal = 0;
+}
 
 Logfile *
 logfileOpen(const char *path, size_t bufsz, int fatal_flag)
@@ -27,10 +42,8 @@ logfileOpen(const char *path, size_t bufsz, int fatal_flag)
     const char *patharg;
 
     debugs(50, DBG_IMPORTANT, "Logfile: opening log " << path);
-    CBDATA_INIT_TYPE(Logfile);
 
-    Logfile *lf = cbdataAlloc(Logfile);
-    xstrncpy(lf->path, path, MAXPATHLEN);
+    Logfile *lf = new Logfile(path);
     patharg = path;
     /* need to call the per-logfile-type code */
     if (strncmp(path, "stdio:", 6) == 0) {
@@ -61,7 +74,7 @@ logfileOpen(const char *path, size_t bufsz, int fatal_flag)
         else
             debugs(50, DBG_IMPORTANT, "logfileOpen: " << path << ": couldn't open!");
         lf->f_close(lf);
-        cbdataFree(lf);
+        delete lf;
         return NULL;
     }
     assert(lf->data != NULL);
@@ -80,18 +93,18 @@ logfileClose(Logfile * lf)
     debugs(50, DBG_IMPORTANT, "Logfile: closing log " << lf->path);
     lf->f_flush(lf);
     lf->f_close(lf);
-    cbdataFree(lf);
+    delete lf;
 }
 
 void
-logfileRotate(Logfile * lf)
+logfileRotate(Logfile * lf, int16_t rotateCount)
 {
     debugs(50, DBG_IMPORTANT, "logfileRotate: " << lf->path);
-    lf->f_rotate(lf);
+    lf->f_rotate(lf, rotateCount);
 }
 
 void
-logfileWrite(Logfile * lf, char *buf, size_t len)
+logfileWrite(Logfile * lf, const char *buf, size_t len)
 {
     lf->f_linewrite(lf, buf, len);
 }
@@ -100,21 +113,11 @@ void
 logfilePrintf(Logfile * lf, const char *fmt,...)
 {
     va_list args;
-    char buf[8192];
-    int s;
-
     va_start(args, fmt);
-
-    s = vsnprintf(buf, 8192, fmt, args);
-
-    if (s > 8192) {
-        s = 8192;
-
-        if (fmt[strlen(fmt) - 1] == '\n')
-            buf[8191] = '\n';
-    }
-
-    logfileWrite(lf, buf, (size_t) s);
+    static SBuf sbuf;
+    sbuf.clear();
+    sbuf.vappendf(fmt, args); // Throws on overflow. TODO: handle that better
+    logfileWrite(lf, sbuf.c_str(), sbuf.length());
     va_end(args);
 }