diff options
author | Mihai Moldovan <ionic@ionic.de> | 2017-12-24 22:16:38 +0100 |
---|---|---|
committer | Mihai Moldovan <ionic@ionic.de> | 2017-12-25 04:00:36 +0100 |
commit | dccf5901ec1bbdcd5523169ca20751d9b7569e7d (patch) | |
tree | 9c53c3016a1fedc36c83bb2386ba6a30c9eb0ecc /nxcomp/src/Log.cpp | |
parent | b30cfaa24d28dfcf5ee67d33cb614c9800bbddd2 (diff) | |
download | nx-libs-dccf5901ec1bbdcd5523169ca20751d9b7569e7d.tar.gz nx-libs-dccf5901ec1bbdcd5523169ca20751d9b7569e7d.tar.bz2 nx-libs-dccf5901ec1bbdcd5523169ca20751d9b7569e7d.zip |
nxcomp/src/Log.{cpp,h}: provide infrastructure for appending to already existing log lines.
Also handle errors due to missing buffers gracefully.
This error is surprisingly easy to trigger, so we should make sure that
the program does not crash.
Diffstat (limited to 'nxcomp/src/Log.cpp')
-rw-r--r-- | nxcomp/src/Log.cpp | 53 |
1 files changed, 45 insertions, 8 deletions
diff --git a/nxcomp/src/Log.cpp b/nxcomp/src/Log.cpp index 24f660507..a3a7222ef 100644 --- a/nxcomp/src/Log.cpp +++ b/nxcomp/src/Log.cpp @@ -106,16 +106,53 @@ std::string NXLog::stamp_to_string(const NXLogStamp& stamp) const NXLog& operator<< (NXLog& out, const NXLogStamp& value) { - out.current_level( value.level() ); - out.current_file( value.file() ); + /* + * If appending, the file and function names must be empty and + * the line set to zero. + */ + const bool looks_like_append = ((value.file().empty()) || (value.function().empty()) || (0 == value.line())); + const bool append = ((looks_like_append) && ((value.file().empty()) && (value.function().empty()) && (0 == value.line()))); + + if ((looks_like_append) && (!append)) + { + std::cerr << "WARNING: At least one element in logstamp invalid, but this is not supposed to be an append operation. " + << "Internal state error!\n" << "Log line will be discarded!" << std::endl; + } + else if (append) + { + /* Appending means that the log object's internal level and the message level must match. */ + if (out.current_level() == value.level()) + { + /* And the buffer must of course be non-empty. */ + if (out.has_buffer()) + { + out << " (cont.) "; + } + else + { + std::cerr << "WARNING: Append operation requested, but no queued data available. " + << "Internal state error!\n" << "Log line will be discarded!" << std::endl; + } + } + else + { + std::cerr << "WARNING: Append operation requested, but internal log level not matching line level. " + << "Internal state error!\n" << "Log line will be discarded!" << std::endl; + } + } + else + { + out.current_level( value.level() ); + out.current_file( value.file() ); - // Writing an NXLogStamp to the stream indicates the start of a new entry. - // If there's any content in the buffer and we actually intend to keep that line, - // create a new entry in the output queue. - if ( out.synchronized() && out.will_log() ) - out.new_stack_entry(); + // Writing an NXLogStamp to the stream indicates the start of a new entry. + // If there's any content in the buffer and we actually intend to keep that line, + // create a new entry in the output queue. + if ( out.synchronized() && out.will_log() ) + out.new_stack_entry(); - out << out.stamp_to_string(value); + out << out.stamp_to_string(value); + } return out; } |