Skip to content

Commit

Permalink
log messages with time in human readable way
Browse files Browse the repository at this point in the history
  • Loading branch information
albfan committed Oct 28, 2022
1 parent 380504b commit 0a2ef32
Show file tree
Hide file tree
Showing 15 changed files with 144 additions and 29 deletions.
2 changes: 2 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ add_project_arguments('-DIP_BINARY='+get_option('ip-binary'), language: 'c')
glib2 = dependency('glib-2.0')
udev = dependency('libudev')

m = c_compiler.find_library('m', required: false)

subdir('src')
subdir('res')

Expand Down
7 changes: 6 additions & 1 deletion res/miracle-utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,11 @@ function kill_network_manager {
# ubuntu manager restarts automatically wpa_supplicant
sudo service NetworkManager stop
elif check_archlinux_distro
then
sudo systemctl stop Network.service
else
sudo systemctl stop Network.service
sudo systemctl stop NetworkManager
sudo systemctl stop wpa_supplicant
fi
}

Expand All @@ -183,6 +185,9 @@ function start_network_manager {
then
sudo service NetworkManager start
elif check_archlinux_distro
then
sudo systemctl start Network.service
else
sudo service NetworkManager start
fi
}
34 changes: 34 additions & 0 deletions src/ctl/ctl-cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/ctl/meson.build
Original file line number Diff line number Diff line change
@@ -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
Expand Down
19 changes: 16 additions & 3 deletions src/ctl/sinkctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand All @@ -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 },
Expand Down Expand Up @@ -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;
Expand Down
14 changes: 10 additions & 4 deletions src/dhcp/dhcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,7 @@ static int help(void)
" --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"
" --netdev <dev> Network device to run on\n"
" --ip-binary <path> Path to 'ip' binary [default: "XSTR(IP_BINARY)"]\n"
Expand All @@ -775,6 +776,7 @@ static int parse_argv(int argc, char *argv[])
ARG_VERSION = 0x100,
ARG_LOG_LEVEL,
ARG_LOG_TIME,
ARG_LOG_DATE_TIME,

ARG_NETDEV,
ARG_IP_BINARY,
Expand All @@ -790,10 +792,11 @@ static int parse_argv(int argc, char *argv[])
ARG_TO,
};
static const struct option options[] = {
{ "help", no_argument, NULL, 'h' },
{ "version", no_argument, NULL, ARG_VERSION },
{ "log-level", required_argument, NULL, ARG_LOG_LEVEL },
{ "log-time", no_argument, NULL, ARG_LOG_TIME },
{ "help", no_argument, NULL, 'h' },
{ "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 },

{ "netdev", required_argument, NULL, ARG_NETDEV },
{ "ip-binary", required_argument, NULL, ARG_IP_BINARY },
Expand Down Expand Up @@ -826,6 +829,9 @@ static int parse_argv(int argc, char *argv[])
case ARG_LOG_TIME:
log_init_time();
break;
case ARG_LOG_DATE_TIME:
log_date_time = true;
break;
case ARG_NETDEV:
arg_netdev = optarg;
break;
Expand Down
2 changes: 1 addition & 1 deletion src/dhcp/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ miracle_dhcp_srcs = ['dhcp.c',
executable('miracle-dhcp', miracle_dhcp_srcs,
install: true,
include_directories: include_directories('../..'),
dependencies: [glib2, udev, libmiracle_shared_dep]
dependencies: [glib2, udev, libmiracle_shared_dep, m]
)
2 changes: 1 addition & 1 deletion src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ subdir('ctl')
subdir('uibc')

executable('miracled', 'miracled.c',
dependencies: libmiracle_shared_dep,
dependencies: [libmiracle_shared_dep, m],
include_directories: include_directories('..'),
install: true
)
14 changes: 10 additions & 4 deletions src/miracled.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ static int help(void)
" --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"
, program_invocation_short_name);
/*
* 80-char barrier:
Expand All @@ -81,12 +82,14 @@ static int parse_argv(int argc, char *argv[])
ARG_VERSION = 0x100,
ARG_LOG_LEVEL,
ARG_LOG_TIME,
ARG_LOG_DATE_TIME,
};
static const struct option options[] = {
{ "help", no_argument, NULL, 'h' },
{ "version", no_argument, NULL, ARG_VERSION },
{ "log-level", required_argument, NULL, ARG_LOG_LEVEL },
{ "log-time", no_argument, NULL, ARG_LOG_TIME },
{ "help", no_argument, NULL, 'h' },
{ "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 },
{}
};
int c;
Expand All @@ -104,6 +107,9 @@ static int parse_argv(int argc, char *argv[])
case ARG_LOG_TIME:
log_init_time();
break;
case ARG_LOG_DATE_TIME:
log_date_time = true;
break;
case '?':
return -EINVAL;
}
Expand Down
48 changes: 41 additions & 7 deletions src/shared/shl_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>
#include <math.h>
#include <limits.h>
#include "shl_log.h"

Expand All @@ -37,7 +39,7 @@ static inline void log_unlock()

static struct timeval log__ftime;

static bool log__have_time(void)
bool log__have_time(void)
{
return !(log__ftime.tv_sec == 0 && log__ftime.tv_usec == 0);
}
Expand All @@ -48,7 +50,7 @@ void log_init_time(void)
gettimeofday(&log__ftime, NULL);
}

static void log__time(long long *sec, long long *usec)
void log__time(long long *sec, long long *usec)
{
struct timeval t;

Expand Down Expand Up @@ -84,6 +86,7 @@ const char *LOG_SUBSYSTEM = NULL;
*/

unsigned int log_max_sev = LOG_NOTICE;
bool log_date_time = false;

char *gst_debug = NULL;

Expand Down Expand Up @@ -138,39 +141,70 @@ static void log__submit(const char *file,
const char *prefix = NULL;
FILE *out;
long long sec, usec;
time_t now;
struct tm *timeinfo;
struct timeval tv;
char buffertmp[80];
char buffer[80];
int millisec;

out = stderr;
log__time(&sec, &usec);

if (sev < LOG_SEV_NUM && sev > log_max_sev)
return;

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 (sev < LOG_SEV_NUM)
prefix = log__sev2str[sev];

if (prefix) {
if (subs) {
if (log__have_time())
if (log_date_time)
fprintf(out, "[%s] %s: %s: ", buffer, prefix, subs);
else if (log__have_time())
fprintf(out, "[%.4lld.%.6lld] %s: %s: ",
sec, usec, prefix, subs);
else
fprintf(out, "%s: %s: ", prefix, subs);
} else {
if (log__have_time())
if (log_date_time)
fprintf(out, "[%s] %s: ", buffer, prefix);
else if (log__have_time())
fprintf(out, "[%.4lld.%.6lld] %s: ",
sec, usec, prefix);
else
fprintf(out, "%s: ", prefix);
}
} else {
if (subs) {
if (log__have_time())
if (log_date_time)
fprintf(out, "[%s] %s: ",
buffer, subs);
else if (log__have_time())
fprintf(out, "[%.4lld.%.6lld] %s: ",
sec, usec, subs);
else
fprintf(out, "%s: ", subs);
} else {
if (log__have_time())
if (log_date_time)
fprintf(out, "[%s] ", buffer);
else if (log__have_time())
fprintf(out, "[%.4lld.%.6lld] ", sec, usec);
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/shared/shl_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ enum log_severity {

extern unsigned int log_max_sev;

/*
* Defines if log time should use local time
* Default: false
*/
extern bool log_date_time;

/*
* Defines the debug configuration for gstreamer
*/
Expand All @@ -74,6 +80,10 @@ extern char* gst_debug;

void log_init_time(void);

void log__time(long long *sec, long long *usec);

bool log__have_time(void);

/*
* Log-Functions
* These functions pass a log-message to the log-subsystem. Handy helpers are
Expand Down
1 change: 0 additions & 1 deletion src/uibc/meson.build
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
m = c_compiler.find_library('m', required: false)
executable('miracle-uibcctl', 'miracle-uibcctl.h', 'miracle-uibcctl.c',
install: true,
dependencies: [m, libmiracle_shared_dep]
Expand Down
2 changes: 1 addition & 1 deletion src/wifi/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ miracle_wifid_src = ['wifid.h',
executable('miracle-wifid', miracle_wifid_src,
include_directories: inc,
install: true,
dependencies: [udev, glib2, libsystemd, libmiracle_shared_dep]
dependencies: [udev, glib2, libsystemd, libmiracle_shared_dep, m]
)

0 comments on commit 0a2ef32

Please sign in to comment.