global variables prefix

This commit is contained in:
Devaev Maxim
2021-04-02 13:31:34 +03:00
parent aa1d78a3cd
commit fde89465ac
6 changed files with 69 additions and 69 deletions

View File

@@ -128,12 +128,12 @@ int main(int argc, char *argv[]) {
case _O_OUTPUT: OPT_SET(output_path, optarg); case _O_OUTPUT: OPT_SET(output_path, optarg);
case _O_OUTPUT_JSON: OPT_SET(output_json, true); case _O_OUTPUT_JSON: OPT_SET(output_json, true);
case _O_LOG_LEVEL: OPT_NUMBER("--log-level", log_level, LOG_LEVEL_INFO, LOG_LEVEL_DEBUG, 0); case _O_LOG_LEVEL: OPT_NUMBER("--log-level", us_log_level, LOG_LEVEL_INFO, LOG_LEVEL_DEBUG, 0);
case _O_PERF: OPT_SET(log_level, LOG_LEVEL_PERF); case _O_PERF: OPT_SET(us_log_level, LOG_LEVEL_PERF);
case _O_VERBOSE: OPT_SET(log_level, LOG_LEVEL_VERBOSE); case _O_VERBOSE: OPT_SET(us_log_level, LOG_LEVEL_VERBOSE);
case _O_DEBUG: OPT_SET(log_level, LOG_LEVEL_DEBUG); case _O_DEBUG: OPT_SET(us_log_level, LOG_LEVEL_DEBUG);
case _O_FORCE_LOG_COLORS: OPT_SET(log_colored, true); case _O_FORCE_LOG_COLORS: OPT_SET(us_log_colored, true);
case _O_NO_LOG_COLORS: OPT_SET(log_colored, false); case _O_NO_LOG_COLORS: OPT_SET(us_log_colored, false);
case _O_HELP: _help(stdout); return 0; case _O_HELP: _help(stdout); return 0;
case _O_VERSION: puts(VERSION); return 0; case _O_VERSION: puts(VERSION); return 0;
@@ -287,7 +287,7 @@ static void _help(FILE *fp) {
SAY(" --log-level <N> ──── Verbosity level of messages from 0 (info) to 3 (debug)."); SAY(" --log-level <N> ──── Verbosity level of messages from 0 (info) to 3 (debug).");
SAY(" Enabling debugging messages can slow down the program."); SAY(" Enabling debugging messages can slow down the program.");
SAY(" Available levels: 0 (info), 1 (performance), 2 (verbose), 3 (debug)."); SAY(" Available levels: 0 (info), 1 (performance), 2 (verbose), 3 (debug).");
SAY(" Default: %d.\n", log_level); SAY(" Default: %d.\n", us_log_level);
SAY(" --perf ───────────── Enable performance messages (same as --log-level=1). Default: disabled.\n"); SAY(" --perf ───────────── Enable performance messages (same as --log-level=1). Default: disabled.\n");
SAY(" --verbose ────────── Enable verbose messages and lower (same as --log-level=2). Default: disabled.\n"); SAY(" --verbose ────────── Enable verbose messages and lower (same as --log-level=2). Default: disabled.\n");
SAY(" --debug ──────────── Enable debug messages and lower (same as --log-level=3). Default: disabled.\n"); SAY(" --debug ──────────── Enable debug messages and lower (same as --log-level=3). Default: disabled.\n");

View File

@@ -23,8 +23,8 @@
#include "logging.h" #include "logging.h"
enum log_level_t log_level; enum log_level_t us_log_level;
bool log_colored; bool us_log_colored;
pthread_mutex_t log_mutex; pthread_mutex_t us_log_mutex;

View File

@@ -45,23 +45,23 @@ enum log_level_t {
}; };
extern enum log_level_t log_level; extern enum log_level_t us_log_level;
extern bool log_colored; extern bool us_log_colored;
extern pthread_mutex_t log_mutex; extern pthread_mutex_t us_log_mutex;
#define LOGGING_INIT { \ #define LOGGING_INIT { \
log_level = LOG_LEVEL_INFO; \ us_log_level = LOG_LEVEL_INFO; \
log_colored = isatty(2); \ us_log_colored = isatty(2); \
A_MUTEX_INIT(&log_mutex); \ A_MUTEX_INIT(&us_log_mutex); \
} }
#define LOGGING_DESTROY A_MUTEX_DESTROY(&log_mutex) #define LOGGING_DESTROY A_MUTEX_DESTROY(&us_log_mutex)
#define LOGGING_LOCK A_MUTEX_LOCK(&log_mutex) #define LOGGING_LOCK A_MUTEX_LOCK(&us_log_mutex)
#define LOGGING_UNLOCK A_MUTEX_UNLOCK(&log_mutex) #define LOGGING_UNLOCK A_MUTEX_UNLOCK(&us_log_mutex)
#define COLOR_GRAY "\x1b[30;1m" #define COLOR_GRAY "\x1b[30;1m"
@@ -84,7 +84,7 @@ extern pthread_mutex_t log_mutex;
} }
#define SEP_DEBUG(_ch) { \ #define SEP_DEBUG(_ch) { \
if (log_level >= LOG_LEVEL_DEBUG) { \ if (us_log_level >= LOG_LEVEL_DEBUG) { \
SEP_INFO(_ch); \ SEP_INFO(_ch); \
} \ } \
} }
@@ -93,7 +93,7 @@ extern pthread_mutex_t log_mutex;
#define LOG_PRINTF_NOLOCK(_label_color, _label, _msg_color, _msg, ...) { \ #define LOG_PRINTF_NOLOCK(_label_color, _label, _msg_color, _msg, ...) { \
char _tname_buf[MAX_THREAD_NAME] = {0}; \ char _tname_buf[MAX_THREAD_NAME] = {0}; \
thread_get_name(_tname_buf); \ thread_get_name(_tname_buf); \
if (log_colored) { \ if (us_log_colored) { \
fprintf(stderr, COLOR_GRAY "-- " _label_color _label COLOR_GRAY \ fprintf(stderr, COLOR_GRAY "-- " _label_color _label COLOR_GRAY \
" [%.03Lf %9s]" " -- " COLOR_RESET _msg_color _msg COLOR_RESET, \ " [%.03Lf %9s]" " -- " COLOR_RESET _msg_color _msg COLOR_RESET, \
get_now_monotonic(), _tname_buf, ##__VA_ARGS__); \ get_now_monotonic(), _tname_buf, ##__VA_ARGS__); \
@@ -130,25 +130,25 @@ extern pthread_mutex_t log_mutex;
} }
#define LOG_PERF(_msg, ...) { \ #define LOG_PERF(_msg, ...) { \
if (log_level >= LOG_LEVEL_PERF) { \ if (us_log_level >= LOG_LEVEL_PERF) { \
LOG_PRINTF(COLOR_CYAN, "PERF ", COLOR_CYAN, _msg, ##__VA_ARGS__); \ LOG_PRINTF(COLOR_CYAN, "PERF ", COLOR_CYAN, _msg, ##__VA_ARGS__); \
} \ } \
} }
#define LOG_PERF_FPS(_msg, ...) { \ #define LOG_PERF_FPS(_msg, ...) { \
if (log_level >= LOG_LEVEL_PERF) { \ if (us_log_level >= LOG_LEVEL_PERF) { \
LOG_PRINTF(COLOR_YELLOW, "PERF ", COLOR_YELLOW, _msg, ##__VA_ARGS__); \ LOG_PRINTF(COLOR_YELLOW, "PERF ", COLOR_YELLOW, _msg, ##__VA_ARGS__); \
} \ } \
} }
#define LOG_VERBOSE(_msg, ...) { \ #define LOG_VERBOSE(_msg, ...) { \
if (log_level >= LOG_LEVEL_VERBOSE) { \ if (us_log_level >= LOG_LEVEL_VERBOSE) { \
LOG_PRINTF(COLOR_BLUE, "VERB ", COLOR_BLUE, _msg, ##__VA_ARGS__); \ LOG_PRINTF(COLOR_BLUE, "VERB ", COLOR_BLUE, _msg, ##__VA_ARGS__); \
} \ } \
} }
#define LOG_VERBOSE_PERROR(_msg, ...) { \ #define LOG_VERBOSE_PERROR(_msg, ...) { \
if (log_level >= LOG_LEVEL_VERBOSE) { \ if (us_log_level >= LOG_LEVEL_VERBOSE) { \
char _perror_buf[1024] = {0}; \ char _perror_buf[1024] = {0}; \
char *_perror_ptr = errno_to_string(errno, _perror_buf, 1023); \ char *_perror_ptr = errno_to_string(errno, _perror_buf, 1023); \
LOG_PRINTF(COLOR_BLUE, "VERB ", COLOR_BLUE, _msg ": %s", ##__VA_ARGS__, _perror_ptr); \ LOG_PRINTF(COLOR_BLUE, "VERB ", COLOR_BLUE, _msg ": %s", ##__VA_ARGS__, _perror_ptr); \
@@ -156,7 +156,7 @@ extern pthread_mutex_t log_mutex;
} }
#define LOG_DEBUG(_msg, ...) { \ #define LOG_DEBUG(_msg, ...) { \
if (log_level >= LOG_LEVEL_DEBUG) { \ if (us_log_level >= LOG_LEVEL_DEBUG) { \
LOG_PRINTF(COLOR_GRAY, "DEBUG", COLOR_GRAY, _msg, ##__VA_ARGS__); \ LOG_PRINTF(COLOR_GRAY, "DEBUG", COLOR_GRAY, _msg, ##__VA_ARGS__); \
} \ } \
} }

View File

@@ -23,7 +23,7 @@
#include "gpio.h" #include "gpio.h"
gpio_s gpio = { gpio_s us_gpio = {
.path = "/dev/gpiochip0", .path = "/dev/gpiochip0",
.consumer_prefix = "ustreamer", .consumer_prefix = "ustreamer",
@@ -51,42 +51,42 @@ static void _gpio_output_destroy(gpio_output_s *output);
void gpio_init(void) { void gpio_init(void) {
assert(gpio.chip == NULL); assert(us_gpio.chip == NULL);
if ( if (
gpio.prog_running.pin >= 0 us_gpio.prog_running.pin >= 0
|| gpio.stream_online.pin >= 0 || us_gpio.stream_online.pin >= 0
|| gpio.has_http_clients.pin >= 0 || us_gpio.has_http_clients.pin >= 0
) { ) {
A_MUTEX_INIT(&gpio.mutex); A_MUTEX_INIT(&us_gpio.mutex);
LOG_INFO("GPIO: Using chip device: %s", gpio.path); LOG_INFO("GPIO: Using chip device: %s", us_gpio.path);
if ((gpio.chip = gpiod_chip_open(gpio.path)) != NULL) { if ((us_gpio.chip = gpiod_chip_open(us_gpio.path)) != NULL) {
_gpio_output_init(&gpio.prog_running); _gpio_output_init(&us_gpio.prog_running);
_gpio_output_init(&gpio.stream_online); _gpio_output_init(&us_gpio.stream_online);
_gpio_output_init(&gpio.has_http_clients); _gpio_output_init(&us_gpio.has_http_clients);
} else { } else {
LOG_PERROR("GPIO: Can't initialize chip device %s", gpio.path); LOG_PERROR("GPIO: Can't initialize chip device %s", us_gpio.path);
} }
} }
} }
void gpio_destroy(void) { void gpio_destroy(void) {
_gpio_output_destroy(&gpio.prog_running); _gpio_output_destroy(&us_gpio.prog_running);
_gpio_output_destroy(&gpio.stream_online); _gpio_output_destroy(&us_gpio.stream_online);
_gpio_output_destroy(&gpio.has_http_clients); _gpio_output_destroy(&us_gpio.has_http_clients);
if (gpio.chip) { if (us_gpio.chip) {
gpiod_chip_close(gpio.chip); gpiod_chip_close(us_gpio.chip);
gpio.chip = NULL; us_gpio.chip = NULL;
A_MUTEX_DESTROY(&gpio.mutex); A_MUTEX_DESTROY(&us_gpio.mutex);
} }
} }
int gpio_inner_set(gpio_output_s *output, bool state) { int gpio_inner_set(gpio_output_s *output, bool state) {
int retval = 0; int retval = 0;
assert(gpio.chip); assert(us_gpio.chip);
assert(output->line); assert(output->line);
assert(output->state != state); // Must be checked in macro for the performance assert(output->state != state); // Must be checked in macro for the performance
A_MUTEX_LOCK(&gpio.mutex); A_MUTEX_LOCK(&us_gpio.mutex);
if (gpiod_line_set_value(output->line, (int)state) < 0) { \ if (gpiod_line_set_value(output->line, (int)state) < 0) { \
LOG_PERROR("GPIO: Can't write value %d to line %s (will be disabled)", state, output->consumer); \ LOG_PERROR("GPIO: Can't write value %d to line %s (will be disabled)", state, output->consumer); \
@@ -94,18 +94,18 @@ int gpio_inner_set(gpio_output_s *output, bool state) {
retval = -1; retval = -1;
} }
A_MUTEX_UNLOCK(&gpio.mutex); A_MUTEX_UNLOCK(&us_gpio.mutex);
return retval; return retval;
} }
static void _gpio_output_init(gpio_output_s *output) { static void _gpio_output_init(gpio_output_s *output) {
assert(gpio.chip); assert(us_gpio.chip);
assert(output->line == NULL); assert(output->line == NULL);
A_ASPRINTF(output->consumer, "%s::%s", gpio.consumer_prefix, output->role); A_ASPRINTF(output->consumer, "%s::%s", us_gpio.consumer_prefix, output->role);
if (output->pin >= 0) { if (output->pin >= 0) {
if ((output->line = gpiod_chip_get_line(gpio.chip, output->pin)) != NULL) { if ((output->line = gpiod_chip_get_line(us_gpio.chip, output->pin)) != NULL) {
if (gpiod_line_request_output(output->line, output->consumer, 0) < 0) { if (gpiod_line_request_output(output->line, output->consumer, 0) < 0) {
LOG_PERROR("GPIO: Can't request pin=%d as %s", output->pin, output->consumer); LOG_PERROR("GPIO: Can't request pin=%d as %s", output->pin, output->consumer);
_gpio_output_destroy(output); _gpio_output_destroy(output);

View File

@@ -56,7 +56,7 @@ typedef struct {
} gpio_s; } gpio_s;
extern gpio_s gpio; extern gpio_s us_gpio;
void gpio_init(void); void gpio_init(void);
@@ -73,15 +73,15 @@ int gpio_inner_set(gpio_output_s *output, bool state);
} }
INLINE void gpio_set_prog_running(bool state) { INLINE void gpio_set_prog_running(bool state) {
SET_STATE(gpio.prog_running, state); SET_STATE(us_gpio.prog_running, state);
} }
INLINE void gpio_set_stream_online(bool state) { INLINE void gpio_set_stream_online(bool state) {
SET_STATE(gpio.stream_online, state); SET_STATE(us_gpio.stream_online, state);
} }
INLINE void gpio_set_has_http_clients(bool state) { INLINE void gpio_set_has_http_clients(bool state) {
SET_STATE(gpio.has_http_clients, state); SET_STATE(us_gpio.has_http_clients, state);
} }
#undef SET_STATE #undef SET_STATE

View File

@@ -440,11 +440,11 @@ int options_parse(options_s *options, device_s *dev, encoder_s *enc, stream_s *s
# undef ADD_SINK # undef ADD_SINK
# ifdef WITH_GPIO # ifdef WITH_GPIO
case _O_GPIO_DEVICE: OPT_SET(gpio.path, optarg); case _O_GPIO_DEVICE: OPT_SET(us_gpio.path, optarg);
case _O_GPIO_CONSUMER_PREFIX: OPT_SET(gpio.consumer_prefix, optarg); case _O_GPIO_CONSUMER_PREFIX: OPT_SET(us_gpio.consumer_prefix, optarg);
case _O_GPIO_PROG_RUNNING: OPT_NUMBER("--gpio-prog-running", gpio.prog_running.pin, 0, 256, 0); case _O_GPIO_PROG_RUNNING: OPT_NUMBER("--gpio-prog-running", us_gpio.prog_running.pin, 0, 256, 0);
case _O_GPIO_STREAM_ONLINE: OPT_NUMBER("--gpio-stream-online", gpio.stream_online.pin, 0, 256, 0); case _O_GPIO_STREAM_ONLINE: OPT_NUMBER("--gpio-stream-online", us_gpio.stream_online.pin, 0, 256, 0);
case _O_GPIO_HAS_HTTP_CLIENTS: OPT_NUMBER("--gpio-has-http-clients", gpio.has_http_clients.pin, 0, 256, 0); case _O_GPIO_HAS_HTTP_CLIENTS: OPT_NUMBER("--gpio-has-http-clients", us_gpio.has_http_clients.pin, 0, 256, 0);
# endif # endif
# ifdef HAS_PDEATHSIG # ifdef HAS_PDEATHSIG
@@ -459,12 +459,12 @@ int options_parse(options_s *options, device_s *dev, encoder_s *enc, stream_s *s
# endif # endif
case _O_NOTIFY_PARENT: OPT_SET(server->notify_parent, true); case _O_NOTIFY_PARENT: OPT_SET(server->notify_parent, true);
case _O_LOG_LEVEL: OPT_NUMBER("--log-level", log_level, LOG_LEVEL_INFO, LOG_LEVEL_DEBUG, 0); case _O_LOG_LEVEL: OPT_NUMBER("--log-level", us_log_level, LOG_LEVEL_INFO, LOG_LEVEL_DEBUG, 0);
case _O_PERF: OPT_SET(log_level, LOG_LEVEL_PERF); case _O_PERF: OPT_SET(us_log_level, LOG_LEVEL_PERF);
case _O_VERBOSE: OPT_SET(log_level, LOG_LEVEL_VERBOSE); case _O_VERBOSE: OPT_SET(us_log_level, LOG_LEVEL_VERBOSE);
case _O_DEBUG: OPT_SET(log_level, LOG_LEVEL_DEBUG); case _O_DEBUG: OPT_SET(us_log_level, LOG_LEVEL_DEBUG);
case _O_FORCE_LOG_COLORS: OPT_SET(log_colored, true); case _O_FORCE_LOG_COLORS: OPT_SET(us_log_colored, true);
case _O_NO_LOG_COLORS: OPT_SET(log_colored, false); case _O_NO_LOG_COLORS: OPT_SET(us_log_colored, false);
case _O_HELP: _help(stdout, dev, enc, stream, server); return 1; case _O_HELP: _help(stdout, dev, enc, stream, server); return 1;
case _O_VERSION: puts(VERSION); return 1; case _O_VERSION: puts(VERSION); return 1;
@@ -679,8 +679,8 @@ static void _help(FILE *fp, device_s *dev, encoder_s *enc, stream_s *stream, ser
# ifdef WITH_GPIO # ifdef WITH_GPIO
SAY("GPIO options:"); SAY("GPIO options:");
SAY("═════════════"); SAY("═════════════");
SAY(" --gpio-device </dev/path> ───── Path to GPIO character device. Default: %s.\n", gpio.path); SAY(" --gpio-device </dev/path> ───── Path to GPIO character device. Default: %s.\n", us_gpio.path);
SAY(" --gpio-consumer-prefix <str> ── Consumer prefix for GPIO outputs. Default: %s.\n", gpio.consumer_prefix); SAY(" --gpio-consumer-prefix <str> ── Consumer prefix for GPIO outputs. Default: %s.\n", us_gpio.consumer_prefix);
SAY(" --gpio-prog-running <pin> ───── Set 1 on GPIO pin while uStreamer is running. Default: disabled.\n"); SAY(" --gpio-prog-running <pin> ───── Set 1 on GPIO pin while uStreamer is running. Default: disabled.\n");
SAY(" --gpio-stream-online <pin> ──── Set 1 while streaming. Default: disabled.\n"); SAY(" --gpio-stream-online <pin> ──── Set 1 while streaming. Default: disabled.\n");
SAY(" --gpio-has-http-clients <pin> ─ Set 1 while stream has at least one client. Default: disabled.\n"); SAY(" --gpio-has-http-clients <pin> ─ Set 1 while stream has at least one client. Default: disabled.\n");
@@ -703,7 +703,7 @@ static void _help(FILE *fp, device_s *dev, encoder_s *enc, stream_s *stream, ser
SAY(" --log-level <N> ──── Verbosity level of messages from 0 (info) to 3 (debug)."); SAY(" --log-level <N> ──── Verbosity level of messages from 0 (info) to 3 (debug).");
SAY(" Enabling debugging messages can slow down the program."); SAY(" Enabling debugging messages can slow down the program.");
SAY(" Available levels: 0 (info), 1 (performance), 2 (verbose), 3 (debug)."); SAY(" Available levels: 0 (info), 1 (performance), 2 (verbose), 3 (debug).");
SAY(" Default: %d.\n", log_level); SAY(" Default: %d.\n", us_log_level);
SAY(" --perf ───────────── Enable performance messages (same as --log-level=1). Default: disabled.\n"); SAY(" --perf ───────────── Enable performance messages (same as --log-level=1). Default: disabled.\n");
SAY(" --verbose ────────── Enable verbose messages and lower (same as --log-level=2). Default: disabled.\n"); SAY(" --verbose ────────── Enable verbose messages and lower (same as --log-level=2). Default: disabled.\n");
SAY(" --debug ──────────── Enable debug messages and lower (same as --log-level=3). Default: disabled.\n"); SAY(" --debug ──────────── Enable debug messages and lower (same as --log-level=3). Default: disabled.\n");