Fix log head/tail functions.
authorCarl Hetherington <cth@carlh.net>
Thu, 6 Nov 2014 23:15:49 +0000 (23:15 +0000)
committerCarl Hetherington <cth@carlh.net>
Thu, 6 Nov 2014 23:15:49 +0000 (23:15 +0000)
src/lib/log.cc
src/lib/log.h
src/tools/dcpomatic_server.cc
test/file_log_test.cc

index 156f8cf8bb3bc38e2c0964dfd19c243b095f802f..934664915b0e5d9d4608a2e81276315ba7553544 100644 (file)
@@ -124,12 +124,12 @@ FileLog::do_log (string m)
 }
 
 string
 }
 
 string
-FileLog::head_and_tail () const
+FileLog::head_and_tail (int amount) const
 {
        boost::mutex::scoped_lock lm (_mutex);
 
 {
        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)) {
        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';
        
        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;
        fclose (f);
 
        return out;
index f20b0a14866b5aedda0b3b4de883e7b40d3cc7c3..cee1ce495e1a3f2fc8e5dd3a3a2882fba0b552a6 100644 (file)
@@ -48,7 +48,7 @@ public:
 
        void set_types (int types);
 
 
        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:
        
 
 protected:
        
@@ -69,7 +69,7 @@ class FileLog : public Log
 public:
        FileLog (boost::filesystem::path file);
 
 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);
 
 private:
        void do_log (std::string m);
@@ -80,7 +80,7 @@ private:
 class NullLog : public Log
 {
 public:
 class NullLog : public Log
 {
 public:
-       std::string head_and_tail () const {
+       std::string head_and_tail (int) const {
                return "";
        }
 
                return "";
        }
 
index bcd5c8df6481254223d8c489c5759c255aec13d7..58310377c04301774b7c77120e334d804a17b094 100644 (file)
@@ -47,12 +47,12 @@ public:
                return _log;
        }
 
                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;
                }
 
-               return _log.substr (0, 1024) + _log.substr (_log.size() - 1025, 1024);
+               return _log.substr (0, amount) + _log.substr (_log.size() - amount - 1, amount);
        }
 
 private:
        }
 
 private:
index 4769503c1314f0f8f5682ba686de28451c835911..1cf7a7757cf39e3dda7ee9aef93fea51cbbb294b 100644 (file)
@@ -25,5 +25,6 @@ using std::cout;
 BOOST_AUTO_TEST_CASE (file_log_test)
 {
        FileLog log ("test/data/short.log");
 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");
 }
 }