2 Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 * @brief A very simple logging class.
27 #include "safe_stringstream.h"
35 int const Log::TYPE_GENERAL = 0x1;
36 int const Log::TYPE_WARNING = 0x2;
37 int const Log::TYPE_ERROR = 0x4;
38 int const Log::TYPE_DEBUG_DECODE = 0x8;
39 int const Log::TYPE_TIMING = 0x10;
44 _config_connection = Config::instance()->Changed.connect (boost::bind (&Log::config_changed, this));
49 Log::config_changed ()
51 set_types (Config::instance()->log_types ());
54 /** @param n String to log */
56 Log::log (string message, int type)
58 boost::mutex::scoped_lock lm (_mutex);
60 if ((_types & type) == 0) {
66 string a = ctime (&t);
69 s << a.substr (0, a.length() - 1) << N_(": ");
71 if (type & TYPE_ERROR) {
75 if (type & TYPE_WARNING) {
84 Log::microsecond_log (string m, int t)
86 boost::mutex::scoped_lock lm (_mutex);
88 if ((_types & t) == 0) {
93 gettimeofday (&tv, 0);
96 s << tv.tv_sec << N_(":") << tv.tv_usec << N_(" ") << m;
101 Log::dcp_log (dcp::NoteType type, string m)
104 case dcp::DCP_PROGRESS:
105 log (m, TYPE_GENERAL);
111 log (m, TYPE_WARNING);
117 Log::set_types (int t)
119 boost::mutex::scoped_lock lm (_mutex);
123 /** @param file Filename to write log to */
124 FileLog::FileLog (boost::filesystem::path file)
131 FileLog::do_log (string m)
133 FILE* f = fopen_boost (_file, "a");
135 cout << "(could not log to " << _file.string() << "): " << m << "\n";
139 fprintf (f, "%s\n", m.c_str ());
144 FileLog::head_and_tail (int amount) const
146 boost::mutex::scoped_lock lm (_mutex);
148 uintmax_t head_amount = amount;
149 uintmax_t tail_amount = amount;
150 uintmax_t size = boost::filesystem::file_size (_file);
152 if (size < (head_amount + tail_amount)) {
157 FILE* f = fopen_boost (_file, "r");
164 char* buffer = new char[max(head_amount, tail_amount) + 1];
166 int N = fread (buffer, 1, head_amount, f);
168 out += string (buffer);
170 if (tail_amount > 0) {
171 out += "\n .\n .\n .\n";
173 fseek (f, - tail_amount - 1, SEEK_END);
175 N = fread (buffer, 1, tail_amount, f);
177 out += string (buffer) + "\n";