Browse Source

feat: log printf

master
dnomd343 2 years ago
parent
commit
63c2b2405b
  1. 25
      include/log.h
  2. 30
      src/local.c
  3. 37
      src/log.c

25
include/log.h

@ -0,0 +1,25 @@
#ifndef _LOG_H
#define _LOG_H
#include <time.h>
#include <stdio.h>
#include <stdarg.h>
enum {
LOG_DEBUG,
LOG_INFO,
LOG_WARN,
LOG_ERROR,
LOG_FATAL
};
#define log_debug(...) log_printf(LOG_DEBUG, __VA_ARGS__)
#define log_info(...) log_printf(LOG_INFO, __VA_ARGS__)
#define log_warn(...) log_printf(LOG_WARN, __VA_ARGS__)
#define log_error(...) log_printf(LOG_ERROR, __VA_ARGS__)
#define log_fatal(...) log_printf(LOG_FATAL, __VA_ARGS__)
extern int log_level;
void log_printf(int level, const char *fmt, ...);
#endif

30
src/local.c

@ -3,6 +3,7 @@
#include <string.h> #include <string.h>
#include "common.h" #include "common.h"
#include "process.h" #include "process.h"
#include "log.h"
#define SHADOWSOCKS_DEFAULT "sslocal" #define SHADOWSOCKS_DEFAULT "sslocal"
@ -38,16 +39,23 @@ void show_help() { // show help message
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
int i; int i;
if (argc <= 1) {
show_help(); log_debug("%s", "shadowsocks-bootstrap log level debug");
} log_info("%s", "shadowsocks-bootstrap log level info");
for (i = 0; i < argc; ++i) { log_warn("%s", "shadowsocks-bootstrap log level warn");
if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) { log_error("%s", "shadowsocks-bootstrap log level error");
show_help(); log_fatal("%s", "shadowsocks-bootstrap log level fatal");
}
} // if (argc <= 1) {
args_decode(argc, argv); // show_help();
params_load(SHADOWSOCKS_DEFAULT); // default file name // }
start_bootstrap(SHADOWSOCKS_DEFAULT, is_udp_proxy); // local or server mode // for (i = 0; i < argc; ++i) {
// if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
// show_help();
// }
// }
// args_decode(argc, argv);
// params_load(SHADOWSOCKS_DEFAULT); // default file name
// start_bootstrap(SHADOWSOCKS_DEFAULT, is_udp_proxy); // local or server mode
return 0; return 0;
} }

37
src/log.c

@ -0,0 +1,37 @@
#include "log.h"
int log_level = LOG_DEBUG; // default log level
static const char *log_string[] = {
"[DEBUG]",
"[INFO]",
"[WARN]",
"[ERROR]",
"[FATAL]",
};
static const char *log_color[] = {
"\x1b[39m", // debug
"\x1b[32m", // info
"\x1b[33m", // warn
"\x1b[31m", // error
"\x1b[95m", // fatal
};
void log_printf(int level, const char *fmt, ...) {
if (level < log_level) { // skip low log level
return;
}
time_t t = time(NULL);
char time_str[20]; // YYYY-mm-dd HH:MM:SS (20 bytes)
time_str[strftime(time_str, 20, "%Y-%m-%d %H:%M:%S", localtime(&t))] = '\0'; // generate time str
va_list ap;
va_start(ap, fmt);
fprintf(stderr, "\x1b[36m[Bootstrap]\x1b[0m \x1b[90m%s\x1b[0m", time_str); // show log prefix
fprintf(stderr, " %s%s\x1b[0m ", log_color[level], log_string[level]); // show log level
vfprintf(stderr, fmt, ap); // output log content
fprintf(stderr, "\n"); // add LF after line
fflush(stderr);
va_end(ap);
}
Loading…
Cancel
Save