diff --git a/Core/Code/IO/mitkLog.cpp b/Core/Code/IO/mitkLog.cpp index 7ca6f555ea..620d12e585 100644 --- a/Core/Code/IO/mitkLog.cpp +++ b/Core/Code/IO/mitkLog.cpp @@ -1,234 +1,234 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #include #include #include #include #include #include #include #include static itk::SimpleFastMutexLock logMutex; static mitk::LoggingBackend *mitkLogBackend = 0; static std::ofstream *logFile = 0; static std::string logFileName = ""; static std::stringstream *outputWindow = 0; static bool logOutputWindow = false; void mitk::LoggingBackend::EnableAdditionalConsoleWindow(bool enable) { logOutputWindow = enable; } void mitk::LoggingBackend::ProcessMessage(const mbilog::LogMessage& l ) { logMutex.Lock(); #ifdef _WIN32 FormatSmart( l, (int)GetCurrentThreadId() ); #else FormatSmart( l ); #endif if(logFile) { #ifdef _WIN32 FormatFull( *logFile, l, (int)GetCurrentThreadId() ); #else FormatFull( *logFile, l ); #endif } if(logOutputWindow) { if(outputWindow == NULL) { outputWindow = new std::stringstream();} outputWindow->str(""); outputWindow->clear(); #ifdef _WIN32 FormatFull( *outputWindow, l, (int)GetCurrentThreadId() ); #else FormatFull( *outputWindow, l ); #endif itk::OutputWindow::GetInstance()->DisplayText(outputWindow->str().c_str()); } logMutex.Unlock(); } void mitk::LoggingBackend::Register() { if(mitkLogBackend) return; mitkLogBackend = new mitk::LoggingBackend(); mbilog::RegisterBackend( mitkLogBackend ); } void mitk::LoggingBackend::Unregister() { if(mitkLogBackend) { SetLogFile(0); mbilog::UnregisterBackend( mitkLogBackend ); delete mitkLogBackend; mitkLogBackend=0; } } void mitk::LoggingBackend::SetLogFile(const char *file) { // closing old logfile { bool closed = false; std::string closedFileName; logMutex.Lock(); if(logFile) { closed = true; closedFileName = logFileName; logFile->close(); delete logFile; logFile = 0; logFileName = ""; } logMutex.Unlock(); if(closed) { MITK_INFO << "closing logfile (" << closedFileName << ")" ; } } // opening new logfile if(file) { logMutex.Lock(); logFileName = file; logFile = new std::ofstream( ); logFile->open( file, std::ios_base::out | std::ios_base::app ); if(logFile->good()) { logMutex.Unlock(); MITK_INFO << "Logfile: " << logFileName ; } else { delete logFile; logFile = 0; logMutex.Unlock(); MITK_WARN << "opening logfile '" << file << "' for writing failed"; } // mutex is now unlocked } } std::string mitk::LoggingBackend::GetLogFile() { return logFileName; } void mitk::LoggingBackend::CatchLogFileCommandLineParameter(int &argc,char **argv) { int r; for(r=1;r=argc) { --argc; MITK_ERROR << "--logfile parameter found, but no file given"; return; } mitk::LoggingBackend::SetLogFile(argv[r+1]); for(r+=2;r= 1 ; r-- ) { std::stringstream dst; dst << prefixPath.c_str() << "-" << r << ".log"; std::stringstream src; src << prefixPath.c_str() << "-" << r-1 << ".log"; //check if the source exists if (CheckIfFileExists(src.str())) //if yes: rename it { int retVal = ::rename( src.str().c_str(), dst.str().c_str() ); if (retVal!=0) {mitkThrow() << "Problem while renaming the log files. Maybe the access to this files is blocked. Aborting!";} } } //create new empty name and return it { std::stringstream s; s << prefixPath.c_str() << "-0.log"; return s.str(); } } -bool mitk::LoggingBackend::CheckIfFileExists(std::string& filename) +bool mitk::LoggingBackend::CheckIfFileExists(const std::string& filename) { bool returnValue = false; std::ifstream File(filename.c_str()); if (File.good()) {returnValue = true;} else {returnValue = false;} File.close(); return returnValue; } diff --git a/Core/Code/IO/mitkLog.h b/Core/Code/IO/mitkLog.h index bdd036ff76..84d3d50a9b 100644 --- a/Core/Code/IO/mitkLog.h +++ b/Core/Code/IO/mitkLog.h @@ -1,104 +1,105 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #ifndef _MITK_LOG_H #define _MITK_LOG_H #include #include namespace mitk { /*! \brief mbilog backend implementation for mitk */ class MITK_CORE_EXPORT LoggingBackend : public mbilog::TextBackendBase { public: /** \brief overloaded method for receiving log message from mbilog */ void ProcessMessage(const mbilog::LogMessage& ); /** \brief registers MITK logging backend at mbilog */ static void Register(); /** \brief Unregisters MITK logging backend at mbilog */ static void Unregister(); /** \brief Sets extra log file path (additionally to the console log) */ static void SetLogFile(const char *file); /** @brief Activates and handles a rolling log file with the given prefix and path. * This method handles 10 log files with a given prefix, e.g. "myLogFile". * The 10 log files will then look like this: * myLogFile-0.log (current file) * myLogFile-1.log (last file) * myLogFile-2.log * [...] * myLogFile-9.log * * Everytime this method this called, normally when MITK is started, then * all log files are copied one file below (0 to 1, 1 to 2, and so on). * The oldes logfile (number 9) is always deleted. So users have access to * the log files of the last 10 runs. * @throws mitk::Exception Throws an exception if there is a problem with renaming the logfiles, e.g., because of file access problems. * @param prefixPath Should hold the prefix of the logfile together with its path. E.g., "C:/programs/mitk/myLogFile". */ static void RotateLogFiles(const std::string& prefixPath); /** @brief Increments the names of logfiles with the given prefixPath. * This means, if the prefixPath is "myLogFile" myLogFile-0.log * is renamed to myLogFile-1.log, myLogFile-1.log to myLogFile-2.log, * and so on. The oldest logfile is deleted. The number of log files is * defined by the parameter "numLogFiles". The first logfile name is * "free" (e.g., [prefix]-0.log) again. This name is retured. * @param prefixPath Should hold the prefix of the logfile together with its path. E.g., "C:/programs/mitk/myLogFile". * @param numLogFiles Sets the number of logfiles. Default value is 10. This means logfiles from [prefix]-0.log * to [prefix]-1.log are stored. * @return Returns a new logfile name which is free again because the old first log file was renamed. * @throws mitk::Exception Throws an exception if there is a problem with renaming the logfiles, e.g., because of file access problems. */ static std::string IncrementLogFileNames(const std::string& prefixPath, int numLogFiles = 10); /** @return Returns the log file if there is one. Returns an empty string * if no log file is active. */ static std::string GetLogFile(); /** \brief Enables an additional logging output window by means of itk::outputwindow * This might be relevant for showing log output in applications with no default output console */ static void EnableAdditionalConsoleWindow(bool enable); /** \brief Automatically extracts and removes the "--logfile " parameters from the standard C main(argc,argv) parameter list and calls SetLogFile if needed */ static void CatchLogFileCommandLineParameter(int &argc,char **argv); + protected: /** Checks if a file exists. * @return Returns true if the file exists, false if not. */ - static bool CheckIfFileExists(std::string& filename); + static bool CheckIfFileExists(const std::string& filename); }; } #endif