From 4e54ac3ac3441c10be3e1d8aec0b5d157bae57f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20D=C3=A9saulniers?= <sim.desaulniers@gmail.com> Date: Fri, 8 Apr 2016 18:34:51 -0400 Subject: [PATCH] dht: adding dht::log common utilities namespace --- CMakeLists.txt | 1 + include/opendht.h | 1 + include/opendht/log.h | 108 ++++++++++++++++++++++++++++++++++++++++++ src/Makefile.am | 1 + tools/dhtnode.cpp | 8 ++-- 5 files changed, 115 insertions(+), 4 deletions(-) create mode 100644 include/opendht/log.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 2db14613..15959caa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -44,6 +44,7 @@ list (APPEND opendht_HEADERS include/opendht/value.h include/opendht/dht.h include/opendht/securedht.h + include/opendht/log.h include/opendht.h ) diff --git a/include/opendht.h b/include/opendht.h index 29fab079..d7caf3dc 100644 --- a/include/opendht.h +++ b/include/opendht.h @@ -24,4 +24,5 @@ #include "opendht/infohash.h" #include "opendht/securedht.h" #include "opendht/dhtrunner.h" +#include "opendht/log.h" #include "opendht/default_types.h" diff --git a/include/opendht/log.h b/include/opendht/log.h new file mode 100644 index 00000000..0838ed6d --- /dev/null +++ b/include/opendht/log.h @@ -0,0 +1,108 @@ +/* + * Copyright (C) 2014-2016 Savoir-faire Linux Inc. + * + * Author: Adrien Béraud <adrien.beraud@savoirfairelinux.com> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#pragma once + +#include "dhtrunner.h" + +#include <iostream> +#include <fstream> + +namespace dht { +namespace log { + +/** + * Terminal colors for logging + */ +namespace Color { + enum Code { + FG_RED = 31, + FG_GREEN = 32, + FG_YELLOW = 33, + FG_BLUE = 34, + FG_DEFAULT = 39, + BG_RED = 41, + BG_GREEN = 42, + BG_BLUE = 44, + BG_DEFAULT = 49 + }; + class Modifier { + const Code code; + public: + constexpr Modifier(Code pCode) : code(pCode) {} + friend std::ostream& + operator<<(std::ostream& os, const Modifier& mod) { + return os << "\033[" << mod.code << 'm'; + } + }; +} + +constexpr const Color::Modifier def(Color::FG_DEFAULT); +constexpr const Color::Modifier red(Color::FG_RED); +constexpr const Color::Modifier yellow(Color::FG_YELLOW); + +/** + * Print va_list to std::ostream (used for logging). + */ +void +printLog(std::ostream& s, char const* m, va_list args) { + static constexpr int BUF_SZ = 8192; + char buffer[BUF_SZ]; + int ret = vsnprintf(buffer, sizeof(buffer), m, args); + if (ret < 0) + return; + s.write(buffer, std::min(ret, BUF_SZ)); + if (ret >= BUF_SZ) + s << "[[TRUNCATED]]"; + s.put('\n'); +} + +void +enableLogging(dht::DhtRunner& dht) +{ + dht.setLoggers( + [](char const* m, va_list args){ std::cerr << red; printLog(std::cerr, m, args); std::cerr << def; }, + [](char const* m, va_list args){ std::cout << yellow; printLog(std::cout, m, args); std::cout << def; }, + [](char const* m, va_list args){ printLog(std::cout, m, args); } + ); +} + +void +enableFileLogging(dht::DhtRunner& dht, const std::string& path) +{ + auto logfile = std::make_shared<std::fstream>(); + logfile->open(path, std::ios::out); + + dht.setLoggers( + [=](char const* m, va_list args){ printLog(*logfile, m, args); }, + [=](char const* m, va_list args){ printLog(*logfile, m, args); }, + [=](char const* m, va_list args){ printLog(*logfile, m, args); } + ); +} + +void +disableLogging(dht::DhtRunner& dht) +{ + dht.setLoggers(dht::NOLOG, dht::NOLOG, dht::NOLOG); +} + +} /* log */ +} /* dht */ + diff --git a/src/Makefile.am b/src/Makefile.am index d0666269..4a645504 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -28,4 +28,5 @@ nobase_include_HEADERS = \ ../include/opendht/securedht.h \ ../include/opendht/dhtrunner.h \ ../include/opendht/default_types.h \ + ../include/opendht/log.h \ ../include/opendht/rng.h diff --git a/tools/dhtnode.cpp b/tools/dhtnode.cpp index 092bbd6b..718733bb 100644 --- a/tools/dhtnode.cpp +++ b/tools/dhtnode.cpp @@ -137,9 +137,9 @@ void cmd_loop(DhtRunner& dht, dht_params& params) } else if (op == "log") { params.log = !params.log; if (params.log) - enableLogging(dht); + log::enableLogging(dht); else - disableLogging(dht); + log::disableLogging(dht); continue; } @@ -261,9 +261,9 @@ main(int argc, char **argv) if (params.log) { if (not params.logfile.empty()) - enableFileLogging(dht, params.logfile); + log::enableFileLogging(dht, params.logfile); else - enableLogging(dht); + log::enableLogging(dht); } if (not params.bootstrap.first.empty()) { -- GitLab