A bit of printf, character escaping, line wrapping, duplicate suppression, format fixing and stuff like that ...
r3757 | dcl | 2012-10-04 13:49:02 +1000 (Thu, 04 Oct 2012) | 1 line
This commit is contained in:
180
servlog.c
180
servlog.c
@@ -47,6 +47,7 @@
|
|||||||
- NETWrites log message (if enabled) before attempt to write to file
|
- NETWrites log message (if enabled) before attempt to write to file
|
||||||
- uses OpenVerifyLogFile helper function (removed duplicate code)
|
- uses OpenVerifyLogFile helper function (removed duplicate code)
|
||||||
-----------------------------------------------------------------------------*/
|
-----------------------------------------------------------------------------*/
|
||||||
|
#include <stdarg.h>
|
||||||
#include "fortify.h"
|
#include "fortify.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@@ -317,7 +318,7 @@ snprintf(fPath, 1023, "%s/", "../log");
|
|||||||
iLogUsable=flag;
|
iLogUsable=flag;
|
||||||
}
|
}
|
||||||
/*---------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------*/
|
||||||
void SICSLogWriteTime(char *pText, OutCode eOut, struct timeval *tp)
|
static void SICSLogWriteFile(char *pText, OutCode eOut, struct timeval *tp)
|
||||||
{
|
{
|
||||||
pCaptureEntry pCurrent;
|
pCaptureEntry pCurrent;
|
||||||
int text_len;
|
int text_len;
|
||||||
@@ -397,16 +398,76 @@ snprintf(fPath, 1023, "%s/", "../log");
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void SICSLogWriteTime(char *pText, OutCode eOut, struct timeval *tp) {
|
||||||
|
char buf[200];
|
||||||
|
const char *cp = pText;
|
||||||
|
int idx = 0;
|
||||||
|
struct timeval tv;
|
||||||
|
|
||||||
|
if (tp == NULL) {
|
||||||
|
gettimeofday(&tv, NULL);
|
||||||
|
tp = &tv;
|
||||||
|
}
|
||||||
|
while (*cp) {
|
||||||
|
if (*cp == '\n') {
|
||||||
|
buf[idx++] = '\n';
|
||||||
|
buf[idx++] = '\0';
|
||||||
|
SICSLogWriteFile(buf, eOut, tp);
|
||||||
|
idx = 0;
|
||||||
|
++cp;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
else if (*cp == '\r') {
|
||||||
|
buf[idx++] = '\\';
|
||||||
|
buf[idx++] = 'r';
|
||||||
|
}
|
||||||
|
else if (*cp == '\t') {
|
||||||
|
buf[idx++] = '\\';
|
||||||
|
buf[idx++] = 't';
|
||||||
|
}
|
||||||
|
else if (*cp < ' ' || *cp > '~') {
|
||||||
|
const char hex[] = "0123456789ABCDEF";
|
||||||
|
buf[idx++] = '<';
|
||||||
|
buf[idx++] = hex[(*cp >> 4) & 0xF];
|
||||||
|
buf[idx++] = hex[(*cp) & 0xF];
|
||||||
|
buf[idx++] = '>';
|
||||||
|
} else {
|
||||||
|
buf[idx++] = *cp;
|
||||||
|
}
|
||||||
|
cp++;
|
||||||
|
if (idx > 132) {
|
||||||
|
if (buf[idx - 1] != '\n')
|
||||||
|
buf[idx++] = '\n';
|
||||||
|
buf[idx++] = '\0';
|
||||||
|
SICSLogWriteFile(buf, eOut, tp);
|
||||||
|
idx = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (idx > 0) {
|
||||||
|
if (buf[idx - 1] != '\n')
|
||||||
|
buf[idx++] = '\n';
|
||||||
|
buf[idx++] = '\0';
|
||||||
|
SICSLogWriteFile(buf, eOut, tp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void SICSLogWrite(char *pText, OutCode eOut) {
|
void SICSLogWrite(char *pText, OutCode eOut) {
|
||||||
SICSLogWriteTime(pText, eOut, NULL);
|
struct timeval tv;
|
||||||
|
gettimeofday(&tv, NULL);
|
||||||
|
SICSLogWriteTime(pText, eOut, &tv);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SICSLogWriteHexTime(const char* text, int count, OutCode eOut, struct timeval *tp) {
|
void SICSLogWriteHexTime(const char* text, int count, OutCode eOut, struct timeval *tp) {
|
||||||
char hex[] = "0123456789ABCDEF";
|
const char hex[] = "0123456789ABCDEF";
|
||||||
char addr[20], left[80], right[80];
|
char addr[20], left[80], right[80];
|
||||||
char *lp = left;
|
char *lp = left;
|
||||||
char *rp = right;
|
char *rp = right;
|
||||||
int i;
|
int i;
|
||||||
|
int duplicates;
|
||||||
|
/* Limit the output */
|
||||||
|
if (count > 1024)
|
||||||
|
count = 1024;
|
||||||
|
duplicates = 0;
|
||||||
for (i = 0; i < count; ++i) {
|
for (i = 0; i < count; ++i) {
|
||||||
if ((i & 0xF) == 0) {
|
if ((i & 0xF) == 0) {
|
||||||
if (i > 0) {
|
if (i > 0) {
|
||||||
@@ -414,7 +475,27 @@ snprintf(fPath, 1023, "%s/", "../log");
|
|||||||
snprintf(line, sizeof(line)-1, "%-6s: %-49s | %-17s |", addr, left, right);
|
snprintf(line, sizeof(line)-1, "%-6s: %-49s | %-17s |", addr, left, right);
|
||||||
SICSLogWriteTime(line, eOut, tp);
|
SICSLogWriteTime(line, eOut, tp);
|
||||||
}
|
}
|
||||||
snprintf(addr, sizeof(addr)-1, "%06x", i);
|
snprintf(addr, sizeof(addr)-1, "0x%04X", i);
|
||||||
|
while (i >= 16 && i + 16 < count) {
|
||||||
|
if (memcmp(&text[i-16], &text[i], 16) != 0)
|
||||||
|
break;
|
||||||
|
++duplicates;
|
||||||
|
i += 16;
|
||||||
|
}
|
||||||
|
if (duplicates > 0) {
|
||||||
|
if (duplicates > 1) {
|
||||||
|
char line[132];
|
||||||
|
snprintf(line, sizeof(line)-1, "%-6s: ... (%d duplicates)", addr, duplicates);
|
||||||
|
SICSLogWriteTime(line, eOut, tp);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
char line[132];
|
||||||
|
snprintf(line, sizeof(line)-1, "%-6s: %-49s | %-17s |", addr, left, right);
|
||||||
|
SICSLogWriteTime(line, eOut, tp);
|
||||||
|
}
|
||||||
|
duplicates = 0;
|
||||||
|
}
|
||||||
|
snprintf(addr, sizeof(addr)-1, "0x%04X", i);
|
||||||
lp = left;
|
lp = left;
|
||||||
rp = right;
|
rp = right;
|
||||||
}
|
}
|
||||||
@@ -425,7 +506,8 @@ snprintf(fPath, 1023, "%s/", "../log");
|
|||||||
*rp++ = text[i];
|
*rp++ = text[i];
|
||||||
else
|
else
|
||||||
*rp++ = '.';
|
*rp++ = '.';
|
||||||
if ((i & 0xF) == 8) {
|
/* if we just did slot 7, insert an extra space */
|
||||||
|
if ((i & 0xF) == 7) {
|
||||||
*lp++ = ' ';
|
*lp++ = ' ';
|
||||||
*rp++ = ' ';
|
*rp++ = ' ';
|
||||||
}
|
}
|
||||||
@@ -439,5 +521,91 @@ snprintf(fPath, 1023, "%s/", "../log");
|
|||||||
}
|
}
|
||||||
|
|
||||||
void SICSLogWriteHex(const char* text, int count, OutCode eOut) {
|
void SICSLogWriteHex(const char* text, int count, OutCode eOut) {
|
||||||
SICSLogWriteHexTime(text, count, eOut, NULL);
|
struct timeval tv;
|
||||||
|
gettimeofday(&tv, NULL);
|
||||||
|
SICSLogWriteHexTime(text, count, eOut, &tv);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void SICSLogTimePrintf(OutCode eOut, struct timeval *tp, const char *fmt, ...)
|
||||||
|
{
|
||||||
|
va_list ap;
|
||||||
|
char buf[256];
|
||||||
|
char *dyn;
|
||||||
|
unsigned int l;
|
||||||
|
int res;
|
||||||
|
|
||||||
|
va_start(ap, fmt);
|
||||||
|
l = vsnprintf(buf, sizeof buf, fmt, ap);
|
||||||
|
va_end(ap);
|
||||||
|
if (l >= sizeof buf) {
|
||||||
|
/* we have probably a C99 conforming snprintf and
|
||||||
|
need a larger buffer
|
||||||
|
*/
|
||||||
|
dyn = malloc(l+1);
|
||||||
|
if (dyn != NULL) {
|
||||||
|
va_start(ap, fmt);
|
||||||
|
vsnprintf(dyn, l+1, fmt, ap);
|
||||||
|
va_end(ap);
|
||||||
|
SICSLogWriteTime(dyn, eOut, tp);
|
||||||
|
free(dyn);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
SICSLogWriteTime(buf, eOut, tp);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
void SICSLogPrintf(OutCode eOut, const char *fmt, ...)
|
||||||
|
{
|
||||||
|
va_list ap;
|
||||||
|
char buf[256];
|
||||||
|
char *dyn;
|
||||||
|
unsigned int l;
|
||||||
|
int res;
|
||||||
|
|
||||||
|
va_start(ap, fmt);
|
||||||
|
l = vsnprintf(buf, sizeof buf, fmt, ap);
|
||||||
|
va_end(ap);
|
||||||
|
if (l >= sizeof buf) {
|
||||||
|
/* we have probably a C99 conforming snprintf and
|
||||||
|
need a larger buffer
|
||||||
|
*/
|
||||||
|
dyn = malloc(l+1);
|
||||||
|
if (dyn != NULL) {
|
||||||
|
va_start(ap, fmt);
|
||||||
|
vsnprintf(dyn, l+1, fmt, ap);
|
||||||
|
va_end(ap);
|
||||||
|
SICSLogWrite(dyn, eOut);
|
||||||
|
free(dyn);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
SICSLogWrite(buf, eOut);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Test of the logging facilities */
|
||||||
|
int testLogCmd(SConnection *pCon, SicsInterp *pInter, void *pData, int argc, char *argv[]) {
|
||||||
|
char lbuf[2048];
|
||||||
|
char sbuf[1000];
|
||||||
|
int i;
|
||||||
|
SICSLogWrite("Multiline:\nLine 1\r\nLine 2\r\nLine 3\r\n", eStatus);
|
||||||
|
memset(lbuf, 0, sizeof(lbuf));
|
||||||
|
memset(sbuf, ' ', sizeof(sbuf));
|
||||||
|
SICSLogPrintf(eStatus, "Hexlog %d all zero bytes", sizeof(lbuf));
|
||||||
|
SICSLogWriteHex(lbuf, sizeof(lbuf), eStatus);
|
||||||
|
for (i = 0; i <= 128; ++i)
|
||||||
|
sbuf[i] = sbuf[sizeof(sbuf)-1 - i] = i;
|
||||||
|
sbuf[sizeof(sbuf)/2] = '!';
|
||||||
|
SICSLogPrintf(eStatus, "Hexlog %d mid space bytes", sizeof(sbuf));
|
||||||
|
SICSLogWriteHex(sbuf, sizeof(sbuf), eStatus);
|
||||||
|
for (i = 0; i < 1000; ++i)
|
||||||
|
sbuf[i] = ' ' + (i % 96);
|
||||||
|
sbuf[sizeof(sbuf)-1] = '\0';
|
||||||
|
SICSLogWrite("Very long line 1000 bytes", eStatus);
|
||||||
|
SICSLogWrite(sbuf, eStatus);
|
||||||
|
SCSendOK(pCon);
|
||||||
|
return OKOK;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -19,6 +19,8 @@
|
|||||||
void SICSLogWriteTime(char *ptext, OutCode eOut, struct timeval *tp );
|
void SICSLogWriteTime(char *ptext, OutCode eOut, struct timeval *tp );
|
||||||
void SICSLogWriteHex(const char* text, int count, OutCode eOut);
|
void SICSLogWriteHex(const char* text, int count, OutCode eOut);
|
||||||
void SICSLogWriteHexTime(const char* text, int count, OutCode eOut, struct timeval *tp);
|
void SICSLogWriteHexTime(const char* text, int count, OutCode eOut, struct timeval *tp);
|
||||||
|
void SICSLogPrintf(OutCode eOut, const char*fmt, ...);
|
||||||
|
void SICSLogTimePrintf(OutCode eOut, struct timeval *tp, const char *fmt, ...);
|
||||||
void SICSLogEnable(int flag);
|
void SICSLogEnable(int flag);
|
||||||
int KillCapture(SConnection *pCon);
|
int KillCapture(SConnection *pCon);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user