OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
gctp_print_message.c
Go to the documentation of this file.
1 /****************************************************************************
2 Name: gctp_print_message
3 
4 Purpose: Provides the means to print messages from GCTP. By default a local
5  routine is provided to print messages to the display. However, by
6  calling gctp_set_message_callback, the user of the library can provide
7  their own routine to deal with messages from the library.
8 
9 ****************************************************************************/
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <stdarg.h>
13 #include "gctp.h"
14 #include "oli_local.h"
15 
16 /* Keep track of the routine the user provided to override the normal print
17  message routine. */
18 static GCTP_CALLBACK_PRINT_FUNC callback_print_func = NULL;
19 
20 /****************************************************************************
21 Name: gctp_print_message
22 
23 Purpose: Formats and prints a message to stdout. If the user has provided a
24  callback routine to handle messages, call that one instead.
25 
26 Returns:
27  nothing
28 
29 ****************************************************************************/
31 (
32  GCTP_MESSAGE_TYPE_ENUM message_type, /* I: message type */
33  const char *filename, /* I: source code file name for input */
34  int line_number, /* I: source code line number for input */
35  const char *format, ... /* I: format string for message */
36 )
37 {
38  static const char *message_type_string[GCTP_MESSAGE_TYPE_COUNT + 1]
39  = {"GCTP Info", "GCTP Error", "GCTP Unknown Message"};
40  char buffer[2000];
41  char buffer2[2000];
42  va_list ap;
43 
44  if (callback_print_func)
45  {
46  va_start(ap, format);
47  callback_print_func(message_type, filename, line_number, format, ap);
48  va_end(ap);
49  }
50  else
51  {
52  /* limit the message type to the defined types */
53  if (message_type < 0)
54  message_type = 0;
55  else if (message_type > GCTP_MESSAGE_TYPE_COUNT)
56  message_type = GCTP_MESSAGE_TYPE_COUNT;
57 
58  if (message_type == GCTP_INFO_MESSAGE)
59  {
60  /* Skip the file and line number for info messages */
61  snprintf(buffer, sizeof(buffer), "%s: ",
62  message_type_string[message_type]);
63  }
64  else
65  {
66  /* Default message formatting */
67  snprintf(buffer, sizeof(buffer), "%s:%s:%d",
68  message_type_string[message_type], filename, line_number);
69  }
70 
71  /* Set arg_ptr to beginning of list of optional arguments */
72  va_start(ap, format);
73  vsnprintf(buffer2, sizeof(buffer2), format, ap);
74  /* Reset arg_ptr */
75  va_end(ap);
76 
77  printf("%s %s\n", buffer, buffer2);
78  }
79 }
80 
81 /****************************************************************************
82 Name: gctp_set_message_callback
83 
84 Purpose: Allows the user to set their own routine to handling printing out
85  messages. This allows the user to control what goes to stdout and what
86  format it uses if they want to.
87 
88 Returns:
89  nothing
90 
91 ****************************************************************************/
93 {
94  callback_print_func = callback;
95 }
@ GCTP_MESSAGE_TYPE_COUNT
Definition: gctp.h:144
@ GCTP_INFO_MESSAGE
Definition: gctp.h:142
#define NULL
Definition: decode_rs.h:63
void gctp_set_message_callback(GCTP_CALLBACK_PRINT_FUNC callback)
void gctp_print_message(GCTP_MESSAGE_TYPE_ENUM message_type, const char *filename, int line_number, const char *format,...)
char filename[FILENAME_MAX]
Definition: atrem_corl1.h:122
void(* GCTP_CALLBACK_PRINT_FUNC)(GCTP_MESSAGE_TYPE_ENUM message_type, const char *filename, int line_number, const char *format,...)
Definition: gctp.h:150