twproxy.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //
  2. // client.cpp
  3. // ~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2016 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #include <boost/asio.hpp>
  11. #include <boost/log/attributes.hpp>
  12. #include <boost/log/core.hpp>
  13. #include <boost/log/utility/setup/common_attributes.hpp>
  14. #include <boost/log/utility/setup/console.hpp>
  15. // #include <boost/log/sinks/text_file_backend.hpp>
  16. #include <boost/log/trivial.hpp>
  17. #include <boost/log/utility/setup/file.hpp>
  18. #include <cstdlib>
  19. #include <fstream>
  20. #include <iomanip>
  21. #include <iostream>
  22. #include <map>
  23. #include <string>
  24. #include "config.h"
  25. #include "logging.h"
  26. #include "session.h"
  27. // #define BOOST_ASIO_ENABLE_HANDLER_TRACKING
  28. // std::map<std::string, std::string> CONFIG;
  29. // #include <boost/date_time/posix_time/posix_time_types.hpp>
  30. #include <boost/log/expressions.hpp>
  31. #include <boost/log/support/date_time.hpp>
  32. /*
  33. boost log linking -
  34. undefined reference to `void boost::log::v2_mt_posix::init_from_stream
  35. https://github.com/boostorg/log/issues/46
  36. */
  37. void init_logging(void) {
  38. // because TimeStamp is missing by default.
  39. boost::log::add_common_attributes();
  40. // "proxy-%Y-%m-%d.log"
  41. std::string log_filename = "proxy.log";
  42. if (CONFIG["log_file"]) log_filename = CONFIG["log_file"].as<std::string>();
  43. // = from_config("log_file", "proxy.log");
  44. // "%I:%M:%S.%f %p"
  45. std::string log_timeformat = "%H:%M:%S.%f";
  46. if (CONFIG["log_timeformat"])
  47. log_timeformat = CONFIG["log_timeformat"].as<std::string>();
  48. // from_config("log_timeformat", "%H:%M:%S.%f");
  49. bool log_autoflush = false;
  50. if (CONFIG["log_autoflush"]) {
  51. log_autoflush = CONFIG["log_autoflush"].as<int>() == 1;
  52. }
  53. int log_level = 2;
  54. if (CONFIG["log_level"]) {
  55. log_level = CONFIG["log_level"].as<int>();
  56. }
  57. bool console = false;
  58. if (CONFIG["log_console"]) {
  59. console = CONFIG["log_console"].as<int>() == 1;
  60. }
  61. std::cout << "Logging to: ";
  62. if (console) std::cout << "console + ";
  63. std::cout << log_filename << " level: " << log_level
  64. << " flush: " << log_autoflush << " format: " << log_timeformat
  65. << std::endl;
  66. if (console)
  67. boost::log::add_console_log(
  68. std::clog, boost::log::keywords::auto_flush = log_autoflush,
  69. boost::log::keywords::format =
  70. (boost::log::expressions::stream
  71. << boost::log::expressions::format_date_time<
  72. boost::posix_time::ptime>("TimeStamp", log_timeformat)
  73. << " " << std::setw(8) << boost::log::trivial::severity << " "
  74. << boost::log::expressions::smessage));
  75. boost::log::add_file_log(
  76. boost::log::keywords::file_name = log_filename,
  77. // This appends to the logfile (instead of overwrite)
  78. boost::log::keywords::open_mode = std::ios_base::out | std::ios_base::app,
  79. boost::log::keywords::auto_flush = log_autoflush,
  80. // boost::log::keywords::format = "[%TimeStamp%] %Severity% : %Message%"
  81. boost::log::keywords::format =
  82. (boost::log::expressions::stream
  83. << boost::log::expressions::format_date_time<
  84. boost::posix_time::ptime>("TimeStamp", log_timeformat)
  85. << " " << std::setw(8) << boost::log::trivial::severity << " "
  86. << boost::log::expressions::smessage));
  87. auto core = boost::log::core::get();
  88. core->set_filter(boost::log::trivial::severity >= log_level);
  89. }
  90. int main(int argc, char *argv[]) {
  91. if (argc != 2) {
  92. std::cerr << "Usage: twproxy <filename>" << std::endl;
  93. return EXIT_FAILURE;
  94. }
  95. CONFIG = YAML::LoadFile(argv[1]);
  96. init_logging();
  97. bool config_ok = true;
  98. // for (const char *key : {"server", "host", "port"}) {
  99. for (auto key : {"server", "host", "port", "basename"}) {
  100. if (!CONFIG[key]) {
  101. config_ok = false;
  102. std::cout << "Config file missing: " << key << std::endl;
  103. BUGZ_LOG(fatal) << "Config file missing: " << key;
  104. }
  105. // The leaks are reported here, because this is the first usage of the
  106. // BOOST_LOG_TRIVIAL()! Comment these out, and the leaks are reported at the
  107. // next logging usage instance.
  108. std::string value = CONFIG[key].as<std::string>();
  109. BUGZ_LOG(info) << "Config: " << key << " : " << value;
  110. }
  111. if (!config_ok) return EXIT_FAILURE;
  112. int port = CONFIG["server"].as<int>();
  113. // int port = 9999; // 2002;
  114. try {
  115. bool telnet = false;
  116. if (CONFIG["server_telnet"]) {
  117. telnet = true;
  118. BUGZ_LOG(fatal) << "Connect to server via TELNET";
  119. }
  120. boost::asio::io_service io_service;
  121. boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::tcp::v4(),
  122. port); // std::atoi(argv[i]));
  123. // connect to the game server
  124. std::string host = CONFIG["host"].as<std::string>();
  125. std::string port = CONFIG["port"].as<std::string>();
  126. BUGZ_LOG(fatal) << "host: " << host << " port: " << port;
  127. Server serve(io_service, endpoint, host, port, telnet);
  128. io_service.run();
  129. } catch (std::exception &e) {
  130. BUGZ_LOG(fatal) << "Exception: " << e.what();
  131. std::cerr << "Exception: " << e.what() << "\n";
  132. }
  133. return EXIT_SUCCESS;
  134. }