forked from bcosorg/bcos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommon.cpp
107 lines (90 loc) · 2.71 KB
/
Common.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
This file is part of cpp-ethereum.
cpp-ethereum is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
cpp-ethereum is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file Common.cpp
* @author Gav Wood <i@gavwood.com>
* @date 2014
*/
#include "Common.h"
#include "Exceptions.h"
#include "Log.h"
#include "BuildInfo.h"
using namespace std;
using namespace dev;
namespace dev
{
char const* Version = ETH_PROJECT_VERSION;
char const* Copyright="By BCOS (c) 2017.";
const u256 Invalid256 = ~(u256)0;
void InvariantChecker::checkInvariants(HasInvariants const* _this, char const* _fn, char const* _file, int _line, bool _pre)
{
if (!_this->invariants())
{
LOG(WARNING) << (_pre ? "Pre" : "Post") << "invariant failed in" << _fn << "at" << _file << ":" << _line;
::boost::exception_detail::throw_exception_(FailedInvariant(), _fn, _file, _line);
}
}
struct TimerChannel: public LogChannel { static const char* name(); static const int verbosity = 0; };
#if defined(_WIN32)
const char* TimerChannel::name() { return EthRed " ! "; }
#else
const char* TimerChannel::name() { return EthRed " ⚡ "; }
#endif
TimerHelper::~TimerHelper()
{
auto e = std::chrono::high_resolution_clock::now() - m_t;
if (!m_ms || e > chrono::milliseconds(m_ms))
clog(TimerChannel) << m_id << chrono::duration_cast<chrono::milliseconds>(e).count() << "ms";
}
uint64_t utcTime()
{
struct timeval tv;
gettimeofday(&tv,NULL);
return tv.tv_sec * 1000 + tv.tv_usec / 1000;
}
string inUnits(bigint const& _b, strings const& _units)
{
ostringstream ret;
u256 b;
if (_b < 0)
{
ret << "-";
b = (u256)-_b;
}
else
b = (u256)_b;
u256 biggest = 1;
for (unsigned i = _units.size() - 1; !!i; --i)
biggest *= 1000;
if (b > biggest * 1000)
{
ret << (b / biggest) << " " << _units.back();
return ret.str();
}
ret << setprecision(3);
u256 unit = biggest;
for (auto it = _units.rbegin(); it != _units.rend(); ++it)
{
auto i = *it;
if (i != _units.front() && b >= unit)
{
ret << (double(b / (unit / 1000)) / 1000.0) << " " << i;
return ret.str();
}
else
unit /= 1000;
}
ret << b << " " << _units.front();
return ret.str();
}
}