diff --git a/include/villas/atomic.h b/include/villas/atomic.h new file mode 100644 index 000000000..a27e9b00d --- /dev/null +++ b/include/villas/atomic.h @@ -0,0 +1,19 @@ +/** Workaround for differently named atomic types in C/C++ + * + * @file + * @author Georg Reinke + * @copyright 2017, Institute for Automation of Complex Power Systems, EONERC + *********************************************************************************/ + +#ifdef __cplusplus + +#include + +typedef std::atomic_int atomic_int; +typedef std::atomic_size_t atomic_size_t; + +#else + +#include + +#endif diff --git a/include/villas/hook.h b/include/villas/hook.h index 3835069d9..de309eecc 100644 --- a/include/villas/hook.h +++ b/include/villas/hook.h @@ -22,6 +22,7 @@ #include #include +#include "log_config.h" #include "queue.h" #include "list.h" #include "super_node.h" @@ -111,4 +112,4 @@ int hook_cmp_priority(const void *a, const void *b); * hooks = [ "print" ] * } */ -int hook_parse_list(struct list *list, config_setting_t *cfg, struct path *p); \ No newline at end of file +int hook_parse_list(struct list *list, config_setting_t *cfg, struct path *p); diff --git a/include/villas/log.h b/include/villas/log.h index 5b4a968d3..83fd9c35c 100644 --- a/include/villas/log.h +++ b/include/villas/log.h @@ -7,11 +7,15 @@ #pragma once +#ifdef __cplusplus +extern "C" { +#endif + #include #include -#include #include "common.h" +#include "log_config.h" #ifdef __GNUC__ #define INDENT int __attribute__ ((__cleanup__(log_outdent), unused)) _old_indent = log_indent(1); @@ -81,6 +85,10 @@ struct log { FILE *file; }; +/** The global log instance. */ +struct log *global_log; +struct log default_log; + /** Initialize log object */ int log_init(struct log *l, int level, long faciltities); @@ -118,9 +126,6 @@ void log_outdent(int *); */ int log_set_facility_expression(struct log *l, const char *expression); -/** Parse logging configuration. */ -int log_parse(struct log *l, config_setting_t *cfg); - /** Logs variadic messages to stdout. * * @param lvl The log level @@ -164,6 +169,6 @@ void error(const char *fmt, ...) void serror(const char *fmt, ...) __attribute__ ((format(printf, 1, 2))); -/** Print configuration error and exit. */ -void cerror(config_setting_t *cfg, const char *fmt, ...) - __attribute__ ((format(printf, 2, 3))); \ No newline at end of file +#ifdef __cplusplus +} +#endif diff --git a/include/villas/log_config.h b/include/villas/log_config.h new file mode 100644 index 000000000..11aaf8e33 --- /dev/null +++ b/include/villas/log_config.h @@ -0,0 +1,22 @@ +/** Logging routines that depend on libconfig. + * + * @file + * @author Steffen Vogel + * @copyright 2017, Institute for Automation of Complex Power Systems, EONERC + *********************************************************************************/ + +#pragma once + +struct log; + +#include + +#include "log.h" + +/** Parse logging configuration. */ +int log_parse(struct log *l, config_setting_t *cfg); + +/** Print configuration error and exit. */ +void cerror(config_setting_t *cfg, const char *fmt, ...) + __attribute__ ((format(printf, 2, 3))); + diff --git a/include/villas/queue.h b/include/villas/queue.h index 71d615d02..fa88fb8d8 100644 --- a/include/villas/queue.h +++ b/include/villas/queue.h @@ -33,11 +33,12 @@ #pragma once + #include #include -#include #include +#include "atomic.h" #include "common.h" /* Forward declarations */ diff --git a/include/villas/sample.h b/include/villas/sample.h index c7557ae08..df98180b1 100644 --- a/include/villas/sample.h +++ b/include/villas/sample.h @@ -7,10 +7,15 @@ #pragma once +#include "atomic.h" + +#ifdef __cplusplus +extern "C" { +#endif + #include #include #include -#include #include #include @@ -81,3 +86,7 @@ int sample_get_data_format(struct sample *s, int idx); /** Set number representation for a single value of a sample. */ int sample_set_data_format(struct sample *s, int idx, enum sample_data_format fmt); + +#ifdef __cplusplus +} +#endif diff --git a/include/villas/shmem.h b/include/villas/shmem.h index 271654495..d75826ca0 100644 --- a/include/villas/shmem.h +++ b/include/villas/shmem.h @@ -13,6 +13,10 @@ #pragma once +#ifdef __cplusplus +extern "C" { +#endif + #include "pool.h" #include "queue.h" #include "queue_signalled.h" @@ -80,3 +84,7 @@ int shmem_shared_write(struct shmem_shared *shm, struct sample *smps[], unsigned size_t shmem_total_size(int insize, int outsize, int sample_size); /** @} */ + +#ifdef __cplusplus +} +#endif diff --git a/lib/Makefile.villas-ext.inc b/lib/Makefile.villas-ext.inc index 4f513a106..e005b3122 100644 --- a/lib/Makefile.villas-ext.inc +++ b/lib/Makefile.villas-ext.inc @@ -2,9 +2,9 @@ LIBEXT_NAME = libvillas-ext LIBEXT_ABI_VERSION = 1 LIBEXT = $(BUILDDIR)/$(LIBEXT_NAME).so.$(LIBEXT_ABI_VERSION) -LIBEXT_SRCS += $(addprefix lib/, sample.c node.c queue.c queue_signalled.c \ - memory.c shmem.c utils.c kernel/kernel.c log.c list.c \ - plugin.c timing.c \ +LIBEXT_SRCS += $(addprefix lib/, sample.c queue.c queue_signalled.c \ + memory.c log.c shmem.c utils.c kernel/kernel.c list.c \ + timing.c \ ) LIBEXT_LDFLAGS = -shared @@ -25,4 +25,4 @@ install-libvillas-ext: libvillas-ext clean-libvillas-ext: rm -rf $(BUILDDIR)/lib $(LIBEXT) -.PHONY: install-libvillas-ext clean-libvillas-ext $(LIBEXT_NAME) \ No newline at end of file +.PHONY: install-libvillas-ext clean-libvillas-ext $(LIBEXT_NAME) diff --git a/lib/Makefile.villas.inc b/lib/Makefile.villas.inc index a6f41aa7b..1f44dff0e 100644 --- a/lib/Makefile.villas.inc +++ b/lib/Makefile.villas.inc @@ -6,7 +6,7 @@ LIB = $(BUILDDIR)/$(LIB_NAME).so.$(LIB_ABI_VERSION) LIB_SRCS += $(addprefix lib/nodes/, file.c cbuilder.c shmem.c) \ $(addprefix lib/kernel/, kernel.c rt.c) \ $(addprefix lib/, sample.c path.c node.c hook.c \ - log.c utils.c super_node.c hist.c timing.c pool.c \ + log.c log_config.c utils.c super_node.c hist.c timing.c pool.c \ list.c queue.c queue_signalled.c memory.c advio.c web.c api.c \ plugin.c node_type.c stats.c mapping.c sample_io.c shmem.c \ json.c crypt.c compat.c \ @@ -94,4 +94,4 @@ install-libvillas: libvillas clean-libvillas: rm -rf $(BUILDDIR)/lib $(LIB) -.PHONY: install-libvillas clean-libvillas $(LIB_NAME) \ No newline at end of file +.PHONY: install-libvillas clean-libvillas $(LIB_NAME) diff --git a/lib/fpga/card.c b/lib/fpga/card.c index a8b90cc7b..20724a979 100644 --- a/lib/fpga/card.c +++ b/lib/fpga/card.c @@ -8,6 +8,7 @@ #include "config.h" #include "log.h" +#include "log_config.h" #include "list.h" #include "utils.h" @@ -283,4 +284,4 @@ int fpga_card_reset(struct fpga_card *c) c->state = STATE_INITIALIZED; return 0; -} \ No newline at end of file +} diff --git a/lib/fpga/ip.c b/lib/fpga/ip.c index cde0080d3..4bdeeafd2 100644 --- a/lib/fpga/ip.c +++ b/lib/fpga/ip.c @@ -6,6 +6,7 @@ #include +#include "log_config.h" #include "log.h" #include "plugin.h" @@ -150,4 +151,4 @@ struct fpga_ip_type * fpga_ip_type_lookup(const char *vstr) } return NULL; -} \ No newline at end of file +} diff --git a/lib/fpga/ips/dft.c b/lib/fpga/ips/dft.c index c4877ceb5..ff1288d14 100644 --- a/lib/fpga/ips/dft.c +++ b/lib/fpga/ips/dft.c @@ -6,6 +6,7 @@ **********************************************************************************/ #include "log.h" +#include "log_config.h" #include "plugin.h" #include "fpga/ip.h" @@ -114,4 +115,4 @@ static struct plugin p = { } }; -REGISTER_PLUGIN(&p) \ No newline at end of file +REGISTER_PLUGIN(&p) diff --git a/lib/fpga/ips/model.c b/lib/fpga/ips/model.c index 1eb2707a1..f80ff551a 100644 --- a/lib/fpga/ips/model.c +++ b/lib/fpga/ips/model.c @@ -11,6 +11,7 @@ #include "utils.h" #include "log.h" +#include "log_config.h" #include "plugin.h" #include "fpga/ip.h" diff --git a/lib/fpga/ips/switch.c b/lib/fpga/ips/switch.c index e9ca582ce..2fe2bbea5 100644 --- a/lib/fpga/ips/switch.c +++ b/lib/fpga/ips/switch.c @@ -8,6 +8,7 @@ #include "list.h" #include "log.h" +#include "log_config.h" #include "plugin.h" #include "fpga/ip.h" @@ -202,4 +203,4 @@ static struct plugin p = { } }; -REGISTER_PLUGIN(&p) \ No newline at end of file +REGISTER_PLUGIN(&p) diff --git a/lib/hooks/convert.c b/lib/hooks/convert.c index f0d5c589f..4178763c0 100644 --- a/lib/hooks/convert.c +++ b/lib/hooks/convert.c @@ -96,4 +96,4 @@ static struct plugin p = { REGISTER_PLUGIN(&p) -/** @} */ \ No newline at end of file +/** @} */ diff --git a/lib/log.c b/lib/log.c index 1d69c1bcc..d82cbab34 100644 --- a/lib/log.c +++ b/lib/log.c @@ -10,9 +10,9 @@ #include #include +#include "config.h" #include "log.h" #include "utils.h" -#include "config.h" #include "timing.h" #ifdef ENABLE_OPAL_ASYNC @@ -23,8 +23,8 @@ #endif /** The global log instance. */ -static struct log *log; -static struct log default_log = { +struct log *global_log; +struct log default_log = { .level = V, .facilities = LOG_ALL, .file = NULL, @@ -66,7 +66,7 @@ static __thread int indent = 0; int log_init(struct log *l, int level, long facilitites) { /* Register this log instance globally */ - log = l; + global_log = l; l->level = level; l->facilities = facilitites; @@ -78,24 +78,6 @@ int log_init(struct log *l, int level, long facilitites) return 0; } -int log_parse(struct log *l, config_setting_t *cfg) -{ - const char *facilities; - - if (!config_setting_is_group(cfg)) - cerror(cfg, "Setting 'log' must be a group."); - - config_setting_lookup_int(cfg, "level", &l->level); - config_setting_lookup_string(cfg, "file", &l->path); - - if (config_setting_lookup_string(cfg, "facilities", &facilities)) - log_set_facility_expression(l, facilities); - - l->state = STATE_PARSED; - - return 0; -} - int log_start(struct log *l) { l->epoch = time_now(); @@ -129,7 +111,7 @@ int log_destroy(struct log *l) { default_log.epoch = l->epoch; - log = NULL; + global_log = NULL; l->state = STATE_DESTROYED; @@ -243,14 +225,14 @@ void line() char buf[LOG_WIDTH]; memset(buf, 0x71, sizeof(buf)); - log_print(log, "", "\b" ACS("%.*s"), LOG_WIDTH, buf); + log_print(global_log, "", "\b" ACS("%.*s"), LOG_WIDTH, buf); } void debug(long class, const char *fmt, ...) { va_list ap; - struct log *l = log ? log : &default_log; + struct log *l = global_log ? global_log : &default_log; int lvl = class & 0xFF; int fac = class & ~0xFF; @@ -266,7 +248,7 @@ void info(const char *fmt, ...) { va_list ap; - struct log *l = log ? log : &default_log; + struct log *l = global_log ? global_log : &default_log; va_start(ap, fmt); log_vprint(l, LOG_LVL_INFO, fmt, ap); @@ -277,7 +259,7 @@ void warn(const char *fmt, ...) { va_list ap; - struct log *l = log ? log : &default_log; + struct log *l = global_log ? global_log : &default_log; va_start(ap, fmt); log_vprint(l, LOG_LVL_WARN, fmt, ap); @@ -288,7 +270,7 @@ void stats(const char *fmt, ...) { va_list ap; - struct log *l = log ? log : &default_log; + struct log *l = global_log ? global_log : &default_log; va_start(ap, fmt); log_vprint(l, LOG_LVL_STATS, fmt, ap); @@ -299,7 +281,7 @@ void error(const char *fmt, ...) { va_list ap; - struct log *l = log ? log : &default_log; + struct log *l = global_log ? global_log : &default_log; va_start(ap, fmt); log_vprint(l, LOG_LVL_ERROR, fmt, ap); @@ -313,7 +295,7 @@ void serror(const char *fmt, ...) va_list ap; char *buf = NULL; - struct log *l = log ? log : &default_log; + struct log *l = global_log ? global_log : &default_log; va_start(ap, fmt); vstrcatf(&buf, fmt, ap); @@ -324,27 +306,3 @@ void serror(const char *fmt, ...) free(buf); die(); } - -void cerror(config_setting_t *cfg, const char *fmt, ...) -{ - va_list ap; - char *buf = NULL; - const char *file; - int line; - - struct log *l = log ? log : &default_log; - - va_start(ap, fmt); - vstrcatf(&buf, fmt, ap); - va_end(ap); - - line = config_setting_source_line(cfg); - file = config_setting_source_file(cfg); - if (!file) - file = config_setting_get_hook(config_root_setting(cfg->config)); - - log_print(l, LOG_LVL_ERROR, "%s in %s:%u", buf, file, line); - - free(buf); - die(); -} diff --git a/lib/log_config.c b/lib/log_config.c new file mode 100644 index 000000000..63d1f6142 --- /dev/null +++ b/lib/log_config.c @@ -0,0 +1,55 @@ +/** Logging routines that depend on libconfig. + * + * @author Steffen Vogel + * @copyright 2017, Institute for Automation of Complex Power Systems, EONERC + *********************************************************************************/ + +#include +#include + +#include "config.h" +#include "log.h" +#include "log_config.h" +#include "utils.h" + +int log_parse(struct log *l, config_setting_t *cfg) +{ + const char *facilities; + + if (!config_setting_is_group(cfg)) + cerror(cfg, "Setting 'log' must be a group."); + + config_setting_lookup_int(cfg, "level", &l->level); + config_setting_lookup_string(cfg, "file", &l->path); + + if (config_setting_lookup_string(cfg, "facilities", &facilities)) + log_set_facility_expression(l, facilities); + + l->state = STATE_PARSED; + + return 0; +} + +void cerror(config_setting_t *cfg, const char *fmt, ...) +{ + va_list ap; + char *buf = NULL; + const char *file; + int line; + + struct log *l = global_log ? global_log : &default_log; + + va_start(ap, fmt); + vstrcatf(&buf, fmt, ap); + va_end(ap); + + line = config_setting_source_line(cfg); + file = config_setting_source_file(cfg); + if (!file) + file = config_setting_get_hook(config_root_setting(cfg->config)); + + log_print(l, LOG_LVL_ERROR, "%s in %s:%u", buf, file, line); + + free(buf); + die(); +} diff --git a/src/Makefile.inc b/src/Makefile.inc index f1e7a9d67..72d869f79 100644 --- a/src/Makefile.inc +++ b/src/Makefile.inc @@ -38,7 +38,7 @@ $(BUILDDIR)/src/%.o: src/%.c $(BUILDDIR)/defines | $$(dir $$@) $(CC) $(SRC_CFLAGS) -c $< -o $@ # Build villas-shmem only with libvillas-ext (to ensure that libext contains everything needed) -$(BUILDDIR)/villas-shmem: $(BUILDDIR)/src/shmem.o $(LIBS) libvillas-ext +$(BUILDDIR)/villas-shmem: $(BUILDDIR)/src/shmem.o libvillas-ext $(CC) $(SRC_LDFLAGS) $(BUILDDIR)/src/shmem.o $(filter-out -lvillas,$(SRC_LDLIBS)) -lvillas-ext -o $@ # Link target executables