Skip to content

Commit

Permalink
#21= logger implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
sinev-valentine committed Nov 14, 2018
2 parents 359c6d6 + 847d00a commit fbb6ff2
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 3 deletions.
2 changes: 1 addition & 1 deletion keychain_cmd_app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ if (NOT MSVC)
endif()

SET(BOOST_COMPONENTS)
LIST(APPEND BOOST_COMPONENTS date_time system filesystem program_options signals serialization chrono unit_test_framework locale thread exception iostreams)
LIST(APPEND BOOST_COMPONENTS log date_time system filesystem program_options signals serialization chrono unit_test_framework locale thread exception iostreams)
SET( Boost_USE_STATIC_LIBS ON CACHE STRING "ON or OFF" )

SET(BOOST_ROOT $ENV{BOOST_ROOT})
Expand Down
10 changes: 9 additions & 1 deletion keychain_cmd_app/cmd_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#include "sec_mod.hpp"

#include <boost/program_options.hpp>
#include <keychain_lib/keychain_logger.hpp>

//#include <boost/program_options/options_description.hpp>
//#include <boost/program_options/option.hpp>

Expand All @@ -33,6 +35,7 @@ int cmd_parser::run(int argc, const char* const argv [])
{

const secure_dlg_mod_base* sec_mod;
auto log = logger_singletone::instance();

std::string task_type;
po::options_description desc("Options");
Expand All @@ -54,12 +57,17 @@ int cmd_parser::run(int argc, const char* const argv [])
}

if (task_type == "test_run")
sec_mod = secure_module<sec_mod_dummy>::instance();
{
BOOST_LOG_SEV(log.lg, info) << "secure_module: <sec_mod_dummy>";
sec_mod = secure_module<sec_mod_dummy>::instance();
}
else if (task_type == "")
{
#ifdef LINUX
BOOST_LOG_SEV(log.lg, info) << "secure_module: <sec_mod_linux>";
sec_mod = secure_module<sec_mod_linux>::instance();
#else
BOOST_LOG_SEV(log.lg, info) << "secure_module: <sec_mod_dummy>";
sec_mod = secure_module<sec_mod_dummy>::instance();
#endif
}
Expand Down
16 changes: 16 additions & 0 deletions keychain_cmd_app/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,27 @@
//

#include "cmd_parser.hpp"
#include <keychain_lib/keychain_logger.hpp>


using namespace keychain_app;
namespace attrs = boost::log::attributes;


int main(int argc, char* argv[])
{
auto log = logger_singletone::instance();

BOOST_LOG_SEV(log.lg, info) << "Start";

#ifdef LINUX
BOOST_LOG_SEV(log.lg, info) << "OS: Linux";
#elif defined(_MSC_VER)
BOOST_LOG_SEV(log.lg, info) << "OS: Win";
#else
BOOST_LOG_SEV(log.lg, info) << "OS: unknown";
#endif

cmd_parser cmd_parser_;
return cmd_parser_.run(argc, argv);
}
Expand Down
2 changes: 1 addition & 1 deletion keychain_lib/include/keychain_lib/keychain_commands.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#include <openssl/sha.h>
#include <openssl/evp.h>
#include <eth-crypto/core/sha3_wrap.h>

#include "keychain_logger.hpp"

namespace keychain_app {

Expand Down
23 changes: 23 additions & 0 deletions keychain_lib/include/keychain_lib/keychain_logger.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef KEYCHAIN_LOGGER
#define KEYCHAIN_LOGGER

#include <boost/log/trivial.hpp>

namespace logging = boost::log;
namespace src = boost::log::sources;
namespace sinks = boost::log::sinks;
namespace keywords = boost::log::keywords;
namespace expr = boost::log::expressions;

using namespace logging::trivial;


class logger_singletone
{
public:
logger_singletone();
static const logger_singletone& instance();
src::severity_logger< severity_level > lg;
};

#endif //KEYCHAIN_LOGGER
56 changes: 56 additions & 0 deletions keychain_lib/src/keychain_logger.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include "keychain_logger.hpp"

#include <boost/log/core.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/sources/record_ostream.hpp>
#include <boost/log/sinks/text_ostream_backend.hpp>
#include <boost/core/null_deleter.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <fstream>


logger_singletone::logger_singletone()
{
logging::add_file_log
(
keywords::file_name = "sample_%N.log",
keywords::rotation_size = 10 * 1024 * 1024,
keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0),
keywords::format = "[%TimeStamp%]: %Message%"
);

boost::shared_ptr< logging::core > core = logging::core::get();

logging::add_common_attributes();

// Create a backend and attach a couple of streams to it
boost::shared_ptr< sinks::text_ostream_backend > backend =
boost::make_shared< sinks::text_ostream_backend >();
backend->add_stream(
boost::shared_ptr< std::ostream >(new std::ofstream("sample.log")));
// Enable auto-flushing after each log record written
backend->auto_flush(true);

typedef sinks::synchronous_sink< sinks::text_ostream_backend > sink_t;
boost::shared_ptr< sink_t > sink(new sink_t(backend));

sink->set_formatter(
expr::format("%1% %2% %3%")
% expr::attr< boost::posix_time::ptime >("TimeStamp")
% expr::attr< severity_level >("Severity")
% expr::message
);

core->add_sink(sink);

}

const logger_singletone& logger_singletone::instance()
{
static const logger_singletone instance;
return instance;
}

7 changes: 7 additions & 0 deletions keychain_lib/src/pipeline_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <iostream>
#include <stdexcept>
#include <fc_light/io/json.hpp>
#include <keychain_logger.hpp>

#ifdef _MSC_VER
#include <io.h>
Expand All @@ -29,6 +30,8 @@ pipeline_parser::pipeline_parser(keychain_invoke_f&& keychain_f, int pipein_desc

int pipeline_parser::run()
{
auto log = logger_singletone::instance();

buf_type read_buf(4096, 0x00);
auto ptr_from_it = [&read_buf](buf_iterator &i){
assert(i <= read_buf.end());
Expand All @@ -46,6 +49,8 @@ int pipeline_parser::run()
{

size_t bytes_read = read(m_pipein_desc, ptr_from_it(it), bytes_remaining(it));
BOOST_LOG_SEV(log.lg, info) <<"stdin:" << std::string (ptr_from_it(it), ptr_from_it(it) + bytes_read );

if ( bytes_read == 0 )
break;
if( bytes_read == -1 )
Expand All @@ -64,6 +69,7 @@ int pipeline_parser::run()
std::stringstream strbuf(std::ios_base::out);
strbuf << res << std::endl;
write(m_pipeout_desc, static_cast<const void*>(strbuf.str().c_str()), res.size());
BOOST_LOG_SEV(log.lg, info) <<"stdout:" << res;
}
catch(fc_light::exception& exc)
{
Expand All @@ -72,6 +78,7 @@ int pipeline_parser::run()
std::stringstream strbuf(std::ios_base::out);
strbuf << res << std::endl;
write(m_pipeout_desc, static_cast<const void*>(strbuf.str().c_str()), res.size());
BOOST_LOG_SEV(log.lg, error) <<"stdout:" << res;
}
it = std::copy(buf_range.second, it, read_buf.begin());
continue;//try to parse remaining data
Expand Down

0 comments on commit fbb6ff2

Please sign in to comment.