home *** CD-ROM | disk | FTP | other *** search
- /*
- *
- * Title : NetLog.C
- * System : Any */
- #define Version "2.0"
- /* Copyright : John H. Winters
- * Date : 23rd September, 1992
- * Author : John H. Winters
- *
- * Function : Defines the netlog routines.
- *
- * Modification history.
- *
- * Version : 1.1
- * Date : 10th January, 1993
- * Author : John H. Winters
- * Changes : Adapted to new SDF i/f.
- *
- * Version : 2.0
- * Date : 31st May, 1993
- * Author : John H. Winters
- * Changes : Updated for RISC OS 3.1 and new WIMP_ReportError
- *
- * Version :
- * Date :
- * Author :
- * Changes :
- *
- */
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
-
- #if defined (OS2)
- #define INCL_DOSQUEUES
- #define INCL_VIO
- #include <os2.h>
- #endif
-
- #include "global.h"
- #if defined (ARTHUR)
- #include "kernel.h"
- #include "wimplib.h"
- #include "winapp.h"
- #endif
- #include "sdf.h"
- #include "logging.h"
- #include "netlog.h"
- #include "internal.h"
-
- /*
- *============================================================================
- *
- * Hash defines.
- *
- *============================================================================
- */
-
- #define MESSAGEACTION "Logger$MessNo"
-
- /*
- *============================================================================
- *
- * Local data.
- *
- *============================================================================
- */
-
- #if defined (OS2)
- static USHORT LoggerId ;
- static uint LogQueueOpen = FALSE ;
- static HQUEUE QueueHandle ;
- #endif
-
- #if defined (ARTHUR)
- static uint MessageAction = 0 ;
- #endif
-
- static u8 ProcessName [LOG_MAX_TASKNAME + 1] ;
-
- /*
- *============================================================================
- *
- * Forward declarations.
- *
- *============================================================================
- */
-
- #if defined (OS2)
- static void CloseLogQueue (void) ;
- #endif
-
- #if defined (ARTHUR)
- static uint FallBack (
- t_LOG_Severity severity,
- const u8 *message,
- const u8 *timestamp) ;
- #endif
-
- static uint MessageHandler (
- t_LOG_Severity severity,
- const u8 *message,
- const u8 *timestamp) ;
-
- #if defined (OS2)
- static void OpenLogQueue (void) ;
-
- static void SendText (
- t_LOG_Severity severity,
- const u8 *message,
- const u8 *timestamp) ;
- #endif
-
- /*
- *============================================================================
- *
- * Externally visible routines.
- *
- *============================================================================
- */
-
- uint NL_Init (
- const char *proc_name)
-
- /*
- * Function :
- * Registers our routine as a message handler.
- *
- * Parameters :
- * None.
- *
- * Returns :
- * TRUE if we succeed, FALSE if we fail.
- *
- */
-
- {
- uint length ;
- t_LOG_HandlerMask mask ;
-
- length = strlen (proc_name) ;
- if (length > LOG_MAX_TASKNAME)
- {
- length = LOG_MAX_TASKNAME ;
- }
- memset (ProcessName, ' ', LOG_MAX_TASKNAME) ;
- memcpy (ProcessName, proc_name, length) ;
- ProcessName [LOG_MAX_TASKNAME] = '\0' ;
- mask.value = 0 ;
- mask.b.LHM_Debug = TRUE ;
- mask.b.LHM_Info = TRUE ;
- mask.b.LHM_Warning = TRUE ;
- mask.b.LHM_Error = TRUE ;
- mask.b.LHM_SevereError = TRUE ;
- return (LOG_AddHandler (mask, MessageHandler)
- #if defined (ARTHUR)
- && LOG_AddHandler (mask, FallBack)
- #endif
- ) ;
- }
-
- /*
- *============================================================================
- *
- * Local routines.
- *
- *============================================================================
- */
-
- #if defined (OS2)
- static void CloseLogQueue (void)
-
- /*
- * Function :
- * Close the queue to the logger process.
- *
- * Parameters :
- * None.
- *
- * Returns :
- * None, but may alter log_queue_open.
- *
- */
-
- {
- if (LogQueueOpen)
- {
- DosCloseQueue (QueueHandle) ;
- LogQueueOpen = FALSE ;
- }
- }
- #endif
-
-
- #if defined (ARTHUR)
- static uint FallBack (
- t_LOG_Severity severity,
- const u8 *message,
- const u8 *timestamp)
-
- /*
- * Function :
- * Fall back handler for when the main one fails.
- *
- * Parameters :
- * severity Severity of the relevant message.
- * message Message to log.
- * timestamp Timestamp for the message.
- *
- * Returns :
- * TRUE
- *
- */
-
- {
- _kernel_oserror error ;
- t_error_flags flags ;
- unsigned int result ;
-
- severity = severity ;
- timestamp = timestamp ;
- error.errnum = 0 ;
- strcpy (error.errmess, message) ;
- flags.value = 0 ;
- flags.m.ok = TRUE ;
- WIMP_ReportError (&error, flags, ProcessName, &result) ;
- return (TRUE) ;
- }
- #endif
-
-
- static uint MessageHandler (
- t_LOG_Severity severity,
- const u8 *message,
- const u8 *timestamp)
-
- /*
- * Function :
- * Handles messages and sends them off to the log process.
- * N.B. The logging module guarantees that this routine
- * will not be pre-emptively or recursively re-entered.
- *
- * Parameters :
- * severity Message severity.
- * message Message text.
- * timestamp Textual time stamp.
- *
- * Returns :
- * TRUE if we handled it, FALSE otherwise.
- *
- */
-
- {
- #if defined (ARTHUR)
- u8 *body ;
- t_destinee destinee ;
- t_NL_Header header ;
- t_message_block item ;
- uint length ;
- #endif
- uint result ;
- #if !defined (OS2)
- const u8 *string ;
- #endif
-
- result = FALSE ;
- #if defined (OS2)
- if (!LogQueueOpen)
- {
- OpenLogQueue () ;
- }
- if (LogQueueOpen)
- {
- SendText (severity, message, timestamp) ;
- if (LogQueueOpen)
- {
- result = TRUE ;
- }
- }
- #else
- /*
- * Do we know what message number to use?
- */
- if (MessageAction == 0)
- {
- string = getenv (MESSAGEACTION) ;
- if (string != NULL)
- {
- MessageAction = atoi (string) ;
- }
- }
- if (MessageAction != 0)
- {
- /*
- * Let's try to send it.
- */
- length = strlen (message) ;
- if (length > 236 - sizeof (t_NL_Header))
- {
- length = 236 - sizeof (t_NL_Header) ;
- }
- while ((length > 0) &&
- (message [length - 1] == '\n'))
- {
- length-- ;
- }
- header.ident = LOG_IDENT ;
- header.format_version = FORMAT_VERSION ;
- SDF_Putu16 ((u16) length,
- header.text_length) ;
- header.severity = severity ;
- memcpy (header.task_name, ProcessName, LOG_MAX_TASKNAME) ;
- memcpy (header.timestamp,
- timestamp,
- LOG_TIMESTAMP_LEN) ;
- memcpy (item.data.bytes,
- &header,
- sizeof (t_NL_Header)) ;
- body = item.data.bytes + sizeof (t_NL_Header) ;
- memcpy (body,
- message,
- length) ;
- while ((((length + 3) / 4) * 4) != length)
- {
- length++ ;
- }
- item.size = sizeof (t_message_header) + sizeof (t_NL_Header) + length ;
- item.sender = 0 ;
- item.my_ref = 0 ;
- item.your_ref = 0 ;
- item.action = MessageAction ;
- destinee.value = 0 ;
- if (LOG_OSError (WIMP_SendMessage (ET_UserMessage,
- &item,
- destinee,
- 0)) == NULL)
- {
- result = TRUE ;
- }
- }
- #endif
- return (result) ;
- }
-
-
- #if defined (OS2)
- static void OpenLogQueue (void)
-
- /*
- * Function :
- * Attempts to open the queue to a logger process.
- *
- * Parameters :
- * None.
- *
- * Returns :
- * None, but updates the value of log_queue_open
- * depending on the result.
- *
- */
-
- {
- if (!LogQueueOpen)
- {
- if (DosOpenQueue (&LoggerId,
- &QueueHandle,
- QUEUE_NAME) == 0)
- {
- LogQueueOpen = TRUE ;
- }
- }
- }
-
-
- static void SendText (
- t_LOG_Severity severity,
- const u8 *message,
- const u8 *timestamp)
-
- /*
- * Function :
- * Send a text message to the log process.
- *
- * Parameters :
- * severity Severity of the message.
- * message Text of the message.
- * timestamp Textual time stamp.
- *
- * Returns :
- *
- */
-
- {
- u8 *body ;
- t_NL_Header *buffer ;
- SEL handle ;
- SEL his_handle ;
- u16 size ;
-
- size = (U16) strlen (message) ;
- /*
- * Need to allocate a shared segment to pass the data in.
- */
- if (DosAllocSeg (size + sizeof (t_NL_Header),
- &handle,
- SEG_GIVEABLE) == 0)
- {
- buffer = MAKEP (handle, 0) ;
- buffer->ident = LOG_IDENT ;
- buffer->format_version = FORMAT_VERSION ;
- SDF_Putu16 (size,
- buffer->text_length) ;
- buffer->severity = (u8) severity ;
- memcpy (buffer->task_name, ProcessName, LOG_MAX_TASKNAME) ;
- memcpy (buffer->timestamp,
- timestamp,
- LOG_TIMESTAMP_LEN) ;
- body = (u8 *) (buffer + 1) ;
- memcpy (body,
- message,
- size) ;
- /*
- * Give the destination process access to the segment.
- */
- if (DosGiveSeg (handle,
- LoggerId,
- &his_handle) == 0)
- {
- /*
- * Notify him about it.
- */
- if (DosWriteQueue (QueueHandle,
- his_handle,
- size + sizeof (t_NL_Header),
- (PBYTE) buffer,
- QUEUE_PRIORITY) != 0)
- {
- LOG_Error ("LOG: Failed to write to logger's queue.\n") ;
- CloseLogQueue () ;
- }
- }
- else
- {
- LOG_Error ("LOG: Failed to pass a segment to logger.\n") ;
- CloseLogQueue () ;
- }
- /*
- * Relinquish access to the segment.
- */
- DosFreeSeg (handle) ;
- }
- else
- {
- LOG_Error ("LOG: Failed to allocate a shared segment.\n") ;
- }
- }
- #endif
-
-