From 8369f226124bac93dc9b57ef710610d6512c5c03 Mon Sep 17 00:00:00 2001 From: Chris Love Date: Sat, 4 Sep 2021 15:30:11 -0700 Subject: [PATCH] Add spdlog_tcp --- CMakeLists.txt | 1 + spdlog_tcp/CMakeLists.txt | 14 +++ spdlog_tcp/tcp.cpp | 215 ++++++++++++++++++++++++++++++++++++++ spdlog_udp/udp.cpp | 2 +- 4 files changed, 231 insertions(+), 1 deletion(-) create mode 100644 spdlog_tcp/CMakeLists.txt create mode 100644 spdlog_tcp/tcp.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index f4b3d1b..7ed2ee1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,5 +33,6 @@ find_package(Threads) add_subdirectory(spdlog_console) add_subdirectory(spdlog_file) add_subdirectory(spdlog_udp) +add_subdirectory(spdlog_tcp) add_subdirectory(boost_console) add_subdirectory(boost_file) diff --git a/spdlog_tcp/CMakeLists.txt b/spdlog_tcp/CMakeLists.txt new file mode 100644 index 0000000..bfb92e1 --- /dev/null +++ b/spdlog_tcp/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.1) + +project(spdlog_tcp) + +include_directories(/usr/local/include /usr/local/include/spdlog ) + +set (spdlog_tcp_SRC + tcp.cpp +) + +add_executable( spdlog_tcp ${spdlog_tcp_SRC}) + +target_link_libraries( spdlog_tcp spdlog::spdlog Threads::Threads ) + diff --git a/spdlog_tcp/tcp.cpp b/spdlog_tcp/tcp.cpp new file mode 100644 index 0000000..a36fcbd --- /dev/null +++ b/spdlog_tcp/tcp.cpp @@ -0,0 +1,215 @@ +#include +#include +#include +#include +#include +#include +#include +#ifndef WIN32 +#include +#endif + +class TraceMethod +{ + public: + TraceMethod(const char *name, + std::shared_ptr logger) : m_name(name), + m_logger(logger) + { + m_logger->trace("{}() Enter", m_name); + } + + ~TraceMethod() + { + m_logger->trace("{}() Leave", m_name); + } + + private: + std::string m_name; + std::shared_ptr m_logger; +}; + +class AppClass +{ + public: + AppClass(const std::string &name) : m_thread([this]() { this->thread(); }), +m_name(name), + m_logger(spdlog::get("logger")) + { + } + + ~AppClass() + { + { + std::lock_guard l(m_mutex); + m_stop = true; + } + m_cond.notify_one(); + m_thread.join(); + } + + void thread() + { + m_logger->trace("Thread {} starting", m_name); + while (this->wait_for(std::chrono::seconds(5))) + { + anotherMethod(); + m_logger->info("Thread {} doing something", m_name); + } + m_logger->trace("Thread {} exiting", m_name); + } + + void anotherMethod() + { + TraceMethod tm("anotherMethod", m_logger); + } + + template + bool wait_for(Duration duration) + { + std::unique_lock l(m_mutex); + return !m_cond.wait_for(l, duration, [this]() { return m_stop; }); + } + + private: + std::condition_variable m_cond; + std::mutex m_mutex; + std::thread m_thread; + bool m_stop = false; + std::string m_name; + std::shared_ptr m_logger; +}; + +void usage() +{ + std::cerr << "Usage:\n" + << "spdlog_tcp [-l ][-i ][-p ]\n"; +} + +#ifdef WIN32 +#include +#include + +int opterr = 1, /* if error message should be printed */ + optind = 1, /* index into parent argv vector */ + optopt, /* character checked for validity */ + optreset; /* reset getopt */ +char *optarg; /* argument associated with option */ + +#define BADCH (int)'?' +#define BADARG (int)':' +#define EMSG "" + +/* +* getopt -- +* Parse argc/argv argument vector. +*/ +int + getopt(int nargc, char * const nargv[], const char *ostr) +{ + static char *place = EMSG; /* option letter processing */ + const char *oli; /* option letter list index */ + + if (optreset || !*place) { /* update scanning pointer */ + optreset = 0; + if (optind >= nargc || *(place = nargv[optind]) != '-') { + place = EMSG; + return (-1); + } + if (place[1] && *++place == '-') { /* found "--" */ + ++optind; + place = EMSG; + return (-1); + } + } /* option letter okay? */ + if ((optopt = (int)*place++) == (int)':' || + !(oli = strchr(ostr, optopt))) { + /* + * if the user didn't specify '-' as an option, + * assume it means -1. + */ + if (optopt == (int)'-') + return (-1); + if (!*place) + ++optind; + if (opterr && *ostr != ':') + (void)printf("illegal option -- %c\n", optopt); + return (BADCH); + } + if (*++oli != ':') { /* don't need argument */ + optarg = NULL; + if (!*place) + ++optind; + } + else { /* need an argument */ + if (*place) /* no white space */ + optarg = place; + else if (nargc <= ++optind) { /* no arg */ + place = EMSG; + if (*ostr == ':') + return (BADARG); + if (opterr) + (void)printf("option requires an argument -- %c\n", optopt); + return (BADCH); + } + else /* white space */ + optarg = nargv[optind]; + place = EMSG; + ++optind; + } + return (optopt); /* dump back option letter */ +} +#endif + +int main(int argc, char **argv) +{ + int logLevel = spdlog::level::trace; + std::string ipAddr {"127.0.0.1"}; + uint16_t port = 9000; + int c; + while ((c = getopt(argc, argv, "l:i:p:?")) != EOF) + { + switch (c) + { + case 'l': + logLevel = std::stoi(optarg); + break; + case 'i': + ipAddr = optarg; + break; + case 'p': + port = static_cast(std::stoi(optarg)); + break; + case '?': + usage(); + exit(1); + break; + default: + break; + } + } + + // Create the main logger named "logger" and configure it + spdlog::sinks::tcp_sink_config cfg(ipAddr,port); + auto sink = std::make_shared(cfg); + auto logger = std::make_shared("logger",sink); + spdlog::register_logger(logger); + // Log format: + // 2018-10-08 21:08:31.633|020288|I|Thread Worker thread 3 doing something + logger->set_pattern("%Y-%m-%d %H:%M:%S.%e|%t|%L|%v"); + // Set the log level for filtering + spdlog::set_level(static_cast(logLevel)); + + logger->info("Begin spdlog_udp logging"); + + std::vector> threads; + for (int i = 0; i < 10; i++) + { + threads.emplace_back(std::make_unique(std::string("Worker thread ") + std::to_string(i))); + } + + logger->info("Main thread sleeping for 2 minutes"); + std::this_thread::sleep_for(std::chrono::minutes(2)); + + logger->info("End spdlog_file logging"); +} diff --git a/spdlog_udp/udp.cpp b/spdlog_udp/udp.cpp index 8e75fd3..0861ec7 100644 --- a/spdlog_udp/udp.cpp +++ b/spdlog_udp/udp.cpp @@ -83,7 +83,7 @@ m_name(name), void usage() { std::cerr << "Usage:\n" - << "spdlog_console [-l ][-i ][-p ]\n"; + << "spdlog_udp [-l ][-i ][-p ]\n"; } #ifdef WIN32