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

Commite214cf6

Browse files
committed
Added logging library.
1 parentd224a03 commite214cf6

File tree

3 files changed

+145
-0
lines changed

3 files changed

+145
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#ifndef NETWORK_LOGGING_HPP_20121112
2+
#defineNETWORK_LOGGING_HPP_20121112
3+
4+
#include<sstream>
5+
#include<functional>
6+
7+
namespacenetwork {namespacelogging {
8+
9+
classlog_record;
10+
11+
//using log_record_handler = std::function< void (const std::string&) >; // use this when VS can compile it...
12+
typedef std::function<void (const log_record&) > log_record_handler;
13+
14+
voidset_log_record_handler( log_record_handler handler );
15+
voidlog(const log_record& message );
16+
17+
namespacehandler
18+
{
19+
log_record_handlerget_std_log_handler();
20+
log_record_handlerget_default_log_handler();
21+
}
22+
23+
/** Helper to build a log record as a stream.*/
24+
classlog_record
25+
{
26+
public:
27+
log_record(){}// = default;
28+
29+
// Implicit construction from string
30+
log_record(const std::string& message )
31+
{
32+
write( message );
33+
}
34+
35+
~log_record()
36+
{
37+
log( *this );
38+
}
39+
40+
template<typename TypeOfSomething >
41+
log_record&write(const TypeOfSomething& something )// THINK: use universal references?
42+
{
43+
m_text_stream << something;
44+
return *this;
45+
}
46+
47+
std::stringfull_message()const {return m_text_stream.str(); }
48+
49+
private:
50+
// disable copy
51+
log_record(const log_record& );// = delete;
52+
log_record&operator=(const log_record& );// = delete;
53+
54+
std::ostringstream m_text_stream;// stream in which we build the message
55+
56+
};
57+
58+
template<typename TypeOfSomething >
59+
inline log_record&operator<<( log_record& log,const TypeOfSomething& something )// THINK: use universal references?
60+
{
61+
return log.write( something );
62+
}
63+
64+
}}
65+
66+
67+
#endif/* end of include guard: NETWORK_LOGGING_HPP_20121112*/

‎libs/network/src/CMakeLists.txt‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,28 @@ if (${CMAKE_CXX_COMPILER_ID} MATCHES GNU)
2020
endif()
2121
endif()
2222

23+
if( CPP-NETLIB_ALWAYS_LOGGING )
24+
add_definitions( /D NETWORK_ENABLE_LOGGING )
25+
endif()
26+
27+
if(NOT CPP-NETLIB_DISABLE_LOGGING )
28+
set( CPP-NETLIB_LOGGING_SRCS
29+
logging/logging.cpp
30+
)
31+
add_library(cppnetlib-logging ${CPP-NETLIB_LOGGING_SRCS})
32+
foreach (src_file ${CPP-NETLIB_LOGGING_SRCS})
33+
if (${CMAKE_CXX_COMPILER_ID}MATCHES GNU)
34+
set_source_files_properties(${src_file}
35+
PROPERTIESCOMPILE_FLAGS ${CPP-NETLIB_CXXFLAGS})
36+
endif()
37+
endforeach(src_file)
38+
39+
# this library name is defined only if we created the target
40+
# if not then it will be empty
41+
set( CPP-NETLIB_LOGGING_LIB cppnetlib-logging )
42+
43+
endif()
44+
2345
set(CPP-NETLIB_URI_SRCS uri/uri.cpp uri/schemes.cpp uri/normalize.cpp)
2446
add_library(cppnetlib-uri ${CPP-NETLIB_URI_SRCS})
2547
foreach (src_file ${CPP-NETLIB_URI_SRCS})
@@ -100,6 +122,7 @@ set(CPP-NETLIB_HTTP_SERVER_SRCS
100122
add_library(cppnetlib-http-server ${CPP-NETLIB_HTTP_SERVER_SRCS})
101123
add_dependencies(cppnetlib-http-server
102124
${Boost_LIBRARIES}
125+
${CPP-NETLIB_LOGGING_LIB}
103126
cppnetlib-constants
104127
cppnetlib-uri
105128
cppnetlib-message
@@ -111,6 +134,7 @@ add_dependencies(cppnetlib-http-server
111134
)
112135
target_link_libraries(cppnetlib-http-server
113136
${Boost_LIBRARIES}
137+
${CPP-NETLIB_LOGGING_LIB}
114138
cppnetlib-constants
115139
cppnetlib-uri
116140
cppnetlib-message
@@ -151,6 +175,7 @@ set(CPP-NETLIB_HTTP_CLIENT_SRCS
151175
add_library(cppnetlib-http-client ${CPP-NETLIB_HTTP_CLIENT_SRCS})
152176
add_dependencies(cppnetlib-http-client
153177
${Boost_LIBRARIES}
178+
${CPP-NETLIB_LOGGING_LIB}
154179
cppnetlib-constants
155180
cppnetlib-uri
156181
cppnetlib-message
@@ -162,6 +187,7 @@ add_dependencies(cppnetlib-http-client
162187
)
163188
target_link_libraries(cppnetlib-http-client
164189
${Boost_LIBRARIES}
190+
${CPP-NETLIB_LOGGING_LIB}
165191
cppnetlib-constants
166192
cppnetlib-uri
167193
cppnetlib-message
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2+
#ifdef NETWORK_NO_LIB
3+
#undef NETWORK_NO_LIB
4+
#endif
5+
6+
#include<iostream>
7+
#include<boost/thread/shared_mutex.hpp>
8+
#include<boost/thread/shared_lock_guard.hpp>
9+
#include<network/logging/logging.hpp>
10+
11+
namespacenetwork {namespacelogging {
12+
13+
namespacehandler
14+
{
15+
namespace
16+
{
17+
voidstd_log_handler(const log_record& log )
18+
{
19+
std::cerr << log.full_message() << std::endl;
20+
}
21+
}
22+
23+
log_record_handlerget_std_log_handler() {return &std_log_handler; }
24+
log_record_handlerget_default_log_handler() {return &std_log_handler; }
25+
}
26+
27+
28+
namespace
29+
{
30+
// the log handler have to manage itself the thread safety on call
31+
log_record_handler current_log_record_handler = handler::std_log_handler;
32+
boost::upgrade_mutex mutex_log_handler;// we still need to not change the log handler concurrently
33+
}
34+
35+
36+
voidset_log_record_handler( log_record_handler handler )
37+
{
38+
boost::lock_guard<boost::upgrade_mutex>write_lock( mutex_log_handler );
39+
current_log_record_handler = handler;
40+
}
41+
42+
voidlog(const log_record& log )
43+
{
44+
boost::shared_lock<boost::upgrade_mutex>read_lock( mutex_log_handler );
45+
if( current_log_record_handler )
46+
{
47+
current_log_record_handler( log );
48+
}
49+
}
50+
51+
52+
}}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp