forked from glynos/cpp-netlib
-
Notifications
You must be signed in to change notification settings - Fork 425
Logging improvements: options & custom log record handling #167
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
d224a03
Added options to enable logging in non-debug or totally disable logging.
Klaim e214cf6
Added logging library.
Klaim fe870cb
Fixed indentation.
Klaim 47e8f86
Added missing copyright informations (klaim)
Klaim 44297c6
Replaced logging handler mutex by shared pointers.
Klaim 69b6499
Using universal references in logging templates.
Klaim f6e6ee6
Some file cleaning - sorry
Klaim 98a3100
Improved log record.
Klaim 5bd2f7b
Fixed indentation.
Klaim 8452e5c
Added logging test projects (no code yet)
Klaim 4510497
Implemented basic log_record tests (and fixes)
Klaim f89c2f2
Implemented custom log handler tests.
Klaim 6b9c321
Fixed indentation.
Klaim 8e63590
Merge branch 'master' of https://github.com/cpp-netlib/cpp-netlib
Klaim 892c4c5
Gathered and prefixed all CMake options.
Klaim 084c8fa
Moved down one line as requested.
Klaim 4157a27
Fixed typo in comment.
Klaim File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
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 */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} | ||
} | ||
|
||
|
||
}} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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. :)
There was a problem hiding this comment.
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.