Nife version Beta
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

165 lines
3.0 KiB

  1. /* Copyright (C) 2011-2022 Patrick H. E. Foubet - S.E.R.I.A.N.E.
  2. This program is free software: you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation, either version 3 of the License, or any
  5. later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program. If not, see <http://www.gnu.org/licenses/>
  12. *******************************************************************/
  13. /* debug.c */
  14. #include "conf.h"
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <unistd.h>
  18. #include <string.h>
  19. #include <sys/types.h>
  20. #include <sys/stat.h>
  21. #include <fcntl.h>
  22. #include "nife.h"
  23. #include "mth.h"
  24. #include "foncs.h"
  25. #include "debug.h"
  26. #include "stackC.h"
  27. int Debug=1; /* OK by default */
  28. int InDebugFct=0;
  29. static void D_Logname(char*B)
  30. {
  31. switch(Debug) {
  32. case 1:
  33. sprintf(B,".nife_%d.log",getpid());
  34. break;
  35. case 2:
  36. sprintf(B,".nifeNS_%d.log",getpid());
  37. break;
  38. case 3:
  39. sprintf(B,".nifeRPC_%d.log",getpid());
  40. break;
  41. default:
  42. *B='\0';
  43. }
  44. }
  45. void D_Reset(void)
  46. {
  47. int fd, nc;
  48. char NF[24];
  49. if (Debug) {
  50. nc=chdir(".nife");
  51. D_Logname(NF);
  52. if ((fd=open(NF,O_CREAT|O_RDWR|O_TRUNC,0644)) < 0) perror(NF);
  53. else {
  54. dup2(fd,2);
  55. close(fd);
  56. }
  57. nc=chdir("..");
  58. } else dup2(1,2);
  59. }
  60. void IFD_SaveLog(void)
  61. {
  62. char NF[24],comm[50],*newf;
  63. newf = getString();
  64. if (Debug) {
  65. D_Logname(NF);
  66. sprintf(comm,"cp .nife/%s %s",NF, newf);
  67. runCommand(comm);
  68. }
  69. }
  70. static void DebugTerms(char c)
  71. {
  72. char comm[24];
  73. if (Debug) {
  74. sprintf(comm,"ex/NifeDebugTerms -%c", c);
  75. runCommand(comm);
  76. }
  77. }
  78. void IFD_DebugTOn(void)
  79. {
  80. DebugTerms('r');
  81. }
  82. void IFD_DebugTOff(void)
  83. {
  84. DebugTerms('s');
  85. }
  86. void IFD_Update(void)
  87. {
  88. if (Debug) Debug=0;
  89. else Debug=1;
  90. D_Reset();
  91. }
  92. void D_Trace(char * M)
  93. {
  94. if (Debug) {
  95. fprintf(stderr,"%s ",M);
  96. fflush(stderr);
  97. }
  98. }
  99. void D_Close(void)
  100. {
  101. int nc;
  102. char NF[24];
  103. fclose(stderr);
  104. if (Debug) {
  105. nc=chdir(".nife");
  106. D_Logname(NF);
  107. unlink(NF);
  108. nc=chdir("..");
  109. }
  110. }
  111. void D_Tracenl(char * M)
  112. {
  113. if (Debug) {
  114. fprintf(stderr,"%s\n",M);
  115. fflush(stderr);
  116. }
  117. }
  118. void D_TraceH(char * M, int l)
  119. {
  120. int i;
  121. if (Debug) {
  122. fprintf(stderr,"HEX:");
  123. for (i=0;i<l;i++) fprintf(stderr," %x",(int)((unsigned char)M[i]));
  124. fprintf(stderr,"\n");
  125. fflush(stderr);
  126. }
  127. }
  128. static char D_Mes[20];
  129. void * Malloc(int s)
  130. {
  131. void * M;
  132. D_Tracenl("EOL");
  133. M=malloc(s);
  134. sprintf(D_Mes,"Malloc %d : %lx",s,(long)M);
  135. D_Tracenl(D_Mes);
  136. return M;
  137. }
  138. void Free(void *A)
  139. {
  140. D_Tracenl("EOL");
  141. sprintf(D_Mes,"Free : %lx",(long)A);
  142. D_Tracenl(D_Mes);
  143. free(A);
  144. }