123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- #include <boost/asio.hpp>
- #include <boost/log/attributes.hpp>
- #include <boost/log/core.hpp>
- #include <boost/log/utility/setup/common_attributes.hpp>
- #include <boost/log/trivial.hpp>
- #include <boost/log/utility/setup/file.hpp>
- #include <cstdlib>
- #include <fstream>
- #include <iostream>
- #include <map>
- #include <string>
- #include "config.h"
- #include "session.h"
- #include <boost/date_time/posix_time/posix_time_types.hpp>
- #include <boost/log/expressions.hpp>
- void init_logging(void) {
-
- boost::log::add_common_attributes();
- auto fmtScope = boost::log::expressions::format_named_scope(
- "Scope", boost::log::keywords::format = "%n(%f:%l)",
-
- boost::log::keywords::depth = 2);
- auto fmtTimeStamp =
- boost::log::expressions::format_date_time<boost::posix_time::ptime>(
- "TimeStamp", "%Y-%m-%d %H:%M:%S.%f");
- boost::log::add_file_log(
-
- boost::log::keywords::file_name = "proxy-%Y-%m-%d.log",
-
- boost::log::keywords::open_mode = std::ios_base::out | std::ios_base::app,
- boost::log::keywords::format =
- (boost::log::expressions::stream <<
-
-
-
- fmtTimeStamp <<
-
- ": <" << boost::log::trivial::severity << " [" << fmtScope << "] "
- << "> "
- << boost::log::expressions::smessage)
-
-
- );
- auto core = boost::log::core::get();
-
-
- core->set_filter(boost::log::trivial::severity >= boost::log::trivial::info);
-
-
-
-
- core->add_global_attribute("Scope", boost::log::attributes::named_scope());
- }
- int main(int argc, char *argv[]) {
-
- if (argc != 2) {
- std::cerr << "Usage: twproxy <filename>" << std::endl;
- return EXIT_FAILURE;
- }
- init_logging();
- std::map<std::string, std::string> config = yaml_parse(argv[1]);
-
- bool config_ok = true;
-
- for (auto key : {"server", "host", "port"}) {
- auto pos = config.find(key);
- if (pos == config.end()) {
- config_ok = false;
- std::cout << "Config file missing: " << key << std::endl;
- BOOST_LOG_TRIVIAL(fatal) << "Config file missing: " << key;
- }
- BOOST_LOG_TRIVIAL(info) << "Config: " << key << " : " << config[key];
- }
- if (!config_ok)
- return 2;
- int port = std::stoi(config["server"]);
-
- try {
- boost::asio::io_service io_service;
- boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::tcp::v4(),
- port);
-
- server s(io_service, endpoint, config["host"], config["port"]);
-
- io_service.run();
- } catch (std::exception &e) {
- std::cerr << "Exception: " << e.what() << "\n";
- }
- return EXIT_SUCCESS;
- }
|