Ethereum  PoC-8
The C++ Implementation of Ethereum
Common.cpp
Go to the documentation of this file.
1 /*
2  This file is part of cpp-ethereum.
3 
4  cpp-ethereum is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  cpp-ethereum is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
16 */
17 
18 #include "Common.h"
19 #include "Exceptions.h"
20 #include "Log.h"
21 
22 #if defined(_WIN32)
23 #include <windows.h>
24 #endif
25 
26 #include <aleth/buildinfo.h>
27 
28 using namespace std;
29 
30 namespace dev
31 {
32 char const* Version = aleth_get_buildinfo()->project_version;
34 std::string const EmptyString;
35 
36 void InvariantChecker::checkInvariants(HasInvariants const* _this, char const* _fn, char const* _file, int _line, bool _pre)
37 {
38  if (!_this->invariants())
39  {
40  cwarn << (_pre ? "Pre" : "Post") << "invariant failed in" << _fn << "at" << _file << ":" << _line;
41  ::boost::exception_detail::throw_exception_(FailedInvariant(), _fn, _file, _line);
42  }
43 }
44 
45 TimerHelper::~TimerHelper()
46 {
47  auto e = std::chrono::high_resolution_clock::now() - m_t;
48  if (!m_ms || e > chrono::milliseconds(m_ms))
49  clog(VerbosityDebug, "timer")
50  << m_id << " " << chrono::duration_cast<chrono::milliseconds>(e).count() << " ms";
51 }
52 
53 int64_t utcTime()
54 {
55  // TODO: Fix if possible to not use time(0) and merge only after testing in all platforms
56  // time_t t = time(0);
57  // return mktime(gmtime(&t));
58  return time(0);
59 }
60 
61 string inUnits(bigint const& _b, strings const& _units)
62 {
63  ostringstream ret;
64  u256 b;
65  if (_b < 0)
66  {
67  ret << "-";
68  b = (u256)-_b;
69  }
70  else
71  b = (u256)_b;
72 
73  u256 biggest = 1;
74  for (unsigned i = _units.size() - 1; !!i; --i)
75  biggest *= 1000;
76 
77  if (b > biggest * 1000)
78  {
79  ret << (b / biggest) << " " << _units.back();
80  return ret.str();
81  }
82  ret << setprecision(3);
83 
84  u256 unit = biggest;
85  for (auto it = _units.rbegin(); it != _units.rend(); ++it)
86  {
87  auto i = *it;
88  if (i != _units.front() && b >= unit)
89  {
90  ret << (double(b / (unit / 1000)) / 1000.0) << " " << i;
91  return ret.str();
92  }
93  else
94  unit /= 1000;
95  }
96  ret << b << " " << _units.front();
97  return ret.str();
98 }
99 
100 /*
101 The equivalent of setlocale(LC_ALL, ā€œCā€) is called before any user code is run.
102 If the user has an invalid environment setting then it is possible for the call
103 to set locale to fail, so there are only two possible actions, the first is to
104 throw a runtime exception and cause the program to quit (default behaviour),
105 or the second is to modify the environment to something sensible (least
106 surprising behaviour).
107 
108 The follow code produces the least surprising behaviour. It will use the user
109 specified default locale if it is valid, and if not then it will modify the
110 environment the process is running in to use a sensible default. This also means
111 that users do not need to install language packs for their OS.
112 */
114 {
115 #if __unix__
116  if (!std::setlocale(LC_ALL, ""))
117  {
118  setenv("LC_ALL", "C", 1);
119  }
120 #endif
121 
122 #if defined(_WIN32)
123  // Change the code page from the default OEM code page (437) so that UTF-8 characters are
124  // displayed correctly in the console.
125  SetConsoleOutputCP(CP_UTF8);
126 #endif
127 }
128 
129 bool ExitHandler::s_shouldExit = false;
130 
131 bool isTrue(std::string const& _m)
132 {
133  return _m == "on" || _m == "yes" || _m == "true" || _m == "1";
134 }
135 
136 bool isFalse(std::string const& _m)
137 {
138  return _m == "off" || _m == "no" || _m == "false" || _m == "0";
139 }
140 
141 } // namespace dev
Inheritable for classes that have invariants.
Definition: Common.h:210
Definition: Address.cpp:20
bool isTrue(std::string const &_m)
Definition: Common.cpp:131
int64_t utcTime()
Get the current time in seconds since the epoch in UTC.
Definition: Common.cpp:53
std::vector< std::string > strings
Definition: Common.h:143
Definition: FixedHash.h:390
boost::multiprecision::number< boost::multiprecision::cpp_int_backend<> > bigint
Definition: Common.h:118
bytes const NullBytes
Definition: Common.cpp:33
string inUnits(bigint const &_b, strings const &_units)
Converts given int to a string and appends one of a series of units according to its size...
Definition: Common.cpp:61
std::string const EmptyString
Definition: Common.cpp:34
std::vector< byte > bytes
Definition: Common.h:72
virtual bool invariants() const =0
Reimplement to specify the invariants.
#define cwarn
void setDefaultOrCLocale()
Definition: Common.cpp:113
bool isFalse(std::string const &_m)
Definition: Common.cpp:136
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void > > u256
Definition: Common.h:121
#define clog(SEVERITY, CHANNEL)
char const * Version
Definition: Common.cpp:32