diff options
author | Rainer Gerhards <rgerhards@adiscon.com> | 2008-01-10 08:00:47 +0000 |
---|---|---|
committer | Rainer Gerhards <rgerhards@adiscon.com> | 2008-01-10 08:00:47 +0000 |
commit | 24c125cfc3032e6269e6e5de91c72c91508adde0 (patch) | |
tree | a3f86f5dc3ff9e23c46f845bdb64e0a50ae84a24 /srUtils.c | |
parent | 2dccfb6780c89cf9ac5e215afd7d2a18ee211840 (diff) | |
download | rsyslog-24c125cfc3032e6269e6e5de91c72c91508adde0.tar.gz rsyslog-24c125cfc3032e6269e6e5de91c72c91508adde0.tar.xz rsyslog-24c125cfc3032e6269e6e5de91c72c91508adde0.zip |
made queue file names better readable
Diffstat (limited to 'srUtils.c')
-rwxr-xr-x | srUtils.c | 33 |
1 files changed, 30 insertions, 3 deletions
@@ -243,25 +243,35 @@ void skipWhiteSpace(uchar **pp) * <directory name>/<name>.<number> * If number is negative, it is not used. If any of the strings is * NULL, an empty string is used instead. Length must be provided. + * lNumDigits is the minimum number of digits that lNum should have. This + * is to pretty-print the file name, e.g. lNum = 3, lNumDigits= 4 will + * result in "0003" being used inside the file name. Set lNumDigits to 0 + * to use as few space as possible. * rgerhards, 2008-01-03 */ -rsRetVal genFileName(uchar **ppName, uchar *pDirName, size_t lenDirName, uchar *pFName, size_t lenFName, long lNum) +rsRetVal genFileName(uchar **ppName, uchar *pDirName, size_t lenDirName, uchar *pFName, + size_t lenFName, long lNum, int lNumDigits) { DEFiRet; uchar *pName; uchar *pNameWork; size_t lenName; uchar szBuf[128]; /* buffer for number */ + char szFmtBuf[32]; /* buffer for snprintf format */ size_t lenBuf; if(lNum < 0) { szBuf[0] = '\0'; lenBuf = 0; } else { - lenBuf = snprintf((char*)szBuf, sizeof(szBuf), ".%ld", lNum); + if(lNumDigits > 0) { + snprintf(szFmtBuf, sizeof(szFmtBuf), ".%%0%dld", lNumDigits); + lenBuf = snprintf((char*)szBuf, sizeof(szBuf), szFmtBuf, lNum); + } else + lenBuf = snprintf((char*)szBuf, sizeof(szBuf), ".%ld", lNum); } - lenName = lenDirName + 1 + lenName + lenBuf + 1; /* last +1 for \0 char! */ + lenName = lenDirName + 1 + lenFName + lenBuf + 1; /* last +1 for \0 char! */ if((pName = malloc(sizeof(uchar) * lenName)) == NULL) ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); @@ -283,6 +293,23 @@ finalize_it: return iRet; } +/* get the number of digits required to represent a given number. We use an + * iterative approach as we do not like to draw in the floating point + * library just for log(). -- rgerhards, 2008-01-10 + */ +int getNumberDigits(long lNum) +{ + int iDig; + + if(lNum == 0) + iDig = 1; + else + for(iDig = 0 ; lNum != 0 ; ++iDig) + lNum /= 10; + + return iDig; +} + /* * vi:set ai: |