]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Support maximum field width for string access.log fields.
authorAlex Rousskov <rousskov@measurement-factory.com>
Tue, 6 Sep 2011 18:28:11 +0000 (12:28 -0600)
committerAlex Rousskov <rousskov@measurement-factory.com>
Tue, 6 Sep 2011 18:28:11 +0000 (12:28 -0600)
Some standard command-line and some log processing tools have trouble
handling URLs or other logged fields exceeding 8KB in length. Moreover,
Squid violates its own log line format and truncates the entire log line
if, for example, the URL is 8KB long. By supporting .precision format
argument, we allow the administrator to specify logged URL size and
avoid these problems.

Limiting logged field width has no effect on traffic on the wire, with
the exception of log records if they are sent over the network, of course.

TODO: The name comes from the printf(3) "precision" format part. It may
be a good idea to rename our "precision" into max_width or similar,
especially if we do not support floating point precision logging.

TODO: Old code used chars to store user-configured field width and
precision. That does not work for URLs, headers, and other entries
longer than 256 characters. This patch changes the storage type to int.
The code should probably be polished further to remove unsigned->signed
conversions.

src/cf.data.pre
src/format/Format.cc
src/format/Tokens.h

index 307885dbeb28195b47a20d5fff3dfeefc0f5bb52..6737e056eb94a9c5a5f16c28ac57d8651b9c3f01 100644 (file)
@@ -2876,8 +2876,12 @@ DOC_START
                '       output as-is
 
                -       left aligned
-               width   field width. If starting with 0 the
-                       output is zero padded
+
+               width   minimum and/or maximum field width:
+                           [width_min][.width_max]
+                       When minimum starts with 0, the field is zero-padded.
+                       String values exceeding maximum width are truncated.
+
                {arg}   argument such as header name etc
 
        Format codes:
index e39861b4c65509cb4b3abc2495b0195e6818ae0b..18ab080072d9b6011837c651912743cbe7ef025d 100644 (file)
@@ -1034,11 +1034,18 @@ Format::Format::assemble(MemBuf &mb, AccessLogEntry *al, int logSequenceNumber)
                 }
             }
 
-            if (fmt->width) {
+            // enforce width limits if configured
+            const bool haveMaxWidth = fmt->precision && !doint && !dooff;
+            if (haveMaxWidth || fmt->width) {
+                const int minWidth = fmt->width ?
+                                     static_cast<int>(fmt->width) : 0;
+                const int maxWidth = haveMaxWidth ?
+                                     static_cast<int>(fmt->precision) : strlen(out);
+
                 if (fmt->left)
-                    mb.Printf("%-*s", (int) fmt->width, out);
+                    mb.Printf("%-*.*s", minWidth, maxWidth, out);
                 else
-                    mb.Printf("%*s", (int) fmt->width, out);
+                    mb.Printf("%*.*s", minWidth, maxWidth, out);
             } else
                 mb.append(out, strlen(out));
         } else {
index a609718e5bd2bcde5e91604258b78299b00042c9..aff0212f1102cb9afaf9024738918a5a0d1edac4 100644 (file)
@@ -215,8 +215,8 @@ public:
         } header;
         char *timespec;
     } data;
-    unsigned char width;
-    unsigned char precision;
+    unsigned int width;
+    unsigned int precision;
     enum Quoting quote;
     unsigned int left:1;
     unsigned int space:1;