Commit d965485ad2bc8d03e29206e632b200b0201f0bbb
1 parent
154178a5
Avoid dates in the middle of a text block.
Dates were appended in the middle of a text block if the buffer was flushed before a \n character was reached. A private member m_linestart has been added that tracks whether the buffer is actually at a line start or the middle of a text block. Dates are only set at the beginning of the buffer is the buffer is at a line start.
Showing
2 changed files
with
15 additions
and
15 deletions
include/GLog.hpp
| ... | ... | @@ -123,6 +123,7 @@ protected: |
| 123 | 123 | bool m_stdout; //!< Dump in standard output |
| 124 | 124 | bool m_stderr; //!< Dump in standard error |
| 125 | 125 | bool m_use_date; //!< Dump date in prefix |
| 126 | + bool m_linestart; //!< Signals that buffer is at line start | |
| 126 | 127 | FILE* m_file; //!< Log file pointer |
| 127 | 128 | std::string m_filename; //!< Log file name |
| 128 | 129 | std::string m_name; //!< Name for prefix | ... | ... |
src/app/GLog.cpp
| ... | ... | @@ -537,6 +537,7 @@ void GLog::init_members(void) |
| 537 | 537 | m_stdout = false; |
| 538 | 538 | m_stderr = false; |
| 539 | 539 | m_use_date = false; |
| 540 | + m_linestart = true; | |
| 540 | 541 | m_file = NULL; |
| 541 | 542 | m_filename.clear(); |
| 542 | 543 | m_name.clear(); |
| ... | ... | @@ -561,6 +562,7 @@ void GLog::copy_members(const GLog& log) |
| 561 | 562 | m_stdout = log.m_stdout; |
| 562 | 563 | m_stderr = log.m_stderr; |
| 563 | 564 | m_use_date = log.m_use_date; |
| 565 | + m_linestart = log.m_linestart; | |
| 564 | 566 | m_file = log.m_file; |
| 565 | 567 | m_filename = log.m_filename; |
| 566 | 568 | m_name = log.m_name; |
| ... | ... | @@ -616,23 +618,17 @@ void GLog::flush(const bool& force) |
| 616 | 618 | std::string line; |
| 617 | 619 | std::size_t pos = m_buffer.find_first_of("\n", 0); |
| 618 | 620 | if (pos == std::string::npos) { |
| 619 | - line = m_buffer; | |
| 621 | + line = m_buffer; | |
| 622 | + m_linestart = false; | |
| 620 | 623 | m_buffer.clear(); |
| 621 | 624 | } |
| 622 | 625 | else { |
| 623 | - line = m_buffer.substr(0, pos) + "\n"; | |
| 624 | - m_buffer = m_buffer.substr(pos+1, m_buffer.size()-pos); | |
| 626 | + line = m_buffer.substr(0, pos) + "\n"; | |
| 627 | + m_buffer = m_buffer.substr(pos+1, m_buffer.size()-pos); | |
| 628 | + m_linestart = true; | |
| 625 | 629 | } |
| 626 | 630 | |
| 627 | - // Put line into requested streams | |
| 628 | - /* | |
| 629 | - if (m_stdout) { | |
| 630 | - std::cout << line; | |
| 631 | - } | |
| 632 | - if (m_stderr) { | |
| 633 | - std::cerr << line; | |
| 634 | - } | |
| 635 | - */ | |
| 631 | + // Put line into file | |
| 636 | 632 | if (m_file != NULL) { |
| 637 | 633 | std::fprintf(m_file, "%s", line.c_str()); |
| 638 | 634 | } |
| ... | ... | @@ -780,12 +776,15 @@ std::string GLog::prefix(void) const |
| 780 | 776 | ***************************************************************************/ |
| 781 | 777 | void GLog::append(std::string arg) |
| 782 | 778 | { |
| 783 | - // If the buffer is empty or if the last charater is a \n, prepend a | |
| 784 | - // prefix at the beginning of the string to be inserted. | |
| 785 | - if (m_buffer.size() == 0 || m_buffer[m_buffer.size()-1] == '\n') { | |
| 779 | + // If the buffer is empty and at the beginning of a line or if the last | |
| 780 | + // charater is a \n, prepend a prefix at the beginning of the string to | |
| 781 | + // be inserted. | |
| 782 | + if (m_buffer.size() == 0 && m_linestart || | |
| 783 | + m_buffer[m_buffer.size()-1] == '\n') { | |
| 786 | 784 | |
| 787 | 785 | // Prepend prefix |
| 788 | 786 | arg.insert(0, prefix()); |
| 787 | + | |
| 789 | 788 | } |
| 790 | 789 | |
| 791 | 790 | // Search the first CR (\n) | ... | ... |