DPDK patches and discussions
 help / color / mirror / Atom feed
From: Stephen Hemminger <stephen@networkplumber.org>
To: Soumya Muralidhar <soumya.muralidhar@gigamon.com>
Cc: "dev@dpdk.org" <dev@dpdk.org>
Subject: Re: Add timestamp to dpdk.log
Date: Wed, 5 Jan 2022 09:58:31 -0800	[thread overview]
Message-ID: <20220105095831.20e74174@hermes.local> (raw)
In-Reply-To: <892917C6-9F3F-4AF0-9429-A4AC19DC47BC@gigamon.com>

On Wed, 5 Jan 2022 17:42:49 +0000
Soumya Muralidhar <soumya.muralidhar@gigamon.com> wrote:

> Hi Stephen,
> 
> Thanks for getting back. But I am looking at storing logs with timestamps in a file, basically something similar to syslog. Could you help me with that ?
> 
> Regards,
> Soumya
> 
> On 1/5/22, 8:17 AM, "Stephen Hemminger" <stephen@networkplumber.org> wrote:
> 
>     WARNING: This email originated from outside of Gigamon. Use caution with any links or attachments.
> 
> 
>     Your message is garbaled with lots of bogus URL's. Please avoid HTML mail
>     or weird links.
> 
>     I use this. There was a more general version submitted as a patch to
>     DPDK but no one seemed interested.  The reason for redirecting stdout
>     is that are libraries we use that print to stdout and want to get timestamps
>     from them if debugging startup issues.
> 
> 
>     /*
>      * Sample of redirecting DPDK log stream.
>      *
>      * If program is run for debugging then stdout will be a console and
>      * put messages onto console stream with timestamps.  Otherwise, just
>      * put message onto stderr which systemd journal handles.  No need for
>      * syslog because that causes duplicate messages.
>      */
> 
>     static struct timespec log_t0; /* Start program in monotonic time */
> 
>     /*
>      * Write message to standard output with timestamp.
>      * Use writev() so that timestamp and message do not get interleaved.
>      */
>     static ssize_t
>     dpdk_log_tty_write(__rte_unused void *ctx, const char *buf, size_t size)
>     {
>             struct timespec ts;
>             struct iovec iov[2];
>             char tbuf[64];
> 
>             /* format up monotonic timestamp */
>             clock_gettime(CLOCK_MONOTONIC, &ts);
> 
>             ts.tv_sec -= log_t0.tv_sec;
>             ts.tv_nsec -= log_t0.tv_nsec;
>             if (ts.tv_nsec < 0) {
>                     --ts.tv_sec;
>                     ts.tv_nsec += NS_PER_S;
>             }
> 
>             iov[0].iov_base = tbuf;
>             iov[0].iov_len  = snprintf(tbuf, sizeof(tbuf), "[%8lu.%06lu] ",
>                                        ts.tv_sec, ts.tv_nsec / 1000u);
> 
>             /* extra cast is workaround to remove const qualifier */
>             iov[1].iov_base = (void *)(uintptr_t)buf;
>             iov[1].iov_len = size;
> 
>             return writev(STDOUT_FILENO, iov, 2);
>     }
> 
>     static cookie_io_functions_t dpdk_log_tty_func = {
>             .write = dpdk_log_tty_write,
>     };
> 
>     /*
>      * Print message to stderr with priority format so that
>      * Systemd journal can handle it. Alternative would be
>      * to use syslog or use sd_journal; but less is more.
>      */
>     static ssize_t
>     dpdk_log_write(__rte_unused void *ctx, const char *buf, size_t size)
>     {
>             /* Syslog error levels are from 0 to 7, DPDK uses 1 to 8 */
>             int priority = rte_log_cur_msg_loglevel() - 1;
> 
>             fprintf(stderr, "<%i>%.*s\n", priority, (int)size, buf);
>             return size;
>     }
> 
>     static cookie_io_functions_t dpdk_log_func = {
>             .write = dpdk_log_write,
>     };
> 
>     /*
>      * Override default DPDK logging which duplicates messages
>      * to both stderr and syslog.
>      */
>     static void log_init(void)
>     {
>             backplane_logtype = rte_log_register("backplane");
>             if (backplane_logtype >= 0)
>                     rte_log_set_level(backplane_logtype, RTE_LOG_INFO);
> 
>             if (isatty(STDOUT_FILENO)) {
>                     clock_gettime(CLOCK_MONOTONIC, &log_t0);
> 
>                     stdout = fopencookie(NULL, "w+", dpdk_log_tty_func);
>                     setlinebuf(stdout);
>                     rte_openlog_stream(stdout);
>             } else {
>                     FILE *jf;
> 
>                     jf = fopencookie(NULL, "w+", dpdk_log_func);
>                     rte_openlog_stream(jf);
>             }
>     }
> 
>     /* Put DPDK log back onto stderr */
>     static void log_uninit(void)
>     {
>             FILE *logf;
> 
>             logf = rte_log_get_stream();
>             if (logf != stderr) {
>                     rte_openlog_stream(stderr);
>                     fclose(logf);
>             }
>     }
> 
> This message may contain confidential and privileged information. If it has been sent to you in error, please reply to advise the sender of the error and then immediately delete it. If you are not the intended recipient, do not read, copy, disclose or otherwise use this message. The sender disclaims any liability for such unauthorized use. NOTE that all incoming emails sent to Gigamon email accounts will be archived and may be scanned by us and/or by external service providers to detect and prevent threats to our systems, investigate illegal or inappropriate behavior, and/or eliminate unsolicited promotional emails (“spam”).

Just use my example and make code like dpdk_tty_log_write that does writev to your file.

      reply	other threads:[~2022-01-05 17:58 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-04 19:15 Soumya Muralidhar
2022-01-05 16:17 ` Stephen Hemminger
2022-01-05 17:42   ` Soumya Muralidhar
2022-01-05 17:58     ` Stephen Hemminger [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220105095831.20e74174@hermes.local \
    --to=stephen@networkplumber.org \
    --cc=dev@dpdk.org \
    --cc=soumya.muralidhar@gigamon.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).