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