Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Restructured logging code into a new subdirectory.
  • Loading branch information
glynos committed Dec 26, 2012
commit 9c4f0a5bcc344b598269482c73230ae764d8a3bc
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,4 @@ message(STATUS " CPP-NETLIB_DISABLE_LOGGING: ${CPP-NETLIB_DISABLE_LOGGING}\t(

add_subdirectory(uri)
add_subdirectory(message)
add_subdirectory(logging)
11 changes: 11 additions & 0 deletions logging/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Copyright (c) Glyn Matthews 2012.
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file LICENSE_1_0.txt or copy at
# http://www.boost.org/LICENSE_1_0.txt)

add_subdirectory(src)

if(CPP-NETLIB_BUILD_TESTS)
enable_testing()
add_subdirectory(test)
endif(CPP-NETLIB_BUILD_TESTS)
29 changes: 29 additions & 0 deletions logging/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Copyright (c) Glyn Matthews 2012.
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file LICENSE_1_0.txt or copy at
# http://www.boost.org/LICENSE_1_0.txt)

if (${CMAKE_CXX_COMPILER_ID} MATCHES GNU)
if (HAVE_STD11)
set(CPP-NETLIB_CXXFLAGS "-Wall -std=c++11")
elseif (HAVE_STD0X)
set(CPP-NETLIB_CXXFLAGS "-Wall -std=c++0x")
endif()
elseif (${CMAKE_CXX_COMPILER_ID} MATCHES Clang)
CHECK_CXX_COMPILER_FLAG(-std=c++11 HAVE_STD11)
set(CPP-NETLIB_CXXFLAGS "-Wall -std=c++11 -stdlib=libc++")
set(CPP-NETLIB_CXXFLAGS "-Wall -std=c++11 -stdlib=libc++")
endif()

include_directories(${CPP-NETLIB_SOURCE_DIR}/logging/src)
set(CPP-NETLIB_LOGGING_SRCS logging.cpp)
add_library(cppnetlib-logging ${CPP-NETLIB_LOGGING_SRCS})
foreach (src_file ${CPP-NETLIB_LOGGING_SRCS})
if (${CMAKE_CXX_COMPILER_ID} MATCHES GNU)
set_source_files_properties(${src_file}
PROPERTIES COMPILE_FLAGS ${CPP-NETLIB_CXXFLAGS})
elseif (${CMAKE_CXX_COMPILER_ID} MATCHES Clang)
set_source_files_properties(${src_file}
PROPERTIES COMPILE_FLAGS ${CPP-NETLIB_CXXFLAGS})
endif()
endforeach(src_file)
58 changes: 58 additions & 0 deletions logging/src/logging.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright 2011 A. Joel Lamotte <mjklaim@gmail.com>.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#ifdef NETWORK_NO_LIB
#undef NETWORK_NO_LIB
#endif

#include <iostream>
#include <memory>
#include <network/logging/logging.hpp>

namespace network { namespace logging {

const char* log_record::UNKNOWN_FILE_NAME = "unknown";


namespace handler
{
namespace
{
void std_log_handler( const log_record& log )
{
std::cerr << "[network " << log.filename() << ":" << log.line() << "] "
<< log.message() << std::endl;
}
}

log_record_handler get_std_log_handler() { return &std_log_handler; }
log_record_handler get_default_log_handler() { return &std_log_handler; }
}


namespace
{
// the log handler have to manage itself the thread safety on call
static auto current_log_record_handler = std::make_shared<log_record_handler>( &handler::std_log_handler );

}


void set_log_record_handler( log_record_handler handler )
{
current_log_record_handler = std::make_shared<log_record_handler>( handler );
}

void log( const log_record& log )
{
auto log_handler = current_log_record_handler;
if( log_handler )
{
(*log_handler)( log );
}
}


}}
86 changes: 86 additions & 0 deletions logging/src/network/logging/logging.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Copyright (c) 2012 A. Joel Lamotte <mjklaim@gmail.com>.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#ifndef NETWORK_LOGGING_HPP_20121112
#define NETWORK_LOGGING_HPP_20121112

#include <sstream>
#include <functional>

namespace network { namespace logging {

class log_record;

//using log_record_handler = std::function< void (const std::string&) >; // use this when VS can compile it...
typedef std::function< void (const log_record&) > log_record_handler;

void set_log_record_handler( log_record_handler handler );
void log( const log_record& message );

namespace handler
{
log_record_handler get_std_log_handler();
log_record_handler get_default_log_handler();
}

/** Helper to build a log record as a stream. */
class log_record
{
public:
log_record()
: m_filename( UNKNOWN_FILE_NAME )
, m_line(0)
{} // = default;

static const char* UNKNOWN_FILE_NAME;

// Implicit construction from anything serializable to text.
template< typename TypeOfSomething >
log_record( TypeOfSomething&& message )
: m_filename( UNKNOWN_FILE_NAME )
, m_line(0)
{
write( std::forward<TypeOfSomething>(message) );
}

// Construction with recording context informations.
log_record( std::string filename, unsigned long line )
: m_filename( filename )
, m_line( line )
{
}

template< typename TypeOfSomething >
log_record& write( TypeOfSomething&& something )
{
m_text_stream << something;
return *this;
}

template< typename TypeOfSomething >
inline log_record& operator<<( TypeOfSomething&& something )
{
return write( std::forward<TypeOfSomething>(something) );
}

std::string message() const { return m_text_stream.str(); }
const std::string& filename() const { return m_filename; }
unsigned long line() const { return m_line; }

private:

// disable copy
log_record( const log_record& ); // = delete;
log_record& operator=( const log_record& ); // = delete;

std::ostringstream m_text_stream; // stream in which we build the message
std::string m_filename; // = UNKNOWN_FILE_NAME;
unsigned long m_line; // = 0;
};

}}


#endif /* end of include guard: NETWORK_LOGGING_HPP_20121112 */
29 changes: 29 additions & 0 deletions logging/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Copyright (c) A. Joel Lamotte 2012.
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file LICENSE_1_0.txt or copy at
# http://www.boost.org/LICENSE_1_0.txt)

include_directories(${CPP-NETLIB_SOURCE_DIR}/include)
include_directories(${CPP-NETLIB_SOURCE_DIR})

if (Boost_FOUND)
set(
TESTS
logging_log_record
logging_custom_handler
)
foreach (test ${TESTS})
if (${CMAKE_CXX_COMPILER_ID} MATCHES GNU)
set_source_files_properties(${test}.cpp
PROPERTIES COMPILE_FLAGS "-Wall")
endif()
add_executable(cpp-netlib-${test} ${test}.cpp)
add_dependencies(cpp-netlib-${test} cppnetlib-logging)
target_link_libraries(cpp-netlib-${test}
${Boost_LIBRARIES} cppnetlib-logging)
set_target_properties(cpp-netlib-${test}
PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CPP-NETLIB_BINARY_DIR}/tests)
add_test(cpp-netlib-${test}
${CPP-NETLIB_BINARY_DIR}/tests/cpp-netlib-${test})
endforeach (test)
endif()
37 changes: 37 additions & 0 deletions logging/test/logging_custom_handler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) 2012 A. Joel Lamotte <mjklaim@gmail.com>
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#include <string>
#include <sstream>

#define BOOST_TEST_MODULE logging log_record
#include <boost/config/warning_disable.hpp>
#include <boost/test/unit_test.hpp>

#include <network/logging/logging.hpp>

using namespace network::logging;

BOOST_AUTO_TEST_CASE(custom_log_handler_output) {

std::stringstream log_output;
auto custom_log_handler = [&]( const log_record& log )
{
log_output << "[CPPNETLIB]<" << log.filename() << ":" << log.line() << "> "
<< log.message();
};

const auto line_num = 42;
const auto file_name = "somewhere.cpp";
const auto message = "At line " + std::to_string(line_num) + " we check the code.";

set_log_record_handler( custom_log_handler );
log( log_record( file_name, line_num ) << "At line " << line_num << " we check the code." );

const auto result_output = log_output.str();

BOOST_CHECK( !result_output.empty() );
BOOST_CHECK( result_output == "[CPPNETLIB]<somewhere.cpp:42> " + message );
}
78 changes: 78 additions & 0 deletions logging/test/logging_log_record.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright (c) 2012 A. Joel Lamotte <mjklaim@gmail.com>
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#include <string>

#define BOOST_TEST_MODULE logging log_record
#include <boost/config/warning_disable.hpp>
#include <boost/test/unit_test.hpp>

#include <network/logging/logging.hpp>
#define NETWORK_ENABLE_LOGGING
#include <network/detail/debug.hpp>

using namespace network::logging;

BOOST_AUTO_TEST_CASE(default_constructor) {
log_record record;
BOOST_CHECK( record.message() == "" );
BOOST_CHECK( record.filename() == log_record::UNKNOWN_FILE_NAME );
BOOST_CHECK( record.line() == 0 );
}

BOOST_AUTO_TEST_CASE(cstring_constructor) {
const auto message = "This is a test.";
log_record record( message );
BOOST_CHECK( record.message() == message );
BOOST_CHECK( record.filename() == log_record::UNKNOWN_FILE_NAME );
BOOST_CHECK( record.line() == 0 );
}

BOOST_AUTO_TEST_CASE(string_constructor) {
const std::string message("This is a test.");
log_record record( message );
BOOST_CHECK( record.message() == message );
BOOST_CHECK( record.filename() == log_record::UNKNOWN_FILE_NAME );
BOOST_CHECK( record.line() == 0 );
}

BOOST_AUTO_TEST_CASE(int_constructor) {
const auto num = 42;
log_record record( num );
BOOST_CHECK( record.message() == std::to_string( num ) );
BOOST_CHECK( record.filename() == log_record::UNKNOWN_FILE_NAME );
BOOST_CHECK( record.line() == 0 );
}

BOOST_AUTO_TEST_CASE(info_constructor) {
const auto line_num = 42;
const auto file_name = "somewhere.cpp";
log_record record( file_name, line_num );
BOOST_CHECK( record.message() == "" );
BOOST_CHECK( record.filename() == file_name );
BOOST_CHECK( record.line() == line_num );
}

BOOST_AUTO_TEST_CASE(text_stream) {
const auto line_num = 42;
const auto file_name = "somewhere.cpp";
const auto message = "At line " + std::to_string(line_num) + " we check the code.";
log_record record( file_name, line_num );

record << "At line " << line_num << " we check the code.";

BOOST_CHECK( record.message() == message );
BOOST_CHECK( record.filename() == file_name );
BOOST_CHECK( record.line() == line_num );
}

BOOST_AUTO_TEST_CASE(raw_log) {
log( "This is a raw log." );
}

BOOST_AUTO_TEST_CASE(macro_log) {
NETWORK_MESSAGE( "This is a log through the macro." );
NETWORK_MESSAGE( "This is a log through the macro, with a stream! Num=" << 42 << " - OK!" );
}