home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 13 / AACD13.ISO / AACD / Online / Samba / source / amiga / Assert.c < prev    next >
C/C++ Source or Header  |  2000-05-22  |  7KB  |  365 lines

  1. /*
  2.  * $Id: Assert.c 1.2 2000/05/22 19:10:25 olsen Exp olsen $
  3.  *
  4.  * :ts=8
  5.  *
  6.  * AmigaOS wrapper routines for Samba 2.0.0, using the AmiTCP V4 API
  7.  * and the SAS/C V6.58 compiler.
  8.  *
  9.  * Copyright (C) 1999-2000 by Olaf `Olsen' Barthel <olsen@sourcery.han.de>
  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 of the License, or
  14.  * (at your option) 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 program; if not, write to the Free Software
  23.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24.  */
  25.  
  26. /****************************************************************************/
  27.  
  28. #include <dos/dos.h>
  29.  
  30. #include <clib/exec_protos.h>
  31. #include <clib/dos_protos.h>
  32.  
  33. #include <pragmas/exec_pragmas.h>
  34. #include <pragmas/dos_pragmas.h>
  35.  
  36. #include <string.h>
  37.  
  38. extern struct Library * SysBase;
  39.  
  40. extern void kprintf(const char *,...);
  41. extern void __stdargs kputc(char c);
  42.  
  43. /****************************************************************************/
  44.  
  45. #include <stdarg.h>
  46.  
  47. /****************************************************************************/
  48.  
  49. #define DEBUGLEVEL_OnlyAsserts    0
  50. #define DEBUGLEVEL_Reports    1
  51. #define DEBUGLEVEL_CallTracing    2
  52.  
  53. /****************************************************************************/
  54.  
  55. static int indent_level = 0;
  56. static int debug_level = DEBUGLEVEL_CallTracing;
  57.  
  58. static char program_name[40];
  59. static int program_name_len = 0;
  60.  
  61. /****************************************************************************/
  62.  
  63. void
  64. _SETPROGRAMNAME(char *name)
  65. {
  66.     if(name != NULL && name[0] != '\0')
  67.     {
  68.         program_name_len = strlen(name);
  69.         if(program_name_len >= sizeof(program_name))
  70.             program_name_len = sizeof(program_name)-1;
  71.  
  72.         strncpy(program_name,name,program_name_len);
  73.         program_name[program_name_len] = '\0';
  74.     }
  75.     else
  76.     {
  77.         program_name_len = 0;
  78.     }
  79. }
  80.  
  81. /****************************************************************************/
  82.  
  83. int
  84. _SETDEBUGLEVEL(int level)
  85. {
  86.     int old_level = debug_level;
  87.  
  88.     debug_level = level;
  89.  
  90.     return(old_level);
  91. }
  92.  
  93. /****************************************************************************/
  94.  
  95. static int previous_debug_level = -1;
  96.  
  97. void
  98. _PUSHDEBUGLEVEL(int level)
  99. {
  100.     previous_debug_level = _SETDEBUGLEVEL(level);
  101. }
  102.  
  103. void
  104. _POPDEBUGLEVEL(void)
  105. {
  106.     if(previous_debug_level != -1)
  107.     {
  108.         _SETDEBUGLEVEL(previous_debug_level);
  109.  
  110.         previous_debug_level = -1;
  111.     }
  112. }
  113.  
  114. /****************************************************************************/
  115.  
  116. void
  117. _INDENT(void)
  118. {
  119.     if(program_name_len > 0)
  120.         kprintf("(%s) ",program_name);
  121.  
  122.     if(debug_level >= DEBUGLEVEL_CallTracing)
  123.     {
  124.         int i;
  125.  
  126.         for(i = 0 ; i < indent_level ; i++)
  127.             kprintf("   ");
  128.     }
  129. }
  130.  
  131. /****************************************************************************/
  132.  
  133. void
  134. _SHOWVALUE(
  135.     unsigned long value,
  136.     int size,
  137.     const char *name,
  138.     const char *file,
  139.     int line)
  140. {
  141.     if(debug_level >= DEBUGLEVEL_Reports)
  142.     {
  143.         char *fmt;
  144.  
  145.         switch(size)
  146.         {
  147.             case 1:
  148.     
  149.                 fmt = "%s:%ld:%s = %ld, 0x%02lx";
  150.                 break;
  151.     
  152.             case 2:
  153.     
  154.                 fmt = "%s:%ld:%s = %ld, 0x%04lx";
  155.                 break;
  156.     
  157.             default:
  158.     
  159.                 fmt = "%s:%ld:%s = %ld, 0x%08lx";
  160.                 break;
  161.         }
  162.     
  163.         _INDENT();
  164.     
  165.         kprintf(fmt,file,line,name,value,value);
  166.     
  167.         if(size == 1 && value < 256)
  168.         {
  169.             if(value < ' ' || (value >= 127 && value < 160))
  170.                 kprintf(", '\\x%02lx'",value);
  171.             else
  172.                 kprintf(", '%lc'",value);
  173.         }
  174.     
  175.         kprintf("\n");
  176.     }
  177. }
  178.  
  179. /****************************************************************************/
  180.  
  181. void
  182. _SHOWSTRING(
  183.     const char *string,
  184.     const char *name,
  185.     const char *file,
  186.     int line)
  187. {
  188.     if(debug_level >= DEBUGLEVEL_Reports)
  189.     {
  190.         _INDENT();
  191.         kprintf("%s:%ld:%s = 0x%08lx \"%s\"\n",file,line,name,string,string);
  192.     }
  193. }
  194.  
  195. /****************************************************************************/
  196.  
  197. void
  198. _SHOWMSG(
  199.     const char *string,
  200.     const char *file,
  201.     int line)
  202. {
  203.     if(debug_level >= DEBUGLEVEL_Reports)
  204.     {
  205.         _INDENT();
  206.         kprintf("%s:%ld:%s\n",file,line,string);
  207.     }
  208. }
  209.  
  210. /****************************************************************************/
  211.  
  212. void
  213. _DPRINTF_HEADER(
  214.     const char *file,
  215.     int line)
  216. {
  217.     if(debug_level >= DEBUGLEVEL_Reports)
  218.     {
  219.         _INDENT();
  220.         kprintf("%s:%ld:",file,line);
  221.     }
  222. }
  223.  
  224. static void __asm
  225. putch(register __d0 c)
  226. {
  227.     if(c != '\0')
  228.         kputc(c);
  229. }
  230.  
  231. void
  232. _DPRINTF(const char *fmt,...)
  233. {
  234.     if(debug_level >= DEBUGLEVEL_Reports)
  235.     {
  236.         va_list args;
  237.  
  238.         va_start(args,fmt);
  239.         RawDoFmt((char *)fmt,args,(VOID (*)())putch,NULL);
  240.         va_end(args);
  241.  
  242.         kprintf("\n");
  243.     }
  244. }
  245.  
  246. /****************************************************************************/
  247.  
  248. void
  249. _ENTER(
  250.     const char *file,
  251.     int line,
  252.     const char *function)
  253. {
  254.     if(debug_level >= DEBUGLEVEL_CallTracing)
  255.     {
  256.         _INDENT();
  257.         kprintf("%s:%ld:Entering %s\n",file,line,function);
  258.     }
  259.  
  260.     indent_level++;
  261. }
  262.  
  263. void
  264. _LEAVE(
  265.     const char *file,
  266.     int line,
  267.     const char *function)
  268. {
  269.     indent_level--;
  270.  
  271.     if(debug_level >= DEBUGLEVEL_CallTracing)
  272.     {
  273.         _INDENT();
  274.         kprintf("%s:%ld: Leaving %s\n",file,line,function);
  275.     }
  276. }
  277.  
  278. void
  279. _RETURN(
  280.     const char *file,
  281.     int line,
  282.     const char *function,
  283.     unsigned long result)
  284. {
  285.     indent_level--;
  286.  
  287.     if(debug_level >= DEBUGLEVEL_CallTracing)
  288.     {
  289.         _INDENT();
  290.         kprintf("%s:%ld: Leaving %s (result 0x%08lx, %ld)\n",file,line,function,result,result);
  291.     }
  292. }
  293.  
  294. /****************************************************************************/
  295.  
  296. void
  297. _ASSERT(
  298.     int x,
  299.     const char *xs,
  300.     const char *file,
  301.     int line,
  302.     const char *function)
  303. {
  304.     #ifdef CONFIRM
  305.     {
  306.         STATIC BOOL ScrollMode    = FALSE;
  307.         STATIC BOOL BatchMode    = FALSE;
  308.     
  309.         if(BatchMode == FALSE)
  310.         {
  311.             if(x == 0)
  312.             {
  313.                 kprintf("%s:%ld:Expression `%s' failed assertion in %s().\n",
  314.                         file,
  315.                         line,
  316.                         xs,
  317.                         function);
  318.     
  319.                 if(ScrollMode == FALSE)
  320.                 {
  321.                     ULONG Signals;
  322.     
  323.                     SetSignal(0,SIGBREAKF_CTRL_C | SIGBREAKF_CTRL_D | SIGBREAKF_CTRL_E);
  324.     
  325.                     kprintf(" ^C to continue, ^D to enter scroll mode, ^E to enter batch mode\r");
  326.     
  327.                     Signals = Wait(SIGBREAKF_CTRL_C | SIGBREAKF_CTRL_D | SIGBREAKF_CTRL_E);
  328.     
  329.                     if(Signals & SIGBREAKF_CTRL_D)
  330.                     {
  331.                         ScrollMode = TRUE;
  332.     
  333.                         kprintf("Ok, entering scroll mode\033[K\n");
  334.                     }
  335.                     else if (Signals & SIGBREAKF_CTRL_E)
  336.                     {
  337.                         BatchMode = TRUE;
  338.     
  339.                         kprintf("Ok, entering batch mode\033[K\n");
  340.                     }
  341.                     else
  342.                     {
  343.                         /* Continue */
  344.     
  345.                         kprintf("\033[K\r");
  346.                     }
  347.                 }
  348.             }
  349.         }
  350.     }
  351.     #else
  352.     {
  353.         if(x == 0)
  354.         {
  355.             _INDENT();
  356.             kprintf("%s:%ld:Expression `%s' failed assertion in %s().\n",
  357.                     file,
  358.                     line,
  359.                     xs,
  360.                     function);
  361.         }
  362.     }
  363.     #endif    /* CONFIRM */
  364. }
  365.