twproxy.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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.contains("log_file")) log_filename = json_str(CONFIG["log_file"]);
  43. // "%I:%M:%S.%f %p"
  44. std::string log_timeformat = "%H:%M:%S.%f";
  45. if (CONFIG.contains("log_timeformat"))
  46. log_timeformat = json_str(CONFIG["log_timeformat"]);
  47. // from_config("log_timeformat", "%H:%M:%S.%f");
  48. bool log_autoflush = false;
  49. if (CONFIG.contains("log_autoflush")) {
  50. log_autoflush = json_bool(CONFIG["log_autoflush"]);
  51. }
  52. int log_level = 2;
  53. if (CONFIG.contains("log_level")) {
  54. log_level = json_int(CONFIG["log_level"]);
  55. }
  56. bool console = false;
  57. if (CONFIG.contains("log_console")) {
  58. console = json_bool(CONFIG["log_console"]);
  59. }
  60. std::cout << "Logging to: ";
  61. if (console) std::cout << "console + ";
  62. std::cout << log_filename << " level: " << log_level
  63. << " flush: " << log_autoflush << " format: " << log_timeformat
  64. << std::endl;
  65. if (console)
  66. boost::log::add_console_log(
  67. std::clog, boost::log::keywords::auto_flush = log_autoflush,
  68. boost::log::keywords::format =
  69. (boost::log::expressions::stream
  70. << boost::log::expressions::format_date_time<
  71. boost::posix_time::ptime>("TimeStamp", log_timeformat)
  72. << " " << std::setw(8) << boost::log::trivial::severity << " "
  73. << boost::log::expressions::smessage));
  74. boost::log::add_file_log(
  75. boost::log::keywords::file_name = log_filename,
  76. // This appends to the logfile (instead of overwrite)
  77. boost::log::keywords::open_mode = std::ios_base::out | std::ios_base::app,
  78. boost::log::keywords::auto_flush = log_autoflush,
  79. // boost::log::keywords::format = "[%TimeStamp%] %Severity% : %Message%"
  80. boost::log::keywords::format =
  81. (boost::log::expressions::stream
  82. << boost::log::expressions::format_date_time<
  83. boost::posix_time::ptime>("TimeStamp", log_timeformat)
  84. << " " << std::setw(8) << boost::log::trivial::severity << " "
  85. << boost::log::expressions::smessage));
  86. auto core = boost::log::core::get();
  87. core->set_filter(boost::log::trivial::severity >= log_level);
  88. }
  89. int main(int argc, char *argv[]) {
  90. if (argc != 2) {
  91. std::cerr << "Usage: twproxy <filename>" << std::endl;
  92. return EXIT_FAILURE;
  93. }
  94. {
  95. std::ifstream fin(argv[1]);
  96. CONFIG = json::parse(fin); // YAML::LoadFile(argv[1]);
  97. }
  98. init_logging();
  99. bool config_ok = true;
  100. // for (const char *key : {"server", "host", "port"}) {
  101. for (auto key : {"server", "host", "port", "basename"}) {
  102. if (!CONFIG.contains(key)) {
  103. config_ok = false;
  104. std::cout << "Config file missing: " << key << std::endl;
  105. BUGZ_LOG(fatal) << "Config file missing: " << key;
  106. }
  107. // The leaks are reported here, because this is the first usage of the
  108. // BOOST_LOG_TRIVIAL()! Comment these out, and the leaks are reported at the
  109. // next logging usage instance.
  110. std::string value = json_str(CONFIG[key]);
  111. BUGZ_LOG(info) << "Config: " << key << " : " << value;
  112. }
  113. if (!config_ok) return EXIT_FAILURE;
  114. int listen_port = json_int(CONFIG["server"]);
  115. #define BURN
  116. #ifndef BURN
  117. try {
  118. #endif
  119. bool telnet = false;
  120. if (CONFIG.contains("server_telnet")) {
  121. telnet = true;
  122. BUGZ_LOG(fatal) << "Connect to server via TELNET";
  123. }
  124. boost::asio::io_service io_service;
  125. boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::tcp::v4(),
  126. listen_port);
  127. // connect to the game server
  128. std::string host = json_str(CONFIG["host"]);
  129. std::string port = json_str(CONFIG["port"]);
  130. BUGZ_LOG(fatal) << "host: " << host << " port: " << port;
  131. Server serve(io_service, endpoint, host, port, telnet);
  132. io_service.run();
  133. #ifndef BURN
  134. } catch (std::exception &e) {
  135. BUGZ_LOG(fatal) << "Exception: " << e.what();
  136. std::cerr << "Exception: " << e.what() << "\n";
  137. }
  138. #endif
  139. return EXIT_SUCCESS;
  140. }