}
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)) {
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;
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:
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);
class NullLog : public Log
{
public:
- std::string head_and_tail () const {
+ std::string head_and_tail (int) const {
return "";
}
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:
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");
}