From 5c95d18beb55e9dc24bf8b650042fcabeccce63f Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Sun, 20 Jun 2021 12:46:41 +0200 Subject: [PATCH] Move log level conversion to log API --- app/meson.build | 1 + app/src/main.c | 23 +---------------------- app/src/util/log.c | 28 ++++++++++++++++++++++++++++ app/src/util/log.h | 11 +++++++++-- 4 files changed, 39 insertions(+), 24 deletions(-) create mode 100644 app/src/util/log.c diff --git a/app/meson.build b/app/meson.build index 6b6991aa..0663c641 100644 --- a/app/meson.build +++ b/app/meson.build @@ -20,6 +20,7 @@ src = [ 'src/stream.c', 'src/tiny_xpm.c', 'src/video_buffer.c', + 'src/util/log.c', 'src/util/net.c', 'src/util/process.c', 'src/util/str_util.c', diff --git a/app/src/main.c b/app/src/main.c index c3a7ad2e..2afa3c4e 100644 --- a/app/src/main.c +++ b/app/src/main.c @@ -38,26 +38,6 @@ print_version(void) { #endif } -static SDL_LogPriority -convert_log_level_to_sdl(enum sc_log_level level) { - switch (level) { - case SC_LOG_LEVEL_VERBOSE: - return SDL_LOG_PRIORITY_VERBOSE; - case SC_LOG_LEVEL_DEBUG: - return SDL_LOG_PRIORITY_DEBUG; - case SC_LOG_LEVEL_INFO: - return SDL_LOG_PRIORITY_INFO; - case SC_LOG_LEVEL_WARN: - return SDL_LOG_PRIORITY_WARN; - case SC_LOG_LEVEL_ERROR: - return SDL_LOG_PRIORITY_ERROR; - default: - assert(!"unexpected log level"); - return SDL_LOG_PRIORITY_INFO; - } -} - - int main(int argc, char *argv[]) { #ifdef __WINDOWS__ @@ -81,8 +61,7 @@ main(int argc, char *argv[]) { return 1; } - SDL_LogPriority sdl_log = convert_log_level_to_sdl(args.opts.log_level); - SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, sdl_log); + sc_set_log_level(args.opts.log_level); if (args.help) { scrcpy_print_usage(argv[0]); diff --git a/app/src/util/log.c b/app/src/util/log.c new file mode 100644 index 00000000..ac26de1c --- /dev/null +++ b/app/src/util/log.c @@ -0,0 +1,28 @@ +#include "log.h" + +#include + +static SDL_LogPriority +log_level_sc_to_sdl(enum sc_log_level level) { + switch (level) { + case SC_LOG_LEVEL_VERBOSE: + return SDL_LOG_PRIORITY_VERBOSE; + case SC_LOG_LEVEL_DEBUG: + return SDL_LOG_PRIORITY_DEBUG; + case SC_LOG_LEVEL_INFO: + return SDL_LOG_PRIORITY_INFO; + case SC_LOG_LEVEL_WARN: + return SDL_LOG_PRIORITY_WARN; + case SC_LOG_LEVEL_ERROR: + return SDL_LOG_PRIORITY_ERROR; + default: + assert(!"unexpected log level"); + return SDL_LOG_PRIORITY_INFO; + } +} + +void +sc_set_log_level(enum sc_log_level level) { + SDL_LogPriority sdl_log = log_level_sc_to_sdl(level); + SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, sdl_log); +} diff --git a/app/src/util/log.h b/app/src/util/log.h index 5955c7fb..8b6c175e 100644 --- a/app/src/util/log.h +++ b/app/src/util/log.h @@ -1,8 +1,12 @@ -#ifndef LOG_H -#define LOG_H +#ifndef SC_LOG_H +#define SC_LOG_H + +#include "common.h" #include +#include "scrcpy.h" + #define LOGV(...) SDL_LogVerbose(SDL_LOG_CATEGORY_APPLICATION, __VA_ARGS__) #define LOGD(...) SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, __VA_ARGS__) #define LOGI(...) SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, __VA_ARGS__) @@ -10,4 +14,7 @@ #define LOGE(...) SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, __VA_ARGS__) #define LOGC(...) SDL_LogCritical(SDL_LOG_CATEGORY_APPLICATION, __VA_ARGS__) +void +sc_set_log_level(enum sc_log_level level); + #endif