Skip to content

Commit dea445c

Browse files
authored
Merge pull request #9 from rokonrad/master
Use stamp_fmt from config for stdout + fix missing kernel logs on stdout
2 parents c886b24 + babde02 commit dea445c

File tree

1 file changed

+78
-56
lines changed

1 file changed

+78
-56
lines changed

src/metalog.c

Lines changed: 78 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,6 +1064,78 @@ static void flushAll(void)
10641064
}
10651065
}
10661066

1067+
static int get_stamp_fmt_timestamp(const char *stamp_fmt, char *datebuf, int datebuf_size)
1068+
{
1069+
struct timespec ts;
1070+
clock_gettime(CLOCK_REALTIME, &ts);
1071+
struct tm *tm = localtime(&ts.tv_sec);
1072+
1073+
if ((!stamp_fmt) || (!datebuf) || datebuf_size < 1) {
1074+
return -1;
1075+
}
1076+
1077+
if (tm) {
1078+
/* make a copy as we may change the string */
1079+
char *fmt_str = strdup(stamp_fmt);
1080+
/* Search for and process "%N" */
1081+
char *p = fmt_str;
1082+
while ((p = strchr(p, '%')) != NULL) {
1083+
char *tmp = NULL;
1084+
int n, m;
1085+
unsigned scale;
1086+
unsigned long precision;
1087+
p++;
1088+
if (*p == '%') {
1089+
p++;
1090+
continue;
1091+
}
1092+
n = strspn(p, "0123456789");
1093+
if (p[n] != 'N') {
1094+
p += n;
1095+
continue;
1096+
}
1097+
/* We have "%[nnn]N" */
1098+
p[-1] = '\0';
1099+
p[n] = '\0';
1100+
scale = 1;
1101+
precision = 9;
1102+
if (n) {
1103+
int old_errno = errno;
1104+
char *e = NULL;
1105+
errno = 0;
1106+
precision = strtoul(p, &e, 10);
1107+
if (!errno && p != e && !*e) {
1108+
if (precision == 0 || precision > 9)
1109+
precision = 9;
1110+
m = 9 - precision;
1111+
while (--m >= 0)
1112+
scale *= 10;
1113+
}
1114+
errno = old_errno;
1115+
}
1116+
m = p - fmt_str;
1117+
p += n + 1;
1118+
if (asprintf(&tmp, "%s%0*u%s", fmt_str, (unsigned)precision, (unsigned)ts.tv_nsec / scale, p) <= 0) {
1119+
break;
1120+
}
1121+
free(fmt_str);
1122+
fmt_str = tmp;
1123+
p = fmt_str + m;
1124+
}
1125+
strftime(datebuf, datebuf_size, fmt_str, tm);
1126+
free(fmt_str);
1127+
}
1128+
1129+
return 0;
1130+
}
1131+
1132+
static int log_stdout(const char * const date,
1133+
const char * const prg, const char * const info)
1134+
{
1135+
printf("%s [%s] %s\n", date, prg, info);
1136+
return 0;
1137+
}
1138+
10671139
static int processLogLine(const int logcode,
10681140
const char * const prg, char * const info)
10691141
{
@@ -1161,67 +1233,19 @@ static int processLogLine(const int logcode,
11611233

11621234
char datebuf[100] = { '\0' };
11631235
if (block->output && block->output->stamp_fmt[0]) {
1164-
struct timespec ts;
1165-
clock_gettime(CLOCK_REALTIME, &ts);
1166-
struct tm *tm = localtime(&ts.tv_sec);
1167-
if (tm) {
1168-
/* make a copy as we may change the string */
1169-
char *fmt_str = strdup(block->output->stamp_fmt);
1170-
/* Search for and process "%N" */
1171-
char *p = fmt_str;
1172-
while ((p = strchr(p, '%')) != NULL) {
1173-
char *tmp = NULL;
1174-
int n, m;
1175-
unsigned scale;
1176-
unsigned long precision;
1177-
p++;
1178-
if (*p == '%') {
1179-
p++;
1180-
continue;
1181-
}
1182-
n = strspn(p, "0123456789");
1183-
if (p[n] != 'N') {
1184-
p += n;
1185-
continue;
1186-
}
1187-
/* We have "%[nnn]N" */
1188-
p[-1] = '\0';
1189-
p[n] = '\0';
1190-
scale = 1;
1191-
precision = 9;
1192-
if (n) {
1193-
int old_errno = errno;
1194-
char *e = NULL;
1195-
errno = 0;
1196-
precision = strtoul(p, &e, 10);
1197-
if (!errno && p != e && !*e) {
1198-
if (precision == 0 || precision > 9)
1199-
precision = 9;
1200-
m = 9 - precision;
1201-
while (--m >= 0)
1202-
scale *= 10;
1203-
}
1204-
errno = old_errno;
1205-
}
1206-
m = p - fmt_str;
1207-
p += n + 1;
1208-
if (asprintf(&tmp, "%s%0*u%s", fmt_str, (unsigned)precision, (unsigned)ts.tv_nsec / scale, p) <= 0) {
1209-
break;
1210-
}
1211-
free(fmt_str);
1212-
fmt_str = tmp;
1213-
p = fmt_str + m;
1214-
}
1215-
strftime(datebuf, sizeof datebuf, fmt_str, tm);
1216-
free(fmt_str);
1217-
}
1236+
get_stamp_fmt_timestamp(block->output->stamp_fmt,
1237+
datebuf,
1238+
sizeof(datebuf));
12181239
}
12191240

12201241
bool do_break = false;
12211242
if (block->output != NULL) {
12221243
/* write a real log entry */
12231244
writeLogLine(block->output, datebuf, prg, info);
12241245

1246+
/* write to stdout */
1247+
log_stdout(datebuf, prg, info);
1248+
12251249
/* send the log entry to the remote host */
12261250
if (remote_host.hostname != NULL && block->remote_log) {
12271251
sendRemote(prg, info);
@@ -1287,8 +1311,6 @@ static int log_udp(char *buf, int bsize)
12871311
buf[bsize] = '\0';
12881312
if (write(1, buf, strlen(buf)) != (ssize_t) strlen(buf))
12891313
return -1;
1290-
if (write(1, "\n", 1) != 1)
1291-
return -1;
12921314

12931315
return log_line(LOGLINETYPE_SYSLOG, buf);
12941316
}

0 commit comments

Comments
 (0)