Merge master.
[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 "log.h"
27 #include "cross.h"
28 #include "config.h"
29
30 #include "i18n.h"
31
32 using namespace std;
33
34 int const Log::TYPE_GENERAL = 0x1;
35 int const Log::TYPE_WARNING = 0x2;
36 int const Log::TYPE_ERROR   = 0x4;
37 int const Log::TYPE_TIMING  = 0x8;
38
39 Log::Log ()
40         : _types (0)
41 {
42         _config_connection = Config::instance()->Changed.connect (boost::bind (&Log::config_changed, this));
43         config_changed ();
44 }
45
46 void
47 Log::config_changed ()
48 {
49         set_types (Config::instance()->log_types ());
50 }
51
52 /** @param n String to log */
53 void
54 Log::log (string message, int type)
55 {
56         boost::mutex::scoped_lock lm (_mutex);
57
58         if ((_types & type) == 0) {
59                 return;
60         }
61
62         time_t t;
63         time (&t);
64         string a = ctime (&t);
65
66         stringstream s;
67         s << a.substr (0, a.length() - 1) << N_(": ");
68
69         if (type & TYPE_ERROR) {
70                 s << "ERROR: ";
71         }
72
73         if (type & TYPE_WARNING) {
74                 s << "WARNING: ";
75         }
76         
77         s << message;
78         do_log (s.str ());
79 }
80
81 void
82 Log::microsecond_log (string m, int t)
83 {
84         boost::mutex::scoped_lock lm (_mutex);
85
86         if ((_types & t) == 0) {
87                 return;
88         }
89
90         struct timeval tv;
91         gettimeofday (&tv, 0);
92
93         stringstream s;
94         s << tv.tv_sec << N_(":") << tv.tv_usec << N_(" ") << m;
95         do_log (s.str ());
96 }       
97
98 void
99 Log::set_types (int t)
100 {
101         boost::mutex::scoped_lock lm (_mutex);
102         _types = t;
103 }
104
105 /** @param file Filename to write log to */
106 FileLog::FileLog (boost::filesystem::path file)
107         : _file (file)
108 {
109
110 }
111
112 void
113 FileLog::do_log (string m)
114 {
115         FILE* f = fopen_boost (_file, "a");
116         if (!f) {
117                 cout << "(could not log to " << _file.string() << "): " << m << "\n";
118                 return;
119         }
120
121         fprintf (f, "%s\n", m.c_str ());
122         fclose (f);
123 }
124