Electroneum
wallet_args.cpp
Go to the documentation of this file.
1 // Copyrights(c) 2017-2021, The Electroneum Project
2 // Copyrights(c) 2014-2019, The Monero Project
3 //
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without modification, are
7 // permitted provided that the following conditions are met:
8 //
9 // 1. Redistributions of source code must retain the above copyright notice, this list of
10 // conditions and the following disclaimer.
11 //
12 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
13 // of conditions and the following disclaimer in the documentation and/or other
14 // materials provided with the distribution.
15 //
16 // 3. Neither the name of the copyright holder nor the names of its contributors may be
17 // used to endorse or promote products derived from this software without specific
18 // prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
21 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
28 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "wallet/wallet_args.h"
30 
31 #include <boost/filesystem/path.hpp>
32 #include <boost/filesystem/operations.hpp>
33 #include <boost/format.hpp>
34 #include "common/i18n.h"
35 #include "common/util.h"
36 #include "misc_log_ex.h"
37 #include "string_tools.h"
38 #include "version.h"
39 
40 #if defined(WIN32)
41 #include <crtdbg.h>
42 #endif
43 
44 #undef ELECTRONEUM_DEFAULT_LOG_CATEGORY
45 #define ELECTRONEUM_DEFAULT_LOG_CATEGORY "wallet.wallet2"
46 
47 // workaround for a suspected bug in pthread/kernel on MacOS X
48 #ifdef __APPLE__
49 #define DEFAULT_MAX_CONCURRENCY 1
50 #else
51 #define DEFAULT_MAX_CONCURRENCY 0
52 #endif
53 
54 namespace
55 {
56  class Print
57  {
58  public:
59  Print(const std::function<void(const std::string&, bool)> &p, bool em = false): print(p), emphasis(em) {}
60  ~Print() { print(ss.str(), emphasis); }
61  template<typename T> std::ostream &operator<<(const T &t) { ss << t; return ss; }
62  private:
63  const std::function<void(const std::string&, bool)> &print;
64  std::stringstream ss;
65  bool emphasis;
66  };
67 }
68 
69 namespace wallet_args
70 {
71  // Create on-demand to prevent static initialization order fiasco issues.
73  {
74  return {"generate-from-json", wallet_args::tr("Generate wallet from JSON format file"), ""};
75  }
77  {
78  return {"wallet-file", wallet_args::tr("Use wallet <arg>"), ""};
79  }
80 
81  const char* tr(const char* str)
82  {
83  return i18n_translate(str, "wallet_args");
84  }
85 
86  std::pair<boost::optional<boost::program_options::variables_map>, bool> main(
87  int argc, char** argv,
88  const char* const usage,
89  const char* const notice,
90  boost::program_options::options_description desc_params,
91  const boost::program_options::positional_options_description& positional_options,
92  const std::function<void(const std::string&, bool)> &print,
93  const char *default_log_name,
94  bool log_to_console)
95 
96  {
97  namespace bf = boost::filesystem;
98  namespace po = boost::program_options;
99 #ifdef WIN32
100  _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
101 #endif
102 
103  const command_line::arg_descriptor<std::string> arg_log_level = {"log-level", "0-4 or categories", ""};
104  const command_line::arg_descriptor<std::size_t> arg_max_log_file_size = {"max-log-file-size", "Specify maximum log file size [B]", MAX_LOG_FILE_SIZE};
105  const command_line::arg_descriptor<std::size_t> arg_max_log_files = {"max-log-files", "Specify maximum number of rotated log files to be saved (no limit by setting to 0)", MAX_LOG_FILES};
106  const command_line::arg_descriptor<uint32_t> arg_max_concurrency = {"max-concurrency", wallet_args::tr("Max number of threads to use for a parallel job"), DEFAULT_MAX_CONCURRENCY};
107  const command_line::arg_descriptor<std::string> arg_log_file = {"log-file", wallet_args::tr("Specify log file"), ""};
108  const command_line::arg_descriptor<std::string> arg_config_file = {"config-file", wallet_args::tr("Config file"), "", true};
109 
110 
113 #ifdef NDEBUG
115 #endif
117 
119 
120  po::options_description desc_general(wallet_args::tr("General options"));
123 
124  command_line::add_arg(desc_params, arg_log_file);
125  command_line::add_arg(desc_params, arg_log_level);
130 
131  i18n_set_language("translations", "electroneum", lang);
132 
133  po::options_description desc_all;
134  desc_all.add(desc_general).add(desc_params);
135  po::variables_map vm;
136  bool should_terminate = false;
137  bool r = command_line::handle_error_helper(desc_all, [&]()
138  {
139  auto parser = po::command_line_parser(argc, argv).options(desc_all).positional(positional_options);
140  po::store(parser.run(), vm);
141 
143  {
144  Print(print) << "Electroneum '" << ELECTRONEUM_RELEASE_NAME << "' (v" << ELECTRONEUM_VERSION_FULL << ")" << ENDL;
145  Print(print) << wallet_args::tr("This is the command line electroneum wallet. It needs to connect to a electroneum\n"
146  "daemon to work correctly.") << ENDL;
147  Print(print) << wallet_args::tr("Usage:") << ENDL << " " << usage;
148  Print(print) << desc_all;
149  should_terminate = true;
150  return true;
151  }
153  {
154  Print(print) << "Electroneum '" << ELECTRONEUM_RELEASE_NAME << "' (v" << ELECTRONEUM_VERSION_FULL << ")";
155  should_terminate = true;
156  return true;
157  }
158 
160  {
162  bf::path config_path(config);
163  boost::system::error_code ec;
164  if (bf::exists(config_path, ec))
165  {
166  po::store(po::parse_config_file<char>(config_path.string<std::string>().c_str(), desc_params), vm);
167  }
168  else
169  {
170  MERROR(wallet_args::tr("Can't find config file ") << config);
171  return false;
172  }
173  }
174 
175  po::notify(vm);
176  return true;
177  });
178  if (!r)
179  return {boost::none, true};
180 
181  if (should_terminate)
182  return {std::move(vm), should_terminate};
183 
184  std::string log_path;
186  log_path = command_line::get_arg(vm, arg_log_file);
187  else
188  log_path = mlog_get_default_log_path(default_log_name);
191  {
193  }
194  else if (!log_to_console)
195  {
197  }
198 
200  {
201  Print(print) << "Electroneum '" << ELECTRONEUM_RELEASE_NAME << "' (v" << ELECTRONEUM_VERSION_FULL << ")" << ENDL;
202  Print(print) << wallet_args::tr("This is the command line electroneum wallet. It needs to connect to a electroneum\n"
203  "daemon to work correctly.") << ENDL;
204  Print(print) << wallet_args::tr("Usage:") << ENDL << " " << usage;
205  Print(print) << desc_all;
206  return {boost::none, true};
207  }
209  {
210  Print(print) << "Electroneum '" << ELECTRONEUM_RELEASE_NAME << "' (v" << ELECTRONEUM_VERSION_FULL << ")";
211  return {boost::none, true};
212  }
213 
214  if (notice)
215  Print(print) << notice << ENDL;
216 
219 
220  Print(print) << "Electroneum '" << ELECTRONEUM_RELEASE_NAME << "' (v" << ELECTRONEUM_VERSION_FULL << ")";
221 
223  MINFO("Setting log level = " << command_line::get_arg(vm, arg_log_level));
224  else
225  MINFO("Setting log levels = " << getenv("ELECTRONEUM_LOGS"));
226  MINFO(wallet_args::tr("Logging to: ") << log_path);
227 
228  Print(print) << boost::format(wallet_args::tr("Logging to %s")) % log_path;
229 
230  const ssize_t lockable_memory = tools::get_lockable_memory();
231  if (lockable_memory >= 0 && lockable_memory < 256 * 4096) // 256 pages -> at least 256 secret keys and other such small/medium objects
232  Print(print) << tr("WARNING: You may not have a high enough lockable memory limit")
233 #ifdef ELPP_OS_UNIX
234  << ", " << tr("see ulimit -l")
235 #endif
236  ;
237 
238  return {std::move(vm), should_terminate};
239  }
240 }
const char *const ELECTRONEUM_RELEASE_NAME
#define MERROR(x)
Definition: misc_log_ex.h:73
const uint32_t T[512]
command_line::arg_descriptor< std::string > arg_wallet_file()
Definition: wallet_args.cpp:76
#define MINFO(x)
Definition: misc_log_ex.h:75
bool set_module_name_and_folder(const std::string &path_to_process_)
Definition: string_tools.h:249
::std::string string
Definition: gtest-port.h:1097
void mlog_set_log(const char *log)
Definition: mlog.cpp:288
std::string mlog_get_default_log_path(const char *default_filename)
Definition: mlog.cpp:72
void set_max_concurrency(unsigned n)
Definition: util.cpp:857
void mlog_configure(const std::string &filename_base, bool console, const std::size_t max_log_file_size=MAX_LOG_FILE_SIZE, const std::size_t max_log_files=MAX_LOG_FILES)
Definition: mlog.cpp:148
bool disable_core_dumps()
Definition: util.cpp:748
std::string i18n_get_language()
Definition: i18n.cpp:54
const arg_descriptor< bool > arg_help
const command_line::arg_descriptor< std::string, false, true, 2 > arg_log_file
const char * tr(const char *str)
Definition: wallet_args.cpp:81
const char * i18n_translate(const char *s, const std::string &context)
Definition: i18n.cpp:323
const command_line::arg_descriptor< std::size_t > arg_max_log_file_size
void mlog_set_categories(const char *categories)
Definition: mlog.cpp:238
const command_line::arg_descriptor< unsigned > arg_max_concurrency
bool on_startup()
Definition: util.cpp:778
#define MAX_LOG_FILE_SIZE
Definition: misc_log_ex.h:38
const char *const ELECTRONEUM_VERSION_FULL
bool handle_error_helper(const boost::program_options::options_description &desc, F parser)
Definition: command_line.h:237
std::enable_if<!std::is_same< T, bool >::value, bool >::type has_arg(const boost::program_options::variables_map &vm, const arg_descriptor< T, required, dependent, NUM_DEPS > &arg)
Definition: command_line.h:258
int i18n_set_language(const char *directory, const char *base, std::string language)
Definition: i18n.cpp:133
const command_line::arg_descriptor< std::string > arg_log_level
std::stringstream & operator<<(std::stringstream &out, const std::wstring &ws)
command_line::arg_descriptor< std::string > arg_generate_from_json()
Definition: wallet_args.cpp:72
#define DEFAULT_MAX_CONCURRENCY
Definition: wallet_args.cpp:51
void add_arg(boost::program_options::options_description &description, const arg_descriptor< T, required, dependent, NUM_DEPS > &arg, bool unique=true)
Definition: command_line.h:188
const command_line::arg_descriptor< std::string, false, true, 2 > arg_config_file
std::pair< boost::optional< boost::program_options::variables_map >, bool > main(int argc, char **argv, const char *const usage, const char *const notice, boost::program_options::options_description desc_params, const boost::program_options::positional_options_description &positional_options, const std::function< void(const std::string &, bool)> &print, const char *default_log_name, bool log_to_console)
Definition: wallet_args.cpp:86
const T & move(const T &t)
Definition: gtest-port.h:1317
void set_strict_default_file_permissions(bool strict)
Definition: util.cpp:803
#define ENDL
Definition: misc_log_ex.h:149
#define MAX_LOG_FILES
Definition: misc_log_ex.h:39
T get_arg(const boost::program_options::variables_map &vm, const arg_descriptor< T, false, true > &arg)
Definition: command_line.h:271
ssize_t get_lockable_memory()
Definition: util.cpp:763
const arg_descriptor< bool > arg_version
const command_line::arg_descriptor< std::size_t > arg_max_log_files
bool is_arg_defaulted(const boost::program_options::variables_map &vm, const arg_descriptor< T, required, dependent, NUM_DEPS > &arg)
Definition: command_line.h:265