Skip to content

Commit 98a3100

Browse files
committed
Improved log record.
- log record informations are now separated - the log handler decide how to format the log record - log record accepts more than compatible-to-string types on construction, anything string-streamable will work (need additional tests though)
1 parent f6e6ee6 commit 98a3100

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

include/network/detail/debug.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
# include <network/logging/logging.hpp>
2424
# ifndef NETWORK_MESSAGE
25-
# define NETWORK_MESSAGE(msg) network::logging::log_record() << "[network " << __FILE__ << ':' << __LINE__ << "]: " << msg;
25+
# define NETWORK_MESSAGE(msg) network::logging::log_record( __FILE__, __LINE__ ) << msg;
2626
# endif
2727

2828
#else

include/network/logging/logging.hpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,20 @@ class log_record
3131
public:
3232
log_record(){} // = default;
3333

34-
// Implicit construction from string
35-
log_record( const std::string& message )
34+
// Implicit construction from anything serializable to text.
35+
template< typename TypeOfSomething >
36+
log_record( TypeOfSomething&& message )
37+
: m_filename( "unknown" )
38+
, m_line(0)
39+
{
40+
write( std::forward<TypeOfSomething>(message) );
41+
}
42+
43+
// Construction with recording context informations.
44+
log_record( std::string filename, unsigned long line )
45+
: m_filename( filename )
46+
, m_line( line )
3647
{
37-
write( message );
3848
}
3949

4050
~log_record()
@@ -49,15 +59,18 @@ class log_record
4959
return *this;
5060
}
5161

52-
std::string full_message() const { return m_text_stream.str(); }
62+
std::string message() const { return m_text_stream.str(); }
63+
const std::string& filename() const { return m_filename; }
64+
unsigned long line() const { return m_line; }
5365

5466
private:
5567
// disable copy
5668
log_record( const log_record& ); // = delete;
5769
log_record& operator=( const log_record& ); // = delete;
5870

5971
std::ostringstream m_text_stream; // stream in which we build the message
60-
72+
std::string m_filename; // = "unknown";
73+
unsigned long m_line; // = 0;
6174
};
6275

6376
template< typename TypeOfSomething >

libs/network/src/logging/logging.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ namespace handler
1818
namespace
1919
{
2020
void std_log_handler( const log_record& log )
21-
{
22-
std::cerr << log.full_message() << std::endl;
21+
{
22+
std::cerr << "[network " << log.filename() << ":" << log.line() << "] "
23+
<< log.message() << std::endl;
2324
}
2425
}
2526

0 commit comments

Comments
 (0)