qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Stefan Hajnoczi <stefanha@gmail.com>
To: Seiji Aguchi <saguchi@redhat.com>
Cc: kwolf@redhat.com, aliguori@us.ibm.com,
	Seiji Aguchi <seiji.aguchi@hds.com>,
	stefanha@redhat.com, mst@redhat.com, mtosatti@redhat.com,
	qemu-devel@nongnu.org, armbru@redhat.com,
	dle-develop@lists.sourceforge.net, av1474@comtv.ru,
	tomoki.sekiyama@hds.com, pbonzini@redhat.com,
	lcapitulino@redhat.com
Subject: Re: [Qemu-devel] [RFC][PATCH v2]Add timestamp to error message
Date: Wed, 1 May 2013 14:05:50 +0200	[thread overview]
Message-ID: <20130501120550.GC28932@stefanha-thinkpad.redhat.com> (raw)
In-Reply-To: <1367265445-27365-1-git-send-email-saguchi@redhat.com>

On Mon, Apr 29, 2013 at 03:57:25PM -0400, Seiji Aguchi wrote:
> diff --git a/util/qemu-error.c b/util/qemu-error.c
> index 08a36f4..35ef9ab 100644
> --- a/util/qemu-error.c
> +++ b/util/qemu-error.c
> @@ -196,6 +196,96 @@ void error_print_loc(void)
>      }
>  }
>  
> +
> +#define SECS_PER_HOUR   (60 * 60)
> +#define SECS_PER_DAY    (SECS_PER_HOUR * 24)
> +#define DIV(a, b) ((a) / (b) - ((a) % (b) < 0))
> +#define LEAPS_THRU_END_OF(y) (DIV(y, 4) - DIV(y, 100) + DIV(y, 400))
> +
> +const unsigned short int __mon_yday[2][13] = {
> +    /* Normal years.  */
> +    { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
> +    /* Leap years.  */
> +    { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
> +};
> +
> +#define is_leap_year(y) \
> +    ((y) % 4 == 0 && ((y) % 100 != 0 || (y) % 400 == 0))
> +
> +static void error_print_timestamp(void)
> +{
> +    struct timespec ts;
> +    unsigned long long now;
> +
> +    struct tm fields;
> +    long int days, rem, y;
> +    const unsigned short int *ip;
> +    unsigned long long whenSecs;
> +    unsigned int offset = 0; /* We hardcoded GMT */
> +
> +    if (clock_gettime(CLOCK_REALTIME, &ts) < 0) {
> +        return;
> +    }
> +
> +    now = (ts.tv_sec * 1000ull) + (ts.tv_nsec / (1000ull * 1000ull));
> +    /* This code is taken from GLibC under terms of LGPLv2+ */
> +
> +    whenSecs = now / 1000ull;
> +
> +    days = whenSecs / SECS_PER_DAY;
> +    rem = whenSecs % SECS_PER_DAY;
> +    rem += offset;
> +    while (rem < 0) {
> +        rem += SECS_PER_DAY;
> +        --days;
> +    }
> +    while (rem >= SECS_PER_DAY) {
> +        rem -= SECS_PER_DAY;
> +        ++days;
> +    }
> +    fields.tm_hour = rem / SECS_PER_HOUR;
> +    rem %= SECS_PER_HOUR;
> +    fields.tm_min = rem / 60;
> +    fields.tm_sec = rem % 60;
> +    /* January 1, 1970 was a Thursday.  */
> +    fields.tm_wday = (4 + days) % 7;
> +    if (fields.tm_wday < 0) {
> +        fields.tm_wday += 7;
> +    }
> +    y = 1970;
> +
> +    while (days < 0 || days >= (is_leap_year(y) ? 366 : 365)) {
> +        /* Guess a corrected year, assuming 365 days per year.  */
> +        long int yg = y + days / 365 - (days % 365 < 0);
> +
> +      /* Adjust DAYS and Y to match the guessed year.  */
> +      days -= ((yg - y) * 365
> +               + LEAPS_THRU_END_OF(yg - 1)
> +               - LEAPS_THRU_END_OF(y - 1));
> +      y = yg;
> +    }
> +    fields.tm_year = y - 1900;
> +
> +    fields.tm_yday = days;
> +    ip = __mon_yday[is_leap_year(y)];
> +    for (y = 11; days < (long int) ip[y]; --y) {
> +        continue;
> +    }
> +
> +    days -= ip[y];
> +    fields.tm_mon = y;
> +    fields.tm_mday = days + 1;
> +
> +    error_printf(
> +           "%4d-%02d-%02d %02d:%02d:%02d.%03lld+0000 ",
> +           fields.tm_year + 1900, fields.tm_mon + 1, fields.tm_mday,
> +           fields.tm_hour, fields.tm_min, fields.tm_sec,
> +           now % 1000);

Can strftime(3) be used instead of copying code from glibc?

  reply	other threads:[~2013-05-01 12:05 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-04-29 19:57 [Qemu-devel] [RFC][PATCH v2]Add timestamp to error message Seiji Aguchi
2013-05-01 12:05 ` Stefan Hajnoczi [this message]
2013-05-01 12:16   ` Eric Blake
2013-05-01 12:25     ` Daniel P. Berrange
2013-05-01 14:26       ` Anthony Liguori
2013-05-02 20:39         ` Seiji Aguchi

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=20130501120550.GC28932@stefanha-thinkpad.redhat.com \
    --to=stefanha@gmail.com \
    --cc=aliguori@us.ibm.com \
    --cc=armbru@redhat.com \
    --cc=av1474@comtv.ru \
    --cc=dle-develop@lists.sourceforge.net \
    --cc=kwolf@redhat.com \
    --cc=lcapitulino@redhat.com \
    --cc=mst@redhat.com \
    --cc=mtosatti@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=saguchi@redhat.com \
    --cc=seiji.aguchi@hds.com \
    --cc=stefanha@redhat.com \
    --cc=tomoki.sekiyama@hds.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).