Skip to content

Commit

Permalink
Merge pull request #40 from mslacken/add_quiet_option
Browse files Browse the repository at this point in the history
Add quiet and verbose option
  • Loading branch information
mvarlese committed Mar 29, 2021
2 parents 170bc1e + 1482bc1 commit 814ee5e
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 26 deletions.
2 changes: 1 addition & 1 deletion headers/plugins.h
Expand Up @@ -13,7 +13,7 @@ typedef struct plugin_s {
char name[PLUGIN_NAME_LEN];
double version;
void (*init)(char *, app_settings_t *, tuning_params_t *,
weights_reference_t *, all_values_t *, double);
weights_reference_t *, all_values_t *, double, unsigned int);
void *(*inference)(void *thread_args);
void (*training)(char *inputFileName);
void (*livetraining)(char *inputFileName);
Expand Down
30 changes: 9 additions & 21 deletions headers/utils.h
Expand Up @@ -55,28 +55,16 @@ unsigned short digits(unsigned long int num);

char *onOrOff(unsigned int input);

static inline void write_log(const char *format, ...) {
#ifdef PRINT_MSGS
va_list args;
va_start(args, format);

vprintf(format, args);
va_end(args);
#else
(void)format;
#endif
}
void set_verbosity(unsigned int verbosity);

static inline void write_adv_log(const char *format, ...) {
#ifdef PRINT_ADV_MSGS
va_list args;
va_start(args, format);
unsigned int get_verbosity(void);

vprintf(format, args);
va_end(args);
#else
(void)format;
#endif
}
void write_log(const char *format, ...);
/// Write to stdout if quiet is not set
void write_adv_log(const char *format, ...);
/// Write to stdout when verbose flag is set
void write_verb_log(const char *format, ...);
/// Write to stdout when more verbose flag is set
void write_dbg_log(const char *format, ...);

#endif
3 changes: 2 additions & 1 deletion src/network_plugin.c
Expand Up @@ -313,14 +313,15 @@ void *networkRunInference(void *args __attribute__((unused))) {
void networkInit(char *interfaceName, app_settings_t *network_app_settings,
tuning_params_t *network_settings,
weights_reference_t *weights, all_values_t *all_values,
double bias) {
double bias, unsigned int v_level) {
pthread_mutex_init(&tableWriteLock, NULL);

_network_app_settings = network_app_settings;
_network_settings = network_settings;
_all_values = all_values;
_weights = weights;
_bias = bias;
set_verbosity(v_level);

memcpy(stats_input_params.monitored_interface, interfaceName,
MAX_INTERFACE_NAME_LENGTH);
Expand Down
23 changes: 20 additions & 3 deletions src/phoebe.c
Expand Up @@ -105,6 +105,8 @@ void printHelp(char *argv0) {
printf("\t-i, --interface\t\tinterface to monitor\n");
printf("\t-m, --mode\t\ttraining | live-training | inference\n");
printf("\t-s, --settings\t\tJSON file for app-settings\n");
printf("\t-v, --verbose\t\tBe verbose, repeat to be more verbose\n");
printf("\t-q, --quite\t\tBe quite, just print startup message\n");
printf("\t-?\t\t\tprints this help and exit\n");
printf("\tDeprecated switches:\n");
printf("\t-f, --csvfile\t\tinput-file path\n");
Expand All @@ -130,11 +132,14 @@ int handleCommandLineArguments(int argc, char **argv) {
{"interface", required_argument, 0, 'i'},
{"mode", required_argument, 0, 'm'},
{"settings", optional_argument, 0, 's'},
{"quiet", no_argument, 0, 'q'},
{"verbose", optional_argument, 0, 'v'},
{0, 0, 0, 0}};
/* getopt_long stores the option index here. */
int option_index = 0;

c = getopt_long(argc, argv, "f:i:m:s:", _longOptions, &option_index);
c = getopt_long(argc, argv, "f:i:m:s:qv::", _longOptions,
&option_index);

/* Detect the end of the options. */
if (c == -1)
Expand Down Expand Up @@ -180,6 +185,17 @@ int handleCommandLineArguments(int argc, char **argv) {
memcpy(settingsFileName, optarg, strlen(optarg));
} break;

case 'q': {
set_verbosity(0);
} break;

case 'v': {
set_verbosity(1);
if (optarg != NULL && strncmp(optarg, "v", strlen("v"))) {
set_verbosity(2);
}
} break;

case '?':
printHelp(argv[0]);
return RET_FAIL;
Expand Down Expand Up @@ -254,7 +270,7 @@ int registerAllPlugins() {

plugins[registered_plugin_count]->init(
interfaceName, &app_settings, &system_settings,
&weights, &reference_values, bias);
&weights, &reference_values, bias, get_verbosity());

registered_plugin_count++;
}
Expand All @@ -275,9 +291,10 @@ int main(int argc, char **argv) {
signal(SIGINT, handleSigint);
signal(SIGHUP, handleSighup);

// set default verbosity setting before cmdline parsing, so
// that it can be used before
if (handleCommandLineArguments(argc, argv) == RET_FAIL)
exit(RET_FAIL);

#ifdef M_THREADS
retrieveNumberOfCores(&n_threads);
#else
Expand Down
56 changes: 56 additions & 0 deletions src/utils.c
Expand Up @@ -23,6 +23,13 @@
#include "stats.h"
#include "utils.h"

/*
* has to reside here, when it would be in the header
* every object file would have its own copy of
* verbosity_level
* */
static unsigned int verbosity_level = 1;

int _positionToInserElementAt(unsigned long transferRate,
all_values_t *destParams, unsigned int *pivot) {
for (*pivot = 0; *pivot < destParams->validValues; (*pivot)++) {
Expand Down Expand Up @@ -521,3 +528,52 @@ extern inline void readSystemSettings(tuning_params_t *systemSettings) {

write_log("DONE.\n");
}

void set_verbosity(unsigned int verbosity) { verbosity_level = verbosity; }

unsigned int get_verbosity(void) { return verbosity_level; }

void write_log(const char *format, ...) {

va_list args;
va_start(args, format);

vprintf(format, args);
va_end(args);
}
/// Write to stdout if quiet is not set
void write_adv_log(const char *format, ...) {
if (verbosity_level >= 1) {
va_list args;
va_start(args, format);

vprintf(format, args);
va_end(args);
} else {
(void)format;
}
}
/// Write to stdout when verbose flag is set
void write_verb_log(const char *format, ...) {
if (verbosity_level >= 2) {
va_list args;
va_start(args, format);

vprintf(format, args);
va_end(args);
} else {
(void)format;
}
}
/// Write to stdout when more verbose flag is set
void write_dbg_log(const char *format, ...) {
if (verbosity_level >= 3) {
va_list args;
va_start(args, format);

vprintf(format, args);
va_end(args);
} else {
(void)format;
}
}

0 comments on commit 814ee5e

Please sign in to comment.