Calm down default logging a bit.
[dcpomatic.git] / src / lib / log.cc
1 /*
2     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
20 /** @file src/log.cc
21  *  @brief A very simple logging class.
22  */
23
24 #include "log.h"
25 #include "cross.h"
26 #include "config.h"
27 #include "safe_stringstream.h"
28 #include <time.h>
29 #include <cstdio>
30
31 #include "i18n.h"
32
33 using namespace std;
34
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_DEBUG_ENCODE = 0x10;
40 int const Log::TYPE_TIMING       = 0x20;
41
42 Log::Log ()
43         : _types (0)
44 {
45         _config_connection = Config::instance()->Changed.connect (boost::bind (&Log::config_changed, this));
46         config_changed ();
47 }
48
49 void
50 Log::config_changed ()
51 {
52         set_types (Config::instance()->log_types ());
53 }
54
55 /** @param n String to log */
56 void
57 Log::log (string message, int type)
58 {
59         boost::mutex::scoped_lock lm (_mutex);
60
61         if ((_types & type) == 0) {
62                 return;
63         }
64
65         time_t t;
66         time (&t);
67         string a = ctime (&t);
68
69         SafeStringStream s;
70         s << a.substr (0, a.length() - 1) << N_(": ");
71
72         if (type & TYPE_ERROR) {
73                 s << "ERROR: ";
74         }
75
76         if (type & TYPE_WARNING) {
77                 s << "WARNING: ";
78         }
79
80         s << message;
81         do_log (s.str ());
82 }
83
84 void
85 Log::microsecond_log (string m, int t)
86 {
87         boost::mutex::scoped_lock lm (_mutex);
88
89         if ((_types & t) == 0) {
90                 return;
91         }
92
93         struct timeval tv;
94         gettimeofday (&tv, 0);
95
96         SafeStringStream s;
97         s << tv.tv_sec << N_(":") << tv.tv_usec << N_(" ") << m;
98         do_log (s.str ());
99 }
100
101 void
102 Log::dcp_log (dcp::NoteType type, string m)
103 {
104         switch (type) {
105         case dcp::DCP_PROGRESS:
106                 log (m, TYPE_GENERAL);
107                 break;
108         case dcp::DCP_ERROR:
109                 log (m, TYPE_ERROR);
110                 break;
111         case dcp::DCP_NOTE:
112                 log (m, TYPE_WARNING);
113                 break;
114         }
115 }
116
117 void
118 Log::set_types (int t)
119 {
120         boost::mutex::scoped_lock lm (_mutex);
121         _types = t;
122 }
123
124 /** @param file Filename to write log to */
125 FileLog::FileLog (boost::filesystem::path file)
126         : _file (file)
127 {
128
129 }
130
131 void
132 FileLog::do_log (string m)
133 {
134         FILE* f = fopen_boost (_file, "a");
135         if (!f) {
136                 cout << "(could not log to " << _file.string() << "): " << m << "\n";
137                 return;
138         }
139
140         fprintf (f, "%s\n", m.c_str ());
141         fclose (f);
142 }
143
144 string
145 FileLog::head_and_tail (int amount) const
146 {
147         boost::mutex::scoped_lock lm (_mutex);
148
149         uintmax_t head_amount = amount;
150         uintmax_t tail_amount = amount;
151         uintmax_t size = boost::filesystem::file_size (_file);
152
153         if (size < (head_amount + tail_amount)) {
154                 head_amount = size;
155                 tail_amount = 0;
156         }
157
158         FILE* f = fopen_boost (_file, "r");
159         if (!f) {
160                 return "";
161         }
162
163         string out;
164
165         char* buffer = new char[max(head_amount, tail_amount) + 1];
166
167         int N = fread (buffer, 1, head_amount, f);
168         buffer[N] = '\0';
169         out += string (buffer);
170
171         if (tail_amount > 0) {
172                 out +=  "\n .\n .\n .\n";
173
174                 fseek (f, - tail_amount - 1, SEEK_END);
175
176                 N = fread (buffer, 1, tail_amount, f);
177                 buffer[N] = '\0';
178                 out += string (buffer) + "\n";
179         }
180
181         delete[] buffer;
182         fclose (f);
183
184         return out;
185 }