-#include <iostream>
+/*=========================================================================
+
+ Program: gdcm
+ Module: $RCSfile: gdcmDebug.cxx,v $
+ Language: C++
+ Date: $Date: 2005/11/28 15:20:32 $
+ Version: $Revision: 1.27 $
+
+ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
+ l'Image). All rights reserved. See Doc/License.txt or
+ http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
+
+ This software is distributed WITHOUT ANY WARRANTY; without even
+ the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ PURPOSE. See the above copyright notices for more information.
+
+=========================================================================*/
+
#include "gdcmDebug.h"
+#include "gdcmCommandManager.h"
-/**
- * \ingroup Globals
- * \brief Instance of debugging utility.
- */
-gdcmDebug dbg;
+#include <iostream>
+
+namespace gdcm
+{
+//-----------------------------------------------------------------------------
+// Warning message level to be displayed
+const int Debug::LINE_LENGTH = 79;
+
+bool Debug::DebugFlag = false;
+bool Debug::WarningFlag = false;
+bool Debug::OutputToFile = false;
+std::ofstream Debug::OutputFileStream;
+std::ostream &Debug::StandardStream = std::cerr;
+
+//-----------------------------------------------------------------------------
+// Constructor / Destructor
+Debug::Debug()
+{
+}
+
+Debug::~Debug()
+{
+ if ( OutputFileStream.is_open() )
+ OutputFileStream.close();
+}
+
+//-----------------------------------------------------------------------------
+// Public
/**
- * \ingroup gdcmDebug
- * \brief constructor
- * @param level debug level
+ * \brief Sets both the debug flag and warning flag
+ * (both used for debugging purpose)
+ * @param flag Set the debug flag and warning flag
*/
-gdcmDebug::gdcmDebug(int level) {
- DebugLevel = level;
+void Debug::SetDebugFlag (bool flag)
+{
+ // To help tracking a bug, both flags are necessary
+ DebugFlag = flag;
+ WarningFlag = flag;
}
/**
- * \ingroup gdcmDebug
- * \brief Accessor
- * @param level Set the debug level
+ * \brief Sets the warning flag
+ * @param flag Set the warning flag
*/
-void gdcmDebug::SetDebug(int level) {
- DebugLevel = level;
+void Debug::SetWarningFlag (bool flag)
+{
+ // Cannot unset Warning flag if Debug flag is on.
+ if (flag == false && DebugFlag == true)
+ return;
+ WarningFlag = flag;
}
/**
- * \ingroup gdcmDebug
- * \brief Verbose
- * @param Level level
- * @param Msg1 first message part
- * @param Msg2 second message part
- */
-void gdcmDebug::Verbose(int Level, const char * Msg1, const char * Msg2) {
- if (Level > DebugLevel)
- return ;
- std::cerr << Msg1 << ' ' << Msg2 << std::endl;
+ * \brief Accessor
+ * @param flag whether we want to redirect to file
+ */
+void Debug::SetOutputToFile (bool flag)
+{
+ OutputToFile = flag;
}
/**
- * \ingroup gdcmDebug
- * \brief Error
- * @param Test test
- * @param Msg1 first message part
- * @param Msg2 second message part
- */
-void gdcmDebug::Error( bool Test, const char * Msg1, const char * Msg2) {
- if (!Test)
- return;
- std::cerr << Msg1 << ' ' << Msg2 << std::endl;
- Exit(1);
+ * \brief Accessor to know whether debug info are redirected to file
+ */
+bool Debug::GetOutputToFile ()
+{
+ return OutputToFile;
}
/**
- * \ingroup gdcmDebug
- * \brief Error
- * @param Msg1 first message part
- * @param Msg2 second message part
- * @param Msg3 Third message part
- */
-void gdcmDebug::Error(const char* Msg1, const char* Msg2,
- const char* Msg3) {
- std::cerr << Msg1 << ' ' << Msg2 << ' ' << Msg3 << std::endl;
- Exit(1);
+ * \brief Set the filename the debug stream should be redirect to
+ * Settting a filename also sets DebugToFile to true
+ * @param filename File to redirect debug info
+ * Absolutely nothing is check. You have to pass in
+ * a correct filename
+ */
+void Debug::SetOutputFileName (std::string const &filename)
+{
+ OutputToFile = true; // Just in case ...
+ DebugFlag = true; // Just in case ...
+ if ( OutputFileStream.is_open() )
+ OutputFileStream.close();
+ OutputFileStream.open( filename.c_str() );
}
/**
- * \ingroup gdcmDebug
- * \brief Assert
- * @param Level level
- * @param Test test
- * @param Msg1 first message part
- * @param Msg2 second message part
+ * \brief Internal use only. Allow us to retrieve the static from anywhere
+ * in gdcm code
+ * @return Debug file
*/
- void gdcmDebug::Assert(int Level, bool Test,
- const char * Msg1, const char * Msg2) {
- if (Level > DebugLevel)
- return ;
- if (!Test)
- std::cerr << Msg1 << ' ' << Msg2 << std::endl;
+std::ostream &Debug::GetOutput ()
+{
+ if(OutputToFile)
+ return OutputFileStream;
+ else
+ return StandardStream;
}
-/**
- * \ingroup gdcmDebug
- * \brief Exit
- * @param a return code
- */
-void gdcmDebug::Exit(int a) {
-#ifdef __GNUC__
- std::exit(a);
-#endif
-#ifdef _MSC_VER
- exit(a); // Found in #include <stdlib.h>
-#endif
+void Debug::SendToOutput(unsigned int type,std::string const &msg,const CommandManager *mgr)
+{
+ bool executed=false;
+ if(mgr)
+ executed=mgr->ConstExecuteCommand(type,msg);
+
+ if(!executed)
+ GetOutput() << Command::GetCommandAsString(type) << ": " << msg;
}
+
+//-----------------------------------------------------------------------------
+// Protected
+
+//-----------------------------------------------------------------------------
+// Private
+
+//-----------------------------------------------------------------------------
+// Print
+
+//-----------------------------------------------------------------------------
+} // end namespace gdcm