OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
ias_logging.h
Go to the documentation of this file.
1 #ifndef LOGGING_H
2 #define LOGGING_H
3 
4 #include <stdio.h>
5 
6 /****************************************************************************
7 Notes on use:
8  - All applications should call ias_log_initialize
9  - The IAS_LOG_* macros should be used to issue log messages.
10  ias_log_message should not be called directly, except in unusual
11  circumstances.
12  - The level of messages can be controlled by setting the IAS_LOG_LEVEL
13  environment variable to DEBUG, INFO, WARN, or ERROR. If the environment
14  variable is not set, the default level of output is INFO.
15  - If you want to change the output level for specific routines during
16  debugging, you can call ias_log_set_output_level with an IAS_LOG_LEVEL_*
17  setting. But that should not be done on a permanent basis.
18  - The child processor needs to call ias_log_initialize to get the pid
19  set properly.
20  - If you want a debug log message written to a specific channel, in the
21  code calling the logging functions define a IAS_LOG_CHANNEL constant
22  before including this header file. The value of IAS_LOG_CHANNEL
23  should be the name of the channel. If IAS_LOG_CHANNEL is defined all
24  debug messages will be written to that channel.
25  - The IAS_LOG_CHANNELS environment variable is used to enable channels
26  for an application. If IAS_LOG_CHANNELS is set, the logging library
27  expects the value to be a comma separated list of channels to enable
28  and any channels not listed are disabled. If the initial character is
29  a '-' character, the list is treated as a blacklist and all channels
30  will be enabled except the listed channels. If IAS_LOG_CHANNELS is not
31  set, all channels are enabled.
32 
33 ****************************************************************************/
34 /* Allow GCC to error check the parameters to the ias_log_message routine
35  like it is a printf statement */
36 #ifdef SWIG
37 #define PRINT_FORMAT_ATTRIBUTE
38 #define PRINT_FORMAT_ATTRIBUTE_WC
39 #else
40 #define PRINT_FORMAT_ATTRIBUTE __attribute__ ((format(printf,4,5)))
41 #define PRINT_FORMAT_ATTRIBUTE_WC __attribute__ ((format(printf,5,6)))
42 #endif
43 
44 /* Definition of supported log message levels */
45 typedef enum IAS_LOG_MESSAGE_LEVEL {
50  IAS_LOG_LEVEL_DISABLE /* disable logging entirely (only used for tests) */
52 
53 /* Declare the ias_log_message_level variable as an external variable for
54  every file except the .c file where is is declared */
55 #ifndef LOGGING_C
57 #endif
58 
60 (
61  const char *log_program_name /* I: name to output with each log message */
62 );
63 
65 (
66  int new_level /* I: minimum logging level to output */
67 );
68 
70 (
71  FILE *new_fp /* I: File pointer for output message */ );
72 
73 void ias_log_message
74 (
75  int log_level, /* I: message level for input */
76  const char *filename, /* I: source code file name for input */
77  int line_number, /* I: source code line number for input */
78  const char *format, ... /* I: format string for message */
80 
82 (
83  int log_level, /* I: message level for input */
84  const char *channel, /* I: message channel name */
85  const char *filename, /* I: source code file name for input */
86  int line_number, /* I: source code line number for input */
87  const char *format, ... /* I: format string for message */
89 
90 
91 /************************************************************************/
92 #define IAS_LOG_DEBUG_ENABLED() \
93  (((IAS_LOG_LEVEL_DEBUG) >= (ias_log_message_level)) ? (1) : (0))
94 
95 /************************************************************************/
96 #define IAS_LOG_ERROR(format,...) \
97 ias_log_message(IAS_LOG_LEVEL_ERROR,__FILE__,__LINE__,format,##__VA_ARGS__)
98 
99 /************************************************************************/
100 #define IAS_LOG_WARNING(format,...) \
101  if (IAS_LOG_LEVEL_WARN >= ias_log_message_level) \
102  ias_log_message(IAS_LOG_LEVEL_WARN,__FILE__,__LINE__, \
103  format,##__VA_ARGS__)
104 
105 /************************************************************************/
106 #define IAS_LOG_INFO(format,...) \
107  if (IAS_LOG_LEVEL_INFO >= ias_log_message_level) \
108  ias_log_message(IAS_LOG_LEVEL_INFO,__FILE__,__LINE__, \
109  format,##__VA_ARGS__)
110 
111 /************************************************************************/
112 #ifndef IAS_LOG_CHANNEL
113 #define IAS_LOG_DEBUG(format,...) \
114  if (IAS_LOG_LEVEL_DEBUG >= ias_log_message_level) \
115  ias_log_message(IAS_LOG_LEVEL_DEBUG,__FILE__,__LINE__, \
116  format,##__VA_ARGS__)
117 #else
118 #define IAS_LOG_DEBUG(format,...) \
119  if (IAS_LOG_LEVEL_DEBUG >= ias_log_message_level) \
120  ias_log_message_with_channel(IAS_LOG_LEVEL_DEBUG, IAS_LOG_CHANNEL, \
121  __FILE__,__LINE__, format,##__VA_ARGS__)
122 #endif
123 
124 /************************************************************************/
125 #define IAS_LOG_DEBUG_TO_CHANNEL(channel,format,...) \
126  if (IAS_LOG_LEVEL_DEBUG >= ias_log_message_level) \
127  ias_log_message_with_channel(IAS_LOG_LEVEL_DEBUG, channel, \
128  __FILE__, __LINE__, format, ##__VA_ARGS__)
129 
130 /************************************************************************/
131 
132 #undef PRINT_FORMAT_ATTRIBUTE
133 #undef PRINT_FORMAT_ATTRIBUTE_WC
134 
135 #endif
136 
enum IAS_LOG_MESSAGE_LEVEL ias_log_message_level
Definition: ias_logging.c:22
@ IAS_LOG_LEVEL_DISABLE
Definition: ias_logging.h:50
@ IAS_LOG_LEVEL_WARN
Definition: ias_logging.h:48
@ IAS_LOG_LEVEL_ERROR
Definition: ias_logging.h:49
@ IAS_LOG_LEVEL_DEBUG
Definition: ias_logging.h:46
int ias_log_initialize(const char *log_program_name)
Definition: ias_logging.c:55
char filename[FILENAME_MAX]
Definition: atrem_corl1.h:122
void ias_log_message_with_channel(int log_level, const char *channel, const char *filename, int line_number, const char *format,...) PRINT_FORMAT_ATTRIBUTE_WC
Definition: ias_logging.c:357
IAS_LOG_MESSAGE_LEVEL ias_log_set_output_level(int new_level)
Definition: ias_logging.c:144
#define PRINT_FORMAT_ATTRIBUTE_WC
Definition: ias_logging.h:41
IAS_LOG_MESSAGE_LEVEL
Definition: ias_logging.h:45
@ IAS_LOG_LEVEL_INFO
Definition: ias_logging.h:47
int ias_log_set_output_target(FILE *new_fp)
Definition: ias_logging.c:167
#define PRINT_FORMAT_ATTRIBUTE
Definition: ias_logging.h:40
void ias_log_message(int log_level, const char *filename, int line_number, const char *format,...) PRINT_FORMAT_ATTRIBUTE
Definition: ias_logging.c:330