home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2004 #9 / Amiga Plus CD - 2004 - No. 09.iso / amigaplus / tools / amigaos4_only / smbfs / source / assert.c next >
Encoding:
C/C++ Source or Header  |  2004-08-03  |  7.6 KB  |  429 lines

  1. /*
  2.  * $Id: assert.c,v 1.11 2004/04/27 20:34:23 obarthel Exp $
  3.  *
  4.  * :ts=8
  5.  *
  6.  * SMB file system wrapper for AmigaOS, using the AmiTCP V3 API
  7.  *
  8.  * Copyright (C) 2000-2004 by Olaf `Olsen' Barthel <olsen@sourcery.han.de>
  9.  *
  10.  * This program is free software; you can redistribute it and/or modify
  11.  * it under the terms of the GNU General Public License as published by
  12.  * the Free Software Foundation; either version 2 of the License, or
  13.  * (at your option) any later version.
  14.  *
  15.  * This program is distributed in the hope that it will be useful,
  16.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.  * GNU General Public License for more details.
  19.  *
  20.  * You should have received a copy of the GNU General Public License
  21.  * along with this program; if not, write to the Free Software
  22.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23.  */
  24.  
  25. /****************************************************************************/
  26.  
  27. #include <dos/dos.h>
  28.  
  29. #include <clib/exec_protos.h>
  30. #include <clib/dos_protos.h>
  31.  
  32. #ifdef __SASC
  33. #include <pragmas/exec_pragmas.h>
  34. #include <pragmas/dos_pragmas.h>
  35. #else
  36. #include <inline/exec.h>
  37. #include <inline/dos.h>
  38. #endif /* __SASC */
  39.  
  40. #include <string.h>
  41.  
  42. extern struct Library * AbsExecBase;
  43.  
  44. #define SysBase AbsExecBase
  45.  
  46. extern void kprintf(const char *,...);
  47. extern void __stdargs kputc(char c);
  48.  
  49. /****************************************************************************/
  50.  
  51. #include <stdarg.h>
  52.  
  53. /****************************************************************************/
  54.  
  55. #define DEBUGLEVEL_OnlyAsserts    0
  56. #define DEBUGLEVEL_Reports    1
  57. #define DEBUGLEVEL_CallTracing    2
  58.  
  59. /****************************************************************************/
  60.  
  61. static int indent_level = 0;
  62. int __debug_level = DEBUGLEVEL_CallTracing;
  63.  
  64. static char program_name[40];
  65. static int program_name_len = 0;
  66.  
  67. /****************************************************************************/
  68.  
  69. void
  70. _SETPROGRAMNAME(char *name)
  71. {
  72.     if(name != NULL && name[0] != '\0')
  73.     {
  74.         program_name_len = strlen(name);
  75.         if(program_name_len >= sizeof(program_name))
  76.             program_name_len = sizeof(program_name)-1;
  77.  
  78.         strncpy(program_name,name,program_name_len);
  79.         program_name[program_name_len] = '\0';
  80.     }
  81.     else
  82.     {
  83.         program_name_len = 0;
  84.     }
  85. }
  86.  
  87. /****************************************************************************/
  88.  
  89. int
  90. _SETDEBUGLEVEL(int level)
  91. {
  92.     int old_level = __debug_level;
  93.  
  94.     __debug_level = level;
  95.  
  96.     return(old_level);
  97. }
  98.  
  99. /****************************************************************************/
  100.  
  101. int
  102. _GETDEBUGLEVEL(void)
  103. {
  104.     return(__debug_level);
  105. }
  106.  
  107. /****************************************************************************/
  108.  
  109. static int previous_debug_level = -1;
  110.  
  111. void
  112. _PUSHDEBUGLEVEL(int level)
  113. {
  114.     previous_debug_level = _SETDEBUGLEVEL(level);
  115. }
  116.  
  117. void
  118. _POPDEBUGLEVEL(void)
  119. {
  120.     if(previous_debug_level != -1)
  121.     {
  122.         _SETDEBUGLEVEL(previous_debug_level);
  123.  
  124.         previous_debug_level = -1;
  125.     }
  126. }
  127.  
  128. /****************************************************************************/
  129.  
  130. void
  131. _INDENT(void)
  132. {
  133.     if(program_name_len > 0)
  134.         kprintf("(%s) ",program_name);
  135.  
  136.     if(__debug_level >= DEBUGLEVEL_CallTracing)
  137.     {
  138.         int i;
  139.  
  140.         for(i = 0 ; i < indent_level ; i++)
  141.             kprintf("   ");
  142.     }
  143. }
  144.  
  145. /****************************************************************************/
  146.  
  147. void
  148. _SHOWVALUE(
  149.     unsigned long value,
  150.     int size,
  151.     const char *name,
  152.     const char *file,
  153.     int line)
  154. {
  155.     if(__debug_level >= DEBUGLEVEL_Reports)
  156.     {
  157.         char *fmt;
  158.  
  159.         switch(size)
  160.         {
  161.             case 1:
  162.  
  163.                 fmt = "%s:%ld:%s = %ld, 0x%02lx";
  164.                 break;
  165.  
  166.             case 2:
  167.  
  168.                 fmt = "%s:%ld:%s = %ld, 0x%04lx";
  169.                 break;
  170.  
  171.             default:
  172.  
  173.                 fmt = "%s:%ld:%s = %ld, 0x%08lx";
  174.                 break;
  175.         }
  176.  
  177.         _INDENT();
  178.  
  179.         kprintf(fmt,file,line,name,value,value);
  180.  
  181.         if(size == 1 && value < 256)
  182.         {
  183.             if(value < ' ' || (value >= 127 && value < 160))
  184.                 kprintf(", '\\x%02lx'",value);
  185.             else
  186.                 kprintf(", '%lc'",value);
  187.         }
  188.  
  189.         kprintf("\n");
  190.     }
  191. }
  192.  
  193. /****************************************************************************/
  194.  
  195. void
  196. _SHOWPOINTER(
  197.     void *pointer,
  198.     const char *name,
  199.     const char *file,
  200.     int line)
  201. {
  202.     if(__debug_level >= DEBUGLEVEL_Reports)
  203.     {
  204.         char *fmt;
  205.  
  206.         _INDENT();
  207.  
  208.         if(pointer != NULL)
  209.             fmt = "%s:%ld:%s = 0x%08lx\n";
  210.         else
  211.             fmt = "%s:%ld:%s = NULL\n";
  212.  
  213.         kprintf(fmt,file,line,name,pointer);
  214.     }
  215. }
  216.  
  217. /****************************************************************************/
  218.  
  219. void
  220. _SHOWSTRING(
  221.     const char *string,
  222.     const char *name,
  223.     const char *file,
  224.     int line)
  225. {
  226.     if(__debug_level >= DEBUGLEVEL_Reports)
  227.     {
  228.         _INDENT();
  229.         kprintf("%s:%ld:%s = 0x%08lx \"%s\"\n",file,line,name,string,string);
  230.     }
  231. }
  232.  
  233. /****************************************************************************/
  234.  
  235. void
  236. _SHOWMSG(
  237.     const char *string,
  238.     const char *file,
  239.     int line)
  240. {
  241.     if(__debug_level >= DEBUGLEVEL_Reports)
  242.     {
  243.         _INDENT();
  244.         kprintf("%s:%ld:%s\n",file,line,string);
  245.     }
  246. }
  247.  
  248. /****************************************************************************/
  249.  
  250. void
  251. _DPRINTF_HEADER(
  252.     const char *file,
  253.     int line)
  254. {
  255.     if(__debug_level >= DEBUGLEVEL_Reports)
  256.     {
  257.         _INDENT();
  258.         kprintf("%s:%ld:",file,line);
  259.     }
  260. }
  261.  
  262. #ifdef __SASC
  263.  
  264. static void __asm
  265. putch(register __d0 UBYTE c)
  266. {
  267.     if(c != '\0')
  268.         kputc(c);
  269. }
  270.  
  271. #else
  272.  
  273. static void
  274. putch(UBYTE c __asm("d0"))
  275. {
  276.     if(c != '\0')
  277.         kputc(c);
  278. }
  279.  
  280. #endif /* __SASC */
  281.  
  282. void
  283. _DPRINTF(const char *fmt,...)
  284. {
  285.     if(__debug_level >= DEBUGLEVEL_Reports)
  286.     {
  287.         va_list args;
  288.  
  289.         va_start(args,fmt);
  290.         RawDoFmt((char *)fmt,args,(VOID (*)())putch,NULL);
  291.         va_end(args);
  292.  
  293.         kprintf("\n");
  294.     }
  295. }
  296.  
  297. void
  298. _DLOG(const char *fmt,...)
  299. {
  300.     if(__debug_level >= DEBUGLEVEL_Reports)
  301.     {
  302.         va_list args;
  303.  
  304.         va_start(args,fmt);
  305.         RawDoFmt((char *)fmt,args,(VOID (*)())putch,NULL);
  306.         va_end(args);
  307.     }
  308. }
  309.  
  310. /****************************************************************************/
  311.  
  312. void
  313. _ENTER(
  314.     const char *file,
  315.     int line,
  316.     const char *function)
  317. {
  318.     if(__debug_level >= DEBUGLEVEL_CallTracing)
  319.     {
  320.         _INDENT();
  321.         kprintf("%s:%ld:Entering %s\n",file,line,function);
  322.     }
  323.  
  324.     indent_level++;
  325. }
  326.  
  327. void
  328. _LEAVE(
  329.     const char *file,
  330.     int line,
  331.     const char *function)
  332. {
  333.     indent_level--;
  334.  
  335.     if(__debug_level >= DEBUGLEVEL_CallTracing)
  336.     {
  337.         _INDENT();
  338.         kprintf("%s:%ld: Leaving %s\n",file,line,function);
  339.     }
  340. }
  341.  
  342. void
  343. _RETURN(
  344.     const char *file,
  345.     int line,
  346.     const char *function,
  347.     unsigned long result)
  348. {
  349.     indent_level--;
  350.  
  351.     if(__debug_level >= DEBUGLEVEL_CallTracing)
  352.     {
  353.         _INDENT();
  354.         kprintf("%s:%ld: Leaving %s (result 0x%08lx, %ld)\n",file,line,function,result,result);
  355.     }
  356. }
  357.  
  358. /****************************************************************************/
  359.  
  360. void
  361. _ASSERT(
  362.     int x,
  363.     const char *xs,
  364.     const char *file,
  365.     int line,
  366.     const char *function)
  367. {
  368.     #ifdef CONFIRM
  369.     {
  370.         STATIC BOOL ScrollMode    = FALSE;
  371.         STATIC BOOL BatchMode    = FALSE;
  372.  
  373.         if(BatchMode == FALSE)
  374.         {
  375.             if(x == 0)
  376.             {
  377.                 kprintf("%s:%ld:Expression `%s' failed assertion in %s().\n",
  378.                         file,
  379.                         line,
  380.                         xs,
  381.                         function);
  382.  
  383.                 if(ScrollMode == FALSE)
  384.                 {
  385.                     ULONG Signals;
  386.  
  387.                     SetSignal(0,SIGBREAKF_CTRL_C | SIGBREAKF_CTRL_D | SIGBREAKF_CTRL_E);
  388.  
  389.                     kprintf(" ^C to continue, ^D to enter scroll mode, ^E to enter batch mode\r");
  390.  
  391.                     Signals = Wait(SIGBREAKF_CTRL_C | SIGBREAKF_CTRL_D | SIGBREAKF_CTRL_E);
  392.  
  393.                     if(Signals & SIGBREAKF_CTRL_D)
  394.                     {
  395.                         ScrollMode = TRUE;
  396.  
  397.                         kprintf("Ok, entering scroll mode\033[K\n");
  398.                     }
  399.                     else if (Signals & SIGBREAKF_CTRL_E)
  400.                     {
  401.                         BatchMode = TRUE;
  402.  
  403.                         kprintf("Ok, entering batch mode\033[K\n");
  404.                     }
  405.                     else
  406.                     {
  407.                         /* Continue */
  408.  
  409.                         kprintf("\033[K\r");
  410.                     }
  411.                 }
  412.             }
  413.         }
  414.     }
  415.     #else
  416.     {
  417.         if(x == 0)
  418.         {
  419.             _INDENT();
  420.             kprintf("%s:%ld:Expression `%s' failed assertion in %s().\n",
  421.                     file,
  422.                     line,
  423.                     xs,
  424.                     function);
  425.         }
  426.     }
  427.     #endif    /* CONFIRM */
  428. }
  429.