History

avutil/log: support logging of date and time information

softworkz committed 1 year ago

Commit c79cdae1 parent e417f934 files · +43 −4Browse files

Unified · Split

4 files
  1. doc/APIchanges +3 −0
  2. libavutil/log.c +29 −3
  3. libavutil/log.h +10 −0
  4. libavutil/version.h +1 −1
doc/APIchanges+3−0
@@ -2,6 +2,9 @@ The last version increases of all libraries were on 2024-03-07 · 1 unmodified line
22
33 API changes, most recent first:
44
5+2025-02-xx - xxxxxxxxxx - lavu 59.57.100 - log.h
6+ Add flags AV_LOG_PRINT_TIME and AV_LOG_PRINT_DATETIME.
7+
58 2025-02-09 - xxxxxxxxxx - lavc 61.32.100 - codec_id.h
69 Add AV_CODEC_ID_IVTV_VBI.
710
libavutil/log.c+29−3
@@ -42,6 +42,8 @@ · 41 unmodified lines
4242 #include "internal.h"
4343 #include "log.h"
4444 #include "thread.h"
45+#include "time.h"
46+#include "time_internal.h"
4547
4648 static AVMutex mutex = AV_MUTEX_INITIALIZER;
4749
@@ -296,14 +298,32 @@ static const char *item_name(void *obj, const AVClass *cls) · 248 unmodified lines
296298 return (cls->item_name ? cls->item_name : av_default_item_name)(obj);
297299 }
298300
301+static void format_date_now(AVBPrint* bp_time, int include_date)
302+{
303+ struct tm *ptm, tmbuf;
304+ const int64_t time_us = av_gettime();
305+ const int64_t time_ms = time_us / 1000;
306+ const time_t time_s = time_ms / 1000;
307+ const int millisec = time_ms - (time_s * 1000);
308+ ptm = localtime_r(&time_s, &tmbuf);
309+ if (ptm) {
310+ if (include_date)
311+ av_bprint_strftime(bp_time, "%Y-%m-%d ", ptm);
312+
313+ av_bprint_strftime(bp_time, "%H:%M:%S", ptm);
314+ av_bprintf(bp_time, ".%03d ", millisec);
315+ }
316+}
317+
299318 static void format_line(void *avcl, int level, const char *fmt, va_list vl,
300− AVBPrint part[4], int *print_prefix, int type[2])
319+ AVBPrint part[5], int *print_prefix, int type[2])
301320 {
302321 AVClass* avc = avcl ? *(AVClass **) avcl : NULL;
303322 av_bprint_init(part+0, 0, AV_BPRINT_SIZE_AUTOMATIC);
304323 av_bprint_init(part+1, 0, AV_BPRINT_SIZE_AUTOMATIC);
305324 av_bprint_init(part+2, 0, AV_BPRINT_SIZE_AUTOMATIC);
306325 av_bprint_init(part+3, 0, 65536);
326+ av_bprint_init(part+4, 0, AV_BPRINT_SIZE_AUTOMATIC);
307327
308328 if(type) type[0] = type[1] = AV_CLASS_CATEGORY_NA + 16;
309329 if (*print_prefix && avc) {
@@ -321,6 +341,9 @@ static void format_line(void *avcl, int level, const char *fmt, va_list vl, · 11 unmodified lines
321341 if(type) type[1] = get_category(avcl);
322342 }
323343
344+ if (*print_prefix && (level > AV_LOG_QUIET) && (flags & (AV_LOG_PRINT_TIME | AV_LOG_PRINT_DATETIME)))
345+ format_date_now(&part[4], flags & AV_LOG_PRINT_DATETIME);
346+
324347 if (*print_prefix && (level > AV_LOG_QUIET) && (flags & AV_LOG_PRINT_LEVEL))
325348 av_bprintf(part+2, "[%s] ", get_level_str(level));
326349
@@ -341,7 +364,7 @@ void av_log_format_line(void *ptr, int level, const char *fmt, va_list vl, · 14 unmodified lines
341364 int av_log_format_line2(void *ptr, int level, const char *fmt, va_list vl,
342365 char *line, int line_size, int *print_prefix)
343366 {
344− AVBPrint part[4];
367+ AVBPrint part[5];
345368 int ret;
346369
347370 format_line(ptr, level, fmt, vl, part, print_prefix, NULL);
@@ -355,7 +378,7 @@ void av_log_default_callback(void* ptr, int level, const char* fmt, va_list vl) · 7 unmodified lines
355378 static int print_prefix = 1;
356379 static int count;
357380 static char prev[LINE_SZ];
358− AVBPrint part[4];
381+ AVBPrint part[5];
359382 char line[LINE_SZ];
360383 static int is_atty;
361384 int type[2];
@@ -390,6 +413,9 @@ void av_log_default_callback(void* ptr, int level, const char* fmt, va_list vl) · 28 unmodified lines
390413 count = 0;
391414 }
392415 strcpy(prev, line);
416+
417+ sanitize(part[4].str);
418+ colored_fputs(7, 0, part[4].str);
393419 sanitize(part[0].str);
394420 colored_fputs(type[0], 0, part[0].str);
395421 sanitize(part[1].str);
libavutil/log.h+10−0
@@ -406,6 +406,16 @@ int av_log_format_line2(void *ptr, int level, const char *fmt, va_list vl, · 405 unmodified lines
406406 */
407407 #define AV_LOG_PRINT_LEVEL 2
408408
409+/**
410+ * Include system time in log output.
411+ */
412+#define AV_LOG_PRINT_TIME 4
413+
414+/**
415+ * Include system date and time in log output.
416+ */
417+#define AV_LOG_PRINT_DATETIME 8
418+
409419 void av_log_set_flags(int arg);
410420 int av_log_get_flags(void);
411421
libavutil/version.h+1−1
@@ -79,7 +79,7 @@ · 78 unmodified lines
7979 */
8080
8181 #define LIBAVUTIL_VERSION_MAJOR 59
82−#define LIBAVUTIL_VERSION_MINOR 56
82+#define LIBAVUTIL_VERSION_MINOR 57
8383 #define LIBAVUTIL_VERSION_MICRO 100
8484
8585 #define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t