Skip to content

Commit

Permalink
Move log level conversion to log API
Browse files Browse the repository at this point in the history
  • Loading branch information
rom1v committed Jun 20, 2021
1 parent 1039f9b commit 5c95d18
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 24 deletions.
1 change: 1 addition & 0 deletions app/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
23 changes: 1 addition & 22 deletions app/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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__
Expand All @@ -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]);
Expand Down
28 changes: 28 additions & 0 deletions app/src/util/log.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "log.h"

#include <assert.h>

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);
}
11 changes: 9 additions & 2 deletions app/src/util/log.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
#ifndef LOG_H
#define LOG_H
#ifndef SC_LOG_H
#define SC_LOG_H

#include "common.h"

#include <SDL2/SDL_log.h>

#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__)
#define LOGW(...) SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, __VA_ARGS__)
#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

0 comments on commit 5c95d18

Please sign in to comment.