Rizin
unix-like reverse engineering framework and cli tools
log.c File Reference
#include <rz_core.h>
#include <stdarg.h>

Go to the source code of this file.

Macros

#define LOG_CONFIGSTR_SIZE   512
 
#define LOG_OUTPUTBUF_SIZE   512
 

Functions

RZ_API void rz_log_set_level (RzLogLevel level)
 
RZ_API void rz_log_set_traplevel (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_info)
 
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_vlog (const char *funcname, const char *filename, ut32 lineno, RzLogLevel level, const char *tag, const char *fmtstr, va_list args)
 
RZ_API void rz_log (const char *funcname, const char *filename, ut32 lineno, RzLogLevel level, const char *tag, const char *fmtstr,...)
 Internal logging function used by preprocessor macros. More...
 

Variables

static RzListlog_cbs = NULL
 
static int cfg_loglvl = RZ_LOGLVL_WARN
 
static int cfg_logtraplvl = RZ_LOGLVL_FATAL
 
static bool cfg_logsrcinfo = false
 
static bool cfg_logcolors = false
 
static char cfg_logfile [LOG_CONFIGSTR_SIZE] = ""
 
static const char * level_tags []
 

Macro Definition Documentation

◆ LOG_CONFIGSTR_SIZE

#define LOG_CONFIGSTR_SIZE   512

Definition at line 5 of file log.c.

◆ LOG_OUTPUTBUF_SIZE

#define LOG_OUTPUTBUF_SIZE   512

Definition at line 6 of file log.c.

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,
  ... 
)

Internal logging function used by preprocessor macros.

Parameters
funcnameContains the function name of the calling function
filenameContains the filename that funcname is defined in
linenoThe line number that this log call is being made from in filename
lvlLogging level for output
fmtstrA printf like string

This function is used by the RZ_LOG_* preprocessor macros for logging

Definition at line 143 of file log.c.

144  {
145  va_list args;
146 
147  va_start(args, fmtstr);
148  rz_vlog(funcname, filename, lineno, level, tag, fmtstr, args);
149  va_end(args);
150 }
const char * filename
Definition: ioapi.h:137
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
int args
Definition: mipsasm.c:18
static int level
Definition: vmenus.c:2424

References args, level, rz_vlog(), and test-lz4-versions::tag.

◆ 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_info)

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 }
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

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_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 }
#define NULL
Definition: cris-opc.c:27
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
#define MACRO_LOG_FUNC
Definition: rz_log.h:13
@ RZ_LOGLVL_NONE
Definition: rz_log.h:24
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().

Variable Documentation

◆ cfg_logcolors

bool cfg_logcolors = false
static

Definition at line 16 of file log.c.

Referenced by rz_log_set_colors().

◆ cfg_logfile

char cfg_logfile[LOG_CONFIGSTR_SIZE] = ""
static

Definition at line 17 of file log.c.

Referenced by rz_log_set_file(), and rz_vlog().

◆ cfg_loglvl

int cfg_loglvl = RZ_LOGLVL_WARN
static

Definition at line 13 of file log.c.

Referenced by rz_log_set_level(), and rz_vlog().

◆ cfg_logsrcinfo

bool cfg_logsrcinfo = false
static

Definition at line 15 of file log.c.

Referenced by rz_log_set_srcinfo(), and rz_vlog().

◆ cfg_logtraplvl

int cfg_logtraplvl = RZ_LOGLVL_FATAL
static

Definition at line 14 of file log.c.

Referenced by rz_log_set_traplevel(), and rz_vlog().

◆ level_tags

const char* level_tags[]
static
Initial value:
= {
[RZ_LOGLVL_SILLY] = "SILLY",
[RZ_LOGLVL_VERBOSE] = "VERBOSE",
[RZ_LOGLVL_DEBUG] = "DEBUG",
[RZ_LOGLVL_INFO] = "INFO",
[RZ_LOGLVL_WARN] = "WARNING",
[RZ_LOGLVL_ERROR] = "ERROR",
[RZ_LOGLVL_FATAL] = "FATAL"
}
@ RZ_LOGLVL_SILLY
Definition: rz_log.h:17
@ RZ_LOGLVL_WARN
Definition: rz_log.h:21
@ RZ_LOGLVL_INFO
Definition: rz_log.h:20
@ RZ_LOGLVL_ERROR
Definition: rz_log.h:22
@ RZ_LOGLVL_VERBOSE
Definition: rz_log.h:19
@ RZ_LOGLVL_FATAL
Definition: rz_log.h:23
@ RZ_LOGLVL_DEBUG
Definition: rz_log.h:18

Definition at line 18 of file log.c.

Referenced by rz_vlog().

◆ log_cbs

RzList* log_cbs = NULL
static

Definition at line 12 of file log.c.

Referenced by rz_log_add_callback(), rz_log_del_callback(), and rz_vlog().