1
0
Fork 0
mirror of https://github.com/albfan/miraclecast.git synced 2025-03-09 23:38:56 +00:00

log messages with time in human readable way

This commit is contained in:
Alberto Fanjul 2022-10-28 08:36:17 +02:00
parent 380504b0f8
commit 72f61549d9
15 changed files with 144 additions and 29 deletions

View file

@ -32,6 +32,9 @@
#include "ctl.h"
#include "shl_macro.h"
#include "shl_util.h"
#include "shl_log.h"
#include <math.h>
#include <time.h>
/* *sigh* readline doesn't include all their deps, so put them last */
#include <readline/history.h>
@ -73,6 +76,37 @@ void cli_printv(const char *fmt, va_list args)
rl_redisplay();
}
long long sec, usec;
time_t now;
struct tm *timeinfo;
struct timeval tv;
char buffertmp[80];
char buffer[80];
int millisec;
log__time(&sec, &usec);
if (log_date_time) {
gettimeofday(&tv, NULL);
millisec = lrint(tv.tv_usec/1000.0);
if (millisec>=1000) {
millisec -=1000;
tv.tv_sec++;
}
time(&now);
timeinfo = localtime(&now);
strftime(buffertmp, 80, "%x - %X.%03d", timeinfo);
sprintf(buffer, "%s.%03d", buffertmp, millisec);
}
if (log_date_time)
printf("[%s] ", buffer);
else if (log__have_time())
printf("[%.4lld.%.6lld] ", sec, usec);
vprintf(fmt, args);
if (async) {

View file

@ -1,5 +1,5 @@
inc = include_directories('../..')
deps = [libsystemd, libmiracle_shared_dep, glib2]
deps = [libsystemd, libmiracle_shared_dep, glib2, m]
if readline.found()
deps += readline
endif

View file

@ -726,6 +726,9 @@ void cli_fn_help()
" --help-commands Show available commands\n"
" --version Show package version\n"
" --log-level <lvl> Maximum level for log messages\n"
" --log-time Prefix log-messages with timestamp\n"
" --log-date-time Prefix log-messages with date time\n"
"\n"
" --log-journal-level <lvl> Maximum level for journal log messages\n"
" --gst-debug [cat:]lvl[,...] List of categories an level of debug\n"
" --audio <0/1> Enable audio support (default %d)\n"
@ -813,6 +816,8 @@ static int parse_argv(int argc, char *argv[])
enum {
ARG_VERSION = 0x100,
ARG_LOG_LEVEL,
ARG_LOG_TIME,
ARG_LOG_DATE_TIME,
ARG_JOURNAL_LEVEL,
ARG_GST_DEBUG,
ARG_AUDIO,
@ -823,10 +828,12 @@ static int parse_argv(int argc, char *argv[])
ARG_HELP_COMMANDS,
};
static const struct option options[] = {
{ "help", no_argument, NULL, 'h' },
{ "help", no_argument, NULL, 'h' },
{ "help-commands", no_argument, NULL, ARG_HELP_COMMANDS },
{ "version", no_argument, NULL, ARG_VERSION },
{ "log-level", required_argument, NULL, ARG_LOG_LEVEL },
{ "version" , no_argument, NULL, ARG_VERSION },
{ "log-level", required_argument, NULL, ARG_LOG_LEVEL },
{ "log-time", no_argument, NULL, ARG_LOG_TIME },
{ "log-date-time", no_argument, NULL, ARG_LOG_DATE_TIME },
{ "log-journal-level", required_argument, NULL, ARG_JOURNAL_LEVEL },
{ "gst-debug", required_argument, NULL, ARG_GST_DEBUG },
{ "audio", required_argument, NULL, ARG_AUDIO },
@ -861,6 +868,12 @@ static int parse_argv(int argc, char *argv[])
case ARG_LOG_LEVEL:
cli_max_sev = log_parse_arg(optarg);
break;
case ARG_LOG_TIME:
log_init_time();
break;
case ARG_LOG_DATE_TIME:
log_date_time = true;
break;
case ARG_GST_DEBUG:
gst_debug = optarg;
break;