]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: log: add lf_text_len
authorWilly Tarreau <w@1wt.eu>
Fri, 21 Dec 2012 18:23:44 +0000 (19:23 +0100)
committerWilly Tarreau <w@1wt.eu>
Fri, 21 Dec 2012 18:24:48 +0000 (19:24 +0100)
This function allows to log a text of a specific length.

include/proto/log.h
src/log.c

index 5d1215afb93574a42ee84d89ce6f1e02947645f2..173375e4e285e3f880d2c14beb28b2eed810e720 100644 (file)
@@ -122,7 +122,7 @@ int get_log_facility(const char *fac);
  *
  * Return the adress of the \0 character, or NULL on error
  */
-char *lf_text(char *dst, const char *src, size_t size, struct logformat_node *node);
+char *lf_text_len(char *dst, const char *src, size_t len, size_t size, struct logformat_node *node);
 
 /*
  * Write a IP adress to the log string
index a5860024e6b41653137edc6382e9a891c005b887..a1bc78fdc0a53cf59f2efabd0c04450dcadaa971 100644 (file)
--- a/src/log.c
+++ b/src/log.c
@@ -510,53 +510,40 @@ int get_log_facility(const char *fac)
  *
  * Return the adress of the \0 character, or NULL on error
  */
-char *lf_text(char *dst, const char *src, size_t size, struct logformat_node *node)
+char *lf_text_len(char *dst, const char *src, size_t len, size_t size, struct logformat_node *node)
 {
-       int n;
-
-       if (src == NULL || *src == '\0') {
-               if (node->options & LOG_OPT_QUOTE) {
-                       if (size > 2) {
-                               *(dst++) = '"';
-                               *(dst++) = '"';
-                               *dst = '\0';
-                       } else {
-                               dst = NULL;
-                               return dst;
-                       }
-               } else {
-                       if (size > 1) {
-                               *(dst++) = '-';
-                               *dst = '\0';
-                       } else { // error no space available
-                               dst = NULL;
-                               return dst;
-                       }
-               }
-       } else {
-               if (node->options & LOG_OPT_QUOTE) {
-                       if (size-- > 1 ) {
-                               *(dst++) = '"';
-                       } else {
-                               dst = NULL;
-                               return NULL;
-                       }
-                       n = strlcpy2(dst, src, size);
-                       size -= n;
-                       dst += n;
-                       if (size > 1) {
-                               *(dst++) = '"';
-                               *dst = '\0';
-                       } else {
-                               dst = NULL;
-                       }
-               } else {
-                       dst += strlcpy2(dst, src, size);
-               }
+       if (size < 2)
+               return NULL;
+
+       if (node->options & LOG_OPT_QUOTE) {
+               *(dst++) = '"';
+               size--;
+       }
+
+       if (src) {
+               if (++len > size)
+                       len = size;
+               len = strlcpy2(dst, src, len);
+
+               size -= len;
+               dst += len;
        }
+
+       if (node->options & LOG_OPT_QUOTE) {
+               if (size < 2)
+                       return NULL;
+               *(dst++) = '"';
+       }
+
+       *dst = '\0';
        return dst;
 }
 
+static inline char *lf_text(char *dst, const char *src, size_t size, struct logformat_node *node)
+{
+       return lf_text_len(dst, src, size, size, node);
+}
+
 /*
  * Write a IP adress to the log string
  * +X option write in hexadecimal notation, most signifant byte on the left