Rizin
unix-like reverse engineering framework and cli tools
rz_log.h File Reference
#include <rz_types.h>
#include <rz_userconf.h>

Go to the source code of this file.

Macros

#define MACRO_LOG_FUNC   __func__
 
#define RZ_DEFAULT_LOGLVL   RZ_LOGLVL_ERROR
 
#define RZ_VLOG(lvl, tag, fmtstr, args)
 
#define RZ_LOG(lvl, tag, fmtstr, ...)
 
#define RZ_LOG_SILLY(fmtstr, ...)
 
#define RZ_LOG_DEBUG(fmtstr, ...)
 
#define RZ_LOG_VERBOSE(fmtstr, ...)
 
#define RZ_LOG_INFO(fmtstr, ...)
 
#define RZ_LOG_WARN(fmtstr, ...)
 
#define RZ_LOG_ERROR(fmtstr, ...)
 
#define RZ_LOG_FATAL(fmtstr, ...)
 

Typedefs

typedef enum rz_log_level RzLogLevel
 
typedef void(* RzLogCallback) (const char *output, const char *funcname, const char *filename, ut32 lineno, RzLogLevel level, const char *tag, const char *fmtstr,...) RZ_PRINTF_CHECK(7
 

Enumerations

enum  rz_log_level {
  RZ_LOGLVL_SILLY = 0 , RZ_LOGLVL_DEBUG = 1 , RZ_LOGLVL_VERBOSE = 2 , RZ_LOGLVL_INFO = 3 ,
  RZ_LOGLVL_WARN = 4 , RZ_LOGLVL_ERROR = 5 , RZ_LOGLVL_FATAL = 6 , RZ_LOGLVL_NONE = 0xFF
}
 

Functions

RZ_API void rz_log_set_level (RzLogLevel level)
 
RZ_API void rz_log_set_file (const char *filename)
 
RZ_API void rz_log_set_srcinfo (bool show_info)
 
RZ_API void rz_log_set_colors (bool show_colors)
 
RZ_API void rz_log_set_traplevel (RzLogLevel level)
 
RZ_API void rz_log_add_callback (RzLogCallback cbfunc)
 Add a logging callback. More...
 
RZ_API void rz_log_del_callback (RzLogCallback cbfunc)
 Remove a logging callback. More...
 
RZ_API void rz_log (const char *funcname, const char *filename, ut32 lineno, RzLogLevel level, const char *tag, const char *fmtstr,...) RZ_PRINTF_CHECK(6
 
RZ_API void RZ_API void rz_vlog (const char *funcname, const char *filename, ut32 lineno, RzLogLevel level, const char *tag, const char *fmtstr, va_list args)
 

Macro Definition Documentation

◆ MACRO_LOG_FUNC

#define MACRO_LOG_FUNC   __func__

Definition at line 13 of file rz_log.h.

◆ RZ_DEFAULT_LOGLVL

#define RZ_DEFAULT_LOGLVL   RZ_LOGLVL_ERROR

Definition at line 30 of file rz_log.h.

◆ RZ_LOG

#define RZ_LOG (   lvl,
  tag,
  fmtstr,
  ... 
)
Value:
rz_log(MACRO_LOG_FUNC, __FILE__, \
__LINE__, lvl, tag, fmtstr, ##__VA_ARGS__);
RZ_API void rz_log(const char *funcname, const char *filename, ut32 lineno, RzLogLevel level, const char *tag, const char *fmtstr,...) RZ_PRINTF_CHECK(6
#define MACRO_LOG_FUNC
Definition: rz_log.h:13

Definition at line 39 of file rz_log.h.

◆ RZ_LOG_DEBUG

#define RZ_LOG_DEBUG (   fmtstr,
  ... 
)

Definition at line 49 of file rz_log.h.

◆ RZ_LOG_ERROR

#define RZ_LOG_ERROR (   fmtstr,
  ... 
)
Value:
rz_log(MACRO_LOG_FUNC, __FILE__, \
__LINE__, RZ_LOGLVL_ERROR, NULL, fmtstr, ##__VA_ARGS__);
#define NULL
Definition: cris-opc.c:27
@ RZ_LOGLVL_ERROR
Definition: rz_log.h:22

Definition at line 58 of file rz_log.h.

◆ RZ_LOG_FATAL

#define RZ_LOG_FATAL (   fmtstr,
  ... 
)
Value:
rz_log(MACRO_LOG_FUNC, __FILE__, \
__LINE__, RZ_LOGLVL_FATAL, NULL, fmtstr, ##__VA_ARGS__);
@ RZ_LOGLVL_FATAL
Definition: rz_log.h:23

Definition at line 60 of file rz_log.h.

◆ RZ_LOG_INFO

#define RZ_LOG_INFO (   fmtstr,
  ... 
)
Value:
rz_log(MACRO_LOG_FUNC, __FILE__, \
__LINE__, RZ_LOGLVL_INFO, NULL, fmtstr, ##__VA_ARGS__);
@ RZ_LOGLVL_INFO
Definition: rz_log.h:20

Definition at line 54 of file rz_log.h.

◆ RZ_LOG_SILLY

#define RZ_LOG_SILLY (   fmtstr,
  ... 
)

Definition at line 48 of file rz_log.h.

◆ RZ_LOG_VERBOSE

#define RZ_LOG_VERBOSE (   fmtstr,
  ... 
)
Value:
rz_log(MACRO_LOG_FUNC, __FILE__, \
__LINE__, RZ_LOGLVL_VERBOSE, NULL, fmtstr, ##__VA_ARGS__);
@ RZ_LOGLVL_VERBOSE
Definition: rz_log.h:19

Definition at line 52 of file rz_log.h.

◆ RZ_LOG_WARN

#define RZ_LOG_WARN (   fmtstr,
  ... 
)
Value:
rz_log(MACRO_LOG_FUNC, __FILE__, \
__LINE__, RZ_LOGLVL_WARN, NULL, fmtstr, ##__VA_ARGS__);
@ RZ_LOGLVL_WARN
Definition: rz_log.h:21

Definition at line 56 of file rz_log.h.

◆ RZ_VLOG

#define RZ_VLOG (   lvl,
  tag,
  fmtstr,
  args 
)
Value:
rz_vlog(MACRO_LOG_FUNC, __FILE__, \
__LINE__, lvl, tag, fmtstr, args);
int args
Definition: mipsasm.c:18
RZ_API void RZ_API void rz_vlog(const char *funcname, const char *filename, ut32 lineno, RzLogLevel level, const char *tag, const char *fmtstr, va_list args)
Definition: log.c:73

Definition at line 36 of file rz_log.h.

Typedef Documentation

◆ RzLogCallback

typedef void(* RzLogCallback) (const char *output, const char *funcname, const char *filename, ut32 lineno, RzLogLevel level, const char *tag, const char *fmtstr,...) RZ_PRINTF_CHECK(7

Definition at line 33 of file rz_log.h.

◆ RzLogLevel

typedef enum rz_log_level RzLogLevel

Enumeration Type Documentation

◆ rz_log_level

Enumerator
RZ_LOGLVL_SILLY 
RZ_LOGLVL_DEBUG 
RZ_LOGLVL_VERBOSE 
RZ_LOGLVL_INFO 
RZ_LOGLVL_WARN 
RZ_LOGLVL_ERROR 
RZ_LOGLVL_FATAL 
RZ_LOGLVL_NONE 

Definition at line 16 of file rz_log.h.

16  {
17  RZ_LOGLVL_SILLY = 0,
18  RZ_LOGLVL_DEBUG = 1,
20  RZ_LOGLVL_INFO = 3,
21  RZ_LOGLVL_WARN = 4,
22  RZ_LOGLVL_ERROR = 5,
23  RZ_LOGLVL_FATAL = 6, // This will call rz_sys_breakpoint() and trap the process for debugging!
24  RZ_LOGLVL_NONE = 0xFF
25 } RzLogLevel;
enum rz_log_level RzLogLevel
@ RZ_LOGLVL_SILLY
Definition: rz_log.h:17
@ RZ_LOGLVL_NONE
Definition: rz_log.h:24
@ RZ_LOGLVL_DEBUG
Definition: rz_log.h:18

Function Documentation

◆ rz_log()

RZ_API void rz_log ( const char *  funcname,
const char *  filename,
ut32  lineno,
RzLogLevel  level,
const char *  tag,
const char *  fmtstr,
  ... 
)

◆ rz_log_add_callback()

RZ_API void rz_log_add_callback ( RzLogCallback  cbfunc)

Add a logging callback.

Parameters
cbfuncRzLogCallback style function to be called

Definition at line 54 of file log.c.

54  {
55  if (!log_cbs) {
56  log_cbs = rz_list_new();
57  }
58  if (!rz_list_contains(log_cbs, cbfunc)) {
59  rz_list_append(log_cbs, cbfunc);
60  }
61 }
RZ_API RZ_BORROW RzListIter * rz_list_contains(RZ_NONNULL const RzList *list, RZ_NONNULL const void *ptr)
Returns the RzListIter of the given pointer, if found.
Definition: list.c:592
RZ_API RZ_OWN RzList * rz_list_new(void)
Returns a new initialized RzList pointer (free method is not initialized)
Definition: list.c:235
RZ_API RZ_BORROW RzListIter * rz_list_append(RZ_NONNULL RzList *list, void *data)
Appends at the end of the list a new element.
Definition: list.c:288
static RzList * log_cbs
Definition: log.c:12

References log_cbs, rz_list_append(), rz_list_contains(), and rz_list_new().

◆ rz_log_del_callback()

RZ_API void rz_log_del_callback ( RzLogCallback  cbfunc)

Remove a logging callback.

Parameters
cbfuncRzLogCallback style function to be called

Definition at line 67 of file log.c.

67  {
68  if (log_cbs) {
70  }
71 }
RZ_API bool rz_list_delete_data(RZ_NONNULL RzList *list, void *ptr)
Deletes an entry in the list by searching for a pointer.
Definition: list.c:148

References log_cbs, and rz_list_delete_data().

◆ rz_log_set_colors()

RZ_API void rz_log_set_colors ( bool  show_colors)

Definition at line 46 of file log.c.

46  {
47  cfg_logcolors = show_info;
48 }
static bool cfg_logcolors
Definition: log.c:16

References cfg_logcolors.

Referenced by cb_log_config_colors().

◆ rz_log_set_file()

RZ_API void rz_log_set_file ( const char *  filename)

Definition at line 37 of file log.c.

37  {
38  int value_len = rz_str_nlen(filename, LOG_CONFIGSTR_SIZE) + 1;
39  strncpy(cfg_logfile, filename, value_len);
40 }
const char * filename
Definition: ioapi.h:137
static char cfg_logfile[LOG_CONFIGSTR_SIZE]
Definition: log.c:17
#define LOG_CONFIGSTR_SIZE
Definition: log.c:5
RZ_API size_t rz_str_nlen(const char *s, size_t n)
Definition: str.c:1949

References cfg_logfile, LOG_CONFIGSTR_SIZE, and rz_str_nlen().

Referenced by cb_log_config_file().

◆ rz_log_set_level()

RZ_API void rz_log_set_level ( RzLogLevel  level)

Definition at line 29 of file log.c.

29  {
30  cfg_loglvl = level;
31 }
static int cfg_loglvl
Definition: log.c:13
static int level
Definition: vmenus.c:2424

References cfg_loglvl, and level.

Referenced by cb_log_config_level().

◆ rz_log_set_srcinfo()

RZ_API void rz_log_set_srcinfo ( bool  show_info)

Definition at line 42 of file log.c.

42  {
43  cfg_logsrcinfo = show_info;
44 }
static bool cfg_logsrcinfo
Definition: log.c:15

References cfg_logsrcinfo.

Referenced by cb_log_config_srcinfo().

◆ rz_log_set_traplevel()

RZ_API void rz_log_set_traplevel ( RzLogLevel  level)

Definition at line 33 of file log.c.

33  {
35 }
static int cfg_logtraplvl
Definition: log.c:14

References cfg_logtraplvl, and level.

Referenced by cb_log_config_traplevel().

◆ rz_vlog()

RZ_API void RZ_API void rz_vlog ( const char *  funcname,
const char *  filename,
ut32  lineno,
RzLogLevel  level,
const char *  tag,
const char *  fmtstr,
va_list  args 
)

Definition at line 73 of file log.c.

74  {
75  va_list args_copy;
76  va_copy(args_copy, args);
77 
78  if (level < cfg_loglvl && level < cfg_logtraplvl) {
79  // Don't print if output level is lower than current level
80  // Don't ignore fatal/trap errors
81  va_end(args_copy);
82  return;
83  }
84 
85  // TODO: Colors
86 
87  // Build output string with src info, and formatted output
88  char output_buf[LOG_OUTPUTBUF_SIZE] = ""; // Big buffer for building the output string
89  if (!tag) {
91  }
92  int offset = snprintf(output_buf, LOG_OUTPUTBUF_SIZE, "%s: ", tag);
93  if (cfg_logsrcinfo) {
94  offset += snprintf(output_buf + offset, LOG_OUTPUTBUF_SIZE - offset, "%s in %s:%i: ", funcname, filename, lineno);
95  }
96  vsnprintf(output_buf + offset, LOG_OUTPUTBUF_SIZE - offset, fmtstr, args);
97 
98  // Actually print out the string with our callbacks
99  if (log_cbs && rz_list_length(log_cbs) > 0) {
100  RzListIter *it;
102 
103  rz_list_foreach (log_cbs, it, cb) {
104  cb(output_buf, funcname, filename, lineno, level, NULL, fmtstr, args_copy);
105  }
106  } else {
107  fprintf(stderr, "%s", output_buf);
108  }
109  va_end(args_copy);
110 
111  // Log to file if enabled
112  if (cfg_logfile[0] != 0x00) {
113  FILE *file = rz_sys_fopen(cfg_logfile, "a+"); // TODO: Optimize (static? Needs to remake on cfg change though)
114  if (!file) {
115  file = rz_sys_fopen(cfg_logfile, "w+");
116  }
117  if (file) {
118  fprintf(file, "%s", output_buf);
119  fclose(file);
120  } else {
121  eprintf("%s failed to write to file: %s\n", MACRO_LOG_FUNC, cfg_logfile);
122  }
123  }
124 
125  if (level >= cfg_logtraplvl && level != RZ_LOGLVL_NONE) {
126  fflush(stdout); // We're about to exit HARD, flush buffers before dying
127  fflush(stderr);
128  // TODO: call rz_cons_flush if librz_cons is being used
129  rz_sys_breakpoint(); // *oof*
130  }
131 }
voidpf uLong offset
Definition: ioapi.h:144
snprintf
Definition: kernel.h:364
vsnprintf
Definition: kernel.h:366
RZ_API ut32 rz_list_length(RZ_NONNULL const RzList *list)
Returns the length of the list.
Definition: list.c:109
static const char * level_tags[]
Definition: log.c:18
#define LOG_OUTPUTBUF_SIZE
Definition: log.c:6
string FILE
Definition: benchmark.py:21
#define eprintf(x, y...)
Definition: rlcc.c:7
void(* RzLogCallback)(const char *output, const char *funcname, const char *filename, ut32 lineno, RzLogLevel level, const char *tag, const char *fmtstr,...) RZ_PRINTF_CHECK(7
Definition: rz_log.h:33
#define rz_sys_breakpoint()
Definition: rz_sys.h:178
RZ_API FILE * rz_sys_fopen(const char *path, const char *mode)
Definition: sys.c:1815
#define RZ_ARRAY_SIZE(x)
Definition: rz_types.h:300
#define RZ_BETWEEN(x, y, z)
Definition: gzappend.c:170
static const char * cb[]
Definition: z80_tab.h:176

References args, cb, cfg_logfile, cfg_loglvl, cfg_logsrcinfo, cfg_logtraplvl, eprintf, benchmark::FILE, level, level_tags, log_cbs, LOG_OUTPUTBUF_SIZE, MACRO_LOG_FUNC, NULL, RZ_ARRAY_SIZE, RZ_BETWEEN, rz_list_length(), RZ_LOGLVL_NONE, rz_sys_breakpoint, rz_sys_fopen(), snprintf, test-lz4-versions::tag, and vsnprintf.

Referenced by rz_log().