Commit 1a30731b authored by Cyrill Gorcunov's avatar Cyrill Gorcunov Committed by Andrei Vagin

compel: Add callback-based log engine

pr_out is only special left in piegen engine, the rest use
compel's pr_x output. Probably we will need to enhance it
one day to make same close to what we have in criu.
Signed-off-by: 's avatarCyrill Gorcunov <gorcunov@openvz.org>
Signed-off-by: 's avatarAndrei Vagin <avagin@virtuozzo.com>
parent 5f6c2856
......@@ -19,6 +19,8 @@ lib-y += arch/$(ARCH)/src/lib/handle-elf.o
host-lib-y += arch/$(ARCH)/src/lib/handle-elf.o
lib-y += src/lib/handle-elf.o
host-lib-y += src/lib/handle-elf.o
lib-y += src/lib/log.o
host-lib-y += src/lib/log.o
ifeq ($(ARCH),x86)
lib-y += src/lib/handle-elf-32.o
......
......@@ -4,6 +4,7 @@
#include "handle-elf.h"
#include "piegen.h"
#include "log.h"
static const unsigned char __maybe_unused
elf_ident_64_le[EI_NIDENT] = {
......
......@@ -4,6 +4,7 @@
#include "handle-elf.h"
#include "piegen.h"
#include "log.h"
static const unsigned char __maybe_unused
elf_ident_32[EI_NIDENT] = {
......
......@@ -4,6 +4,7 @@
#include "handle-elf.h"
#include "piegen.h"
#include "log.h"
static const unsigned char __maybe_unused
elf_ident_64_le[EI_NIDENT] = {
......
......@@ -4,6 +4,7 @@
#include "handle-elf.h"
#include "piegen.h"
#include "log.h"
static const unsigned char __maybe_unused
elf_ident_64_le[EI_NIDENT] = {
......
#ifndef COMPEL_LOG_H__
#define COMPEL_LOG_H__
#include "uapi/compel/compel.h"
#include "uapi/compel/loglevels.h"
#ifndef LOG_PREFIX
# define LOG_PREFIX
#endif
static inline int pr_quelled(unsigned int loglevel)
{
return compel_log_get_loglevel() < loglevel && loglevel != LOG_MSG;
}
extern void compel_print_on_level(unsigned int loglevel, const char *format, ...);
#define pr_msg(fmt, ...) \
compel_print_on_level(LOG_MSG, \
fmt, ##__VA_ARGS__)
#define pr_info(fmt, ...) \
compel_print_on_level(LOG_INFO, \
LOG_PREFIX fmt, ##__VA_ARGS__)
#define pr_err(fmt, ...) \
compel_print_on_level(LOG_ERROR, \
"Error (%s:%d): " LOG_PREFIX fmt, \
__FILE__, __LINE__, ##__VA_ARGS__)
#define pr_err_once(fmt, ...) \
do { \
static bool __printed; \
if (!__printed) { \
pr_err(fmt, ##__VA_ARGS__); \
__printed = 1; \
} \
} while (0)
#define pr_warn(fmt, ...) \
compel_print_on_level(LOG_WARN, \
"Warn (%s:%d): " LOG_PREFIX fmt, \
__FILE__, __LINE__, ##__VA_ARGS__)
#define pr_warn_once(fmt, ...) \
do { \
static bool __printed; \
if (!__printed) { \
pr_warn(fmt, ##__VA_ARGS__); \
__printed = 1; \
} \
} while (0)
#define pr_debug(fmt, ...) \
compel_print_on_level(LOG_DEBUG, \
LOG_PREFIX fmt, ##__VA_ARGS__)
#define pr_perror(fmt, ...) \
pr_err(fmt ": %m\n", ##__VA_ARGS__)
#endif /* COMPEL_LOG_H__ */
......@@ -17,8 +17,6 @@ typedef struct {
char *var_name;
char *nrgotpcrel_name;
FILE *fout;
FILE *ferr;
FILE *fdebug;
} piegen_opt_t;
extern piegen_opt_t opts;
......@@ -29,27 +27,6 @@ do { \
fprintf(opts.fout, fmt, ##__VA_ARGS__); \
} while (0)
#define pr_debug(fmt, ...) \
do { \
if (opts.fdebug) \
fprintf(opts.fdebug, "%s: "fmt, \
opts.stream_name, ##__VA_ARGS__); \
} while (0)
#define pr_err(fmt, ...) \
do { \
if (opts.ferr) \
fprintf(opts.ferr, "%s: Error (%s:%d): "fmt, \
opts.stream_name, __FILE__, __LINE__, ##__VA_ARGS__); \
} while (0)
#define pr_perror(fmt, ...) \
do { \
if (opts.ferr) \
fprintf(opts.ferr, "%s: Error (%s:%d): "fmt ": %m\n", \
opts.stream_name, __FILE__, __LINE__, ##__VA_ARGS__); \
} while (0)
extern int handle_binary(void *mem, size_t size);
#endif /* COMPEL_PIEGEN_H__ */
......@@ -2,6 +2,7 @@
#define UAPI_COMPEL_H__
#include <errno.h>
#include <stdarg.h>
#define COMPEL_TYPE_INT (1u << 0)
#define COMPEL_TYPE_LONG (1u << 1)
......@@ -14,4 +15,11 @@ typedef struct {
long value;
} compel_reloc_t;
/*
* Logging
*/
typedef void (*compel_log_fn)(unsigned int lvl, const char *fmt, va_list parms);
extern void compel_log_init(compel_log_fn log_fn, unsigned int level);
extern unsigned int compel_log_get_loglevel(void);
#endif /* UAPI_COMPEL_H__ */
#ifndef UAPI_COMPEL_LOGLEVELS_H__
#define UAPI_COMPEL_LOGLEVELS_H__
#define LOG_UNSET (-1)
#define LOG_MSG (0) /* Print message regardless of log level */
#define LOG_ERROR (1) /* Errors only, when we're in trouble */
#define LOG_WARN (2) /* Warnings, dazen and confused but trying to continue */
#define LOG_INFO (3) /* Informative, everything is fine */
#define LOG_DEBUG (4) /* Debug only */
#define DEFAULT_LOGLEVEL LOG_WARN
#endif /* UAPI_COMPEL_LOGLEVELS_H__ */
......@@ -16,6 +16,7 @@
#include "handle-elf.h"
#include "piegen.h"
#include "log.h"
/* Check if pointer is out-of-bound */
static bool __ptr_oob(const uintptr_t ptr, const uintptr_t start, const size_t size)
......
log.c
\ No newline at end of file
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <compel/compel.h>
#include "log.h"
static unsigned int current_loglevel = DEFAULT_LOGLEVEL;
static compel_log_fn logfn;
void compel_log_init(compel_log_fn log_fn, unsigned int level)
{
logfn = log_fn;
current_loglevel = level;
}
unsigned int compel_log_get_loglevel(void)
{
return current_loglevel;
}
void compel_print_on_level(unsigned int loglevel, const char *format, ...)
{
va_list params;
compel_log_fn fn = logfn;
if (fn != NULL && !pr_quelled(loglevel)) {
va_start(params, format);
fn(loglevel, format, params);
va_end(params);
}
}
......@@ -12,8 +12,11 @@
#include <sys/stat.h>
#include <sys/mman.h>
#include "uapi/compel/compel.h"
#include "version.h"
#include "piegen.h"
#include "log.h"
#define CFLAGS_DEFAULT_SET \
"-Wstrict-prototypes " \
......@@ -81,15 +84,19 @@ err:
return ret;
}
static void cli_log(unsigned int lvl, const char *fmt, va_list parms)
{
if (!pr_quelled(lvl))
vprintf(fmt, parms);
}
int main(int argc, char *argv[])
{
const char *current_cflags = NULL;
int log_level = DEFAULT_LOGLEVEL;
int opt, idx, i;
char *action;
opts.fdebug = stdout;
opts.ferr = stderr;
typedef struct {
const char *arch;
const char *cflags;
......@@ -114,7 +121,7 @@ int main(int argc, char *argv[])
},
};
static const char short_opts[] = "a:f:o:s:p:v:r:u:hV";
static const char short_opts[] = "a:f:o:s:p:v:r:u:hVl:";
static struct option long_opts[] = {
{ "arch", required_argument, 0, 'a' },
{ "file", required_argument, 0, 'f' },
......@@ -126,6 +133,7 @@ int main(int argc, char *argv[])
{ "pcrelocs", required_argument, 0, 'r' },
{ "help", required_argument, 0, 'h' },
{ "version", no_argument, 0, 'V' },
{ "log-level", required_argument, 0, 'l' },
{ },
};
......@@ -170,6 +178,9 @@ int main(int argc, char *argv[])
case 'r':
opts.nrgotpcrel_name = optarg;
break;
case 'l':
break;
log_level = atoi(optarg);
case 'h':
goto usage;
case 'V':
......@@ -204,6 +215,7 @@ int main(int argc, char *argv[])
if (!strcmp(action, "piegen")) {
if (!opts.input_filename)
goto usage;
compel_log_init(&cli_log, log_level);
return piegen();
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment