Use more RLog, and add log.origin ##shell

This commit is contained in:
pancake 2022-04-22 00:48:58 +02:00 committed by pancake
parent 99b7a74640
commit 9b7cbc9cf7
5 changed files with 71 additions and 50 deletions

View File

@ -3203,7 +3203,7 @@ static bool cb_log_config_traplevel(void *coreptr, void *nodeptr) {
static bool cb_log_config_ts(void *coreptr, void *nodeptr) {
RConfigNode *node = (RConfigNode *)nodeptr;
r_log_set_ts (node->i_value);
r_log_show_ts (node->i_value);
return true;
}
@ -3221,6 +3221,12 @@ static bool cb_log_config_file(void *coreptr, void *nodeptr) {
return true;
}
static bool cb_log_origin(void *coreptr, void *nodeptr) {
RConfigNode *node = (RConfigNode *)nodeptr;
r_log_show_origin (r_str_is_true (node->value));
return true;
}
static bool cb_log_config_colors(void *coreptr, void *nodeptr) {
RConfigNode *node = (RConfigNode *)nodeptr;
r_log_set_colors (r_str_is_true (node->value));
@ -3270,7 +3276,7 @@ static bool cb_prjvctype(void *user, void *data) {
if (!strcmp (node->value, "rvc")) {
return true;
}
eprintf ("Unknown vc %s\n", node->value);
R_LOG_ERROR ("Unknown version control '%s'.", node->value);
return false;
}
@ -3715,15 +3721,13 @@ R_API int r_core_config_init(RCore *core) {
SETCB ("log.events", "false", &cb_log_events, "remote HTTP server to sync events with");
#endif
SETICB ("log.level", R_LOGLVL_DEFAULT, cb_log_config_level, "Target log level/severity"\
" (0:NONE, 1:INFO, 2:WARN, 3:DEBUG, 4:ERROR, 5:FATAL)"
);
SETICB ("log.level", R_LOGLVL_DEFAULT, cb_log_config_level, "Target log level/severity (0:FATAL 1:ERROR 2:INFO 3:WARN 4:DEBUG)");
SETCB ("log.ts", "false", cb_log_config_ts, "Show timestamp in log messages");
SETICB ("log.traplevel", 0, cb_log_config_traplevel, "Log level for trapping R2 when hit");
SETCB ("log.file", "", cb_log_config_file, "Logging output filename / path");
SETCB ("log.filter", "", cb_log_config_filter, "Filter only messages matching given origin");
SETCB ("log.origin", "false", cb_log_origin, "Show [origin] in log messages");
SETCB ("log.color", "false", cb_log_config_colors, "Should the log output use colors");
SETCB ("log.quiet", "false", cb_log_config_quiet, "Be quiet, dont log anything to console");

View File

@ -193,7 +193,7 @@ static const char *help_msg_wx[] = {
};
static void cmd_write_fail(RCore *core) {
eprintf ("ERROR: Cannot write in here, check map permissions or reopen the file with oo+\n");
R_LOG_ERROR ("Cannot write. Check `omp` or reopen the file with `oo+`");
r_core_return_code (core, R_CMD_RC_FAILURE);
}
@ -216,12 +216,13 @@ R_API int cmd_write_hexpair(RCore* core, const char* pairs) {
cmd_write_fail (core);
r_core_return_code (core, R_CMD_RC_FAILURE);
}
if (r_config_get_i (core->config, "cfg.wseek")) {
// call WSEEK for consistency?
if (r_config_get_b (core->config, "cfg.wseek")) {
r_core_seek_delta (core, len);
}
r_core_block_read (core);
} else {
eprintf ("Error: invalid hexpair string\n");
R_LOG_ERROR ("Error: invalid hexpair string");
r_core_return_code (core, R_CMD_RC_FAILURE);
}
free (buf);
@ -472,7 +473,8 @@ static int cmd_wo(void *data, const char *input) {
return 0;
}
#define WSEEK(x,y) if (wseek)r_core_seek_delta (x,y)
#define WSEEK(x,y) if (r_config_get_b (core->config, "cfg.wseek")) { r_core_seek_delta ((x),(y)); }
static void cmd_write_value_float(RCore *core, const char *input) {
float v = 0.0;
sscanf (input, "%f", &v);
@ -495,7 +497,6 @@ static void cmd_write_value(RCore *core, const char *input) {
int type = 0;
ut64 off = 0LL;
ut8 buf[sizeof(ut64)];
int wseek = r_config_get_i (core->config, "cfg.wseek");
bool be = r_config_get_i (core->config, "cfg.bigendian");
r_core_return_code (core, R_CMD_RC_SUCCESS);
@ -582,7 +583,6 @@ static bool cmd_wff(RCore *core, const char *input) {
ut8 *buf = NULL;
size_t size = 0;
const char *arg = input + ((input[0] == ' ') ? 1 : 0);
int wseek = r_config_get_i (core->config, "cfg.wseek");
char *p, *a = r_str_trim_dup (arg);
p = strchr (a, ' ');
if (p) {
@ -919,7 +919,6 @@ static int w_incdec_handler(void *data, const char *input, int inc) {
static int cmd_w6(void *data, const char *input) {
RCore *core = (RCore *)data;
int wseek = r_config_get_i (core->config, "cfg.wseek");
int fail = 0;
ut8 *buf = NULL;
int len = 0, str_len;
@ -1214,7 +1213,6 @@ static int cmd_wu(void *data, const char *input) {
static int cmd_wr(void *data, const char *input) {
RCore *core = (RCore *)data;
int wseek = r_config_get_i (core->config, "cfg.wseek");
ut64 off = r_num_math (core->num, input);
int len = (int)off;
if (len > 0) {
@ -1237,7 +1235,6 @@ static int cmd_wr(void *data, const char *input) {
static int cmd_wA(void *data, const char *input) {
RCore *core = (RCore *)data;
int wseek = r_config_get_i (core->config, "cfg.wseek");
int len;
switch (input[0]) {
case ' ':
@ -1410,7 +1407,6 @@ static int cmd_wc(void *data, const char *input) {
}
static int cmd_w(RCore *core, const char *input) {
int wseek = r_config_get_i (core->config, "cfg.wseek");
char *str = strdup (input);
/* write string */
int len = r_str_unescape (str);
@ -1426,7 +1422,6 @@ static int cmd_w(RCore *core, const char *input) {
}
static int cmd_wz(RCore *core, const char *input) {
int wseek = r_config_get_i (core->config, "cfg.wseek");
char *str = strdup (input + 1);
int len = r_str_unescape (str) + 1;
@ -1458,7 +1453,6 @@ static int cmd_wt(void *data, const char *input) {
char *size_sep;
if (*str == 's') { // "wts"
if (str[1] == ' ') {
eprintf ("Write to server\n");
st64 sz = r_io_size (core->io);
if (sz > 0) {
ut64 addr = 0;
@ -1687,12 +1681,12 @@ static int cmd_ww(void *data, const char *input) {
} else {
eprintf ("Cannot malloc %d\n", len);
}
free (ostr);
return 0;
}
static int cmd_wx(void *data, const char *input) {
RCore *core = (RCore *)data;
int wseek = r_config_get_i (core->config, "cfg.wseek");
const char *arg;
ut8 *buf;
int size;
@ -1737,7 +1731,7 @@ static int cmd_wx(void *data, const char *input) {
eprintf ("This file doesnt contains hexpairs\n");
}
} else {
eprintf ("Cannot open file '%s'\n", arg);
R_LOG_ERROR ("Cannot open file '%s'", arg);
}
break;
case 's': // "wxs"
@ -1763,7 +1757,6 @@ static int cmd_wx(void *data, const char *input) {
static int cmd_wa(void *data, const char *input) {
RCore *core = (RCore *)data;
int wseek = r_config_get_i (core->config, "cfg.wseek");
switch (input[0]) {
case 'o': // "wao"
if (input[1] == ' ') {
@ -1929,7 +1922,6 @@ static int cmd_wb(void *data, const char *input) {
size_t len = strlen (input);
const size_t buf_size = len + 2;
ut8 *buf = malloc (buf_size);
int wseek = r_config_get_i (core->config, "cfg.wseek");
if (buf) {
len = r_hex_str2bin (input, buf);
if (len > 0) {
@ -1954,7 +1946,6 @@ static int cmd_wm(void *data, const char *input) {
RCore *core = (RCore *)data;
char *str = strdup (input);
int size = r_hex_str2bin (input, (ut8 *)str);
int wseek = r_config_get_i (core->config, "cfg.wseek");
switch (input[0]) {
case '\0':
eprintf ("TODO: Display current write mask");
@ -2011,7 +2002,6 @@ static int cmd_wd(void *data, const char *input) {
static int cmd_ws(void *data, const char *input) {
RCore *core = (RCore *)data;
int wseek = r_config_get_i (core->config, "cfg.wseek");
char *str = strdup (input);
if (str && *str) {
char *arg = str;

View File

@ -13,12 +13,12 @@ extern "C" {
#endif
typedef enum r_log_level {
R_LOGLVL_NONE = 0,
R_LOGLVL_FATAL = 1, // This will call r_sys_breakpoint() and trap the process for debugging!
R_LOGLVL_FATAL = 0, // This will call r_sys_breakpoint() and trap the process for debugging!
R_LOGLVL_ERROR = 1,
R_LOGLVL_INFO = 2,
R_LOGLVL_WARN = 3,
R_LOGLVL_DEBUG = 4,
R_LOGLVL_ERROR = 5,
R_LOGLVL_LAST = 5,
} RLogLevel;
#define R_LOGLVL_DEFAULT R_LOGLVL_WARN
@ -33,7 +33,8 @@ typedef struct r_log_t {
char *filter;
bool color; // colorize depending on msg level
bool quiet; // be quiet in the console
bool ts;
bool show_origin;
bool show_ts;
RList *cbs;
} RLog;
@ -52,18 +53,19 @@ R_API void r_log_add_callback(RLogCallback cb);
R_API void r_log_del_callback(RLogCallback cb);
#define R_LOG(f,...) if (r_log_match(R_LOGLVL_INFO, R_LOG_ORIGIN)) {r_log_message(R_LOGLVL_INFO, R_LOG_ORIGIN, f, ##__VA_ARGS__);}
#define R_LOG_WARN(f,...) if (r_log_match(R_LOGLVL_WARN, R_LOG_ORIGIN)) {r_log_message(R_LOGLVL_WARN, R_LOG_ORIGIN, f, ##__VA_ARGS__);}
#define R_LOG_INFO(f,...) if (r_log_match(R_LOGLVL_INFO, R_LOG_ORIGIN)) {r_log_message(R_LOGLVL_INFO, R_LOG_ORIGIN, f, ##__VA_ARGS__);}
#define R_LOG_DEBUG(f,...) if (r_log_match(R_LOGLVL_DEBUG, R_LOG_ORIGIN)) {r_log_message(R_LOGLVL_DEBUG, R_LOG_ORIGIN, f, ##__VA_ARGS__);}
#define R_LOG_ERROR(f,...) if (r_log_match(R_LOGLVL_ERROR, R_LOG_ORIGIN)) {r_log_message(R_LOGLVL_ERROR, R_LOG_ORIGIN, f, ##__VA_ARGS__);}
#define R_LOG_FATAL(f,...) if (r_log_match(R_LOGLVL_FATAL, R_LOG_ORIGIN)) {r_log_message(R_LOGLVL_FATAL, R_LOG_ORIGIN, f, ##__VA_ARGS__);}
#define R_LOG_ERROR(f,...) if (r_log_match(R_LOGLVL_ERROR, R_LOG_ORIGIN)) {r_log_message(R_LOGLVL_ERROR, R_LOG_ORIGIN, f, ##__VA_ARGS__);}
#define R_LOG_INFO(f,...) if (r_log_match(R_LOGLVL_INFO, R_LOG_ORIGIN)) {r_log_message(R_LOGLVL_INFO, R_LOG_ORIGIN, f, ##__VA_ARGS__);}
#define R_LOG_WARN(f,...) if (r_log_match(R_LOGLVL_WARN, R_LOG_ORIGIN)) {r_log_message(R_LOGLVL_WARN, R_LOG_ORIGIN, f, ##__VA_ARGS__);}
#define R_LOG_DEBUG(f,...) if (r_log_match(R_LOGLVL_DEBUG, R_LOG_ORIGIN)) {r_log_message(R_LOGLVL_DEBUG, R_LOG_ORIGIN, f, ##__VA_ARGS__);}
R_API void r_log_set_file(const char *expr);
R_API void r_log_set_filter(const char *expr);
R_API void r_log_set_colors(bool show_colors);
R_API void r_log_show_origin(bool show_origin);
R_API void r_log_set_quiet(bool be_quiet);
R_API void r_log_set_level(RLogLevel level);
R_API void r_log_set_ts(bool ts);
R_API void r_log_show_ts(bool ts);
R_API void r_log_set_traplevel(RLogLevel level);
R_API void r_log_set_callback(RLogCallback cbfunc);

View File

@ -6,19 +6,18 @@
#include <stdarg.h>
static const char *level_tags[] = { // Log level to tag string lookup array
[R_LOGLVL_NONE] = "NONE",
[R_LOGLVL_INFO] = "INFO",
[R_LOGLVL_WARN] = "WARNING",
[R_LOGLVL_DEBUG] = "DEBUG",
[R_LOGLVL_FATAL] = "FATAL",
[R_LOGLVL_ERROR] = "ERROR",
[R_LOGLVL_FATAL] = "FATAL"
[R_LOGLVL_INFO] = "INFO",
[R_LOGLVL_WARN] = "WARN",
[R_LOGLVL_DEBUG] = "DEBG",
};
static const char *level_name(int i) {
if (i >= 0 && i < 6) {
if (i >= 0 && i < R_LOGLVL_LAST) {
return level_tags[i];
}
return "UNKNOWN";
return "UNKN";
}
static R_TH_LOCAL RLog *rlog = NULL;
@ -39,9 +38,9 @@ R_API void r_log_fini(void) {
}
}
R_API void r_log_set_ts(bool ts) {
R_API void r_log_show_ts(bool ts) {
r_log_init ();
rlog->ts = ts;
rlog->show_ts = ts;
}
R_API void r_log_set_level(RLogLevel level) {
@ -68,6 +67,11 @@ R_API void r_log_set_file(const char *filename) {
rlog->file = strdup (filename);
}
R_API void r_log_show_origin(bool show_origin) {
r_log_init ();
rlog->show_origin = show_origin;
}
R_API void r_log_set_colors(bool color) {
r_log_init ();
rlog->color = color;
@ -94,7 +98,6 @@ R_API bool r_log_match(int level, const char *origin) { // , const char *sub_ori
}
}
}
return level < rlog->level;
}
@ -112,16 +115,38 @@ R_API void r_log_vmessage(RLogLevel level, const char *origin, const char *fmt,
}
RStrBuf *sb = r_strbuf_new ("");
if (rlog->color) {
if (level > 3) {
r_strbuf_appendf (sb, Color_RED "[%s] " Color_YELLOW "[%s] " Color_RESET, level_name (level), origin);
const char *k = Color_YELLOW;
switch (level) {
case R_LOGLVL_FATAL:
case R_LOGLVL_ERROR:
k = Color_RED;
break;
case R_LOGLVL_INFO:
k = Color_YELLOW;
break;
case R_LOGLVL_WARN:
k = Color_MAGENTA;
break;
case R_LOGLVL_DEBUG:
k = Color_GREEN;
break;
default:
break;
}
r_strbuf_appendf (sb, "%s[%s] ", k, level_name (level));
if (rlog->show_origin) {
r_strbuf_appendf (sb, Color_YELLOW "[%s] " Color_RESET, origin);
} else {
r_strbuf_appendf (sb, Color_GREEN "[%s] " Color_YELLOW "[%s] " Color_RESET, level_name (level), origin);
r_strbuf_appendf (sb, Color_RESET);
}
} else {
r_strbuf_appendf (sb, "[%s] [%s] ", level_name (level), origin);
r_strbuf_appendf (sb, "[%s] ", level_name (level));
if (rlog->show_origin) {
r_strbuf_appendf (sb, "[%s] ", origin);
}
}
char ts[32] = {0};
if (rlog->ts) {
if (rlog->show_ts) {
ut64 now = r_time_now ();
if (rlog->color) {
r_strbuf_appendf (sb, ts, sizeof (ts), Color_CYAN "[ts:%" PFMT64u "] " Color_RESET, now);

View File

@ -921,13 +921,13 @@ R_API void r_sys_perror_str(const char *fun) {
0, NULL )) {
char *err = r_sys_conv_win_to_utf8 (lpMsgBuf);
if (err) {
eprintf ("%s: (%#lx) %s%s", fun, dw, err,
R_LOG_WARN ("%s: (%#lx) %s%s", fun, dw, err,
r_str_endswith (err, "\n") ? "" : "\n");
free (err);
}
LocalFree (lpMsgBuf);
} else {
eprintf ("%s\n", fun);
R_LOG_INFO ("%s", fun);
}
#endif
}