Move logging into context_t

This commit is contained in:
Matthias Schiffer 2013-07-27 18:53:27 +02:00
parent 379b2b3701
commit 08c6bcb98e
7 changed files with 54 additions and 88 deletions

View file

@ -5,10 +5,10 @@ BISON_TARGET(mmss_config_parse config.y ${CMAKE_CURRENT_BINARY_DIR}/config.yy.cp
add_executable(mmss add_executable(mmss
config.cpp config.cpp
context.cpp
event.cpp event.cpp
gmrf.cpp gmrf.cpp
iface.cpp iface.cpp
log.cpp
mmss.cpp mmss.cpp
protocol.cpp protocol.cpp
${FLEX_mmss_config_lex_OUTPUTS} ${FLEX_mmss_config_lex_OUTPUTS}

View file

@ -38,7 +38,7 @@ namespace MMSS {
namespace Config { namespace Config {
void add_network(context_t *mmss, config_t *conf, const char *name) { 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<network_t> net = std::make_shared<network_t>(); std::shared_ptr<network_t> net = std::make_shared<network_t>();
@ -74,7 +74,7 @@ bool read_config(context_t *mmss, config_t *conf, const char *filename) {
else { else {
file = fopen(filename, "r"); file = fopen(filename, "r");
if (!file) { 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; ret = false;
goto end_free; goto end_free;
} }
@ -87,7 +87,7 @@ bool read_config(context_t *mmss, config_t *conf, const char *filename) {
dir = dirname(filename2); dir = dirname(filename2);
if (chdir(dir)) { 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; ret = false;
goto end_free; 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); token = mmss_config_yylex(&token_val, &loc, scanner);
if (token < 0) { 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; ret = false;
goto end_free; goto end_free;
} }
@ -122,7 +122,7 @@ bool read_config(context_t *mmss, config_t *conf, const char *filename) {
mmss_config_yylex_destroy(scanner); mmss_config_yylex_destroy(scanner);
if(chdir(oldcwd)) 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(filename2);
free(oldcwd); free(oldcwd);

View file

@ -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) { 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);
} }

View file

@ -24,11 +24,7 @@
*/ */
#pragma once #include "mmss.hpp"
#include <cstdarg>
#include <cstdio>
namespace MMSS { namespace MMSS {
@ -41,4 +37,45 @@ static inline int snprintf_safe(char *buffer, size_t size, const char *format, .
return ret < 0 ? 0 : ret > size ? size : ret; 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);
}
} }

View file

@ -25,7 +25,6 @@
#include "mmss.hpp" #include "mmss.hpp"
#include "util.hpp"
#include <cassert> #include <cassert>
@ -92,37 +91,11 @@ void gmrf_random_bytes(gmrf_t *gmrf, void *buffer, size_t len) {
data[i] = rand_r(&gmrf->rand_seed); 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, ...) { 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_list ap;
va_start(ap, format); va_start(ap, format);
vsnprintf(buf+pos, sizeof(buf)-pos, format, ap); gmrf->mmss->vlogf_orig(gmrf, priority, format, ap);
va_end(ap); va_end(ap);
MMSS::logf(gmrf->mmss, priority, "%s", buf);
} }
} }

View file

@ -1,46 +0,0 @@
/*
Copyright (c) 2013, Matthias Schiffer <mschiffer@universe-factory.net>
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);
}
}

View file

@ -29,6 +29,7 @@
#include "event.hpp" #include "event.hpp"
#include "queue.hpp" #include "queue.hpp"
#include <cstdarg>
#include <list> #include <list>
#include <memory> #include <memory>
@ -77,6 +78,9 @@ public:
timeout_queue_t<event_t> event_queue; timeout_queue_t<event_t> event_queue;
context_t() : event_queue(this) {} 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 { class config_t {
@ -102,8 +106,6 @@ void add_iface(const std::shared_ptr<node_t> &node, const std::shared_ptr<networ
void enqueue(context_t *mmss, const std::shared_ptr<iface_t> &source, const std::shared_ptr<iface_t> &dest, const void *data, size_t len); void enqueue(context_t *mmss, const std::shared_ptr<iface_t> &source, const std::shared_ptr<iface_t> &dest, const void *data, size_t len);
void logf(context_t *mmss, int priority, const char *format, ...);
namespace Config { namespace Config {