Skip to content

Commit

Permalink
logging: Add support for setting a log resource timestamp format.
Browse files Browse the repository at this point in the history
This adds support to the generic message resources for overriding the default
timestamp format per message resource defined.
  • Loading branch information
Marco van Wieringen committed Jan 1, 2016
1 parent 7ac3596 commit 273b26d
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 86 deletions.
3 changes: 3 additions & 0 deletions src/dird/dird_conf.c
Expand Up @@ -2649,6 +2649,9 @@ void free_resource(RES *sres, int type)
if (res->res_msgs.operator_cmd) {
free(res->res_msgs.operator_cmd);
}
if (res->res_msgs.timestamp_format) {
free(res->res_msgs.timestamp_format);
}
free_msgs_res((MSGSRES *)res); /* free message resource */
res = NULL;
break;
Expand Down
3 changes: 3 additions & 0 deletions src/filed/filed_conf.c
Expand Up @@ -376,6 +376,9 @@ void free_resource(RES *sres, int type)
if (res->res_msgs.operator_cmd) {
free(res->res_msgs.operator_cmd);
}
if (res->res_msgs.timestamp_format) {
free(res->res_msgs.timestamp_format);
}
free_msgs_res((MSGSRES *)res); /* free message resource */
res = NULL;
break;
Expand Down
50 changes: 39 additions & 11 deletions src/lib/message.c
Expand Up @@ -304,7 +304,7 @@ void init_msg(JCR *jcr, MSGSRES *msg, job_code_callback_t job_code_callback)
daemon_msgs = (MSGSRES *)malloc(sizeof(MSGSRES));
memset(daemon_msgs, 0, sizeof(MSGSRES));
for (i=1; i<=M_MAX; i++) {
add_msg_dest(daemon_msgs, MD_STDOUT, i, NULL, NULL);
add_msg_dest(daemon_msgs, MD_STDOUT, i, NULL, NULL, NULL);
}
Dmsg1(050, "Create daemon global message resource %p\n", daemon_msgs);
return;
Expand Down Expand Up @@ -390,7 +390,8 @@ void init_console_msg(const char *wd)
* but in the case of MAIL is a space separated list of
* email addresses, ...
*/
void add_msg_dest(MSGSRES *msg, int dest_code, int msg_type, char *where, char *mail_cmd)
void add_msg_dest(MSGSRES *msg, int dest_code, int msg_type,
char *where, char *mail_cmd, char *timestamp_format)
{
DEST *d;

Expand All @@ -404,7 +405,7 @@ void add_msg_dest(MSGSRES *msg, int dest_code, int msg_type, char *where, char *
Dmsg4(850, "Add to existing d=%p msgtype=%d destcode=%d where=%s\n",
d, msg_type, dest_code, NPRT(where));
set_bit(msg_type, d->msg_types);
set_bit(msg_type, msg->send_msg); /* set msg_type bit in our local */
set_bit(msg_type, msg->send_msg); /* Set msg_type bit in our local */
return;
}
}
Expand All @@ -416,16 +417,23 @@ void add_msg_dest(MSGSRES *msg, int dest_code, int msg_type, char *where, char *
memset(d, 0, sizeof(DEST));
d->next = msg->dest_chain;
d->dest_code = dest_code;
set_bit(msg_type, d->msg_types); /* set type bit in structure */
set_bit(msg_type, msg->send_msg); /* set type bit in our local */
set_bit(msg_type, d->msg_types); /* Set type bit in structure */
set_bit(msg_type, msg->send_msg); /* Set type bit in our local */

if (where) {
d->where = bstrdup(where);
}

if (mail_cmd) {
d->mail_cmd = bstrdup(mail_cmd);
}
Dmsg5(850, "add new d=%p msgtype=%d destcode=%d where=%s mailcmd=%s\n",
d, msg_type, dest_code, NPRT(where), NPRT(d->mail_cmd));

if (timestamp_format) {
d->timestamp_format = bstrdup(timestamp_format);
}

Dmsg6(850, "add new d=%p msgtype=%d destcode=%d where=%s mailcmd=%s timestampformat=%s\n",
d, msg_type, dest_code, NPRT(where), NPRT(d->mail_cmd), NPRT(d->timestamp_format));
msg->dest_chain = d;
}

Expand Down Expand Up @@ -679,6 +687,9 @@ void free_msgs_res(MSGSRES *msgs)
if (d->mail_cmd) {
free(d->mail_cmd);
}
if (d->timestamp_format) {
free(d->timestamp_format);
}
old = d; /* save pointer to release */
d = d->next; /* point to next buffer */
free(old); /* free the destination item */
Expand Down Expand Up @@ -842,6 +853,7 @@ void dispatch_message(JCR *jcr, int type, utime_t mtime, char *msg)
MSGSRES *msgs;
BPIPE *bpipe;
const char *mode;
bool dt_conversion = false;

Dmsg2(850, "Enter dispatch_message type=%d msg=%s", type, msg);

Expand All @@ -858,11 +870,9 @@ void dispatch_message(JCR *jcr, int type, utime_t mtime, char *msg)
if (mtime == 1) {
*dt = 0;
dtlen = 0;
mtime = time(NULL); /* get time for SQL log */
mtime = time(NULL); /* Get time for SQL log */
} else {
bstrftime(dt, sizeof(dt), mtime, log_timestamp_format);
bstrncat(dt, " ", sizeof(dt));
dtlen = strlen(dt);
dt_conversion = true;
}

/*
Expand Down Expand Up @@ -922,6 +932,10 @@ void dispatch_message(JCR *jcr, int type, utime_t mtime, char *msg)
* If closing this message resource, print and send to syslog, then get out.
*/
if (msgs->is_closing()) {
if (dt_conversion) {
bstrftime(dt, sizeof(dt), mtime, log_timestamp_format);
bstrncat(dt, " ", sizeof(dt));
}
fputs(dt, stdout);
fputs(msg, stdout);
fflush(stdout);
Expand All @@ -931,6 +945,20 @@ void dispatch_message(JCR *jcr, int type, utime_t mtime, char *msg)

for (d = msgs->dest_chain; d; d = d->next) {
if (bit_is_set(type, d->msg_types)) {
/*
* See if a specific timestamp format was specified for this log resource.
* Otherwise apply the global setting in log_timestamp_format.
*/
if (dt_conversion) {
if (d->timestamp_format) {
bstrftime(dt, sizeof(dt), mtime, d->timestamp_format);
} else {
bstrftime(dt, sizeof(dt), mtime, log_timestamp_format);
}
bstrncat(dt, " ", sizeof(dt));
dtlen = strlen(dt);
}

switch (d->dest_code) {
case MD_CATALOG:
if (!jcr || !jcr->db) {
Expand Down
1 change: 1 addition & 0 deletions src/lib/message.h
Expand Up @@ -112,6 +112,7 @@ typedef struct s_dest {
char msg_types[NR_MSG_TYPES]; /* Message type mask */
char *where; /* Filename/program name */
char *mail_cmd; /* Mail command */
char *timestamp_format; /* Timestamp format to use in logging messages */
int syslog_facility; /* Syslog Facility */
POOLMEM *mail_filename; /* Unique mail filename */
} DEST;
Expand Down
1 change: 1 addition & 0 deletions src/lib/msg_res.h
Expand Up @@ -38,6 +38,7 @@ static RES_ITEM msgs_items[] = {
{ "Description", CFG_TYPE_STR, ITEM(res_msgs.hdr.desc), 0, 0, NULL, NULL, NULL },
{ "MailCommand", CFG_TYPE_STR, ITEM(res_msgs.mail_cmd), 0, 0, NULL, NULL, NULL },
{ "OperatorCommand", CFG_TYPE_STR, ITEM(res_msgs.operator_cmd), 0, 0, NULL, NULL, NULL },
{ "TimestampFormat", CFG_TYPE_STR, ITEM(res_msgs.timestamp_format), 0, 0, NULL, NULL, NULL },
{ "Syslog", CFG_TYPE_MSGS, ITEM(res_msgs), MD_SYSLOG, 0, NULL, NULL, NULL },
{ "Mail", CFG_TYPE_MSGS, ITEM(res_msgs), MD_MAIL, 0, NULL, NULL, NULL },
{ "MailOnError", CFG_TYPE_MSGS, ITEM(res_msgs), MD_MAIL_ON_ERROR, 0, NULL, NULL, NULL },
Expand Down
1 change: 1 addition & 0 deletions src/lib/parse_conf.h
Expand Up @@ -280,6 +280,7 @@ class MSGSRES : public BRSRES {
public:
char *mail_cmd; /* Mail command */
char *operator_cmd; /* Operator command */
char *timestamp_format; /* Timestamp format */
DEST *dest_chain; /* chain of destinations */
char send_msg[nbytes_for_bits(M_MAX+1)]; /* Bit array of types */

Expand Down
3 changes: 2 additions & 1 deletion src/lib/protos.h
Expand Up @@ -267,7 +267,8 @@ void my_name_is(int argc, char *argv[], const char *name);
void init_msg(JCR *jcr, MSGSRES *msg, job_code_callback_t job_code_callback = NULL);
void term_msg(void);
void close_msg(JCR *jcr);
void add_msg_dest(MSGSRES *msg, int dest, int type, char *where, char *dest_code);
void add_msg_dest(MSGSRES *msg, int dest, int type,
char *where, char *mail_cmd, char *timestamp_format);
void rem_msg_dest(MSGSRES *msg, int dest, int type, char *where);
void Jmsg(JCR *jcr, int type, utime_t mtime, const char *fmt, ...);
void dispatch_message(JCR *jcr, int type, utime_t mtime, char *buf);
Expand Down

0 comments on commit 273b26d

Please sign in to comment.