Various markup and tweaks.
[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 <fstream>
25 #include <time.h>
26 #include "log.h"
27
28 #include "i18n.h"
29
30 using namespace std;
31
32 Log::Log ()
33         : _level (STANDARD)
34 {
35
36 }
37
38 /** @param n String to log */
39 void
40 Log::log (string m, Level l)
41 {
42         boost::mutex::scoped_lock lm (_mutex);
43
44         if (l > _level) {
45                 return;
46         }
47
48         time_t t;
49         time (&t);
50         string a = ctime (&t);
51
52         stringstream s;
53         s << a.substr (0, a.length() - 1) << N_(": ") << m;
54         do_log (s.str ());
55 }
56
57 void
58 Log::microsecond_log (string m, Level l)
59 {
60         boost::mutex::scoped_lock lm (_mutex);
61
62         if (l > _level) {
63                 return;
64         }
65
66         struct timeval tv;
67         gettimeofday (&tv, 0);
68
69         stringstream s;
70         s << tv.tv_sec << N_(":") << tv.tv_usec << N_(" ") << m;
71         do_log (s.str ());
72 }       
73
74 void
75 Log::set_level (Level l)
76 {
77         boost::mutex::scoped_lock lm (_mutex);
78         _level = l;
79 }
80
81 void
82 Log::set_level (string l)
83 {
84         if (l == N_("verbose")) {
85                 set_level (VERBOSE);
86                 return;
87         } else if (l == N_("timing")) {
88                 set_level (TIMING);
89                 return;
90         }
91
92         set_level (STANDARD);
93 }
94
95 /** @param file Filename to write log to */
96 FileLog::FileLog (string file)
97         : _file (file)
98 {
99
100 }
101
102 void
103 FileLog::do_log (string m)
104 {
105         ofstream f (_file.c_str(), fstream::app);
106         f << m << N_("\n");
107 }
108