Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "mitkLog.h"
00019
00020 #include "itkSimpleFastMutexLock.h"
00021 #include <itkOutputWindow.h>
00022
00023 #include <iostream>
00024 #include <fstream>
00025
00026 static itk::SimpleFastMutexLock logMutex;
00027 static mitk::LoggingBackend *mitkLogBackend = 0;
00028 static std::ofstream *logFile = 0;
00029 static std::stringstream *outputWindow = 0;
00030 static bool logOutputWindow = false;
00031
00032 void mitk::LoggingBackend::EnableAdditionalConsoleWindow(bool enable)
00033 {
00034 logOutputWindow = enable;
00035 }
00036
00037
00038 void mitk::LoggingBackend::ProcessMessage(const mbilog::LogMessage& l )
00039 {
00040 logMutex.Lock();
00041 #ifdef _WIN32
00042 mbilog::BackendCout::FormatSmart( l, (int)GetCurrentThreadId() );
00043 #else
00044 mbilog::BackendCout::FormatSmart( l );
00045 #endif
00046
00047 if(logFile)
00048 {
00049 #ifdef _WIN32
00050 mbilog::BackendCout::FormatFull( *logFile, l, (int)GetCurrentThreadId() );
00051 #else
00052 mbilog::BackendCout::FormatFull( *logFile, l );
00053 #endif
00054 }
00055 if(logOutputWindow)
00056 {
00057 if(outputWindow == NULL)
00058 { outputWindow = new std::stringstream();}
00059 outputWindow->str("");
00060 outputWindow->clear();
00061 #ifdef _WIN32
00062 mbilog::BackendCout::FormatFull( *outputWindow, l, (int)GetCurrentThreadId() );
00063 #else
00064 mbilog::BackendCout::FormatFull( *outputWindow, l );
00065 #endif
00066 itk::OutputWindow::GetInstance()->DisplayText(outputWindow->str().c_str());
00067 }
00068 logMutex.Unlock();
00069 }
00070
00071 void mitk::LoggingBackend::Register()
00072 {
00073 if(mitkLogBackend)
00074 return;
00075 mitkLogBackend = new mitk::LoggingBackend();
00076 mbilog::RegisterBackend( mitkLogBackend );
00077 }
00078
00079 void mitk::LoggingBackend::Unregister()
00080 {
00081 if(mitkLogBackend)
00082 {
00083 SetLogFile(0);
00084 mbilog::UnregisterBackend( mitkLogBackend );
00085 delete mitkLogBackend;
00086 mitkLogBackend=0;
00087 }
00088 }
00089
00090 void mitk::LoggingBackend::SetLogFile(const char *file)
00091 {
00092 logMutex.Lock();
00093 if(logFile)
00094 {
00095 MITK_INFO << "closing logfile";
00096 logFile->close();
00097 delete logFile;
00098 logFile = 0;
00099 }
00100 if(file)
00101 {
00102 logFile = new std::ofstream( file, std::ios_base::out | std::ios_base::app );
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117 }
00118 logMutex.Unlock();
00119 }
00120
00121 void mitk::LoggingBackend::CatchLogFileCommandLineParameter(int &argc,char **argv)
00122 {
00123 int r;
00124
00125 for(r=1;r<argc;r++)
00126 {
00127 if(std::string(argv[r])=="--logfile")
00128 {
00129 if(r+1>=argc)
00130 {
00131 --argc;
00132 MITK_ERROR << "--logfile parameter found, but no file given";
00133 return;
00134 }
00135
00136 mitk::LoggingBackend::SetLogFile(argv[r+1]);
00137
00138 for(r+=2;r<argc;r++)
00139 argv[r-2]=argv[r];
00140
00141 argc-=2;
00142 return;
00143 }
00144 }
00145 }