Last active
July 7, 2018 03:46
-
-
Save Taymindis/4e6d71e017a4979e5263bc1454a03cb9 to your computer and use it in GitHub Desktop.
stream route handler with spdlog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <stream_rt_handler.h> | |
/*https://github.com/gabime/spdlog*/ | |
#include "spdlog/spdlog.h" | |
/***Compile by ***/ | |
/** cd build **/ | |
/** g++ -DSPDLOG_FMT_PRINTF -std=c++11 ../sample_with_spdlog.cpp -lsrh -pthread **/ | |
static std::shared_ptr<spdlog::logger> file_logger = 0; | |
void read_handler(srh_request_t *req); | |
void init_logger_in_instance(void *arg); | |
void init_logger_in_instance(void *arg) { | |
fprintf(stderr, "%s\n", "start instance"); | |
fprintf(stderr, "%s\n", "init logging"); | |
auto rotating = std::make_shared<spdlog::sinks::rotating_file_sink_mt> ("mylog.log", 1024 * 1024, 5); | |
file_logger = spdlog::create_async("my_logger", rotating, 8192, | |
spdlog::async_overflow_policy::block_retry, nullptr, std::chrono::milliseconds{10}/*flush interval*/, nullptr); | |
} | |
void read_handler(srh_request_t *req) { | |
file_logger->info("%.*s", (int) (req->in_buff->end - req->in_buff->start), req->in_buff->start); | |
// file_logger->flush(); | |
srh_write_output_buffer_l(req, req->in_buff->start, (req->in_buff->end - req->in_buff->start)); | |
} | |
int main(int, char* []) { | |
try { | |
srh_instance_t * instance = srh_create_routing_instance(24, init_logger_in_instance, NULL); | |
srh_add_udp_fd(instance, 515, read_handler, 1024); | |
srh_add_tcp_fd(instance, 3232, read_handler, 64); | |
srh_start(instance); | |
} | |
catch (const spdlog::spdlog_ex& ex) | |
{ | |
std::cout << "Log initialization failed: " << ex.what() << std::endl; | |
} | |
spdlog::drop_all(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment