Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit815a529

Browse files
committed
Merge pull request#167 from Klaim/master
Logging improvements: options & custom log record handling
2 parentsb9c3a01 +4157a27 commit815a529

File tree

10 files changed

+351
-20
lines changed

10 files changed

+351
-20
lines changed

‎CMakeLists.txt‎

Lines changed: 19 additions & 13 deletions
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_LIBSOFF)
1922
else()
2023
set(Boost_USE_STATIC_LIBSON)
2124
endif()
2225
set(Boost_USE_MULTITHREADEDON)
23-
if(BUILD_TESTS)
26+
if(CPP-NETLIB_BUILD_TESTS)
2427
set(Boost_COMPONENTS unit_test_frameworksystemregex date_time thread chrono filesystem program_options )
2528
else()
2629
set(Boost_COMPONENTSsystemregex date_time thread chrono filesystem program_options )
@@ -56,6 +59,7 @@ elseif(${CMAKE_CXX_COMPILER_ID} MATCHES Clang)
5659
message("C++ Flags:${CMAKE_CXX_FLAGS} link flags:${CMAKE_CXX_LINK_FLAGS}")
5760
endif()
5861

62+
5963
if (Boost_FOUND)
6064
if (MSVC)
6165
add_definitions(-D_SCL_SECURE_NO_WARNINGS)
@@ -64,27 +68,29 @@ if (Boost_FOUND)
6468
add_definitions(-D_WIN32_WINNT=0x0501)
6569
endif(WIN32)
6670
include_directories(${Boost_INCLUDE_DIRS})
67-
if(BUILD_TESTS)
71+
if(CPP-NETLIB_BUILD_TESTS)
6872
enable_testing()
6973
endif()
7074
add_subdirectory(libs/network/src)
71-
if(BUILD_TESTS)
75+
if(CPP-NETLIB_BUILD_TESTS)
7276
enable_testing()
7377
add_subdirectory(libs/network/test)
7478
if (NOTMSVC)
7579
add_subdirectory(libs/mime/test)
7680
endif(NOTMSVC)
7781
endif()
78-
if(BUILD_EXAMPLES)
82+
if(CPP-NETLIB_BUILD_EXAMPLES)
7983
add_subdirectory(libs/network/example)
8084
endif()
8185
endif(Boost_FOUND)
8286

83-
if(BUILD_TESTS)
87+
if(CPP-NETLIB_BUILD_TESTS)
8488
enable_testing()
8589
endif()
8690

87-
message(STATUS"Options selected:")
88-
message(STATUS" BUILD_SHARED_LIBS:${BUILD_SHARED_LIBS}\t(Build cpp-netlib as shared libraries: OFF, ON)")
89-
message(STATUS" BUILD_TESTS:${BUILD_TESTS}\t(Build the unit tests: ON, OFF)")
90-
message(STATUS" BUILD_EXAMPLES:${BUILD_EXAMPLES}\t(Build the examples using cpp-netlib: ON, OFF)")
91+
message(STATUS"CPP-NETLIB Options selected:")
92+
message(STATUS" CPP-NETLIB_BUILD_SHARED_LIBS: ${CPP-NETLIB_BUILD_SHARED_LIBS}\t(Build cpp-netlib as shared libraries: OFF, ON)")
93+
message(STATUS" CPP-NETLIB_BUILD_TESTS: ${CPP-NETLIB_BUILD_TESTS}\t(Build the unit tests: ON, OFF)")
94+
message(STATUS" CPP-NETLIB_BUILD_EXAMPLES: ${CPP-NETLIB_BUILD_EXAMPLES}\t(Build the examples using cpp-netlib: ON, OFF)")
95+
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)")
96+
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‎

Lines changed: 15 additions & 3 deletions
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+
#defineNETWORK_ENABLE_LOGGING
20+
#endif
21+
22+
#ifdef NETWORK_ENABLE_LOGGING
23+
24+
#include<network/logging/logging.hpp>
1725
#ifndef NETWORK_MESSAGE
18-
#defineNETWORK_MESSAGE(msg)std::cerr <<"[DEBUG" <<__FILE__ <<':' <<__LINE__<<"]:"<< msg << std::endl;
26+
#defineNETWORK_MESSAGE(msg)network::logging::log_record(__FILE__,__LINE__)<< msg;
1927
#endif
28+
2029
#else
30+
2131
#ifndef NETWORK_MESSAGE
2232
#defineNETWORK_MESSAGE(msg)
2333
#endif
34+
2435
#endif
2536

37+
2638
#endif/* end of include guard: NETWORK_DEBUG_HPP_20110410*/
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// Copyright (c) 2012 A. Joel Lamotte <mjklaim@gmail.com>.
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+
#defineNETWORK_LOGGING_HPP_20121112
8+
9+
#include<sstream>
10+
#include<functional>
11+
12+
namespacenetwork {namespacelogging {
13+
14+
classlog_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+
voidset_log_record_handler( log_record_handler handler );
20+
voidlog(const log_record& message );
21+
22+
namespacehandler
23+
{
24+
log_record_handlerget_std_log_handler();
25+
log_record_handlerget_default_log_handler();
26+
}
27+
28+
/** Helper to build a log record as a stream.*/
29+
classlog_record
30+
{
31+
public:
32+
log_record()
33+
: m_filename( UNKNOWN_FILE_NAME )
34+
, m_line(0)
35+
{}// = default;
36+
37+
staticconstchar* 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,unsignedlong 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+
std::stringmessage()const {return m_text_stream.str(); }
68+
const std::string&filename()const {return m_filename; }
69+
unsignedlongline()const {return m_line; }
70+
71+
private:
72+
// disable copy
73+
log_record(const log_record& );// = delete;
74+
log_record&operator=(const log_record& );// = delete;
75+
76+
std::ostringstream m_text_stream;// stream in which we build the message
77+
std::string m_filename;// = UNKNOWN_FILE_NAME;
78+
unsignedlong m_line;// = 0;
79+
};
80+
81+
template<typename TypeOfSomething >
82+
inline log_record&operator<<( log_record& log, TypeOfSomething&& something )
83+
{
84+
return log.write( std::forward<TypeOfSomething>(something) );
85+
}
86+
87+
}}
88+
89+
90+
#endif/* end of include guard: NETWORK_LOGGING_HPP_20121112*/

‎libs/network/src/CMakeLists.txt‎

Lines changed: 27 additions & 0 deletions
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 (dberris@google.com)
3+
# Copyright 2012 A. Joel Lamotte (mjklaim@gmail.com)
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
@@ -20,6 +21,28 @@ if (${CMAKE_CXX_COMPILER_ID} MATCHES GNU)
2021
endif()
2122
endif()
2223

24+
if( CPP-NETLIB_ALWAYS_LOGGING )
25+
add_definitions( /D NETWORK_ENABLE_LOGGING )
26+
endif()
27+
28+
if(NOT CPP-NETLIB_DISABLE_LOGGING )
29+
set( CPP-NETLIB_LOGGING_SRCS
30+
logging/logging.cpp
31+
)
32+
add_library(cppnetlib-logging ${CPP-NETLIB_LOGGING_SRCS})
33+
foreach (src_file ${CPP-NETLIB_LOGGING_SRCS})
34+
if (${CMAKE_CXX_COMPILER_ID}MATCHES GNU)
35+
set_source_files_properties(${src_file}
36+
PROPERTIESCOMPILE_FLAGS ${CPP-NETLIB_CXXFLAGS})
37+
endif()
38+
endforeach(src_file)
39+
40+
# this library name is defined only if we created the target
41+
# if not then it will be empty
42+
set( CPP-NETLIB_LOGGING_LIB cppnetlib-logging )
43+
44+
endif()
45+
2346
set(CPP-NETLIB_URI_SRCS uri/uri.cpp uri/schemes.cpp uri/normalize.cpp)
2447
add_library(cppnetlib-uri ${CPP-NETLIB_URI_SRCS})
2548
foreach (src_file ${CPP-NETLIB_URI_SRCS})
@@ -99,6 +122,7 @@ set(CPP-NETLIB_HTTP_SERVER_SRCS
99122
)
100123
add_library(cppnetlib-http-server ${CPP-NETLIB_HTTP_SERVER_SRCS})
101124
add_dependencies(cppnetlib-http-server
125+
${CPP-NETLIB_LOGGING_LIB}
102126
cppnetlib-constants
103127
cppnetlib-uri
104128
cppnetlib-message
@@ -111,6 +135,7 @@ add_dependencies(cppnetlib-http-server
111135
)
112136
target_link_libraries(cppnetlib-http-server
113137
${Boost_LIBRARIES}
138+
${CPP-NETLIB_LOGGING_LIB}
114139
cppnetlib-constants
115140
cppnetlib-uri
116141
cppnetlib-message
@@ -151,6 +176,7 @@ set(CPP-NETLIB_HTTP_CLIENT_SRCS
151176
http/client.cpp)
152177
add_library(cppnetlib-http-client ${CPP-NETLIB_HTTP_CLIENT_SRCS})
153178
add_dependencies(cppnetlib-http-client
179+
${CPP-NETLIB_LOGGING_LIB}
154180
cppnetlib-constants
155181
cppnetlib-uri
156182
cppnetlib-message
@@ -162,6 +188,7 @@ add_dependencies(cppnetlib-http-client
162188
)
163189
target_link_libraries(cppnetlib-http-client
164190
${Boost_LIBRARIES}
191+
${CPP-NETLIB_LOGGING_LIB}
165192
cppnetlib-constants
166193
cppnetlib-uri
167194
cppnetlib-message
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright 2011 A. Joel Lamotte <mjklaim@gmail.com>.
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+
namespacenetwork {namespacelogging {
15+
16+
constchar* log_record::UNKNOWN_FILE_NAME ="unknown";
17+
18+
19+
namespacehandler
20+
{
21+
namespace
22+
{
23+
voidstd_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_handlerget_std_log_handler() {return &std_log_handler; }
31+
log_record_handlerget_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+
staticauto current_log_record_handler = std::make_shared<log_record_handler>( &handler::std_log_handler );
39+
40+
}
41+
42+
43+
voidset_log_record_handler( log_record_handler handler )
44+
{
45+
current_log_record_handler = std::make_shared<log_record_handler>( handler );
46+
}
47+
48+
voidlog(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‎

Lines changed: 3 additions & 2 deletions
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‎

Lines changed: 2 additions & 2 deletions
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
Lines changed: 29 additions & 0 deletions
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+
PROPERTIESCOMPILE_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+
PROPERTIESRUNTIME_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)

[8]ページ先頭

©2009-2025 Movatter.jp