PageRenderTime 660ms CodeModel.GetById 35ms RepoModel.GetById 3ms app.codeStats 0ms

/trunk/harbour/contrib/xhb/traceprg.prg

#
Unknown | 176 lines | 145 code | 31 blank | 0 comment | 0 complexity | e376c4340ec9ebeda63b80d07b2d8132 MD5 | raw file
Possible License(s): AGPL-1.0, BSD-3-Clause, CC-BY-SA-3.0, LGPL-3.0, GPL-2.0, LGPL-2.0, LGPL-2.1
  1. /*
  2. * $Id: traceprg.prg 16744 2011-05-09 18:07:27Z vszakats $
  3. */
  4. /*
  5. * xHarbour Project source code:
  6. * PRG Tracing System
  7. *
  8. * Copyright 2001 Ron Pinkas <ron@@ronpinkas.com>
  9. * www - http://www.xharbour.org
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2, or (at your option)
  14. * any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this software; see the file COPYING. If not, write to
  23. * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  24. * Boston, MA 02111-1307 USA (or visit the web site http://www.gnu.org/).
  25. *
  26. * As a special exception, xHarbour license gives permission for
  27. * additional uses of the text contained in its release of xHarbour.
  28. *
  29. * The exception is that, if you link the xHarbour libraries with other
  30. * files to produce an executable, this does not by itself cause the
  31. * resulting executable to be covered by the GNU General Public License.
  32. * Your use of that executable is in no way restricted on account of
  33. * linking the xHarbour library code into it.
  34. *
  35. * This exception does not however invalidate any other reasons why
  36. * the executable file might be covered by the GNU General Public License.
  37. *
  38. * This exception applies only to the code released with this xHarbour
  39. * explicit exception. If you add/copy code from other sources,
  40. * as the General Public License permits, the above exception does
  41. * not apply to the code that you add in this way. To avoid misleading
  42. * anyone as to the status of such modified files, you must delete
  43. * this exception notice from them.
  44. *
  45. * If you write modifications of your own for xHarbour, it is your choice
  46. * whether to permit this exception to apply to your modifications.
  47. * If you do not wish that, delete this exception notice.
  48. *
  49. */
  50. #include "set.ch"
  51. #include "fileio.ch"
  52. #define HB_SET_TRACESTACK_NONE 0
  53. #define HB_SET_TRACESTACK_CURRENT 1
  54. #define HB_SET_TRACESTACK_ALL 2
  55. #xtranslate Write( <cString> ) => FWrite( FileHandle, <cString> ) //;HB_OutDebug( <cString> )
  56. STATIC s_lSET_TRACE := .T.
  57. STATIC s_cSET_TRACEFILE := "trace.log"
  58. STATIC s_nSET_TRACESTACK := HB_SET_TRACESTACK_ALL
  59. FUNCTION xhb_setTrace( xTrace )
  60. LOCAL lTrace := s_lSET_TRACE
  61. IF HB_ISLOGICAL( xTrace )
  62. s_lSET_TRACE := xTrace
  63. ELSEIF HB_ISSTRING( xTrace )
  64. IF Upper( xTrace ) == "ON"
  65. s_lSET_TRACE := .T.
  66. ELSEIF Upper( xTrace ) == "OFF"
  67. s_lSET_TRACE := .F.
  68. ENDIF
  69. ENDIF
  70. RETURN lTrace
  71. FUNCTION xhb_setTraceFile( xFile, lAppend )
  72. LOCAL cTraceFile := s_cSET_TRACEFILE
  73. IF HB_ISSTRING( xFile )
  74. s_cSET_TRACEFILE := xFile
  75. IF !HB_ISLOGICAL( lAppend ) .OR. !lAppend
  76. FClose( FCreate( s_cSET_TRACEFILE ) )
  77. ENDIF
  78. ENDIF
  79. RETURN cTraceFile
  80. FUNCTION xhb_setTraceStack( xLevel )
  81. LOCAL nTraceLevel := s_nSET_TRACESTACK
  82. IF HB_ISSTRING( xLevel )
  83. IF Upper( xLevel ) == "NONE"
  84. s_nSET_TRACESTACK := HB_SET_TRACESTACK_NONE
  85. ELSEIF Upper( xLevel ) == "CURRENT"
  86. s_nSET_TRACESTACK := HB_SET_TRACESTACK_CURRENT
  87. ELSEIF Upper( xLevel ) == "ALL"
  88. s_nSET_TRACESTACK := HB_SET_TRACESTACK_ALL
  89. ENDIF
  90. ELSEIF HB_ISNUMERIC( xLevel )
  91. IF xLevel >= 0
  92. s_nSET_TRACESTACK := xLevel
  93. ENDIF
  94. ENDIF
  95. RETURN nTraceLevel
  96. //--------------------------------------------------------------//
  97. FUNCTION TraceLog( ... )
  98. // Using PRIVATE instead of LOCALs so TraceLog() is DIVERT friendly.
  99. LOCAL cFile, FileHandle, nLevel, ProcName, xParam
  100. #ifdef __XHARBOUR__
  101. IF ! SET( _SET_TRACE )
  102. RETURN .T.
  103. ENDIF
  104. cFile := SET( _SET_TRACEFILE )
  105. nLevel := SET( _SET_TRACESTACK )
  106. #else
  107. IF !s_lSET_TRACE
  108. RETURN .T.
  109. ENDIF
  110. cFile := s_cSET_TRACEFILE
  111. nLevel := s_nSET_TRACESTACK
  112. #endif
  113. /* hb_FileExists() and FOpen()/FCreate() make different assumptions rgdg path,
  114. so we have to make sure cFile contains path to avoid ambiguity */
  115. cFile := cWithPath( cFile )
  116. IF hb_FileExists( cFile )
  117. FileHandle := FOpen( cFile, FO_WRITE )
  118. ELSE
  119. FileHandle := FCreate( cFile )
  120. ENDIF
  121. FSeek( FileHandle, 0, FS_END )
  122. IF nLevel > 0
  123. Write( '[' + ProcFile(1) + "->" + ProcName( 1 ) + '] (' + LTrim( Str( Procline(1) ) ) + ')' )
  124. ENDIF
  125. IF nLevel > 1 .AND. ! ( ProcName( 2 ) == '' )
  126. Write( ' Called from: ' + hb_eol() )
  127. nLevel := 1
  128. DO WHILE ! ( ( ProcName := ProcName( ++nLevel ) ) == '' )
  129. Write( space(30) + ProcFile( nLevel ) + "->" + ProcName + '(' + LTrim( Str( Procline( nLevel ) ) ) + ')' + hb_eol() )
  130. ENDDO
  131. ELSE
  132. Write( hb_eol() )
  133. ENDIF
  134. FOR EACH xParam IN HB_aParams()
  135. Write( 'Type: ' + ValType( xParam ) + ' >>>' + hb_CStr( xParam ) + '<<<' + hb_eol() )
  136. NEXT
  137. Write( hb_eol() )
  138. FClose( FileHandle )
  139. RETURN .T.
  140. //--------------------------------------------------------------//
  141. STATIC FUNCTION cWithPath( cFilename )
  142. /* Ensure cFilename contains path. If it doesn't, add current directory to the front of it */
  143. LOCAL cPath
  144. hb_fnamesplit( cFilename, @cPath )
  145. RETURN iif( Empty( cPath ), "." + hb_ps(), "" ) + cFilename