/* Copyright 1998 Acorn Computers Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/**************************************************************************/
/* File:    DebugLib.h                                                    */
/* Purpose: Header file for DebugLib API.                                 */
/*                                                                        */
/* Copyright [1999-2002] Pace Micro Technology PLC.  All rights reserved. */
/*                                                                        */
/* The copyright in this material is owned by Pace Micro Technology PLC   */
/* ("Pace").  This material is regarded as a highly confidential trade    */
/* secret of Pace.  It may not be reproduced, used, sold or in any        */
/* other way exploited or transferred to any third party without the      */
/* prior written permission of Pace.                                      */
/**************************************************************************/

#ifndef __debuglib_h
#define __debuglib_h

#ifdef DEBUGLIB

#    include <stdio.h>
#    include <stdarg.h>
#    include <stdlib.h>

#    ifndef __kernel_h
#      include "kernel.h"
#    endif

/* Lots of fun trying to set up a boolean type */
#    ifndef BOOL_DEFINED
#      define BOOL_DEFINED
#      if defined(bool)
#        define DebugLib_BOOL bool
#      else
#        define DebugLib_BOOL int
#      endif
#      if !defined(BOOL)
#        define BOOL DebugLib_BOOL
#      endif /* !BOOL */
#      if !defined(TRUE)
#        define TRUE 1
#      endif /* !TRUE */
#      if !defined(FALSE)
#        define FALSE 0
#      endif /* !FALSE */
#    else /* BOOL_DEFINED defined */
#      define DebugLib_BOOL BOOL
#    endif /* BOOL_DEFINED */


    typedef enum
    {
      NULL_OUTPUT = 0,       /* No valid debugging mechanisms found. */
      TRACKER_OUTPUT = 1,    /* Output to !Tracker by Andy Armstrong. (Wimp window) */
      PRINTF_OUTPUT = 2,     /* Standard printf statements. */
      NOTUSED_OUTPUT = 3,    /* Not currently used. */
      PDEBUG_OUTPUT = 4,     /* Parallel port debugging (JSmith). */
      SOCKET_OUTPUT = 5,     /* Debug to network socket (RBuckley). */
      DEBUGIT_OUTPUT = 6,    /* Output to !List-Debug - Wimp based debug system (MChallis). */
      TML_OUTPUT = 7,        /* Output to TML debugging podule. */
      FILE_OUTPUT = 8,       /* Output to file. */
      SERIAL_OUTPUT = 9,     /* Output to serial port (dual serial interface only). */
      DADEBUG_OUTPUT = 10,   /* Output to dynamic area. */
      BRAINLINK_OUTPUT = 11, /* Output to BrainLink .*/
      REPORTER_OUTPUT = 12,  /* Output to Reporter. */
      SYSLOG_OUTPUT = 13,    /* Output to SysLog module. */
      HAL_OUTPUT = 14,       /* Output to HAL via HAL_DebugTX. */
      /* Put new devices in this space */
      UNSET_OUTPUT        /* Special case, not equivalent NULL_OUTPUT */
    }debug_device;

#    define DebugLib_Options_TasknamePrefix_Off   0u
#    define DebugLib_Options_TasknamePrefix_On    1u
#    define DebugLib_Options_SerialPortLF_Off     0u
#    define DebugLib_Options_SerialPortLF_On      (1u<<1)
#    define DebugLib_Options_ScreenCornering_Off  0
#    define DebugLib_Options_ScreenCornering_On   (1u<<2)
#    define DebugLib_Options_StampDebug_Off       0
#    define DebugLib_Options_StampDebug_On        (1u<<3)

#    define DebugLib_OutputBufferOff_Flag_UseCurrentDevice   1u

#    ifdef __cplusplus
    extern "C" {
#    endif

    extern void debug_initialise (const char *, const char *, const char *);
    extern void debug_initialise_trace (debug_device, const char *);
    extern void debug_terminate (void);
    extern void debug_output_device (debug_device);
    extern _kernel_oserror *debug_output_buffer_on (int, int);
    extern void debug_output_buffer_off (int flags, debug_device device);
    extern void debug_beep (void);
    extern const char *debug_version (void);
    /* This call now deprecated, DebugLib now adds the atexit itself in debug_initialise */
#    define debug_atexit()

    /* Functions to set options */
    extern void debug_set_device (debug_device);
    extern void debug_set_raw_device (debug_device);
    extern void debug_set_trace_device (debug_device);
    extern void debug_set_taskname_prefix (DebugLib_BOOL);
    extern void debug_set_area_level_prefix (DebugLib_BOOL);
    extern void debug_set_stamp_debug (DebugLib_BOOL);
    extern void debug_set_screen_cornering (DebugLib_BOOL);
    extern void debug_set_unbuffered_files (DebugLib_BOOL);
    extern void debug_set_serial_lf (DebugLib_BOOL);
    extern void debug_set_serial_port_speed (int);
    extern void debug_set_serial_port_number (int);
    extern void debug_set_dump_width (size_t);
    extern void debug_set_area_pad_limit (size_t);

    /* Functions to read settings/options */
    extern debug_device debug_read_device (void);

    /* Deprecated functions */
#    define debug_output_device(debug_device) debug_set_device(debug_device)
    extern void debug_set_options (unsigned int, int, int);

    /* Debug output functions that are masked by the macros. These declarations have to be in the
     * header file to avoid linking problems
     */
#    pragma -v1 /* hint to compiler to check f/s/printf format */
    extern void _dprintf(const char *, const char * /*format*/, ...);
    extern void _dprintf_raw(void *, const char * /*format*/, ...);
    extern void _dfprintf(const char *, debug_device stream, const char * /*format*/, ...);
    extern void _dvprintf(const char *area_level, const char *format, const va_list arg);
#    pragma -v0 /* back to the default */

    extern void ddumpbuf(const char *area_level, const void *buffer, size_t size, size_t offset);

#    ifdef DEBUGLIB_NOBRACKETS
#        define dprintf(a,...)          _dprintf(a,__VA_ARGS__)
#        define dprintf_raw(a,...)      _dprintf_raw(a,__VA_ARGS__)
#        define dfprintf(a,b,...)       _dfprintf(a,b,__VA_ARGS__)
#        define dvprintf(a,b,c)         _dvprintf(a,b,c)
#    else
#        define dprintf(a)              _dprintf a
#        define dprintf_raw(a)          _dprintf_raw a
#        define dfprintf(a)             _dfprintf a
#        define dvprintf(a)             _dvprintf a
#    endif

    /* Include the remotedb headers */
#    ifndef REMOTE_DEBUG
#      define REMOTE_DEBUG
#      define __BODGE
#    endif
#    include "remotedb/remote.h"
#    ifdef __BODGE
#      undef REMOTE_DEBUG
#      undef __BODGE
#    endif

    /* And export the remote debug session variable */
    extern remote_debug_session *debug_remotedb_info;

#    ifdef __cplusplus
    }
#    endif

#else

#    define debug_initialise(a,b,c)
#    define debug_output_device(a)
#    define debug_output_buffer_on(a,b)
#    define debug_output_buffer_off(a,b)
#    define debug_terminate()
#    define debug_beep()
#    define debug_initialise_trace(a,b)
#    define debug_set_options(a,b,c)
#    define debug_set_device(a)
#    define debug_set_trace_device(a)
#    define debug_set_raw_device(a)
#    define debug_set_taskname_prefix(a)
#    define debug_set_area_level_prefix(a)
#    define debug_set_stamp_debug(a)
#    define debug_set_screen_cornering(a)
#    define debug_set_unbuffered_files(a)
#    define debug_set_serial_lf(a)
#    define debug_set_serial_port_speed(a)
#    define debug_set_serial_port_number(a)
#    define debug_set_dump_width(a)
#    define debug_set_area_pad_limit(a)
#    define debug_atexit()
#    define debug_version()
#    define ddumpbuf(a,b,c,d)

#    ifdef DEBUGLIB_NOBRACKETS
#        define dprintf(a,...)
#        define dprintf_raw(a,...)
#        define dfprintf(a,b,...)
#        define dvprintf(a,b,c)
#    else
#        define dprintf(a)
#        define dprintf_raw(a)
#        define dfprintf(a)
#        define dvprintf(a)
#    endif

#endif

#endif

/* -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ END +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ */
