diff --git a/src/utils/debug.h b/src/utils/debug.h new file mode 100644 index 0000000..ea556ac --- /dev/null +++ b/src/utils/debug.h @@ -0,0 +1,161 @@ +/* + * Copyright (C) 2016 Ivan Čukić + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) version 3, or any + * later version accepted by the membership of KDE e.V. (or its + * successor approved by the membership of KDE e.V.), which shall + * act as a proxy defined in Section 6 of version 3 of the license. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. + * If not, see . + */ + +#ifndef UTILS_DEBUG_H +#define UTILS_DEBUG_H + +#include +#include + +// Black 0;30 Dark Gray 1;30 +// Blue 0;34 Light Blue 1;34 +// Green 0;32 Light Green 1;32 +// Cyan 0;36 Light Cyan 1;36 +// Red 0;31 Light Red 1;31 +// Purple 0;35 Light Purple 1;35 +// Brown 0;33 Yellow 1;33 +// Light Gray 0;37 White 1;37 + + +#define COLOR_RESET "\033[0m" + +#define DEBUG_COLOR_GRAY "\033[30;1m" +#define DEBUG_COLOR_RED "\033[31;1m" +#define DEBUG_COLOR_GREEN "\033[32;1m" +#define DEBUG_COLOR_YELLOW "\033[33;1m" +#define DEBUG_COLOR_BLUE "\033[34;1m" +#define DEBUG_COLOR_PURPLE "\033[35;1m" +#define DEBUG_COLOR_CYAN "\033[36;1m" + +#define DEBUG_COLOR_SLIM_BLUE "\033[34m" +#define DEBUG_COLOR_SLIM_GREEN "\033[32m" +#define DEBUG_COLOR_SLIM_CYAN "\033[36m" +#define DEBUG_COLOR_SLIM_RED "\033[31m" +#define DEBUG_COLOR_SLIM_PURPLE "\033[35m" +#define DEBUG_COLOR_SLIM_YELLOW "\033[33m" +#define DEBUG_COLOR_SLIM_GRAY "\033[30m" + +#define DEBUG_COLOR_VALUE COLOR_RESET DEBUG_COLOR_SLIM_BLUE + +namespace debug { + +template +struct pretty_ptr { + pretty_ptr(T* ptr) + : ptr{ptr} + { + } + + T *ptr; +}; + + +template +std::ostream& operator<< (std::ostream& out, const pretty_ptr& pretty) +{ + short ptrhash = ((intptr_t)pretty.ptr / 32) % 8; + // std::string basecol("\033[30m"); + std::string basecol(DEBUG_COLOR_GRAY); + basecol[3] = '0' + ptrhash; + + return out << basecol << '[' << (void*)pretty.ptr << ']' << COLOR_RESET; +} + + +template +std::ostream& debug_for(T* ptr) +{ + return std::cerr << pretty_ptr{ptr} << ' '; +} + + +enum class color { + gray, + red, + green, + yellow, + blue, + purple, + cyan +}; + + +template +class debug_impl { +public: + template + debug_impl& operator<< (const T& value) + { + m_out << value; + return *this; + } + + debug_impl& operator<< (color c) + { + m_out << ( c == color::gray ? DEBUG_COLOR_GRAY + : c == color::red ? DEBUG_COLOR_RED + : c == color::green ? DEBUG_COLOR_GREEN + : c == color::yellow ? DEBUG_COLOR_YELLOW + : c == color::blue ? DEBUG_COLOR_BLUE + : c == color::purple ? DEBUG_COLOR_PURPLE + : c == color::cyan ? DEBUG_COLOR_CYAN + : COLOR_RESET ); + return *this; + } + + debug_impl() + : m_out{std::cerr} + { + } + + debug_impl(color c) + : m_out{std::cerr} + { + operator<<(c); + } + + ~debug_impl() + { + m_out << COLOR_RESET << std::endl; + } + + +private: + std::ostream& m_out; +}; + + +inline debug_impl<> out() +{ + return debug_impl<>{}; +} + + +inline debug_impl<> out(color c) +{ + return debug_impl<>{c}; +} + + +} // namespace debug + +#endif // include guard end + diff --git a/src/voy/dsl/multiprocess.h b/src/voy/dsl/multiprocess.h index 1c02f64..4ff9c7a 100644 --- a/src/voy/dsl/multiprocess.h +++ b/src/voy/dsl/multiprocess.h @@ -1,102 +1,102 @@ /* * Copyright (C) 2018 Ivan Čukić * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) version 3, or any * later version accepted by the membership of KDE e.V. (or its * successor approved by the membership of KDE e.V.), which shall * act as a proxy defined in Section 6 of version 3 of the license. * * This library 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library. * If not, see . */ #ifndef VOY_DSL_MULTIPROCESS_H #define VOY_DSL_MULTIPROCESS_H #include "../operations/identity.h" #include "dsl.h" namespace voy::dsl { template struct pipeline_container { template pipeline_container(LeftTuple&& left, RightTuple&& right) : m_pipelines{std::tuple_cat(voy_fwd(left), voy_fwd(right))} { } template pipeline_container(Tuple&& tuple) : m_pipelines{std::move(tuple)} { } pipeline_container() { } template auto operator|| (NewPipeline&& new_pipeline) && { if constexpr (std::is_same_v) { return pipeline_container( std::move(m_pipelines), std::make_tuple(voy_fwd(new_pipeline))); } else { return pipeline_container( std::move(m_pipelines)); } } std::tuple m_pipelines; }; inline auto multiprocess_pipeline() { return pipeline_container<>{}; } } // namespace voy::dsl #define voy_declare_bridge_out(BridgeName) \ auto BridgeName##_send() \ { \ return voy::zmq::publisher<>( \ - std::string("ipc:///tmp/voy-zmq-bridge-" #BridgeName)); \ + std::string("ipc:///tmp/voy-zmq-bridge-" #BridgeName "-ivan")); \ } \ \ auto BridgeName##_receive() \ { \ return voy::identity<>(); \ } #define voy_declare_bridge_in(BridgeName) \ auto BridgeName##_send() \ { \ return voy::identity<>(); \ } \ \ auto BridgeName##_receive() \ { \ return voy::zmq::subscriber<>( \ - std::string("ipc:///tmp/voy-zmq-bridge-" #BridgeName)); \ + std::string("ipc:///tmp/voy-zmq-bridge-" #BridgeName "-ivan")); \ } #define voy_bridge(BridgeName) BridgeName##_send() || BridgeName##_receive() #define voy_multiprocess voy::dsl::multiprocess_pipeline() || #endif // include guard diff --git a/src/voy/tests_multiprocess.cpp b/src/voy/tests_multiprocess.cpp index b93e74c..d145532 100644 --- a/src/voy/tests_multiprocess.cpp +++ b/src/voy/tests_multiprocess.cpp @@ -1,87 +1,94 @@ /* * Copyright (C) 2018 Ivan Čukić * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) version 3, or any * later version accepted by the membership of KDE e.V. (or its * successor approved by the membership of KDE e.V.), which shall * act as a proxy defined in Section 6 of version 3 of the license. * * This library 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library. * If not, see . */ #include #include "operations/merge.h" #include "operations/slice.h" #include "operations/transform.h" #include "operations/filter.h" #include "operations/identity.h" #include "basic/delayed.h" #include "basic/values.h" #include "basic/sink.h" #include "wrappers/process.h" #include "wrappers/tcp_service.h" #include "wrappers/zmq_service.h" #include "engine/event_loop.h" #include "dsl.h" #include "dsl/multiprocess.h" +#include "../utils/debug.h" + using namespace std::literals::string_literals; using namespace std::literals::chrono_literals; #ifndef TEST_BACKEND voy_declare_bridge_out(to_backend) voy_declare_bridge_in(from_backend) #else voy_declare_bridge_in(to_backend) voy_declare_bridge_out(from_backend) #endif int main(int argc, char *argv[]) { auto cout = [] (auto&& value) { std::cout << "Out: " << voy_fwd(value) << std::endl; }; using voy::dsl::operator|; auto pipeline = voy_multiprocess - voy::system_cmd("ping"s, "1.1.1.1"s) - | voy::transform([] (std::string value) { + voy::system_cmd("ping"s, "localhost"s) + | voy::transform([] (std::string&& value) { std::transform(value.begin(), value.end(), value.begin(), toupper); return value; }) | voy_bridge(to_backend) - | voy::transform([] (const std::string& value) { - return std::string( - std::find(value.begin(), value.end(), ':'), - value.end()); + | voy::transform([] (std::string&& value) { + const auto pos = value.find_last_of('='); + return std::make_pair(std::move(value), pos); + }) + | voy::transform([] (std::pair&& pair) { + auto [ value, pos ] = pair; + return pos == std::string::npos + ? std::move(value) + : std::string(value.cbegin() + pos + 1, value.cend()); }) | voy_bridge(from_backend) - | voy::transform([] (const std::string& value) { - return " >> " + value + " << "; + | voy::filter([] (const std::string& value) { + return value < "0.045"s; }) | voy::sink{cout}; voy::event_loop::run(); return 0; }