Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change many assert calls into FC_ASSERT #19

Merged
merged 1 commit into from
Sep 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 26 additions & 26 deletions include/fc/array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,38 @@
#include <fc/crypto/base64.hpp>
#include <fc/variant.hpp>
#include <fc/reflect/reflect.hpp>
#include <fc/exception/exception.hpp>

namespace fc {

/**
* Provides a fixed size array that is easier for templates to specialize
* against or overload than T[N].
* Provides a fixed size array that is easier for templates to specialize
* against or overload than T[N].
*/
template<typename T, size_t N>
class array {
public:
/**
* Checked indexing (when in debug build) that also simplifies dereferencing
* when you have an array<T,N>*.
* when you have an array<T,N>*.
*/
///@{
T& at( size_t pos ) { assert( pos < N); return data[pos]; }
const T& at( size_t pos )const { assert( pos < N); return data[pos]; }
T& at( size_t pos ) { FC_ASSERT( pos < N, "array out-of-bounds" ); return data[pos]; }
const T& at( size_t pos )const { FC_ASSERT( pos < N, "array out-of-bounds" ); return data[pos]; }
///@}

T& operator[]( size_t pos ) { assert( pos < N); return data[pos]; }
const T& operator[]( size_t pos )const { assert( pos < N); return data[pos]; }
T& operator[]( size_t pos ) { FC_ASSERT( pos < N, "array out-of-bounds" ); return data[pos]; }
const T& operator[]( size_t pos )const { FC_ASSERT( pos < N, "array out-of-bounds" ); return data[pos]; }



const T* begin()const { return &data[0]; }
const T* end()const { return &data[N]; }

T* begin() { return &data[0]; }
T* end() { return &data[N]; }

size_t size()const { return N; }

T data[N];
};

Expand All @@ -45,19 +46,19 @@ namespace fc {
array(){ memset( data, 0, sizeof(data) ); }
/**
* Checked indexing (when in debug build) that also simplifies dereferencing
* when you have an array<T,N>*.
* when you have an array<T,N>*.
*/
///@{
T& at( size_t pos ) { assert( pos < N); return data[pos]; }
const T& at( size_t pos )const { assert( pos < N); return data[pos]; }
T& at( size_t pos ) { FC_ASSERT( pos < N, "array out-of-bounds" ); return data[pos]; }
const T& at( size_t pos )const { FC_ASSERT( pos < N, "array out-of-bounds" ); return data[pos]; }
///@}

T* begin() { return &data[0]; }
const T* begin()const { return &data[0]; }
const T* end()const { return &data[N]; }

size_t size()const { return N; }

T data[N];
};

Expand All @@ -70,19 +71,19 @@ namespace fc {
array(){ memset( data, 0, sizeof(data) ); }
/**
* Checked indexing (when in debug build) that also simplifies dereferencing
* when you have an array<T,N>*.
* when you have an array<T,N>*.
*/
///@{
T& at( size_t pos ) { assert( pos < N); return data[pos]; }
const T& at( size_t pos )const { assert( pos < N); return data[pos]; }
T& at( size_t pos ) { FC_ASSERT( pos < N, "array out-of-bounds" ); return data[pos]; }
const T& at( size_t pos )const { FC_ASSERT( pos < N, "array out-of-bounds" ); return data[pos]; }
///@}

T* begin() { return &data[0]; }
const T* begin()const { return &data[0]; }
const T* end()const { return &data[N]; }

size_t size()const { return N; }

T data[N];
};

Expand Down Expand Up @@ -119,14 +120,14 @@ namespace fc {
}


template<typename T,size_t N> struct get_typename< fc::array<T,N> >
{
static const char* name()
{
template<typename T,size_t N> struct get_typename< fc::array<T,N> >
{
static const char* name()
{
static std::string _name = std::string("fc::array<")+std::string(fc::get_typename<T>::name())+","+ fc::to_string(N) + ">";
return _name.c_str();
}
};
}
};
}

#include <unordered_map>
Expand All @@ -142,4 +143,3 @@ namespace std
}
};
}

34 changes: 17 additions & 17 deletions src/log/gelf_appender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include <iostream>
#include <boost/thread/mutex.hpp>

namespace fc
namespace fc
{
namespace detail
{
Expand All @@ -36,7 +36,7 @@ namespace fc
udp_socket gelf_socket;
boost::mutex gelf_log_mutex;

impl(const config& c) :
impl(const config& c) :
cfg(c)
{
}
Expand Down Expand Up @@ -65,7 +65,7 @@ namespace fc
}
if (!my->gelf_endpoint)
{
// couldn't parse as a numeric ip address, try resolving as a DNS name.
// couldn't parse as a numeric ip address, try resolving as a DNS name.
// This can yield, so don't do it in the catch block above
string::size_type colon_pos = my->cfg.endpoint.find(':');
try
Expand Down Expand Up @@ -112,7 +112,7 @@ namespace fc
gelf_message["version"] = "1.1";
gelf_message["host"] = my->cfg.host;
gelf_message["short_message"] = format_string(message.get_format(), message.get_data());

const auto time_ns = context.get_timestamp().time_since_epoch().count();
gelf_message["timestamp"] = time_ns / 1000000.;
gelf_message["_timestamp_ns"] = time_ns;
Expand Down Expand Up @@ -153,16 +153,16 @@ namespace fc
string gelf_message_as_string = json::to_string(gelf_message, json::legacy_generator); // GELF 1.1 specifies unstringified numbers
//unsigned uncompressed_size = gelf_message_as_string.size();
gelf_message_as_string = zlib_compress(gelf_message_as_string);

// graylog2 expects the zlib header to be 0x78 0x9c
// but miniz.c generates 0x78 0x01 (indicating
// but miniz.c generates 0x78 0x01 (indicating
// low compression instead of default compression)
// so change that here
assert(gelf_message_as_string[0] == (char)0x78);
FC_ASSERT(gelf_message_as_string[0] == (char)0x78);
if (gelf_message_as_string[1] == (char)0x01 ||
gelf_message_as_string[1] == (char)0xda)
gelf_message_as_string[1] = (char)0x9c;
assert(gelf_message_as_string[1] == (char)0x9c);
FC_ASSERT(gelf_message_as_string[1] == (char)0x9c);

std::unique_lock<boost::mutex> lock(my->gelf_log_mutex);

Expand All @@ -179,16 +179,16 @@ namespace fc
// no need to split
std::shared_ptr<char> send_buffer(new char[gelf_message_as_string.size()],
[](char* p){ delete[] p; });
memcpy(send_buffer.get(), gelf_message_as_string.c_str(),
memcpy(send_buffer.get(), gelf_message_as_string.c_str(),
gelf_message_as_string.size());

my->gelf_socket.send_to(send_buffer, gelf_message_as_string.size(),
my->gelf_socket.send_to(send_buffer, gelf_message_as_string.size(),
*my->gelf_endpoint);
}
else
{
// split the message
// we need to generate an 8-byte ID for this message.
// we need to generate an 8-byte ID for this message.
// city hash should do
uint64_t message_id = city_hash64(gelf_message_as_string.c_str(), gelf_message_as_string.size());
const unsigned header_length = 2 /* magic */ + 8 /* msg id */ + 1 /* seq */ + 1 /* count */;
Expand All @@ -198,10 +198,10 @@ namespace fc
unsigned number_of_packets_sent = 0;
while (bytes_sent < gelf_message_as_string.size())
{
unsigned bytes_to_send = std::min((unsigned)gelf_message_as_string.size() - bytes_sent,
unsigned bytes_to_send = std::min((unsigned)gelf_message_as_string.size() - bytes_sent,
body_length);
std::shared_ptr<char> send_buffer(new char[max_payload_size],

std::shared_ptr<char> send_buffer(new char[max_payload_size],
[](char* p){ delete[] p; });
char* ptr = send_buffer.get();
// magic number for chunked message
Expand All @@ -214,14 +214,14 @@ namespace fc

*(unsigned char*)(ptr++) = number_of_packets_sent;
*(unsigned char*)(ptr++) = total_number_of_packets;
memcpy(ptr, gelf_message_as_string.c_str() + bytes_sent,
memcpy(ptr, gelf_message_as_string.c_str() + bytes_sent,
bytes_to_send);
my->gelf_socket.send_to(send_buffer, header_length + bytes_to_send,
my->gelf_socket.send_to(send_buffer, header_length + bytes_to_send,
*my->gelf_endpoint);
++number_of_packets_sent;
bytes_sent += bytes_to_send;
}
assert(number_of_packets_sent == total_number_of_packets);
FC_ASSERT(number_of_packets_sent == total_number_of_packets);
}
}
} // fc
46 changes: 23 additions & 23 deletions src/network/ntp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace fc
namespace detail {
using boost::fibers::future;

class ntp_impl
class ntp_impl
{
public:
/** vector < host, port > */
Expand All @@ -39,13 +39,13 @@ namespace fc
_request_interval_sec( 60*60 /* 1 hr */),
_retry_failed_request_interval_sec(60 * 5),
_last_ntp_delta_microseconds(0)
{
{
_last_ntp_delta_initialized = false;
_ntp_hosts.push_back( std::make_pair( "pool.ntp.org",123 ) );
}
}

~ntp_impl()
{
~ntp_impl()
{
_sock.close();
if( _scheduled_request_time ) {
_scheduled_request_time->cancel();
Expand Down Expand Up @@ -77,10 +77,10 @@ namespace fc

void request_now()
{
assert(_ntp_thread.is_current());
FC_ASSERT(_ntp_thread.is_current());
for( auto item : _ntp_hosts )
{
try
try
{
wlog( "resolving... ${r}", ("r", item) );
auto eps = resolve( item.first, item.second );
Expand All @@ -95,22 +95,22 @@ namespace fc
_sock.send_to(send_buffer, packet_to_send.size(), ep);
break;
}
}
}
catch (const fc::canceled_exception&)
{
throw;
}
// this could fail to resolve but we want to go on to other hosts..
catch ( const fc::exception& e )
{
elog( "${e}", ("e",e.to_detail_string() ) );
elog( "${e}", ("e",e.to_detail_string() ) );
}
}
} // request_now

// started for first time in ntp() constructor, canceled in ~ntp() destructor
// this task gets invoked every _retry_failed_request_interval_sec (currently 5 min), and if
// _request_interval_sec (currently 1 hour) has passed since the last successful update,
// _request_interval_sec (currently 1 hour) has passed since the last successful update,
// it sends a new request
void request_time_task()
{
Expand All @@ -124,7 +124,7 @@ namespace fc

void read_loop()
{
assert(_ntp_thread.is_current());
FC_ASSERT(_ntp_thread.is_current());

uint32_t receive_buffer_size = sizeof(uint64_t) * 1024;
std::shared_ptr<char> receive_buffer(new char[receive_buffer_size], [](char* p){ delete[] p; });
Expand All @@ -134,7 +134,7 @@ namespace fc
// so instead we start the loop after making our first request
_sock.open();
_request_time_task_done = fc::async( [&](){ request_time_task(); } );

while( true )
{
fc::ip::endpoint from;
Expand All @@ -143,20 +143,20 @@ namespace fc
_sock.receive_from( receive_buffer, receive_buffer_size, from );
// wlog("received ntp reply from ${from}",("from",from) );
} FC_RETHROW_EXCEPTIONS(error, "Error reading from NTP socket");

fc::time_point receive_time = fc::time_point::now();
fc::time_point origin_time = ntp_timestamp_to_fc_time_point(recv_buf[3]);
fc::time_point server_receive_time = ntp_timestamp_to_fc_time_point(recv_buf[4]);
fc::time_point server_transmit_time = ntp_timestamp_to_fc_time_point(recv_buf[5]);

fc::microseconds offset(((server_receive_time - origin_time) +
(server_transmit_time - receive_time)).count() / 2);
fc::microseconds round_trip_delay((receive_time - origin_time) -
(server_transmit_time - server_receive_time));
//wlog("origin_time = ${origin_time}, server_receive_time = ${server_receive_time}, server_transmit_time = ${server_transmit_time}, receive_time = ${receive_time}",
// ("origin_time", origin_time)("server_receive_time", server_receive_time)("server_transmit_time", server_transmit_time)("receive_time", receive_time));
// wlog("ntp offset: ${offset}, round_trip_delay ${delay}", ("offset", offset)("delay", round_trip_delay));

//if the reply we just received has occurred more than a second after our last time request (it was more than a second ago since our last request)
if( round_trip_delay > fc::microseconds(300000) )
{
Expand All @@ -174,23 +174,23 @@ namespace fc
wlog("ntp_delta_time updated to ${delta_time} us", ("delta_time",ntp_delta_time) );
}
else
elog( "NTP time and local time vary by more than a day! ntp:${ntp_time} local:${local}",
elog( "NTP time and local time vary by more than a day! ntp:${ntp_time} local:${local}",
("ntp_time", receive_time + offset)("local", fc::time_point::now()) );
}
}
wlog("exiting ntp read_loop");
} //end read_loop()

void reschedule() {
if( _scheduled_request_time )
if( _scheduled_request_time )
_scheduled_request_time->cancel();

_scheduled_request_time = _ntp_thread.schedule(
[&](){
request_now();
reschedule();
},
fc::time_point::now() + fc::seconds(_request_interval_sec) );
_scheduled_request_time = _ntp_thread.schedule(
[&](){
request_now();
reschedule();
},
fc::time_point::now() + fc::seconds(_request_interval_sec) );
}
}; //ntp_impl

Expand Down
Loading