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