comparison Core/Logging.cpp @ 1731:b6f656a0bf2c

"--logdir" creates a single log file instead of 3 separate files for errors/warnings/infos
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 21 Oct 2015 11:45:51 +0200
parents 0dba274074eb
children b1291df2f780
comparison
equal deleted inserted replaced
1730:bc34c69b594a 1731:b6f656a0bf2c
142 142
143 std::ostream* error_; 143 std::ostream* error_;
144 std::ostream* warning_; 144 std::ostream* warning_;
145 std::ostream* info_; 145 std::ostream* info_;
146 146
147 std::auto_ptr<std::ofstream> errorFile_; 147 std::auto_ptr<std::ofstream> file_;
148 std::auto_ptr<std::ofstream> warningFile_;
149 std::auto_ptr<std::ofstream> infoFile_;
150 148
151 LoggingState() : 149 LoggingState() :
152 infoEnabled_(false), 150 infoEnabled_(false),
153 traceEnabled_(false), 151 traceEnabled_(false),
154 error_(&std::cerr), 152 error_(&std::cerr),
170 { 168 {
171 namespace Logging 169 namespace Logging
172 { 170 {
173 static void GetLogPath(boost::filesystem::path& log, 171 static void GetLogPath(boost::filesystem::path& log,
174 boost::filesystem::path& link, 172 boost::filesystem::path& link,
175 const char* level, 173 const std::string& suffix,
176 const std::string& directory) 174 const std::string& directory)
177 { 175 {
178 /** 176 /**
179 From Google Log documentation: 177 From Google Log documentation:
180 178
181 Unless otherwise specified, logs will be written to the filename 179 Unless otherwise specified, logs will be written to the filename
182 "<program name>.<hostname>.<user name>.log.<severity level>.", 180 "<program name>.<hostname>.<user name>.log<suffix>.",
183 followed by the date, time, and pid (you can't prevent the date, 181 followed by the date, time, and pid (you can't prevent the date,
184 time, and pid from being in the filename). 182 time, and pid from being in the filename).
185 183
186 In this implementation : "hostname" and "username" are not used 184 In this implementation : "hostname" and "username" are not used
187 **/ 185 **/
206 now.time_of_day().seconds(), 204 now.time_of_day().seconds(),
207 Toolbox::GetProcessId()); 205 Toolbox::GetProcessId());
208 206
209 std::string programName = exe.filename().replace_extension("").string(); 207 std::string programName = exe.filename().replace_extension("").string();
210 208
211 log = (root / (programName + ".log." + 209 log = (root / (programName + ".log" + suffix + "." + std::string(date)));
212 std::string(level) + "." + 210 link = (root / (programName + ".log" + suffix));
213 std::string(date))); 211 }
214 212
215 link = (root / (programName + "." + std::string(level))); 213
216 } 214 static void PrepareLogFile(std::auto_ptr<std::ofstream>& file,
217 215 const std::string& suffix,
218
219 static void PrepareLogFile(std::ostream*& stream,
220 std::auto_ptr<std::ofstream>& file,
221 const char* level,
222 const std::string& directory) 216 const std::string& directory)
223 { 217 {
224 boost::filesystem::path log, link; 218 boost::filesystem::path log, link;
225 GetLogPath(log, link, level, directory); 219 GetLogPath(log, link, suffix, directory);
226 220
227 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) 221 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
228 boost::filesystem::remove(link); 222 boost::filesystem::remove(link);
229 boost::filesystem::create_symlink(log.filename(), link); 223 boost::filesystem::create_symlink(log.filename(), link);
230 #endif 224 #endif
231 225
232 file.reset(new std::ofstream(log.string().c_str())); 226 file.reset(new std::ofstream(log.string().c_str()));
233 stream = file.get();
234 } 227 }
235 228
236 229
237 void Initialize() 230 void Initialize()
238 { 231 {
271 void SetTargetFolder(const std::string& path) 264 void SetTargetFolder(const std::string& path)
272 { 265 {
273 boost::mutex::scoped_lock lock(loggingMutex_); 266 boost::mutex::scoped_lock lock(loggingMutex_);
274 assert(loggingState_.get() != NULL); 267 assert(loggingState_.get() != NULL);
275 268
276 PrepareLogFile(loggingState_->error_, loggingState_->errorFile_, "ERROR", path); 269 PrepareLogFile(loggingState_->file_, "" /* no suffix */, path);
277 PrepareLogFile(loggingState_->warning_, loggingState_->warningFile_, "WARNING", path); 270
278 PrepareLogFile(loggingState_->info_, loggingState_->infoFile_, "INFO", path); 271 loggingState_->warning_ = loggingState_->file_.get();
272 loggingState_->error_ = loggingState_->file_.get();
273 loggingState_->info_ = loggingState_->file_.get();
279 } 274 }
280 275
281 InternalLogger::InternalLogger(const char* level, 276 InternalLogger::InternalLogger(const char* level,
282 const char* file, 277 const char* file,
283 int line) : 278 int line) :