From 08c6bcb98e4f9c6da6dd819becc136efa3455466 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sat, 27 Jul 2013 18:53:27 +0200 Subject: Move logging into context_t --- mmss/CMakeLists.txt | 2 +- mmss/config.cpp | 10 +++---- mmss/config.y | 2 +- mmss/context.cpp | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++ mmss/gmrf.cpp | 29 +------------------ mmss/log.cpp | 46 ------------------------------ mmss/mmss.hpp | 6 ++-- mmss/util.hpp | 44 ----------------------------- 8 files changed, 93 insertions(+), 127 deletions(-) create mode 100644 mmss/context.cpp delete mode 100644 mmss/log.cpp delete mode 100644 mmss/util.hpp diff --git a/mmss/CMakeLists.txt b/mmss/CMakeLists.txt index 01e8488..bbd6f5b 100644 --- a/mmss/CMakeLists.txt +++ b/mmss/CMakeLists.txt @@ -5,10 +5,10 @@ BISON_TARGET(mmss_config_parse config.y ${CMAKE_CURRENT_BINARY_DIR}/config.yy.cp add_executable(mmss config.cpp + context.cpp event.cpp gmrf.cpp iface.cpp - log.cpp mmss.cpp protocol.cpp ${FLEX_mmss_config_lex_OUTPUTS} diff --git a/mmss/config.cpp b/mmss/config.cpp index 4492ffd..5897720 100644 --- a/mmss/config.cpp +++ b/mmss/config.cpp @@ -38,7 +38,7 @@ namespace MMSS { namespace Config { void add_network(context_t *mmss, config_t *conf, const char *name) { - logf(mmss, LOG_NOTICE, "adding network `%s'", name); + mmss->logf(LOG_NOTICE, "adding network `%s'", name); std::shared_ptr net = std::make_shared(); @@ -74,7 +74,7 @@ bool read_config(context_t *mmss, config_t *conf, const char *filename) { else { file = fopen(filename, "r"); if (!file) { - logf(mmss, LOG_ERR, "can't open config file `%s': %s", filename, strerror(errno)); + mmss->logf(LOG_ERR, "can't open config file `%s': %s", filename, strerror(errno)); ret = false; goto end_free; } @@ -87,7 +87,7 @@ bool read_config(context_t *mmss, config_t *conf, const char *filename) { dir = dirname(filename2); if (chdir(dir)) { - logf(mmss, LOG_ERR, "change from directory `%s' to `%s' failed", oldcwd, dir); + mmss->logf(LOG_ERR, "change from directory `%s' to `%s' failed", oldcwd, dir); ret = false; goto end_free; } @@ -97,7 +97,7 @@ bool read_config(context_t *mmss, config_t *conf, const char *filename) { token = mmss_config_yylex(&token_val, &loc, scanner); if (token < 0) { - logf(mmss, LOG_ERR, "config error: %s at %s:%i:%i", token_val.error, filename, loc.first_line, loc.first_column); + mmss->logf(LOG_ERR, "config error: %s at %s:%i:%i", token_val.error, filename, loc.first_line, loc.first_column); ret = false; goto end_free; } @@ -122,7 +122,7 @@ bool read_config(context_t *mmss, config_t *conf, const char *filename) { mmss_config_yylex_destroy(scanner); if(chdir(oldcwd)) - logf(mmss, LOG_ERR, "can't chdir to `%s': %s", oldcwd, strerror(errno)); + mmss->logf(LOG_ERR, "can't chdir to `%s': %s", oldcwd, strerror(errno)); free(filename2); free(oldcwd); diff --git a/mmss/config.y b/mmss/config.y index 6761d7c..ca14690 100644 --- a/mmss/config.y +++ b/mmss/config.y @@ -90,5 +90,5 @@ boolean: TOK_YES { $$ = true; } %% void mmss_config_error(YYLTYPE *loc, MMSS::context_t *mmss, MMSS::config_t *conf, const char *filename, const char *s) { - MMSS::logf(mmss, LOG_ERR, "config error: %s at %s:%i:%i", s, filename, loc->first_line, loc->first_column); + mmss->logf(LOG_ERR, "config error: %s at %s:%i:%i", s, filename, loc->first_line, loc->first_column); } diff --git a/mmss/context.cpp b/mmss/context.cpp new file mode 100644 index 0000000..5fcb72f --- /dev/null +++ b/mmss/context.cpp @@ -0,0 +1,81 @@ +/* + Copyright (c) 2013, Matthias Schiffer + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + +#include "mmss.hpp" + +namespace MMSS { + +static inline int snprintf_safe(char *buffer, size_t size, const char *format, ...) { + std::va_list ap; + va_start(ap, format); + int ret = std::vsnprintf(buffer, size, format, ap); + va_end(ap); + + return ret < 0 ? 0 : ret > size ? size : ret; +} + +static inline const char* get_log_prefix(int log_level) { + switch(log_level) { + case LOG_CRIT: + return "Fatal: "; + case LOG_ERR: + return "Error: "; + case LOG_WARNING: + return "Warning: "; + case LOG_NOTICE: + return "Info: "; + case LOG_INFO: + return "Verbose: "; + case LOG_DEBUG: + return "DEBUG: "; + default: + return ""; + } +} + + +void context_t::vlogf_orig(const node_t *orig, int priority, const char *format, std::va_list ap) { + char buf[1024]; + size_t pos; + + if (orig) + pos = snprintf_safe(buf, sizeof(buf), "[% 6u.%03u] %s: %s", now()/1000, now()%1000, orig->name.c_str(), get_log_prefix(priority)); + else + pos = snprintf_safe(buf, sizeof(buf), "[% 6u.%03u] %s", now()/1000, now()%1000, get_log_prefix(priority)); + + std::vsnprintf(buf+pos, sizeof(buf)-pos, format, ap); + + std::fprintf(stderr, "%s\n", buf); +} + +void context_t::logf(int priority, const char *format, ...) { + std::va_list ap; + va_start(ap, format); + vlogf_orig(nullptr, priority, format, ap); + va_end(ap); +} + +} diff --git a/mmss/gmrf.cpp b/mmss/gmrf.cpp index 9cda413..1eaa656 100644 --- a/mmss/gmrf.cpp +++ b/mmss/gmrf.cpp @@ -25,7 +25,6 @@ #include "mmss.hpp" -#include "util.hpp" #include @@ -92,37 +91,11 @@ void gmrf_random_bytes(gmrf_t *gmrf, void *buffer, size_t len) { data[i] = rand_r(&gmrf->rand_seed); } -static inline const char* get_log_prefix(int log_level) { - switch(log_level) { - case LOG_CRIT: - return "Fatal: "; - case LOG_ERR: - return "Error: "; - case LOG_WARNING: - return "Warning: "; - case LOG_NOTICE: - return "Info: "; - case LOG_INFO: - return "Verbose: "; - case LOG_DEBUG: - return "DEBUG: "; - default: - return ""; - } -} - void gmrf_logf(gmrf_t *gmrf, int priority, const char *format, ...) { - char buf[1024]; - size_t pos = 0; - - pos += MMSS::snprintf_safe(buf, sizeof(buf), "%s: %s", gmrf->name.c_str(), get_log_prefix(priority)); - va_list ap; va_start(ap, format); - vsnprintf(buf+pos, sizeof(buf)-pos, format, ap); + gmrf->mmss->vlogf_orig(gmrf, priority, format, ap); va_end(ap); - - MMSS::logf(gmrf->mmss, priority, "%s", buf); } } diff --git a/mmss/log.cpp b/mmss/log.cpp deleted file mode 100644 index 79f4ef7..0000000 --- a/mmss/log.cpp +++ /dev/null @@ -1,46 +0,0 @@ -/* - Copyright (c) 2013, Matthias Schiffer - All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: - - 1. Redistributions of source code must retain the above copyright notice, - this list of conditions and the following disclaimer. - 2. Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ - - -#include "mmss.hpp" -#include "util.hpp" - -namespace MMSS { - -void logf(context_t *mmss, int priority, const char *format, ...) { - char buf[1024]; - size_t pos = 0; - - pos += snprintf_safe(buf, sizeof(buf), "[% 5u.%03u] ", mmss->now()/1000, mmss->now()%1000); - - std::va_list ap; - va_start(ap, format); - std::vsnprintf(buf+pos, sizeof(buf)-pos, format, ap); - va_end(ap); - - std::fprintf(stderr, "%s\n", buf); -} - -} diff --git a/mmss/mmss.hpp b/mmss/mmss.hpp index c3bb31c..2789b24 100644 --- a/mmss/mmss.hpp +++ b/mmss/mmss.hpp @@ -29,6 +29,7 @@ #include "event.hpp" #include "queue.hpp" +#include #include #include @@ -77,6 +78,9 @@ public: timeout_queue_t event_queue; context_t() : event_queue(this) {} + + void vlogf_orig(const node_t *orig, int priority, const char *format, std::va_list ap); + void logf(int priority, const char *format, ...); }; class config_t { @@ -102,8 +106,6 @@ void add_iface(const std::shared_ptr &node, const std::shared_ptr &source, const std::shared_ptr &dest, const void *data, size_t len); -void logf(context_t *mmss, int priority, const char *format, ...); - namespace Config { diff --git a/mmss/util.hpp b/mmss/util.hpp deleted file mode 100644 index d4a9351..0000000 --- a/mmss/util.hpp +++ /dev/null @@ -1,44 +0,0 @@ -/* - Copyright (c) 2013, Matthias Schiffer - All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: - - 1. Redistributions of source code must retain the above copyright notice, - this list of conditions and the following disclaimer. - 2. Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ - - -#pragma once - -#include -#include - - -namespace MMSS { - -static inline int snprintf_safe(char *buffer, size_t size, const char *format, ...) { - std::va_list ap; - va_start(ap, format); - int ret = std::vsnprintf(buffer, size, format, ap); - va_end(ap); - - return ret < 0 ? 0 : ret > size ? size : ret; -} - -} -- cgit v1.2.3