Debugger.cpp
Go to the documentation of this file.
00001 //
00002 // Debugger.cpp
00003 //
00004 // $Id: //poco/1.3/Foundation/src/Debugger.cpp#4 $
00005 //
00006 // Library: Foundation
00007 // Package: Core
00008 // Module:  Debugger
00009 //
00010 // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
00011 // and Contributors.
00012 //
00013 // Permission is hereby granted, free of charge, to any person or organization
00014 // obtaining a copy of the software and accompanying documentation covered by
00015 // this license (the "Software") to use, reproduce, display, distribute,
00016 // execute, and transmit the Software, and to prepare derivative works of the
00017 // Software, and to permit third-parties to whom the Software is furnished to
00018 // do so, all subject to the following:
00019 // 
00020 // The copyright notices in the Software and this entire statement, including
00021 // the above license grant, this restriction and the following disclaimer,
00022 // must be included in all copies of the Software, in whole or in part, and
00023 // all derivative works of the Software, unless such copies or derivative
00024 // works are solely in the form of machine-executable object code generated by
00025 // a source language processor.
00026 // 
00027 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00028 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00029 // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
00030 // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
00031 // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
00032 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
00033 // DEALINGS IN THE SOFTWARE.
00034 //
00035 
00036 
00037 #include "Poco/Debugger.h"
00038 #include <sstream>
00039 #include <cstdlib>
00040 #include <cstdio>
00041 #if defined(POCO_OS_FAMILY_WINDOWS)
00042         #include "Poco/UnWindows.h"
00043 #elif defined(POCO_OS_FAMILY_UNIX)
00044         #include <unistd.h>
00045         #include <signal.h>
00046 #elif defined(POCO_OS_FAMILY_VMS)
00047         #include <lib$routines.h>
00048         #include <ssdef.h>
00049 #endif
00050 #if defined(POCO_WIN32_UTF8) && !defined(POCO_NO_WSTRING)
00051 #include "Poco/UnicodeConverter.h"
00052 #endif
00053 
00054 
00055 // NOTE: In this module, we use the C library functions (fputs) for,
00056 // output since, at the time we're called, the C++ iostream objects std::cout, etc.
00057 // might not have been initialized yet.
00058 
00059 
00060 namespace Poco {
00061 
00062 
00063 bool Debugger::isAvailable()
00064 {
00065 #if defined(_DEBUG)
00066         #if defined(POCO_OS_FAMILY_WINDOWS)
00067                 return IsDebuggerPresent() ? true : false;
00068         #elif defined(POCO_OS_FAMILY_UNIX)
00069                 return std::getenv("POCO_ENABLE_DEBUGGER") ? true : false;
00070         #elif defined(POCO_OS_FAMILY_VMS)
00071                 return true;
00072         #endif
00073 #else
00074         return false;
00075 #endif
00076 }
00077 
00078 
00079 void Debugger::message(const std::string& msg)
00080 {
00081 #if defined(_DEBUG)
00082         std::fputs("\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n", stderr);
00083         std::fputs(msg.c_str(), stderr);
00084         std::fputs("\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n", stderr);
00085         #if defined(POCO_OS_FAMILY_WINDOWS)
00086         if (IsDebuggerPresent())
00087         {
00088 #if defined(POCO_WIN32_UTF8) && !defined(POCO_NO_WSTRING)
00089                 std::wstring umsg;
00090                 UnicodeConverter::toUTF16(msg, umsg);
00091                 umsg += '\n';
00092                 OutputDebugStringW(umsg.c_str());
00093 #else
00094                 OutputDebugStringA(msg.c_str());
00095                 OutputDebugStringA("\n");
00096 #endif
00097         }
00098         #elif defined(POCO_OS_FAMILY_UNIX)
00099         #elif defined(POCO_OS_FAMILY_VMS)
00100         #endif
00101 #endif
00102 }
00103 
00104 
00105 void Debugger::message(const std::string& msg, const char* file, int line)
00106 {
00107 #if defined(_DEBUG)
00108         std::ostringstream str;
00109         str << msg << " [in file \"" << file << "\", line " << line << "]";
00110         message(str.str());
00111 #endif
00112 }
00113 
00114 
00115 void Debugger::enter()
00116 {
00117 #if defined(_DEBUG)
00118         #if defined(POCO_OS_FAMILY_WINDOWS)
00119         if (IsDebuggerPresent())
00120         {
00121                 DebugBreak();
00122         }
00123         #elif defined(POCO_OS_FAMILY_UNIX)
00124         if (isAvailable())
00125         {
00126                 kill(getpid(), SIGINT);
00127         }
00128         #elif defined(POCO_OS_FAMILY_VMS)
00129         {
00130                 const char* cmd = "\012SHOW CALLS";
00131                 lib$signal(SS$_DEBUG, 1, cmd);
00132         }
00133         #endif
00134 #endif
00135 }
00136 
00137 
00138 void Debugger::enter(const std::string& msg)
00139 {
00140 #if defined(_DEBUG)
00141         message(msg);
00142         enter();
00143 #endif
00144 }
00145 
00146 
00147 void Debugger::enter(const std::string& msg, const char* file, int line)
00148 {
00149 #if defined(_DEBUG)
00150         message(msg, file, line);
00151         enter();
00152 #endif
00153 }
00154 
00155 
00156 void Debugger::enter(const char* file, int line)
00157 {
00158 #if defined(_DEBUG)
00159         message("BREAK", file, line);
00160         enter();
00161 #endif
00162 }
00163 
00164 
00165 } // namespace Poco


pluginlib
Author(s): Tully Foote and Eitan Marder-Eppstein
autogenerated on Sat Dec 28 2013 17:20:19