diff --git a/src/hci_dump.c b/src/hci_dump.c index aaf175f2d..623daef3d 100644 --- a/src/hci_dump.c +++ b/src/hci_dump.c @@ -164,8 +164,23 @@ static void printf_packet(uint8_t packet_type, uint8_t in, uint8_t * packet, uin printf_hexdump(packet, len); } -#ifndef HAVE_POSIX_FILE_IO static void printf_timestamp(void){ +#ifdef HAVE_POSIX_FILE_IO + struct tm* ptm; + struct timeval curr_time; + gettimeofday(&curr_time, NULL); + time_t curr_time_secs = curr_time.tv_sec; + /* Obtain the time of day, and convert it to a tm struct. */ + ptm = localtime (&curr_time_secs); + /* assert localtime was successful */ + if (!ptm) return; + /* Format the date and time, down to a single second. */ + strftime (time_string, sizeof (time_string), "[%Y-%m-%d %H:%M:%S", ptm); + /* Compute milliseconds from microseconds. */ + uint16_t milliseconds = curr_time.tv_usec / 1000; + /* Print the formatted time, in seconds, followed by a decimal point and the milliseconds. */ + printf ("%s.%03u] ", time_string, milliseconds); +#else uint32_t time_ms = btstack_run_loop_get_time_ms(); int seconds = time_ms / 1000; int minutes = seconds / 60; @@ -175,8 +190,8 @@ static void printf_timestamp(void){ uint16_t p_seconds = seconds - (minutes * 60); uint16_t p_minutes = minutes - (hours * 60); printf("[%02u:%02u:%02u.%03u] ", hours, p_minutes, p_seconds, p_ms); -} #endif +} void hci_dump_packet(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len) { @@ -196,22 +211,11 @@ void hci_dump_packet(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t // get time struct timeval curr_time; - struct tm* ptm; gettimeofday(&curr_time, NULL); - time_t curr_time_secs = curr_time.tv_sec; switch (dump_format){ case HCI_DUMP_STDOUT: { - /* Obtain the time of day, and convert it to a tm struct. */ - ptm = localtime (&curr_time_secs); - /* assert localtime was successful */ - if (!ptm) break; - /* Format the date and time, down to a single second. */ - strftime (time_string, sizeof (time_string), "[%Y-%m-%d %H:%M:%S", ptm); - /* Compute milliseconds from microseconds. */ - uint16_t milliseconds = curr_time.tv_usec / 1000; - /* Print the formatted time, in seconds, followed by a decimal point and the milliseconds. */ - printf ("%s.%03u] ", time_string, milliseconds); + printf_timestamp(); printf_packet(packet_type, in, packet, len); break; } @@ -280,17 +284,19 @@ static int hci_dump_log_level_active(int log_level){ } void hci_dump_log_va_arg(int log_level, const char * format, va_list argptr){ - if (hci_dump_log_level_active(log_level)) { + if (!hci_dump_log_level_active(log_level)) return; + #ifdef HAVE_POSIX_FILE_IO + if (dump_file >= 0){ int len = vsnprintf(log_message_buffer, sizeof(log_message_buffer), format, argptr); hci_dump_packet(LOG_MESSAGE_PACKET, 0, (uint8_t*) log_message_buffer, len); -#else - printf_timestamp(); - printf("LOG -- "); - vprintf(format, argptr); - printf("\n"); -#endif } +#endif + + printf_timestamp(); + printf("LOG -- "); + vprintf(format, argptr); + printf("\n"); } void hci_dump_log(int log_level, const char * format, ...){