Tidy up logging a bit. Make it configurable from the GUI.
[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 <time.h>
25 #include <cstdio>
26 #include <boost/algorithm/string.hpp>
27 #include "log.h"
28 #include "cross.h"
29 #include "config.h"
30
31 #include "i18n.h"
32
33 using namespace std;
34 using boost::algorithm::is_any_of;
35 using boost::algorithm::split;
36
37 int const Log::GENERAL = 0x1;
38 int const Log::WARNING = 0x2;
39 int const Log::ERROR   = 0x4;
40 int const Log::TIMING  = 0x8;
41
42 Log::Log ()
43         : _types (0)
44 {
45         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         stringstream s;
70         s << a.substr (0, a.length() - 1) << N_(": ");
71
72         if (type & ERROR) {
73                 s << "ERROR: ";
74         }
75
76         if (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         stringstream s;
97         s << tv.tv_sec << N_(":") << tv.tv_usec << N_(" ") << m;
98         do_log (s.str ());
99 }       
100
101 void
102 Log::set_types (int t)
103 {
104         boost::mutex::scoped_lock lm (_mutex);
105         _types = t;
106 }
107
108 /** @param A comma-separate list of debug types to enable */
109 void
110 Log::set_types (string t)
111 {
112         boost::mutex::scoped_lock lm (_mutex);
113
114         vector<string> types;
115         split (types, t, is_any_of (","));
116
117         _types = 0;
118
119         for (vector<string>::const_iterator i = types.begin(); i != types.end(); ++i) {
120                 if (*i == N_("general")) {
121                         _types |= GENERAL;
122                 } else if (*i == N_("warning")) {
123                         _types |= WARNING;
124                 } else if (*i == N_("error")) {
125                         _types |= ERROR;
126                 } else if (*i == N_("timing")) {
127                         _types |= TIMING;
128                 }
129         }
130 }
131
132 /** @param file Filename to write log to */
133 FileLog::FileLog (boost::filesystem::path file)
134         : _file (file)
135 {
136
137 }
138
139 void
140 FileLog::do_log (string m)
141 {
142         FILE* f = fopen_boost (_file, "a");
143         if (!f) {
144                 cout << "(could not log to " << _file.string() << "): " << m << "\n";
145                 return;
146         }
147
148         fprintf (f, "%s\n", m.c_str ());
149         fclose (f);
150 }
151