Skip to content

Commit e214cf6

Browse files
committed
Added logging library.
1 parent d224a03 commit e214cf6

File tree

3 files changed

+145
-0
lines changed

3 files changed

+145
-0
lines changed

include/network/logging/logging.hpp

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+
#define NETWORK_LOGGING_HPP_20121112
3+
4+
#include <sstream>
5+
#include <functional>
6+
7+
namespace network { namespace logging {
8+
9+
class log_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+
void set_log_record_handler( log_record_handler handler );
15+
void log( const log_record& message );
16+
17+
namespace handler
18+
{
19+
log_record_handler get_std_log_handler();
20+
log_record_handler get_default_log_handler();
21+
}
22+
23+
/** Helper to build a log record as a stream. */
24+
class log_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::string full_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+
PROPERTIES COMPILE_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

libs/network/src/logging/logging.cpp

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+
namespace network { namespace logging {
12+
13+
namespace handler
14+
{
15+
namespace
16+
{
17+
void std_log_handler( const log_record& log )
18+
{
19+
std::cerr << log.full_message() << std::endl;
20+
}
21+
}
22+
23+
log_record_handler get_std_log_handler() { return &std_log_handler; }
24+
log_record_handler get_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+
void set_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+
void log( 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)