libjmmcg  release_579_6_g8cffd
A C++ library containing an eclectic mix of useful, advanced components.
trace.hpp
Go to the documentation of this file.
1 #ifndef LIBJMMCG_CORE_TRACE_HPP
2 #define LIBJMMCG_CORE_TRACE_HPP
3 
4 /******************************************************************************
5 ** Copyright © 2002 by J.M.McGuiness, coder@hussar.me.uk
6 **
7 ** This library is free software; you can redistribute it and/or
8 ** modify it under the terms of the GNU Lesser General Public
9 ** License as published by the Free Software Foundation; either
10 ** version 2.1 of the License, or (at your option) any later version.
11 **
12 ** This library is distributed in the hope that it will be useful,
13 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 ** Lesser General Public License for more details.
16 **
17 ** You should have received a copy of the GNU Lesser General Public
18 ** License along with this library; if not, write to the Free Software
19 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21 
22 #include "debug_defines.hpp"
24 
25 #include <iostream>
26 
27 #ifdef WIN32
28 # include <crtdbg.h>
29 #endif
30 
31 namespace jmmcg { namespace LIBJMMCG_VER_NAMESPACE {
32 
33 #ifdef NDEBUG
34 # define JMMCG_TRACE(msg)
35 #else
36 # ifdef _MSC_VER
37  /**
38  Because M$ doesn't use std::cerr correctly and doesn't use std::cout consistently across Win2K & WinXP in MSDev. Studio v6.0sp5.
39  */
40  inline void __fastcall
41  Trace(const tostringstream &msg, const unsigned long line, const char *file_name) {
42  _RPT2(_CRT_WARN,_T("%s[%lu]:\n\t"), StringToTString(file_name).c_str(),line);
43  _RPT1(_CRT_WARN,_T("%s\n"),msg.str().c_str());
44  }
45 
46  // TODO JMG: Because M$ doesn't use std::cerr correctly and doesn't use std::cout consistently across Win2K & WinXP in MSDev. Studio v6.0sp5.
47  inline void __fastcall
48  Trace(const tostream &msg, const unsigned long line, const char *file_name) {
49  _RPT2(_CRT_WARN,_T("PID:0x%.8x, PHandle:0x%.8x"), ::GetCurrentProcessId(), ::GetCurrentProcess());
50  _RPT2(_CRT_WARN,_T(", TID:0x%.8x, THandle:0x%.8x, "), ::GetCurrentThreadId(), ::GetCurrentThread());
51  Trace(dynamic_cast<const tostringstream &>(msg), line, file_name);
52  }
53 
54 # define JMMCG_TRACE(msg) {
55  jmmcg::LIBJMMCG_VER_NAMESPACE::tostringstream ss;
56  ss<<jmmcg::LIBJMMCG_VER_NAMESPACE::StringToTString(__PRETTY_FUNCTION__)<<_T("\n\t")<<msg;
57  jmmcg::LIBJMMCG_VER_NAMESPACE::Trace(ss, __LINE__, __FILE__);
58  }
59 
60 # else
61 # include <sys/types.h>
62 # include <unistd.h>
63 
64  /**
65  \todo Add a username: <a href="https://www.unix.com/programming/21041-getting-username-c-program-unix.html"/>
66  */
67 # define JMMCG_TRACE(msg) {
68  jmmcg::LIBJMMCG_VER_NAMESPACE::tostringstream ss;
69  ss<<jmmcg::LIBJMMCG_VER_NAMESPACE::StringToTString(__PRETTY_FUNCTION__)<<_T(":\n\t")
70  <<_T("PID:0x")<<std::hex<<::getpid()<<_T(", TID:")<<std::dec<<::gettid()
71  <<_T(", ") _T(__FILE__) _T("[" LIBJMMCG_ENQUOTE(__LINE__) "]:\n\t")<<msg;
72  std::cerr<<ss.str()<<std::endl;
73  }
74 # endif
75 #endif
76 
77 } }
78 
79 #endif