Use dcp::File in DCP-o-matic (#2231).
[dcpomatic.git] / src / lib / file_log.cc
1 /*
2     Copyright (C) 2012-2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21
22 #include "file_log.h"
23 #include "cross.h"
24 #include "config.h"
25 #include <dcp/file.h>
26 #include <cstdio>
27 #include <iostream>
28 #include <cerrno>
29
30
31 using std::cout;
32 using std::string;
33 using std::max;
34 using std::shared_ptr;
35
36
37 /** @param file Filename to write log to */
38 FileLog::FileLog (boost::filesystem::path file)
39         : _file (file)
40 {
41         set_types (Config::instance()->log_types());
42 }
43
44
45 FileLog::FileLog (boost::filesystem::path file, int types)
46         : _file (file)
47 {
48         set_types (types);
49 }
50
51
52 void
53 FileLog::do_log (shared_ptr<const LogEntry> entry)
54 {
55         dcp::File f(_file, "a");
56         if (!f) {
57                 cout << "(could not log to " << _file.string() << " error " << errno << "): " << entry->get() << "\n";
58                 return;
59         }
60
61         fprintf(f.get(), "%s\n", entry->get().c_str());
62 }
63
64
65 string
66 FileLog::head_and_tail (int amount) const
67 {
68         boost::mutex::scoped_lock lm (_mutex);
69
70         uintmax_t head_amount = amount;
71         uintmax_t tail_amount = amount;
72         boost::system::error_code ec;
73         uintmax_t size = boost::filesystem::file_size (_file, ec);
74         if (size == static_cast<uintmax_t>(-1)) {
75                 return "";
76         }
77
78         if (size < (head_amount + tail_amount)) {
79                 head_amount = size;
80                 tail_amount = 0;
81         }
82
83         dcp::File f(_file, "r");
84         if (!f) {
85                 return "";
86         }
87
88         string out;
89
90         std::vector<char> buffer(max(head_amount, tail_amount) + 1);
91
92         int N = f.read(buffer.data(), 1, head_amount);
93         buffer[N] = '\0';
94         out += string (buffer.data());
95
96         if (tail_amount > 0) {
97                 out +=  "\n .\n .\n .\n";
98
99                 f.seek(- tail_amount - 1, SEEK_END);
100
101                 N = f.read(buffer.data(), 1, tail_amount);
102                 buffer[N] = '\0';
103                 out += string(buffer.data()) + "\n";
104         }
105
106         return out;
107 }