Skip to content

Commit

Permalink
Merge pull request #167 from adshares/merge-esc
Browse files Browse the repository at this point in the history
Merge esc
  • Loading branch information
m-pilarczyk authored Sep 20, 2018
2 parents 549d297 + cd09216 commit 8acab18
Show file tree
Hide file tree
Showing 23 changed files with 394 additions and 105 deletions.
13 changes: 12 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [1.0.1] - 2018-09-20
### Changed
- New log format
- Log level can be changed without recompilation

### Fixed
- Vote counting during full synchronization of the node
- Now secret provided from terminal is not printed on screen
- Fixed network timeout when using client in a interactive mode

## [1.0.0] - 2018-08-24
### Changed
- Genesis file for the launch
Expand Down Expand Up @@ -38,8 +48,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Update default parameters
- Creating a dev version with reference to the last tag

[Unreleased]: https://github.com/adshares/ads/compare/v1.0.0...HEAD
[Unreleased]: https://github.com/adshares/ads/compare/v1.0.1...HEAD

[1.0.1]: https://github.com/adshares/ads/compare/v1.0.0...v1.0.1
[1.0.0]: https://github.com/adshares/ads/compare/v0.0.6...v1.0.0
[0.0.6]: https://github.com/adshares/ads/compare/v0.0.5...v0.0.6
[0.0.5]: https://github.com/adshares/ads/compare/v0.0.4...v0.0.5
Expand Down
3 changes: 3 additions & 0 deletions src/common/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
SET(SOURCE
networkclient.cpp
user.cpp
logging.cpp
)

SET(HEADERS
Expand All @@ -10,6 +11,7 @@ SET(HEADERS
message.hpp
user.hpp
networkclient.h
logging.h
)

include_directories("../../external")
Expand All @@ -18,6 +20,7 @@ add_subdirectory(abstraction)
add_subdirectory(helper)
add_subdirectory(command)
add_subdirectory(parser)
add_subdirectory(utils)


add_library(common ${SOURCE} ${HEADERS})
Expand Down
12 changes: 1 addition & 11 deletions src/common/default.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <boost/thread/recursive_mutex.hpp>

#include "default.h"
#include "logging.h"

#define SERVER_DBL 0x1 /* closed node */
#define SERVER_VIP 0x2 /* VIP node */
Expand Down Expand Up @@ -208,16 +209,5 @@ typedef struct hash_cmp {
#define SHUTDOWN_AND_RETURN() {std::raise(SIGQUIT);return;}
#define SHUTDOWN() {std::raise(SIGQUIT);}
#define RESTART_AND_RETURN() {std::raise(SIGUSR1);return;}
#ifndef ELOG
#define ELOG(...) {extern boost::recursive_mutex flog;extern FILE* stdlog;flog.lock();uint32_t logtime=time(NULL);fprintf(stderr, "[%u] ", logtime);fprintf(stderr,__VA_ARGS__);if(stdlog){fprintf(stdlog,"%08X ",logtime);fprintf(stdlog,__VA_ARGS__);}flog.unlock();}
#endif
#ifndef NDEBUG
//consider printing thread id
#ifndef DLOG
#define DLOG(...) {extern boost::recursive_mutex flog;extern FILE* stdlog;flog.lock();uint32_t logtime=time(NULL);fprintf(stderr, "[%u] ", logtime);fprintf(stderr,__VA_ARGS__);if(stdlog){fprintf(stdlog,"%08X ",logtime);fprintf(stdlog,__VA_ARGS__);}flog.unlock();}
#endif
#else
#define DLOG(...)
#endif

#endif // DEFAULT_HPP
133 changes: 133 additions & 0 deletions src/common/logging.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
#include "logging.h"

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <string>
#include <fstream>
#include <algorithm>
#include <time.h>
#include <mutex>

#include "helper/blocks.h"

namespace logging {

static struct
{
std::recursive_mutex file_mtx;
FILE *log_file;
LoggingLevel level;
LoggingSource source;
} settings;


static const char *level_names[] =
{
"TRACE", "DEBUG", "INFO", "WARN", "ERROR", "FATAL"
};

void update_log_level(const char* iniFilePath)
{
const std::string KEYWORD = "log_level";
const int length = KEYWORD.length();

std::ifstream ini_file(iniFilePath);
std::string line;
while(std::getline(ini_file, line))
{
std::size_t pos = line.find(KEYWORD);
if (pos != std::string::npos)
{
line.erase(std::remove_if(line.begin(), line.end(), isspace), line.end());
if (line[0] != ';' && pos == 0)
{
try {
int log_level = std::stoi(line.substr(length+1));
settings.level = (LoggingLevel)log_level;
} catch (std::exception) {}
break;
}
}
}
}

static void lock(void)
{
settings.file_mtx.lock();
}


static void unlock(void)
{
settings.file_mtx.unlock();
}

void set_level(LoggingLevel level)
{
settings.level = level;
}

void set_log_file(FILE* file)
{
settings.log_file = file;
}

void set_log_source(LoggingSource source)
{
settings.source = source;
}

void log_log(LoggingLevel level, const char* function, int line, const char *fmt, ...)
{
if (level < settings.level) {
return;
}

lock();

time_t t = time(NULL);
va_list args;

if (settings.source & LoggingSource::LOG_CONSOLE)
{
fprintf(stderr, "[%ld] [%s:%d] %-5s: ", t, function, line, level_names[level]);
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
fflush(stderr);
}

if (settings.source & LoggingSource::LOG_FILE)
{
fprintf(settings.log_file, "[%ld] [%s:%d] %-5s: ", t, function, line, level_names[level]);
va_start(args, fmt);
vfprintf(settings.log_file, fmt, args);
va_end(args);
fflush(settings.log_file);
}

unlock();
}

void change_log_file(uint32_t timestamp)
{
lock();

char filename[32];;
uint32_t ntime=time(NULL);
Helper::FileName::getName(filename, timestamp, "log.txt");

fprintf(settings.log_file,"END: %08X\n",ntime);
fclose(settings.log_file);

settings.log_file = fopen(filename, "a");
fprintf(settings.log_file,"START: %08X\n",ntime);

update_log_level("options.cfg");

unlock();
}

}
46 changes: 46 additions & 0 deletions src/common/logging.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#ifndef LOGGING_H
#define LOGGING_H

#include <stdio.h>
#include <stdarg.h>
#include <stdint.h>
#include <boost/current_function.hpp>

#ifndef __LINE__
#define __LINE__ 0
#endif /*__LINE__*/

#define TLOG(...) logging::log_log(logging::LOG_TRACE, BOOST_CURRENT_FUNCTION, __LINE__, __VA_ARGS__)
#define DLOG(...) logging::log_log(logging::LOG_DEBUG, BOOST_CURRENT_FUNCTION, __LINE__, __VA_ARGS__)
#define ILOG(...) logging::log_log(logging::LOG_INFO, BOOST_CURRENT_FUNCTION, __LINE__, __VA_ARGS__)
#define WLOG(...) logging::log_log(logging::LOG_WARN, BOOST_CURRENT_FUNCTION, __LINE__, __VA_ARGS__)
#define ELOG(...) logging::log_log(logging::LOG_ERROR, BOOST_CURRENT_FUNCTION, __LINE__, __VA_ARGS__)
#define FLOG(...) logging::log_log(logging::LOG_FATAL, BOOST_CURRENT_FUNCTION, __LINE__, __VA_ARGS__)

namespace logging {

enum LoggingLevel {
LOG_TRACE = 0,
LOG_DEBUG,
LOG_INFO,
LOG_WARN,
LOG_ERROR,
LOG_FATAL
};

enum LoggingSource {
LOG_NONE = 0,
LOG_CONSOLE,
LOG_FILE,
LOG_ALL
};

void set_level(LoggingLevel level);
void set_log_source(LoggingSource source);
void change_log_file(uint32_t timestamp);
void set_log_file(FILE* file);
void log_log(LoggingLevel level, const char *function, int line, const char *fmt, ...);

}

#endif // LOGGING_H
22 changes: 19 additions & 3 deletions src/common/networkclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,31 @@
#include "helper/ascii.h"
#include "helper/socket.h"

#define NETCL_SOCK_TIMEOUT 10
#define NETCL_SOCK_TIMEOUT 4
#define NETCL_SOCK_IDLE 15
#define NETCL_SOCK_MAXTRY 3

NetworkClient::NetworkClient(const std::string& address, const std::string& port, bool pretty):
m_query(address, port.c_str()),
m_resolver(m_ioService),
m_prettyJson(pretty)
m_prettyJson(pretty),
m_timeout(0)
{
}

NetworkClient::~NetworkClient() {
}

void NetworkClient::setTimeout()
{
m_timeout = time(NULL) + NETCL_SOCK_TIMEOUT;
}

bool NetworkClient::isConnected() {
if(m_timeout && time(NULL) >= m_timeout) {
m_socket->close();
m_connected = false;
}
return m_connected;
}

Expand All @@ -41,7 +51,6 @@ bool NetworkClient::connect() {
if(m_socket) {
m_socket->connect(*connectpoint++, error);
Helper::setSocketTimeout(m_socket, NETCL_SOCK_TIMEOUT, NETCL_SOCK_IDLE, NETCL_SOCK_MAXTRY);
Helper::setSocketNoDelay(m_socket, true);
}
}

Expand All @@ -56,6 +65,7 @@ bool NetworkClient::connect() {
}

m_connected = true;
setTimeout();
}

return true;
Expand Down Expand Up @@ -90,6 +100,7 @@ bool NetworkClient::sendData(std::vector<uint8_t> buff) {
if(m_connected && m_socket) {
auto size = buff.size();
if( boost::asio::write(*m_socket.get(), boost::asio::buffer(std::move(buff))) == size ) {
setTimeout();
return true;
}
}
Expand All @@ -106,6 +117,7 @@ bool NetworkClient::sendData(uint8_t* buff, int size) {
try {
if(m_connected && m_socket) {
if( boost::asio::write(*m_socket.get(), boost::asio::buffer(buff, size)) == (std::size_t)size ) {
setTimeout();
return true;
}
}
Expand All @@ -122,6 +134,7 @@ bool NetworkClient::readData(std::vector<uint8_t>& buff) {
if(m_connected && m_socket) {
auto size = buff.size();
if( boost::asio::read(*m_socket.get(), boost::asio::buffer(buff)) == size ) {
setTimeout();
return true;
}
}
Expand All @@ -137,6 +150,7 @@ bool NetworkClient::readData(uint8_t* buff, int size) {
try {
if(m_connected && m_socket) {
if( boost::asio::read(*m_socket.get(), boost::asio::buffer(buff, size)) == (size_t)size ) {
setTimeout();
return true;
}
}
Expand All @@ -152,6 +166,7 @@ bool NetworkClient::readData(char* buff, int size) {
try {
if(m_connected && m_socket) {
if( boost::asio::read(*m_socket.get(), boost::asio::buffer(buff, size)) == (size_t)size ) {
setTimeout();
return true;
}
}
Expand All @@ -167,6 +182,7 @@ bool NetworkClient::readData(int32_t *buff, int size) {
try {
if(m_connected && m_socket) {
if (boost::asio::read(*m_socket.get(), boost::asio::buffer(buff, size)) == (size_t)size) {
setTimeout();
return true;
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/common/networkclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,17 @@ class NetworkClient : public INetworkClient {
virtual bool readData(int32_t* buff, int size) override;
virtual bool isConnected() override;


private:
boost::asio::io_service m_ioService;
boost::asio::ip::tcp::resolver::query m_query;
boost::asio::ip::tcp::resolver m_resolver;
std::unique_ptr<boost::asio::ip::tcp::socket> m_socket;
std::atomic<bool> m_connected{false};
bool m_prettyJson;
int32_t m_timeout;

void setTimeout();
};

#endif // NETWORKCLIENT_H
Loading

0 comments on commit 8acab18

Please sign in to comment.