File: logging.c

package info (click to toggle)
libmseed 2.19.6-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 1,684 kB
  • sloc: ansic: 10,810; makefile: 145; sh: 114
file content (328 lines) | stat: -rw-r--r-- 8,857 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/***************************************************************************
 * logging.c
 *
 * Log handling routines for libmseed
 *
 * Chad Trabant
 * IRIS Data Management Center
 *
 * modified: 2014.197
 ***************************************************************************/

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

#include "libmseed.h"

void ms_loginit_main (MSLogParam *logp,
                      void (*log_print) (char *), const char *logprefix,
                      void (*diag_print) (char *), const char *errprefix);

int ms_log_main (MSLogParam *logp, int level, va_list *varlist);

/* Initialize the global logging parameters */
MSLogParam gMSLogParam = {NULL, NULL, NULL, NULL};

/***************************************************************************
 * ms_loginit:
 *
 * Initialize the global logging parameters.
 *
 * See ms_loginit_main() description for usage.
 ***************************************************************************/
void
ms_loginit (void (*log_print) (char *), const char *logprefix,
            void (*diag_print) (char *), const char *errprefix)
{
  ms_loginit_main (&gMSLogParam, log_print, logprefix, diag_print, errprefix);
} /* End of ms_loginit() */

/***************************************************************************
 * ms_loginit_l:
 *
 * Initialize MSLogParam specific logging parameters.  If the logging parameters
 * have not been initialized (log == NULL) new parameter space will
 * be allocated.
 *
 * See ms_loginit_main() description for usage.
 *
 * Returns a pointer to the created/re-initialized MSLogParam struct
 * on success and NULL on error.
 ***************************************************************************/
MSLogParam *
ms_loginit_l (MSLogParam *logp,
              void (*log_print) (char *), const char *logprefix,
              void (*diag_print) (char *), const char *errprefix)
{
  MSLogParam *llog;

  if (logp == NULL)
  {
    llog = (MSLogParam *)malloc (sizeof (MSLogParam));

    if (llog == NULL)
    {
      ms_log (2, "ms_loginit_l(): Cannot allocate memory\n");
      return NULL;
    }

    llog->log_print  = NULL;
    llog->logprefix  = NULL;
    llog->diag_print = NULL;
    llog->errprefix  = NULL;
  }
  else
  {
    llog = logp;
  }

  ms_loginit_main (llog, log_print, logprefix, diag_print, errprefix);

  return llog;
} /* End of ms_loginit_l() */

/***************************************************************************
 * ms_loginit_main:
 *
 * Initialize the logging subsystem.  Given values determine how ms_log()
 * and ms_log_l() emit messages.
 *
 * This function modifies the logging parameters in the passed MSLogParam.
 *
 * Any log/error printing functions indicated must except a single
 * argument, namely a string (char *).  The ms_log() and
 * ms_log_r() functions format each message and then pass the result
 * on to the log/error printing functions.
 *
 * If the log/error prefixes have been set they will be pre-pended to the
 * message.
 *
 * Use NULL for the function pointers or the prefixes if they should not
 * be changed from previously set or default values.  The default behavior
 * of the logging subsystem is given in the example below.
 *
 * Example: ms_loginit_main (0, (void*)&printf, NULL, (void*)&printf, "error: ");
 ***************************************************************************/
void
ms_loginit_main (MSLogParam *logp,
                 void (*log_print) (char *), const char *logprefix,
                 void (*diag_print) (char *), const char *errprefix)
{
  if (!logp)
    return;

  if (log_print)
    logp->log_print = log_print;

  if (logprefix)
  {
    if (strlen (logprefix) >= MAX_LOG_MSG_LENGTH)
    {
      ms_log_l (logp, 2, "%s", "log message prefix is too large\n");
    }
    else
    {
      logp->logprefix = logprefix;
    }
  }

  if (diag_print)
    logp->diag_print = diag_print;

  if (errprefix)
  {
    if (strlen (errprefix) >= MAX_LOG_MSG_LENGTH)
    {
      ms_log_l (logp, 2, "%s", "error message prefix is too large\n");
    }
    else
    {
      logp->errprefix = errprefix;
    }
  }

  return;
} /* End of ms_loginit_main() */

/***************************************************************************
 * ms_log:
 *
 * A wrapper to ms_log_main() that uses the global logging parameters.
 *
 * See ms_log_main() description for return values.
 ***************************************************************************/
int
ms_log (int level, ...)
{
  int retval;
  va_list varlist;

  va_start (varlist, level);

  retval = ms_log_main (&gMSLogParam, level, &varlist);

  va_end (varlist);

  return retval;
} /* End of ms_log() */

/***************************************************************************
 * ms_log_l:
 *
 * A wrapper to ms_log_main() that uses the logging parameters in a
 * supplied MSLogParam.  If the supplied pointer is NULL the global logging
 * parameters will be used.
 *
 * See ms_log_main() description for return values.
 ***************************************************************************/
int
ms_log_l (MSLogParam *logp, int level, ...)
{
  int retval;
  va_list varlist;
  MSLogParam *llog;

  if (!logp)
    llog = &gMSLogParam;
  else
    llog = logp;

  va_start (varlist, level);

  retval = ms_log_main (llog, level, &varlist);

  va_end (varlist);

  return retval;
} /* End of ms_log_l() */

/***************************************************************************
 * ms_log_main:
 *
 * A standard logging/printing routine.
 *
 * The function uses logging parameters specified in the supplied
 * MSLogParam.
 *
 * This function expects 2+ arguments: message level, fprintf format,
 * and fprintf arguments.
 *
 * Three levels are recognized:
 * 0  : Normal log messages, printed using log_print with logprefix
 * 1  : Diagnostic messages, printed using diag_print with logprefix
 * 2+ : Error messagess, printed using diag_print with errprefix
 *
 * This function builds the log/error message and passes to it as a
 * string (char *) to the functions defined with ms_loginit() or
 * ms_loginit_l().  If the log/error printing functions have not been
 * defined messages will be printed with fprintf, log messages to
 * stdout and error messages to stderr.
 *
 * If the log/error prefix's have been set with ms_loginit() or
 * ms_loginit_l() they will be pre-pended to the message.
 *
 * All messages will be truncated to the MAX_LOG_MSG_LENGTH, this includes
 * any set prefix.
 *
 * Returns the number of characters formatted on success, and a
 * a negative value on error.
 ***************************************************************************/
int
ms_log_main (MSLogParam *logp, int level, va_list *varlist)
{
  static char message[MAX_LOG_MSG_LENGTH];
  int retvalue = 0;
  int presize;
  const char *format;

  if (!logp)
  {
    fprintf (stderr, "ms_log_main() called without specifying log parameters");
    return -1;
  }

  message[0] = '\0';

  format = va_arg (*varlist, const char *);

  if (level >= 2) /* Error message */
  {
    if (logp->errprefix != NULL)
    {
      strncpy (message, logp->errprefix, MAX_LOG_MSG_LENGTH);
      message[MAX_LOG_MSG_LENGTH - 1] = '\0';
    }
    else
    {
      strncpy (message, "Error: ", MAX_LOG_MSG_LENGTH);
    }

    presize  = strlen (message);
    retvalue = vsnprintf (&message[presize],
                          MAX_LOG_MSG_LENGTH - presize,
                          format, *varlist);

    message[MAX_LOG_MSG_LENGTH - 1] = '\0';

    if (logp->diag_print != NULL)
    {
      logp->diag_print (message);
    }
    else
    {
      fprintf (stderr, "%s", message);
    }
  }
  else if (level == 1) /* Diagnostic message */
  {
    if (logp->logprefix != NULL)
    {
      strncpy (message, logp->logprefix, MAX_LOG_MSG_LENGTH);
      message[MAX_LOG_MSG_LENGTH - 1] = '\0';
    }

    presize  = strlen (message);
    retvalue = vsnprintf (&message[presize],
                          MAX_LOG_MSG_LENGTH - presize,
                          format, *varlist);

    message[MAX_LOG_MSG_LENGTH - 1] = '\0';

    if (logp->diag_print != NULL)
    {
      logp->diag_print (message);
    }
    else
    {
      fprintf (stderr, "%s", message);
    }
  }
  else if (level == 0) /* Normal log message */
  {
    if (logp->logprefix != NULL)
    {
      strncpy (message, logp->logprefix, MAX_LOG_MSG_LENGTH);
      message[MAX_LOG_MSG_LENGTH - 1] = '\0';
    }

    presize  = strlen (message);
    retvalue = vsnprintf (&message[presize],
                          MAX_LOG_MSG_LENGTH - presize,
                          format, *varlist);

    message[MAX_LOG_MSG_LENGTH - 1] = '\0';

    if (logp->log_print != NULL)
    {
      logp->log_print (message);
    }
    else
    {
      fprintf (stdout, "%s", message);
    }
  }

  return retvalue;
} /* End of ms_log_main() */