Skip to main content
added 605 characters in body
Source Link
chux
  • 36.4k
  • 2
  • 43
  • 96

Just a small review of logging time.


[logging time]

Add error checking

Functions like time(), gmtime_r(), strftime() all could return an error indication. Use it.

Use Z

Using "-%Z" to append a '-' and then the time zone name is curious. I fond nothing in ISO 8601 to support that format.

What is common is to append a "Z" for UTC times.

Buffer size

A generous buffer size is good, yet we could reduce it a ways and still maintain a 2x factor.

Standard code

gmtime_r() is not part of the standard C library. The alterative below uses gmtime() for example.

Alternate code

    #define TIME_BUF_LEN (11 /* year */ + 5*2 /* mdHMS */ + 6 /* separators */ )
    char buffer[TIME_BUF_LEN * 2 + 1];

    time_t now = time(NULL); // get current time
    if (now == -1) {
      strcpy(buffer, "Unknown time");
    } else {
      struct tm *tm_now = gmtime(&now);
      if (tm_now == NULL) {
        strcpy(buffer, "Unknown time");
      } else {
        if (strftime(buffer, sizeof buffer, "%Y-%m-%dT%H:%M:%SZ", tm_now) == 0) {
          strcpy(buffer, "Unknown time");
        }
      }
    }

[Edit]

Latent Bug

In sendToSocket(), code tests errno after send(), yet does not clear errno first. Better code would zero it, even if in this application, sendToSocket() is not called with errno != 0 to allow for greater future use. send(), like many functions, does not zero errno on success.

    errno = 0; // Add
    num_bytes = send(sock, &pbuf[total], size-total, 0);
    if (num_bytes < 0) {
        if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) {

Alternatively, sendToSocket() should be documented that it should not get called with errno != 0.

Just a small review of logging time.

Add error checking

Functions like time(), gmtime_r(), strftime() all could return an error indication. Use it.

Use Z

Using "-%Z" to append a '-' and then the time zone name is curious. I fond nothing in ISO 8601 to support that format.

What is common is to append a "Z" for UTC times.

Buffer size

A generous buffer size is good, yet we could reduce it a ways and still maintain a 2x factor.

Standard code

gmtime_r() is not part of the standard C library. The alterative below uses gmtime() for example.

Alternate code

    #define TIME_BUF_LEN (11 /* year */ + 5*2 /* mdHMS */ + 6 /* separators */ )
    char buffer[TIME_BUF_LEN * 2 + 1];

    time_t now = time(NULL); // get current time
    if (now == -1) {
      strcpy(buffer, "Unknown time");
    } else {
      struct tm *tm_now = gmtime(&now);
      if (tm_now == NULL) {
        strcpy(buffer, "Unknown time");
      } else {
        if (strftime(buffer, sizeof buffer, "%Y-%m-%dT%H:%M:%SZ", tm_now) == 0) {
          strcpy(buffer, "Unknown time");
        }
      }
    }

Just a small review.


[logging time]

Add error checking

Functions like time(), gmtime_r(), strftime() all could return an error indication. Use it.

Use Z

Using "-%Z" to append a '-' and then the time zone name is curious. I fond nothing in ISO 8601 to support that format.

What is common is to append a "Z" for UTC times.

Buffer size

A generous buffer size is good, yet we could reduce it a ways and still maintain a 2x factor.

Standard code

gmtime_r() is not part of the standard C library. The alterative below uses gmtime() for example.

Alternate code

    #define TIME_BUF_LEN (11 /* year */ + 5*2 /* mdHMS */ + 6 /* separators */ )
    char buffer[TIME_BUF_LEN * 2 + 1];

    time_t now = time(NULL); // get current time
    if (now == -1) {
      strcpy(buffer, "Unknown time");
    } else {
      struct tm *tm_now = gmtime(&now);
      if (tm_now == NULL) {
        strcpy(buffer, "Unknown time");
      } else {
        if (strftime(buffer, sizeof buffer, "%Y-%m-%dT%H:%M:%SZ", tm_now) == 0) {
          strcpy(buffer, "Unknown time");
        }
      }
    }

[Edit]

Latent Bug

In sendToSocket(), code tests errno after send(), yet does not clear errno first. Better code would zero it, even if in this application, sendToSocket() is not called with errno != 0 to allow for greater future use. send(), like many functions, does not zero errno on success.

    errno = 0; // Add
    num_bytes = send(sock, &pbuf[total], size-total, 0);
    if (num_bytes < 0) {
        if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) {

Alternatively, sendToSocket() should be documented that it should not get called with errno != 0.

Source Link
chux
  • 36.4k
  • 2
  • 43
  • 96

Just a small review of logging time.

Add error checking

Functions like time(), gmtime_r(), strftime() all could return an error indication. Use it.

Use Z

Using "-%Z" to append a '-' and then the time zone name is curious. I fond nothing in ISO 8601 to support that format.

What is common is to append a "Z" for UTC times.

Buffer size

A generous buffer size is good, yet we could reduce it a ways and still maintain a 2x factor.

Standard code

gmtime_r() is not part of the standard C library. The alterative below uses gmtime() for example.

Alternate code

    #define TIME_BUF_LEN (11 /* year */ + 5*2 /* mdHMS */ + 6 /* separators */ )
    char buffer[TIME_BUF_LEN * 2 + 1];

    time_t now = time(NULL); // get current time
    if (now == -1) {
      strcpy(buffer, "Unknown time");
    } else {
      struct tm *tm_now = gmtime(&now);
      if (tm_now == NULL) {
        strcpy(buffer, "Unknown time");
      } else {
        if (strftime(buffer, sizeof buffer, "%Y-%m-%dT%H:%M:%SZ", tm_now) == 0) {
          strcpy(buffer, "Unknown time");
        }
      }
    }