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.

David Lang
---
 rsyslog/runtime/msg.c      | 26 ++++++++++++++++++++++++++
 rsyslog/runtime/typedefs.h | 19 ++++++++++---------
 2 files changed, 36 insertions(+), 9 deletions(-)

diff --git a/rsyslog/runtime/msg.c b/rsyslog/runtime/msg.c
index e25543a..e38d77a 100644
--- a/rsyslog/runtime/msg.c
+++ b/rsyslog/runtime/msg.c
@@ -385,6 +385,8 @@ static short len_syslog_fac_names[LOG_NFACILITIES] = { 4, 4, 4, 6, 4, 6, 3,
 
 /* table of severity names (in numerical order)*/
 static char *syslog_severity_names[8] = { "emerg", "alert", "crit", "err", "warning", "notice", "info", "debug" };
+/* table of severity names (in numerical order), space padded if needed to longest severity string ("warning") */
+static char *syslog_severity_names_padded[8] = { "emerg  ", "alert  ", "crit   ", "err    ", "warning", "notice ", "info   ", "debug  " };
 static short len_syslog_severity_names[8] = { 5, 5, 4, 3, 7, 6, 4, 5 };
 
 /* numerical values as string - this is the most efficient approach to convert severity
@@ -569,6 +571,8 @@ propNameToID(uchar *pName, propid_t *pPropID)
 		*pPropID = PROP_SYSLOGSEVERITY;
 	} else if(!strcmp((char*) pName, "syslogseverity-text") || !strcmp((char*) pName, "syslogpriority-text")) {
 		*pPropID = PROP_SYSLOGSEVERITY_TEXT;
+	} else if(!strcmp((char*) pName, "syslogseverity-text-padded")) {
+		*pPropID = PROP_SYSLOGSEVERITY_TEXT_PADDED;
 	} else if(!strcmp((char*) pName, "timegenerated")) {
 		*pPropID = PROP_TIMEGENERATED;
 	} else if(!strcmp((char*) pName, "programname")) {
@@ -668,6 +672,8 @@ uchar *propIDToName(propid_t propID)
 			return UCHAR_CONSTANT("syslogseverity");
 		case PROP_SYSLOGSEVERITY_TEXT:
 			return UCHAR_CONSTANT("syslogseverity-text");
+		case PROP_SYSLOGSEVERITY_TEXT_PADDED:
+			return UCHAR_CONSTANT("syslogseverity-text-padded");
 		case PROP_TIMEGENERATED:
 			return UCHAR_CONSTANT("timegenerated");
 		case PROP_PROGRAMNAME:
@@ -1872,6 +1878,23 @@ static inline char *getSeverityStr(msg_t * const pM)
 	return name;
 }
 
+
+static inline char *getSeverityStrPadded(msg_t * const pM)
+{
+	char *name = NULL;
+
+	if(pM == NULL)
+		return "";
+
+	if(pM->iSeverity > 7) {
+		name = "invld  ";
+	} else {
+		name = syslog_severity_names_padded[pM->iSeverity];
+	}
+
+	return name;
+}
+
 static inline char *getFacility(msg_t * const pM)
 {
 	char *name = NULL;
@@ -3160,6 +3183,9 @@ uchar *MsgGetProp(msg_t *__restrict__ const pMsg, struct templateEntry *__restri
 		case PROP_SYSLOGSEVERITY_TEXT:
 			pRes = (uchar*)getSeverityStr(pMsg);
 			break;
+		case PROP_SYSLOGSEVERITY_TEXT_PADDED:
+			pRes = (uchar*)getSeverityStrPadded(pMsg);
+			break;
 		case PROP_TIMEGENERATED:
 			if (pTpe != NULL)
 				datefmt = pTpe->data.field.eDateFormat;
diff --git a/rsyslog/runtime/typedefs.h b/rsyslog/runtime/typedefs.h
index e50191f..ff2a202 100644
--- a/rsyslog/runtime/typedefs.h
+++ b/rsyslog/runtime/typedefs.h
@@ -185,15 +185,16 @@ typedef uintTiny	propid_t;
 #define PROP_SYSLOGFACILITY_TEXT	13
 #define PROP_SYSLOGSEVERITY		14
 #define PROP_SYSLOGSEVERITY_TEXT	15
-#define PROP_TIMEGENERATED		16
-#define PROP_PROGRAMNAME		17
-#define PROP_PROTOCOL_VERSION		18
-#define PROP_STRUCTURED_DATA		19
-#define PROP_APP_NAME			20
-#define PROP_PROCID			21
-#define PROP_MSGID			22
-#define PROP_PARSESUCCESS		23
-#define PROP_JSONMESG			24
+#define PROP_SYSLOGSEVERITY_TEXT_PADDED	16
+#define PROP_TIMEGENERATED		17
+#define PROP_PROGRAMNAME		18
+#define PROP_PROTOCOL_VERSION		19
+#define PROP_STRUCTURED_DATA		20
+#define PROP_APP_NAME			21
+#define PROP_PROCID			22
+#define PROP_MSGID			23
+#define PROP_PARSESUCCESS		24
+#define PROP_JSONMESG			25
 #define PROP_SYS_NOW			150
 #define PROP_SYS_YEAR			151
 #define PROP_SYS_MONTH			152
-- 
2.4.1

_______________________________________________
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.
_______________________________________________
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