This is merely a historical archive of years 2008-2021, before the migration to mailman3.
A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.
Neels Hofmeyr gerrit-no-reply at lists.osmocom.orgReview at https://gerrit.osmocom.org/5857 logging: use enum to set timestamp format In preparation of adding a new timestamp format, refactor to use an enum to pick the timestamp style. Introduce log_set_print_timestamp2() to be able to pass an enum value directly (for future additions). Make the old API of log_set_print_timestamp() and log_set_print_extended_timestamp() write an enum value instead. The struct log_target members print_timestamp and print_ext_timestamp now become unused, only the new enum member print_timestamp2 has an effect. The old API functions still work. This slightly changes the API behavior, in that enabling the extended timestamp now disables the epoch timestamp, even after switching the extended timestamp off again. Before, switching off the extended timestamp would bring back the epoch timestamp. The API arguably does behave as a reader would expect, in that it switches the timestamp format absolutely, without "invisible state" in the background. Note this in TODO-RELEASE. Change-Id: I70ecee543c266df54191d3e86401466cd1a5c0a6 --- M TODO-RELEASE M include/osmocom/core/logging.h M src/logging.c M src/vty/logging_vty.c 4 files changed, 91 insertions(+), 34 deletions(-) git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/57/5857/1 diff --git a/TODO-RELEASE b/TODO-RELEASE index 782ba19..8353be7 100644 --- a/TODO-RELEASE +++ b/TODO-RELEASE @@ -10,3 +10,11 @@ core msgb_queue_free() add inline func to msgb.h coding gsm0503_rach_ext-encode() add func to gsm0503_coding.h codec ecu.c / ecu.h implement ECU for FR (Error Concealment Unit) +core log_target.print_timestamp, These two flags are now replaced by + log_target.print_ext_timestamp print_timestamp2 enum -- callers should use + log_set_print*() functions anyway. +core log_set_print_timestamp() Calling any other log_set_print_timestamp*() + function now is like log_set_print_timestamp(0), + so the epoch timestamp does not come back after + log_set_print_extended_timestamp(0); callers + should use log_set_print_timestamp2() now. diff --git a/include/osmocom/core/logging.h b/include/osmocom/core/logging.h index 68dac98..2014a1a 100644 --- a/include/osmocom/core/logging.h +++ b/include/osmocom/core/logging.h @@ -220,6 +220,13 @@ LOG_TGT_TYPE_GSMTAP, /*!< GSMTAP network logging */ }; +/*! Format of the timestamp printed */ +enum log_timestamp_format { + LOG_TIMESTAMP_NONE, + LOG_TIMESTAMP_EPOCH, + LOG_TIMESTAMP_YMDHMSM, +}; + /*! structure representing a logging target */ struct log_target { struct llist_head entry; /*!< linked list */ @@ -236,13 +243,15 @@ uint8_t loglevel; /*! should color be used when printing log messages? */ unsigned int use_color:1; - /*! should log messages be prefixed with a timestamp? */ + /*! should log messages be prefixed with a timestamp? + * DEPRECATED: use print_timestamp2 instead. */ unsigned int print_timestamp:1; /*! should log messages be prefixed with a filename? */ unsigned int print_filename:1; /*! should log messages be prefixed with a category name? */ unsigned int print_category:1; - /*! should log messages be prefixed with an extended timestamp? */ + /*! should log messages be prefixed with an extended timestamp? + * DEPRECATED: use print_timestamp2 instead. */ unsigned int print_ext_timestamp:1; /*! the type of this log taget */ @@ -305,6 +314,8 @@ bool print_category_hex; /* With filename output, print just the basename? */ bool print_basename; + /* Timestamp format */ + enum log_timestamp_format print_timestamp2; }; /* use the above macros */ @@ -325,6 +336,7 @@ void log_set_use_color(struct log_target *target, int); void log_set_print_extended_timestamp(struct log_target *target, int); void log_set_print_timestamp(struct log_target *target, int); +void log_set_print_timestamp2(struct log_target *target, enum log_timestamp_format format); void log_set_print_filename(struct log_target *target, int); void log_set_print_basename(struct log_target *target, int); void log_set_print_category(struct log_target *target, int); diff --git a/src/logging.c b/src/logging.c index 0ec12ab..128f590 100644 --- a/src/logging.c +++ b/src/logging.c @@ -350,28 +350,38 @@ } } if (!cont) { - if (target->print_ext_timestamp) { - struct tm tm; - struct timeval tv; - osmo_gettimeofday(&tv, NULL); - localtime_r(&tv.tv_sec, &tm); - ret = snprintf(buf + offset, rem, "%04d%02d%02d%02d%02d%02d%03d ", - tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, - tm.tm_hour, tm.tm_min, tm.tm_sec, - (int)(tv.tv_usec / 1000)); - if (ret < 0) - goto err; - OSMO_SNPRINTF_RET(ret, rem, offset, len); - } else if (target->print_timestamp) { - char *timestr; - time_t tm; - tm = time(NULL); - timestr = ctime(&tm); - timestr[strlen(timestr)-1] = '\0'; - ret = snprintf(buf + offset, rem, "%s ", timestr); - if (ret < 0) - goto err; - OSMO_SNPRINTF_RET(ret, rem, offset, len); + switch (target->print_timestamp2) { + case LOG_TIMESTAMP_NONE: + break; + case LOG_TIMESTAMP_EPOCH: + { + time_t tm; + char *timestr; + tm = time(NULL); + timestr = ctime(&tm); + timestr[strlen(timestr)-1] = '\0'; + ret = snprintf(buf + offset, rem, "%s ", timestr); + if (ret < 0) + goto err; + OSMO_SNPRINTF_RET(ret, rem, offset, len); + } + break; + case LOG_TIMESTAMP_YMDHMSM: + { + struct tm tm; + struct timeval tv; + const char *ts_fmt = "%04d%02d%02d%02d%02d%02d%03d "; + osmo_gettimeofday(&tv, NULL); + localtime_r(&tv.tv_sec, &tm); + ret = snprintf(buf + offset, rem, ts_fmt, + tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, + tm.tm_hour, tm.tm_min, tm.tm_sec, + (int)(tv.tv_usec / 1000)); + if (ret < 0) + goto err; + OSMO_SNPRINTF_RET(ret, rem, offset, len); + } + break; } if (target->print_category) { ret = snprintf(buf + offset, rem, "%s%s%s%s ", @@ -617,16 +627,18 @@ target->use_color = use_color; } -/*! Enable or disable printing of timestamps while logging +/*! Use log_set_print_timestamp2(LOG_TIMESTAMP_EPOCH) instead. + * Enable or disable printing of timestamps while logging. * \param[in] target Log target to be affected * \param[in] print_timestamp Enable (1) or disable (0) timestamps */ void log_set_print_timestamp(struct log_target *target, int print_timestamp) { - target->print_timestamp = print_timestamp; + target->print_timestamp2 = LOG_TIMESTAMP_EPOCH; } -/*! Enable or disable printing of extended timestamps while logging +/*! Use log_set_print_timestamp2(LOG_TIMESTAMP_YMDHMSM) instead. + * Enable or disable printing of extended timestamps while logging. * \param[in] target Log target to be affected * \param[in] print_timestamp Enable (1) or disable (0) timestamps * @@ -636,7 +648,20 @@ */ void log_set_print_extended_timestamp(struct log_target *target, int print_timestamp) { - target->print_ext_timestamp = print_timestamp; + target->print_timestamp2 = LOG_TIMESTAMP_YMDHMSM; +} + +/*! Enable or disable printing of timestamps while logging. + * \param[in] target Log target to be affected + * \param[in] fmt LOG_TIMESTAMP_* value to indicate the format. + * + * LOG_TIMESTAMP_NONE switches off the timestamp output. + * LOG_TIMESTAMP_EPOCH prints the seconds since the epoch (1970-1-1). + * LOG_TIMESTAMP_YMDHMSM prints YYYYMMDDhhmmssnnn. + */ +void log_set_print_timestamp2(struct log_target *target, enum log_timestamp_format fmt) +{ + target->print_timestamp2 = fmt; } /*! Enable or disable printing of the filename while logging diff --git a/src/vty/logging_vty.c b/src/vty/logging_vty.c index 48f6871..819966e 100644 --- a/src/vty/logging_vty.c +++ b/src/vty/logging_vty.c @@ -165,7 +165,7 @@ if (!tgt) return CMD_WARNING; - log_set_print_timestamp(tgt, atoi(argv[0])); + log_set_print_timestamp2(tgt, atoi(argv[0]) ? LOG_TIMESTAMP_EPOCH : LOG_TIMESTAMP_NONE); return CMD_SUCCESS; } @@ -178,11 +178,16 @@ "Prefix each log message with current timestamp with YYYYMMDDhhmmssnnn\n") { struct log_target *tgt = osmo_log_vty2tgt(vty); + enum log_timestamp_format fmt; if (!tgt) return CMD_WARNING; - log_set_print_extended_timestamp(tgt, atoi(argv[0])); + if (atoi(argv[0])) + fmt = LOG_TIMESTAMP_YMDHMSM; + else + fmt = LOG_TIMESTAMP_NONE; + log_set_print_timestamp2(tgt, fmt); return CMD_SUCCESS; } @@ -789,11 +794,18 @@ VTY_NEWLINE); vty_out(vty, " logging print category %d%s", tgt->print_category ? 1 : 0, VTY_NEWLINE); - if (tgt->print_ext_timestamp) + + switch (tgt->print_timestamp2) { + case LOG_TIMESTAMP_NONE: + break; + case LOG_TIMESTAMP_EPOCH: + vty_out(vty, " logging timestamp 1%s", VTY_NEWLINE); + break; + case LOG_TIMESTAMP_YMDHMSM: vty_out(vty, " logging print extended-timestamp 1%s", VTY_NEWLINE); - else - vty_out(vty, " logging timestamp %u%s", - tgt->print_timestamp ? 1 : 0, VTY_NEWLINE); + break; + } + if (tgt->print_level) vty_out(vty, " logging print level 1%s", VTY_NEWLINE); if (tgt->print_filename) -- To view, visit https://gerrit.osmocom.org/5857 To unsubscribe, visit https://gerrit.osmocom.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I70ecee543c266df54191d3e86401466cd1a5c0a6 Gerrit-PatchSet: 1 Gerrit-Project: libosmocore Gerrit-Branch: master Gerrit-Owner: Neels Hofmeyr <nhofmeyr at sysmocom.de>