Skip to content

Commit d14bfe2

Browse files
committed
Merge branch 'official-master'
Conflicts: CMakeLists.txt
2 parents 5117105 + b2be660 commit d14bfe2

File tree

10 files changed

+351
-20
lines changed

10 files changed

+351
-20
lines changed

CMakeLists.txt

+18-13
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,23 @@
77
cmake_minimum_required(VERSION 2.8)
88
project(CPP-NETLIB)
99

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

1417
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR})
1518
find_package( ICU )
1619

17-
if(BUILD_SHARED_LIBS)
20+
if(CPP-NETLIB_BUILD_SHARED_LIBS)
1821
set(Boost_USE_STATIC_LIBS OFF)
1922
else()
2023
set(Boost_USE_STATIC_LIBS ON)
2124
endif()
2225
set(Boost_USE_MULTITHREADED ON)
23-
if(BUILD_TESTS)
26+
if(CPP-NETLIB_BUILD_TESTS)
2427
set(Boost_COMPONENTS unit_test_framework system regex date_time thread chrono filesystem program_options )
2528
else()
2629
set(Boost_COMPONENTS system regex date_time thread chrono filesystem program_options )
@@ -75,27 +78,29 @@ if (Boost_FOUND)
7578
add_definitions(-D_WIN32_WINNT=0x0501)
7679
endif(WIN32)
7780
include_directories(${Boost_INCLUDE_DIRS})
78-
if(BUILD_TESTS)
81+
if(CPP-NETLIB_BUILD_TESTS)
7982
enable_testing()
8083
endif()
8184
add_subdirectory(libs/network/src)
82-
if(BUILD_TESTS)
85+
if(CPP-NETLIB_BUILD_TESTS)
8386
enable_testing()
8487
add_subdirectory(libs/network/test)
8588
if (NOT MSVC)
8689
add_subdirectory(libs/mime/test)
8790
endif(NOT MSVC)
8891
endif()
89-
if(BUILD_EXAMPLES)
92+
if(CPP-NETLIB_BUILD_EXAMPLES)
9093
add_subdirectory(libs/network/example)
9194
endif()
9295
endif(Boost_FOUND)
9396

94-
if(BUILD_TESTS)
97+
if(CPP-NETLIB_BUILD_TESTS)
9598
enable_testing()
9699
endif()
97100

98-
message(STATUS "Options selected:")
99-
message(STATUS " BUILD_SHARED_LIBS: ${BUILD_SHARED_LIBS}\t(Build cpp-netlib as shared libraries: OFF, ON)")
100-
message(STATUS " BUILD_TESTS: ${BUILD_TESTS}\t(Build the unit tests: ON, OFF)")
101-
message(STATUS " BUILD_EXAMPLES: ${BUILD_EXAMPLES}\t(Build the examples using cpp-netlib: ON, OFF)")
101+
message(STATUS "CPP-NETLIB Options selected:")
102+
message(STATUS " CPP-NETLIB_BUILD_SHARED_LIBS: ${CPP-NETLIB_BUILD_SHARED_LIBS}\t(Build cpp-netlib as shared libraries: OFF, ON)")
103+
message(STATUS " CPP-NETLIB_BUILD_TESTS: ${CPP-NETLIB_BUILD_TESTS}\t(Build the unit tests: ON, OFF)")
104+
message(STATUS " CPP-NETLIB_BUILD_EXAMPLES: ${CPP-NETLIB_BUILD_EXAMPLES}\t(Build the examples using cpp-netlib: ON, OFF)")
105+
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)")
106+
message(STATUS " CPP-NETLIB_DISABLE_LOGGING: ${CPP-NETLIB_DISABLE_LOGGING}\t(Disable logging definitely, no logging code will be generated or compiled: ON, OFF)")

include/network/detail/debug.hpp

+15-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
// (c) Copyright 2011 Dean Michael Berris.
55
// Copyright 2012 Google, Inc.
6+
// Copyright 2012 A. Joel Lamotte
67
// Distributed under the Boost Software License, Version 1.0.
78
// (See accompanying file LICENSE_1_0.txt or copy at
89
// http://www.boost.org/LICENSE_1_0.txt)
@@ -11,16 +12,27 @@
1112
print out network-related errors through standard error. This is only
1213
useful when NETWORK_DEBUG is turned on. Otherwise the macro amounts to a
1314
no-op.
15+
16+
The user can force the logging to be enabled by defining NETWORK_ENABLE_LOGGING.
1417
*/
15-
#ifdef NETWORK_DEBUG
16-
# include <iostream>
18+
#if defined(NETWORK_DEBUG) && !defined(NETWORK_ENABLE_LOGGING)
19+
# define NETWORK_ENABLE_LOGGING
20+
#endif
21+
22+
#ifdef NETWORK_ENABLE_LOGGING
23+
24+
# include <network/logging/logging.hpp>
1725
# ifndef NETWORK_MESSAGE
18-
# define NETWORK_MESSAGE(msg) std::cerr << "[DEBUG " << __FILE__ << ':' << __LINE__ << "]: " << msg << std::endl;
26+
# define NETWORK_MESSAGE(msg) network::logging::log_record( __FILE__, __LINE__ ) << msg;
1927
# endif
28+
2029
#else
30+
2131
# ifndef NETWORK_MESSAGE
2232
# define NETWORK_MESSAGE(msg)
2333
# endif
34+
2435
#endif
2536

37+
2638
#endif /* end of include guard: NETWORK_DEBUG_HPP_20110410 */

include/network/logging/logging.hpp

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// Copyright (c) 2012 A. Joel Lamotte <[email protected]>.
2+
// Distributed under the Boost Software License, Version 1.0.
3+
// (See accompanying file LICENSE_1_0.txt or copy at
4+
// http://www.boost.org/LICENSE_1_0.txt)
5+
6+
#ifndef NETWORK_LOGGING_HPP_20121112
7+
#define NETWORK_LOGGING_HPP_20121112
8+
9+
#include <sstream>
10+
#include <functional>
11+
12+
namespace network { namespace logging {
13+
14+
class log_record;
15+
16+
//using log_record_handler = std::function< void (const std::string&) >; // use this when VS can compile it...
17+
typedef std::function< void (const log_record&) > log_record_handler;
18+
19+
void set_log_record_handler( log_record_handler handler );
20+
void log( const log_record& message );
21+
22+
namespace handler
23+
{
24+
log_record_handler get_std_log_handler();
25+
log_record_handler get_default_log_handler();
26+
}
27+
28+
/** Helper to build a log record as a stream. */
29+
class log_record
30+
{
31+
public:
32+
log_record()
33+
: m_filename( UNKNOWN_FILE_NAME )
34+
, m_line(0)
35+
{} // = default;
36+
37+
static const char* UNKNOWN_FILE_NAME;
38+
39+
// Implicit construction from anything serializable to text.
40+
template< typename TypeOfSomething >
41+
log_record( TypeOfSomething&& message )
42+
: m_filename( UNKNOWN_FILE_NAME )
43+
, m_line(0)
44+
{
45+
write( std::forward<TypeOfSomething>(message) );
46+
}
47+
48+
// Construction with recording context informations.
49+
log_record( std::string filename, unsigned long line )
50+
: m_filename( filename )
51+
, m_line( line )
52+
{
53+
}
54+
55+
~log_record()
56+
{
57+
log( *this );
58+
}
59+
60+
template< typename TypeOfSomething >
61+
log_record& write( TypeOfSomething&& something )
62+
{
63+
m_text_stream << something;
64+
return *this;
65+
}
66+
67+
template< typename TypeOfSomething >
68+
inline log_record& operator<<( TypeOfSomething&& something )
69+
{
70+
return write( std::forward<TypeOfSomething>(something) );
71+
}
72+
73+
std::string message() const { return m_text_stream.str(); }
74+
const std::string& filename() const { return m_filename; }
75+
unsigned long line() const { return m_line; }
76+
77+
private:
78+
79+
// disable copy
80+
log_record( const log_record& ); // = delete;
81+
log_record& operator=( const log_record& ); // = delete;
82+
83+
std::ostringstream m_text_stream; // stream in which we build the message
84+
std::string m_filename; // = UNKNOWN_FILE_NAME;
85+
unsigned long m_line; // = 0;
86+
};
87+
88+
}}
89+
90+
91+
#endif /* end of include guard: NETWORK_LOGGING_HPP_20121112 */

libs/network/src/CMakeLists.txt

+27
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Copyright (c) Glyn Matthews 2011, 2012.
22
# Copyright 2011 Dean Michael Berris ([email protected])
3+
# Copyright 2012 A. Joel Lamotte ([email protected])
34
# Copyright 2011 Google, Inc.
45
# Distributed under the Boost Software License, Version 1.0.
56
# (See accompanying file LICENSE_1_0.txt or copy at
@@ -24,6 +25,28 @@ elseif (${CMAKE_CXX_COMPILER_ID} MATCHES Clang)
2425
set(CPP-NETLIB_CXXFLAGS "-Wall -std=c++11 -stdlib=libc++")
2526
endif()
2627

28+
if( CPP-NETLIB_ALWAYS_LOGGING )
29+
add_definitions( /D NETWORK_ENABLE_LOGGING )
30+
endif()
31+
32+
if( NOT CPP-NETLIB_DISABLE_LOGGING )
33+
set( CPP-NETLIB_LOGGING_SRCS
34+
logging/logging.cpp
35+
)
36+
add_library(cppnetlib-logging ${CPP-NETLIB_LOGGING_SRCS})
37+
foreach (src_file ${CPP-NETLIB_LOGGING_SRCS})
38+
if (${CMAKE_CXX_COMPILER_ID} MATCHES GNU)
39+
set_source_files_properties(${src_file}
40+
PROPERTIES COMPILE_FLAGS ${CPP-NETLIB_CXXFLAGS})
41+
endif()
42+
endforeach(src_file)
43+
44+
# this library name is defined only if we created the target
45+
# if not then it will be empty
46+
set( CPP-NETLIB_LOGGING_LIB cppnetlib-logging )
47+
48+
endif()
49+
2750
set(CPP-NETLIB_URI_SRCS uri/uri.cpp uri/schemes.cpp uri/normalize.cpp)
2851
add_library(cppnetlib-uri ${CPP-NETLIB_URI_SRCS})
2952
foreach (src_file ${CPP-NETLIB_URI_SRCS})
@@ -124,6 +147,7 @@ set(CPP-NETLIB_HTTP_SERVER_SRCS
124147
)
125148
add_library(cppnetlib-http-server ${CPP-NETLIB_HTTP_SERVER_SRCS})
126149
add_dependencies(cppnetlib-http-server
150+
${CPP-NETLIB_LOGGING_LIB}
127151
cppnetlib-constants
128152
cppnetlib-uri
129153
cppnetlib-message
@@ -136,6 +160,7 @@ add_dependencies(cppnetlib-http-server
136160
)
137161
target_link_libraries(cppnetlib-http-server
138162
${Boost_LIBRARIES}
163+
${CPP-NETLIB_LOGGING_LIB}
139164
cppnetlib-constants
140165
cppnetlib-uri
141166
cppnetlib-message
@@ -182,6 +207,7 @@ set(CPP-NETLIB_HTTP_CLIENT_SRCS
182207
http/client.cpp)
183208
add_library(cppnetlib-http-client ${CPP-NETLIB_HTTP_CLIENT_SRCS})
184209
add_dependencies(cppnetlib-http-client
210+
${CPP-NETLIB_LOGGING_LIB}
185211
cppnetlib-constants
186212
cppnetlib-uri
187213
cppnetlib-message
@@ -193,6 +219,7 @@ add_dependencies(cppnetlib-http-client
193219
)
194220
target_link_libraries(cppnetlib-http-client
195221
${Boost_LIBRARIES}
222+
${CPP-NETLIB_LOGGING_LIB}
196223
cppnetlib-constants
197224
cppnetlib-uri
198225
cppnetlib-message

libs/network/src/logging/logging.cpp

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright 2011 A. Joel Lamotte <[email protected]>.
2+
// Distributed under the Boost Software License, Version 1.0.
3+
// (See accompanying file LICENSE_1_0.txt or copy at
4+
// http://www.boost.org/LICENSE_1_0.txt)
5+
6+
#ifdef NETWORK_NO_LIB
7+
#undef NETWORK_NO_LIB
8+
#endif
9+
10+
#include <iostream>
11+
#include <memory>
12+
#include <network/logging/logging.hpp>
13+
14+
namespace network { namespace logging {
15+
16+
const char* log_record::UNKNOWN_FILE_NAME = "unknown";
17+
18+
19+
namespace handler
20+
{
21+
namespace
22+
{
23+
void std_log_handler( const log_record& log )
24+
{
25+
std::cerr << "[network " << log.filename() << ":" << log.line() << "] "
26+
<< log.message() << std::endl;
27+
}
28+
}
29+
30+
log_record_handler get_std_log_handler() { return &std_log_handler; }
31+
log_record_handler get_default_log_handler() { return &std_log_handler; }
32+
}
33+
34+
35+
namespace
36+
{
37+
// the log handler have to manage itself the thread safety on call
38+
static auto current_log_record_handler = std::make_shared<log_record_handler>( &handler::std_log_handler );
39+
40+
}
41+
42+
43+
void set_log_record_handler( log_record_handler handler )
44+
{
45+
current_log_record_handler = std::make_shared<log_record_handler>( handler );
46+
}
47+
48+
void log( const log_record& log )
49+
{
50+
auto log_handler = current_log_record_handler;
51+
if( log_handler )
52+
{
53+
(*log_handler)( log );
54+
}
55+
}
56+
57+
58+
}}

libs/network/test/CMakeLists.txt

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55

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

8-
if(BUILD_SHARED_LIBS)
9-
add_definitions(-DBUILD_SHARED_LIBS)
8+
if(CPP-NETLIB_BUILD_SHARED_LIBS)
9+
add_definitions(-DCPP-NETLIB_BUILD_SHARED_LIBS)
1010
endif()
1111

12+
add_subdirectory(logging)
1213
add_subdirectory(uri)
1314
add_subdirectory(http)
1415

libs/network/test/http/CMakeLists.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ if (Boost_FOUND)
3333
cppnetlib-http-message-wrappers
3434
cppnetlib-uri
3535
cppnetlib-constants
36-
)
36+
)
3737
target_link_libraries(cpp-netlib-http-${test}
3838
${Boost_LIBRARIES}
3939
${ICU_LIBRARIES} ${ICU_I18N_LIBRARIES}
40-
${CMAKE_THREAD_LIBS_INIT}
40+
${CMAKE_THREAD_LIBS_INIT}
4141
cppnetlib-message
4242
cppnetlib-message-wrappers
4343
cppnetlib-http-message
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Copyright (c) A. Joel Lamotte 2012.
2+
# Distributed under the Boost Software License, Version 1.0.
3+
# (See accompanying file LICENSE_1_0.txt or copy at
4+
# http://www.boost.org/LICENSE_1_0.txt)
5+
6+
include_directories(${CPP-NETLIB_SOURCE_DIR}/include)
7+
include_directories(${CPP-NETLIB_SOURCE_DIR})
8+
9+
if (Boost_FOUND)
10+
set(
11+
TESTS
12+
logging_log_record
13+
logging_custom_handler
14+
)
15+
foreach (test ${TESTS})
16+
if (${CMAKE_CXX_COMPILER_ID} MATCHES GNU)
17+
set_source_files_properties(${test}.cpp
18+
PROPERTIES COMPILE_FLAGS "-Wall")
19+
endif()
20+
add_executable(cpp-netlib-${test} ${test}.cpp)
21+
add_dependencies(cpp-netlib-${test} cppnetlib-logging)
22+
target_link_libraries(cpp-netlib-${test}
23+
${Boost_LIBRARIES} cppnetlib-logging)
24+
set_target_properties(cpp-netlib-${test}
25+
PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CPP-NETLIB_BINARY_DIR}/tests)
26+
add_test(cpp-netlib-${test}
27+
${CPP-NETLIB_BINARY_DIR}/tests/cpp-netlib-${test})
28+
endforeach (test)
29+
endif()

0 commit comments

Comments
 (0)