Skip to content
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
32 changes: 19 additions & 13 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,23 @@
cmake_minimum_required(VERSION 2.8)
project(CPP-NETLIB)

option(BUILD_SHARED_LIBS "Build cpp-netlib as shared libraries." OFF)
option(BUILD_TESTS "Build the unit tests." ON)
option(BUILD_EXAMPLES "Build the examples using cpp-netlib." ON)
option( CPP-NETLIB_BUILD_SHARED_LIBS "Build cpp-netlib as shared libraries." OFF )
option( CPP-NETLIB_BUILD_TESTS "Build the unit tests." ON )
option( CPP-NETLIB_BUILD_EXAMPLES "Build the examples using cpp-netlib." ON )
option( CPP-NETLIB_ALWAYS_LOGGING "Allow cpp-netlib to log debug messages even in non-debug mode." OFF )
option( CPP-NETLIB_DISABLE_LOGGING "Disable logging definitely, no logging code will be generated or compiled." OFF )


set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR})
find_package( ICU )

if(BUILD_SHARED_LIBS)
if(CPP-NETLIB_BUILD_SHARED_LIBS)
set(Boost_USE_STATIC_LIBS OFF)
else()
set(Boost_USE_STATIC_LIBS ON)
endif()
set(Boost_USE_MULTITHREADED ON)
if(BUILD_TESTS)
if(CPP-NETLIB_BUILD_TESTS)
set(Boost_COMPONENTS unit_test_framework system regex date_time thread chrono filesystem program_options )
else()
set(Boost_COMPONENTS system regex date_time thread chrono filesystem program_options )
Expand Down Expand Up @@ -56,6 +59,7 @@ elseif(${CMAKE_CXX_COMPILER_ID} MATCHES Clang)
message("C++ Flags: ${CMAKE_CXX_FLAGS} link flags: ${CMAKE_CXX_LINK_FLAGS}")
endif()


if (Boost_FOUND)
if (MSVC)
add_definitions(-D_SCL_SECURE_NO_WARNINGS)
Expand All @@ -64,27 +68,29 @@ if (Boost_FOUND)
add_definitions(-D_WIN32_WINNT=0x0501)
endif(WIN32)
include_directories(${Boost_INCLUDE_DIRS})
if(BUILD_TESTS)
if(CPP-NETLIB_BUILD_TESTS)
enable_testing()
endif()
add_subdirectory(libs/network/src)
if(BUILD_TESTS)
if(CPP-NETLIB_BUILD_TESTS)
enable_testing()
add_subdirectory(libs/network/test)
if (NOT MSVC)
add_subdirectory(libs/mime/test)
endif(NOT MSVC)
endif()
if(BUILD_EXAMPLES)
if(CPP-NETLIB_BUILD_EXAMPLES)
add_subdirectory(libs/network/example)
endif()
endif(Boost_FOUND)

if(BUILD_TESTS)
if(CPP-NETLIB_BUILD_TESTS)
enable_testing()
endif()

message(STATUS "Options selected:")
message(STATUS " BUILD_SHARED_LIBS: ${BUILD_SHARED_LIBS}\t(Build cpp-netlib as shared libraries: OFF, ON)")
message(STATUS " BUILD_TESTS: ${BUILD_TESTS}\t(Build the unit tests: ON, OFF)")
message(STATUS " BUILD_EXAMPLES: ${BUILD_EXAMPLES}\t(Build the examples using cpp-netlib: ON, OFF)")
message(STATUS "CPP-NETLIB Options selected:")
message(STATUS " CPP-NETLIB_BUILD_SHARED_LIBS: ${CPP-NETLIB_BUILD_SHARED_LIBS}\t(Build cpp-netlib as shared libraries: OFF, ON)")
message(STATUS " CPP-NETLIB_BUILD_TESTS: ${CPP-NETLIB_BUILD_TESTS}\t(Build the unit tests: ON, OFF)")
message(STATUS " CPP-NETLIB_BUILD_EXAMPLES: ${CPP-NETLIB_BUILD_EXAMPLES}\t(Build the examples using cpp-netlib: ON, OFF)")
message(STATUS " CPP-NETLIB_ALWAYS_LOGGING: ${CPP-NETLIB_ALWAYS_LOGGING}\t(Allow cpp-netlib to log debug messages even in non-debug mode: ON, OFF)")
message(STATUS " CPP-NETLIB_DISABLE_LOGGING: ${CPP-NETLIB_DISABLE_LOGGING}\t(Disable logging definitely, no logging code will be generated or compiled: ON, OFF)")
18 changes: 15 additions & 3 deletions include/network/detail/debug.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

// (c) Copyright 2011 Dean Michael Berris.
// Copyright 2012 Google, Inc.
// Copyright 2012 A. Joel Lamotte
// 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)
Expand All @@ -11,16 +12,27 @@
print out network-related errors through standard error. This is only
useful when NETWORK_DEBUG is turned on. Otherwise the macro amounts to a
no-op.

The user can force the logging to be enabled by defining NETWORK_ENABLE_LOGGING.
*/
#ifdef NETWORK_DEBUG
# include <iostream>
#if defined(NETWORK_DEBUG) && !defined(NETWORK_ENABLE_LOGGING)
# define NETWORK_ENABLE_LOGGING
#endif

#ifdef NETWORK_ENABLE_LOGGING

# include <network/logging/logging.hpp>
# ifndef NETWORK_MESSAGE
# define NETWORK_MESSAGE(msg) std::cerr << "[DEBUG " << __FILE__ << ':' << __LINE__ << "]: " << msg << std::endl;
# define NETWORK_MESSAGE(msg) network::logging::log_record( __FILE__, __LINE__ ) << msg;
# endif

#else

# ifndef NETWORK_MESSAGE
# define NETWORK_MESSAGE(msg)
# endif

#endif


#endif /* end of include guard: NETWORK_DEBUG_HPP_20110410 */
90 changes: 90 additions & 0 deletions include/network/logging/logging.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// 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 )
{
}

~log_record()
{
log( *this );
}

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

std::string message() const { return m_text_stream.str(); }
const std::string& filename() const { return m_filename; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't need to return a const ref, it can better return a value IMO.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure? Even if it's only to read the value? I know move semantics makes things cheaper but they keep saying it's still not free. I'll do it if you confirm you prefer by value, I have no problem with that. :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think returning a value is necessary here. It's harmless to return a reference at this point.

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;
};

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

}}


#endif /* end of include guard: NETWORK_LOGGING_HPP_20121112 */
27 changes: 27 additions & 0 deletions libs/network/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright (c) Glyn Matthews 2011, 2012.
# Copyright 2011 Dean Michael Berris (dberris@google.com)
# Copyright 2012 A. Joel Lamotte (mjklaim@gmail.com)
# Copyright 2011 Google, Inc.
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file LICENSE_1_0.txt or copy at
Expand All @@ -20,6 +21,28 @@ if (${CMAKE_CXX_COMPILER_ID} MATCHES GNU)
endif()
endif()

if( CPP-NETLIB_ALWAYS_LOGGING )
add_definitions( /D NETWORK_ENABLE_LOGGING )
endif()

if( NOT CPP-NETLIB_DISABLE_LOGGING )
set( CPP-NETLIB_LOGGING_SRCS
logging/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})
endif()
endforeach(src_file)

# this library name is defined only if we created the target
# if not then it will be empty
set( CPP-NETLIB_LOGGING_LIB cppnetlib-logging )

endif()

set(CPP-NETLIB_URI_SRCS uri/uri.cpp uri/schemes.cpp uri/normalize.cpp)
add_library(cppnetlib-uri ${CPP-NETLIB_URI_SRCS})
foreach (src_file ${CPP-NETLIB_URI_SRCS})
Expand Down Expand Up @@ -99,6 +122,7 @@ set(CPP-NETLIB_HTTP_SERVER_SRCS
)
add_library(cppnetlib-http-server ${CPP-NETLIB_HTTP_SERVER_SRCS})
add_dependencies(cppnetlib-http-server
${CPP-NETLIB_LOGGING_LIB}
cppnetlib-constants
cppnetlib-uri
cppnetlib-message
Expand All @@ -111,6 +135,7 @@ add_dependencies(cppnetlib-http-server
)
target_link_libraries(cppnetlib-http-server
${Boost_LIBRARIES}
${CPP-NETLIB_LOGGING_LIB}
cppnetlib-constants
cppnetlib-uri
cppnetlib-message
Expand Down Expand Up @@ -151,6 +176,7 @@ set(CPP-NETLIB_HTTP_CLIENT_SRCS
http/client.cpp)
add_library(cppnetlib-http-client ${CPP-NETLIB_HTTP_CLIENT_SRCS})
add_dependencies(cppnetlib-http-client
${CPP-NETLIB_LOGGING_LIB}
cppnetlib-constants
cppnetlib-uri
cppnetlib-message
Expand All @@ -162,6 +188,7 @@ add_dependencies(cppnetlib-http-client
)
target_link_libraries(cppnetlib-http-client
${Boost_LIBRARIES}
${CPP-NETLIB_LOGGING_LIB}
cppnetlib-constants
cppnetlib-uri
cppnetlib-message
Expand Down
58 changes: 58 additions & 0 deletions libs/network/src/logging/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 );
}
}


}}
5 changes: 3 additions & 2 deletions libs/network/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@

include_directories(${CPP-NETLIB_SOURCE_DIR}/include)

if(BUILD_SHARED_LIBS)
add_definitions(-DBUILD_SHARED_LIBS)
if(CPP-NETLIB_BUILD_SHARED_LIBS)
add_definitions(-DCPP-NETLIB_BUILD_SHARED_LIBS)
endif()

add_subdirectory(logging)
add_subdirectory(uri)
add_subdirectory(http)

Expand Down
4 changes: 2 additions & 2 deletions libs/network/test/http/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ if (Boost_FOUND)
cppnetlib-http-message-wrappers
cppnetlib-uri
cppnetlib-constants
)
)
target_link_libraries(cpp-netlib-http-${test}
${Boost_LIBRARIES}
${ICU_LIBRARIES} ${ICU_I18N_LIBRARIES}
${CMAKE_THREAD_LIBS_INIT}
${CMAKE_THREAD_LIBS_INIT}
cppnetlib-message
cppnetlib-message-wrappers
cppnetlib-http-message
Expand Down
29 changes: 29 additions & 0 deletions libs/network/test/logging/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()
Loading