1 /*! \file
2 
3   Copyright 2003-2018, University Corporation for Atmospheric
4   Research. See netcdf-4/docs/COPYRIGHT file for copying and
5   redistribution conditions. */
6 /**
7  * @file @internal This file is part of netcdf-4, a netCDF-like
8  * interface for HDF5, or a HDF5 backend for netCDF, depending on your
9  * point of view.
10  *
11  * This file contains functions relating to logging errors. Also it
12  * contains the functions nc_malloc, nc_calloc, and nc_free.
13  *
14  * @author Ed Hartnett
15  */
16 
17 #include "config.h"
18 #include <stdarg.h>
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include "assert.h"
22 #ifdef USE_HDF5
23 #include <hdf5.h>
24 #endif /* USE_HDF5 */
25 
26 /* This contents of this file get skipped if LOGGING is not defined
27  * during compile. */
28 #ifdef LOGGING
29 
30 extern int nc_log_level;
31 
32 /* This function prints out a message, if the severity of the message
33    is lower than the global nc_log_level. To use it, do something like
34    this:
35 
36    nc_log(0, "this computer will explode in %d seconds", i);
37 
38    After the first arg (the severity), use the rest like a normal
39    printf statement. Output will appear on stderr.
40 
41    This function is heavily based on the function in section 15.5 of
42    the C FAQ. */
43 void
nc_log(int severity,const char * fmt,...)44 nc_log(int severity, const char *fmt, ...)
45 {
46     va_list argp;
47     int t;
48 
49     /* If the severity is greater than the log level, we don' care to
50        print this message. */
51     if (severity > nc_log_level)
52         return;
53 
54     /* If the severity is zero, this is an error. Otherwise insert that
55        many tabs before the message. */
56     if (!severity)
57         fprintf(stderr, "ERROR: ");
58     for (t=0; t<severity; t++)
59         fprintf(stderr, "\t");
60 
61     /* Print out the variable list of args with vprintf. */
62     va_start(argp, fmt);
63     vfprintf(stderr, fmt, argp);
64     va_end(argp);
65 
66     /* Put on a final linefeed. */
67     fprintf(stderr, "\n");
68     fflush(stderr);
69 }
70 
71 void
nc_log_hdf5(void)72 nc_log_hdf5(void)
73 {
74 #ifdef USE_HDF5
75     H5Eprint(NULL);
76 #endif /* USE_HDF5 */
77 }
78 
79 #endif /* ifdef LOGGING */
80