Common.cpp 2.6 KB

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