1 /*
2     delaboratory - color correction utility
3     Copyright (C) 2011 Jacek Poplawski
4 
5     This program is free software: you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation, either version 3 of the License, or
8     (at your option) any later version.
9 
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14 
15     You should have received a copy of the GNU General Public License
16     along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #include "logger.h"
20 #include "str.h"
21 #include <sstream>
22 #include <wx/wx.h>
23 
24 #define LOCK_THRESHOLD 100
25 #define LOGGING 1
26 
27 static bool loggerActive = false;
28 
29 class deLoggerHelp
30 {
31     private:
32     public:
33         wxThreadIdType main_id;
34         wxStopWatch sw;
deLoggerHelp()35         deLoggerHelp()
36         {
37         }
~deLoggerHelp()38         virtual ~deLoggerHelp()
39         {
40         }
41 
42 };
43 
getLogger()44 deLogger& deLogger::getLogger()
45 {
46     static deLogger logger;
47     return logger;
48 }
49 
deLogger()50 deLogger::deLogger()
51 {
52     help = new deLoggerHelp();
53     f = NULL;
54     started = false;
55     loggerActive = true;
56 }
57 
~deLogger()58 deLogger::~deLogger()
59 {
60     logInfo("closing logger...");
61     loggerActive = false;
62 
63     mutex.lock();
64 
65     if (f)
66     {
67         f->close();
68         delete f;
69     }
70 
71     delete help;
72 
73     mutex.unlock();
74 }
75 
setFile(const std::string & fileName)76 void deLogger::setFile(const std::string& fileName)
77 {
78 #ifdef LOGGING
79     mutex.lock();
80 
81     if (f)
82     {
83         f->close();
84         delete f;
85     }
86 
87     f = new std::ofstream(fileName.c_str());
88     if (!f)
89     {
90         logError("can't create logfile: " + fileName);
91     }
92 
93     mutex.unlock();
94 #endif
95 }
96 
getThreadName()97 std::string deLogger::getThreadName()
98 {
99     if (!started)
100     {
101         help->main_id = wxThread::GetCurrentId();
102         started = true;
103     }
104 
105     wxThreadIdType c_id = wxThread::GetCurrentId();
106 
107     std::string thr = "main";
108 
109     if (help->main_id != c_id)
110     {
111         std::ostringstream oss;
112         oss.str("");
113         oss << c_id;
114         thr = oss.str();
115     }
116 
117     return thr;
118 }
119 
log(const std::string & message)120 void deLogger::log(const std::string& message)
121 {
122     mutex.lock();
123 
124     if (f)
125     {
126         int t = help->sw.Time();
127 
128         (*f) << t << ": [" << getThreadName() << "] " << message << std::endl;
129     }
130 
131     mutex.unlock();
132 }
133 
logInfo(const std::string & message)134 void deLogger::logInfo(const std::string& message)
135 {
136     log("INFO " + message);
137 }
138 
getTime() const139 int deLogger::getTime() const
140 {
141     int t = help->sw.Time();
142     return t;
143 }
144 
logInfo(const std::string & message)145 void logInfo(const std::string& message)
146 {
147 #ifdef LOGGING
148     if (loggerActive)
149     {
150         deLogger::getLogger().logInfo(message);
151     }
152 #endif
153 }
154 
155 static deMutex logErrorMutex;
156 
logError(const std::string & message)157 void logError(const std::string& message)
158 {
159     const std::string m = "ERROR " + message;
160     logErrorMutex.lock();
161     std::cout << m << std::endl;
162     logErrorMutex.unlock();
163 #ifdef LOGGING
164     if (loggerActive)
165     {
166         deLogger::getLogger().log(m);
167     }
168 #endif
169 }
170 
171