Tidy and fix logging.
[dcpomatic.git] / src / tools / dcpomatic_server.cc
1 /*
2     Copyright (C) 2012-2015 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 #include "wx/wx_util.h"
22 #include "wx/wx_signal_manager.h"
23 #include "lib/util.h"
24 #include "lib/encoded_log_entry.h"
25 #include "lib/encode_server.h"
26 #include "lib/config.h"
27 #include "lib/log.h"
28 #include "lib/signaller.h"
29 #include "lib/cross.h"
30 #include "lib/dcpomatic_log.h"
31 #include <wx/taskbar.h>
32 #include <wx/splash.h>
33 #include <wx/icon.h>
34 #include <boost/thread.hpp>
35 #include <boost/foreach.hpp>
36 #include <boost/optional.hpp>
37 #include <iostream>
38
39 using std::cout;
40 using std::string;
41 using std::exception;
42 using std::list;
43 using std::fixed;
44 using std::setprecision;
45 using boost::shared_ptr;
46 using boost::thread;
47 using boost::bind;
48 using boost::optional;
49 using boost::dynamic_pointer_cast;
50
51 enum {
52         ID_status = 1,
53         ID_quit,
54         ID_timer
55 };
56
57 static unsigned int const log_lines = 32;
58
59 class ServerLog : public Log, public Signaller
60 {
61 public:
62
63         string get () const {
64                 string a;
65                 BOOST_FOREACH (string const & i, _log) {
66                         a += i + "\n";
67                 }
68                 return a;
69         }
70
71         string head_and_tail (int) const {
72                 /* Not necessary */
73                 return "";
74         }
75
76         float fps () const {
77                 boost::mutex::scoped_lock lm (_state_mutex);
78                 return _fps;
79         }
80
81         boost::signals2::signal<void(string)> Appended;
82         boost::signals2::signal<void(int)> Removed;
83
84 private:
85         void do_log (shared_ptr<const LogEntry> entry)
86         {
87                 time_t const s = entry->seconds ();
88                 struct tm* local = localtime (&s);
89                 if (
90                         !_last_time ||
91                         local->tm_yday != _last_time->tm_yday ||
92                         local->tm_year != _last_time->tm_year ||
93                         local->tm_hour != _last_time->tm_hour ||
94                         local->tm_min != _last_time->tm_min
95                         ) {
96                         char buffer[64];
97                         strftime (buffer, 64, "%c", local);
98                         append (buffer);
99                 }
100
101                 append (entry->message ());
102                 if (_log.size() > log_lines) {
103                         emit (boost::bind (boost::ref (Removed), _log.front().length()));
104                         _log.pop_front ();
105                 }
106                 _last_time = *local;
107
108                 shared_ptr<const EncodedLogEntry> encoded = dynamic_pointer_cast<const EncodedLogEntry> (entry);
109                 if (encoded) {
110                         _history.push_back (encoded->seconds ());
111                         if (_history.size() > 48) {
112                                 _history.pop_front ();
113                         }
114                         if (_history.size() > 2) {
115                                 boost::mutex::scoped_lock lm (_state_mutex);
116                                 _fps = _history.size() / (_history.back() - _history.front());
117                         }
118                 }
119         }
120
121         void append (string s)
122         {
123                 _log.push_back (s);
124                 emit (boost::bind (boost::ref (Appended), s));
125         }
126
127         list<string> _log;
128         optional<struct tm> _last_time;
129         std::list<double> _history;
130
131         mutable boost::mutex _state_mutex;
132         float _fps;
133 };
134
135 static shared_ptr<ServerLog> server_log;
136
137 class StatusDialog : public wxDialog
138 {
139 public:
140         StatusDialog ()
141                 : wxDialog (
142                         0, wxID_ANY, _("DCP-o-matic Encode Server"),
143                         wxDefaultPosition, wxDefaultSize,
144                         wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER
145                         )
146         {
147                 wxFlexGridSizer* state_sizer = new wxFlexGridSizer (2, DCPOMATIC_SIZER_GAP, DCPOMATIC_SIZER_GAP);
148
149                 add_label_to_sizer (state_sizer, this, _("Frames per second"), true);
150                 _fps = new wxStaticText (this, wxID_ANY, wxT(""));
151                 state_sizer->Add (_fps);
152
153                 wxFlexGridSizer* log_sizer = new wxFlexGridSizer (1, DCPOMATIC_SIZER_GAP, DCPOMATIC_SIZER_GAP);
154                 log_sizer->AddGrowableCol (0, 1);
155
156                 wxClientDC dc (this);
157                 wxSize size = dc.GetTextExtent (wxT ("This is the length of the file label it should be quite long"));
158                 int const height = (size.GetHeight() + 2) * log_lines;
159                 SetSize (700, height + DCPOMATIC_SIZER_GAP * 2);
160
161                 _text = new wxTextCtrl (
162                         this, wxID_ANY, std_to_wx (server_log->get()), wxDefaultPosition, wxSize (-1, height),
163                         wxTE_READONLY | wxTE_MULTILINE
164                         );
165
166                 log_sizer->Add (_text, 1, wxEXPAND);
167
168                 wxBoxSizer* overall_sizer = new wxBoxSizer (wxVERTICAL);
169                 overall_sizer->Add (state_sizer, 0, wxALL, DCPOMATIC_SIZER_GAP);
170                 overall_sizer->Add (log_sizer, 1, wxEXPAND | wxALL, DCPOMATIC_SIZER_GAP);
171                 SetSizer (overall_sizer);
172                 overall_sizer->Layout ();
173
174                 Bind (wxEVT_TIMER, boost::bind (&StatusDialog::update_state, this));
175                 _timer.reset (new wxTimer (this));
176                 _timer->Start (1000);
177
178                 server_log->Appended.connect (bind (&StatusDialog::appended, this, _1));
179                 server_log->Removed.connect (bind (&StatusDialog::removed, this, _1));
180         }
181
182 private:
183         void appended (string s)
184         {
185                 (*_text) << s << "\n";
186         }
187
188         void removed (int n)
189         {
190                 _text->Remove (0, n + 1);
191         }
192
193         void update_state ()
194         {
195                 _fps->SetLabel (wxString::Format ("%.1f", server_log->fps()));
196         }
197
198         wxTextCtrl* _text;
199         wxStaticText* _fps;
200         boost::shared_ptr<wxTimer> _timer;
201 };
202
203 class TaskBarIcon : public wxTaskBarIcon
204 {
205 public:
206         TaskBarIcon ()
207                 : _status (0)
208         {
209 #ifdef DCPOMATIC_WINDOWS
210                 wxIcon icon (std_to_wx ("id"));
211 #else
212                 wxInitAllImageHandlers();
213 #ifdef DCPOMATIC_LINUX
214                 wxBitmap bitmap (wxString::Format (wxT ("%s/dcpomatic2_server_small.png"), std_to_wx (shared_path().string())), wxBITMAP_TYPE_PNG);
215 #endif
216 #ifdef DCPOMATIC_OSX
217                 wxBitmap bitmap (wxString::Format (wxT ("%s/dcpomatic_small.png"), std_to_wx (shared_path().string())), wxBITMAP_TYPE_PNG);
218 #endif
219                 wxIcon icon;
220                 icon.CopyFromBitmap (bitmap);
221 #endif
222
223                 SetIcon (icon, std_to_wx ("DCP-o-matic Encode Server"));
224
225                 Bind (wxEVT_MENU, boost::bind (&TaskBarIcon::status, this), ID_status);
226                 Bind (wxEVT_MENU, boost::bind (&TaskBarIcon::quit, this), ID_quit);
227         }
228
229         wxMenu* CreatePopupMenu ()
230         {
231                 wxMenu* menu = new wxMenu;
232                 menu->Append (ID_status, std_to_wx ("Status..."));
233                 menu->Append (ID_quit, std_to_wx ("Quit"));
234                 return menu;
235         }
236
237 private:
238         void status ()
239         {
240                 if (!_status) {
241                         _status = new StatusDialog ();
242                 }
243                 _status->Show ();
244         }
245
246         void quit ()
247         {
248                 wxTheApp->ExitMainLoop ();
249         }
250
251         StatusDialog* _status;
252 };
253
254 class App : public wxApp, public ExceptionStore
255 {
256 public:
257         App ()
258                 : wxApp ()
259                 , _thread (0)
260                 , _icon (0)
261         {}
262
263 private:
264
265         bool OnInit ()
266         {
267                 if (!wxApp::OnInit ()) {
268                         return false;
269                 }
270
271                 server_log.reset (new ServerLog);
272                 dcpomatic_log = server_log;
273
274                 Config::FailedToLoad.connect (boost::bind (&App::config_failed_to_load, this));
275                 Config::Warning.connect (boost::bind (&App::config_warning, this, _1));
276
277                 wxSplashScreen* splash = maybe_show_splash ();
278
279                 dcpomatic_setup_path_encoding ();
280                 dcpomatic_setup_i18n ();
281                 dcpomatic_setup ();
282                 Config::drop ();
283
284                 signal_manager = new wxSignalManager (this);
285                 Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
286
287                 _icon = new TaskBarIcon;
288                 _thread = new thread (bind (&App::main_thread, this));
289
290                 Bind (wxEVT_TIMER, boost::bind (&App::check, this));
291                 _timer.reset (new wxTimer (this));
292                 _timer->Start (1000);
293
294                 if (splash) {
295                         splash->Destroy ();
296                 }
297
298                 return true;
299         }
300
301         int OnExit ()
302         {
303                 delete _icon;
304                 return wxApp::OnExit ();
305         }
306
307         void main_thread ()
308         try {
309                 EncodeServer server (false, Config::instance()->server_encoding_threads());
310                 server.run ();
311         } catch (...) {
312                 store_current ();
313         }
314
315         void check ()
316         {
317                 try {
318                         rethrow ();
319                 } catch (exception& e) {
320                         error_dialog (0, std_to_wx (e.what ()));
321                         wxTheApp->ExitMainLoop ();
322                 } catch (...) {
323                         error_dialog (0, _("An unknown error has occurred with the DCP-o-matic server."));
324                         wxTheApp->ExitMainLoop ();
325                 }
326         }
327
328         void idle ()
329         {
330                 signal_manager->ui_idle ();
331         }
332
333         void config_failed_to_load ()
334         {
335                 message_dialog (0, _("The existing configuration failed to load.  Default values will be used instead.  These may take a short time to create."));
336         }
337
338         void config_warning (string m)
339         {
340                 message_dialog (0, std_to_wx (m));
341         }
342
343         boost::thread* _thread;
344         TaskBarIcon* _icon;
345         shared_ptr<wxTimer> _timer;
346 };
347
348 IMPLEMENT_APP (App)