From 8749d5a9ea25ba94ab50e6755d1b7033a8bb6004 Mon Sep 17 00:00:00 2001 From: Carl Hetherington Date: Thu, 6 Nov 2014 23:15:49 +0000 Subject: [PATCH] Fix log head/tail functions. --- src/lib/log.cc | 23 +++++++++++++---------- src/lib/log.h | 6 +++--- src/tools/dcpomatic_server.cc | 6 +++--- test/file_log_test.cc | 3 ++- 4 files changed, 21 insertions(+), 17 deletions(-) diff --git a/src/lib/log.cc b/src/lib/log.cc index 156f8cf8b..934664915 100644 --- a/src/lib/log.cc +++ b/src/lib/log.cc @@ -124,12 +124,12 @@ FileLog::do_log (string m) } string -FileLog::head_and_tail () const +FileLog::head_and_tail (int amount) const { boost::mutex::scoped_lock lm (_mutex); - uintmax_t head_amount = 1024; - uintmax_t tail_amount = 1024; + uintmax_t head_amount = amount; + uintmax_t tail_amount = amount; uintmax_t size = boost::filesystem::file_size (_file); if (size < (head_amount + tail_amount)) { @@ -148,16 +148,19 @@ FileLog::head_and_tail () const int N = fread (buffer, 1, head_amount, f); buffer[N] = '\0'; - out += buffer; + out += string (buffer); - fseek (f, tail_amount, SEEK_END); - - N = fread (buffer, 1, tail_amount, f); - buffer[N] = '\0'; - out += buffer; + if (tail_amount > 0) { + out += "\n.\n.\n.\n"; - delete[] buffer; + fseek (f, - tail_amount - 1, SEEK_END); + + N = fread (buffer, 1, tail_amount, f); + buffer[N] = '\0'; + out += string (buffer) + "\n"; + } + delete[] buffer; fclose (f); return out; diff --git a/src/lib/log.h b/src/lib/log.h index f20b0a148..cee1ce495 100644 --- a/src/lib/log.h +++ b/src/lib/log.h @@ -48,7 +48,7 @@ public: void set_types (int types); - virtual std::string head_and_tail () const = 0; + virtual std::string head_and_tail (int amount = 1024) const = 0; protected: @@ -69,7 +69,7 @@ class FileLog : public Log public: FileLog (boost::filesystem::path file); - std::string head_and_tail () const; + std::string head_and_tail (int amount = 1024) const; private: void do_log (std::string m); @@ -80,7 +80,7 @@ private: class NullLog : public Log { public: - std::string head_and_tail () const { + std::string head_and_tail (int) const { return ""; } diff --git a/src/tools/dcpomatic_server.cc b/src/tools/dcpomatic_server.cc index bcd5c8df6..58310377c 100644 --- a/src/tools/dcpomatic_server.cc +++ b/src/tools/dcpomatic_server.cc @@ -47,12 +47,12 @@ public: return _log; } - string head_and_tail () const { - if (_log.size () < 2048) { + string head_and_tail (int amount = 1024) const { + if (_log.size () < (2 * amount)) { return _log; } - return _log.substr (0, 1024) + _log.substr (_log.size() - 1025, 1024); + return _log.substr (0, amount) + _log.substr (_log.size() - amount - 1, amount); } private: diff --git a/test/file_log_test.cc b/test/file_log_test.cc index 4769503c1..1cf7a7757 100644 --- a/test/file_log_test.cc +++ b/test/file_log_test.cc @@ -25,5 +25,6 @@ using std::cout; BOOST_AUTO_TEST_CASE (file_log_test) { FileLog log ("test/data/short.log"); - BOOST_CHECK_EQUAL (log.head_and_tail(), "This is a short log.\nWith only two lines.\n"); + BOOST_CHECK_EQUAL (log.head_and_tail (1024), "This is a short log.\nWith only two lines.\n"); + BOOST_CHECK_EQUAL (log.head_and_tail (8), "This is \n.\n.\n.\no lines.\n"); } -- 2.30.2