On Thu, Jun 18, 2015 at 07:09:24PM +0200, David Lang wrote:
> On Thu, 18 Jun 2015, Jonny Törnbom wrote:
> 
> > Hi,
> >
> > I have a patch proposal for the syslog severity text property: an
> > addition to make strings shorter than "warning" space padded up to the
> > width/length of said "warning". I thought at first about a generic
> > solution that could apply to any field, but by quickly looking at the
> > code and how it's handled, my impression was that it would require quite
> > a bit more work and seemed to increase complexity compared to this
> > solution.
> >
> > I imagine that the severity text is the one field (except for the msg
> > part) that actually changes width between messages in normal cases, so
> > that's why I chose the simplest solution I could imagine (and cheapest
> > since its strings literals).
> >
> > What do you guys think?
> 
> most of the fields change length with different comments. I think the best 
> thing 
> to do would be to create a function that takes a string and a length and 
> padds/truncates the string to that length.
> 
> since the severity text is not used in any default templates, anyone who 
> needs 
> this is going to be creating their own template, so I think it's reasonable 
> to 
> have them set a variable and use that if they need fixed-width columns.
> 
> the other option owuld be to modify the property replacer, I could see either 
> an 
> additional type or have it change how it interprets the 'end char' parameter 
> so 
> that if it's larger than the length of the string, it pads the string to that 
> length (or possibly a combination where it only pads if the end char is 
> longer 
> than the string and the padstring option is provided as well)
> 
> In any case, I don't see something specific to the severity text being the 
> right 
> answer.
> 

I agree. I walked through our old email conversation from 2013 about
this feature and think I implemented it as suggested back then (which is
in line with your suggestion here). Attaching an updated patch proposal
that adds a fixed-width option that space pads up to the toChar field if
string is shorter, does nothing if its equal or truncates if string is
longer than toChar. That is, behaves equal to a simple fromChar/toChar
just that it pads up to toChar if string is shorter.

Feel free to to comment and if there's something obvious that I've
missed, let me know, the rsyslog code is rather new to me.

Regards,
Jonny
---
 rsyslog/runtime/msg.c | 18 ++++++++++++------
 rsyslog/template.c    |  2 ++
 rsyslog/template.h    |  1 +
 3 files changed, 15 insertions(+), 6 deletions(-)

diff --git a/rsyslog/runtime/msg.c b/rsyslog/runtime/msg.c
index e25543a..6492e0c 100644
--- a/rsyslog/runtime/msg.c
+++ b/rsyslog/runtime/msg.c
@@ -3567,7 +3567,7 @@ uchar *MsgGetProp(msg_t *__restrict__ const pMsg, struct templateEntry *__restri
 			if(iTo > 0)
 				--iTo;
 		}
-		if(iFrom == 0 && iTo >=  bufLen) { 
+		if(iFrom == 0 && iTo >= bufLen && pTpe->data.field.options.bFixedWidth == 0) { 
 			/* in this case, the requested string is a superset of what we already have,
 			 * so there is no need to do any processing. This is a frequent case for size-limited
 			 * fields like TAG in the default forwarding template (so it is a useful optimization
@@ -3575,8 +3575,10 @@ uchar *MsgGetProp(msg_t *__restrict__ const pMsg, struct templateEntry *__restri
 			 */
 			; /*DO NOTHING*/
 		} else {
-			if(iTo > bufLen) /* iTo is very large, if no to-position is set in the template! */
-				iTo = bufLen;
+			if(iTo > bufLen)  /* iTo is very large, if no to-position is set in the template! */
+				if (pTpe->data.field.options.bFixedWidth == 0)
+					iTo = bufLen;
+
 			iLen = iTo - iFrom + 1; /* the +1 is for an actual char, NOT \0! */
 			pBufStart = pBuf = MALLOC((iLen + 1) * sizeof(uchar));
 			if(pBuf == NULL) {
@@ -3596,9 +3598,13 @@ uchar *MsgGetProp(msg_t *__restrict__ const pMsg, struct templateEntry *__restri
 			}
 			/* OK, we are at the begin - now let's copy... */
 			bufLen = iLen;
-			while(*pSb && iLen) {
-				*pBuf++ = *pSb;
-				++pSb;
+			while(iLen) {
+				if (*pSb) {
+					*pBuf++ = *pSb;
+					++pSb;
+				} else {
+					*pBuf++ = ' ';
+				}
 				--iLen;
 			}
 			*pBuf = '\0';
diff --git a/rsyslog/template.c b/rsyslog/template.c
index cb45885..120a627 100644
--- a/rsyslog/template.c
+++ b/rsyslog/template.c
@@ -750,6 +750,8 @@ static void doOptions(unsigned char **pp, struct templateEntry *pTpe)
 			pTpe->data.field.options.bSecPathReplace = 1;
 		 } else if(!strcmp((char*)Buf, "pos-end-relative")) {
 			pTpe->data.field.options.bFromPosEndRelative = 1;
+		 } else if(!strcmp((char*)Buf, "fixed-width")) {
+			pTpe->data.field.options.bFixedWidth = 1;
 		 } else if(!strcmp((char*)Buf, "csv")) {
 			if(hasFormat(pTpe)) {
 				errmsg.LogError(0, NO_ERRCODE, "error: can only specify "
diff --git a/rsyslog/template.h b/rsyslog/template.h
index 437d77e..b1a4ec1 100644
--- a/rsyslog/template.h
+++ b/rsyslog/template.h
@@ -126,6 +126,7 @@ struct templateEntry {
 				unsigned bJSONfr: 1;		/* format field JSON *field* non escaped (n/v pair) */
 				unsigned bMandatory: 1;		/* mandatory field - emit even if empty */
 				unsigned bFromPosEndRelative: 1;/* is From/To-Pos relative to end of string? */
+				unsigned bFixedWidth: 1;	/* space pad to toChar if string is shorter */
 			} options;		/* options as bit fields */
 		} field;
 	} data;
-- 
2.4.4

_______________________________________________
rsyslog mailing list
http://lists.adiscon.net/mailman/listinfo/rsyslog
http://www.rsyslog.com/professional-services/
What's up with rsyslog? Follow https://twitter.com/rgerhards
NOTE WELL: This is a PUBLIC mailing list, posts are ARCHIVED by a myriad of 
sites beyond our control. PLEASE UNSUBSCRIBE and DO NOT POST if you DON'T LIKE 
THAT.

Reply via email to