NCEPLIBS-g2c  1.7.0
util.c
Go to the documentation of this file.
1 
7 #include "grib2_int.h"
8 #include <stdarg.h>
9 
25 int
26 g2c_check_msg(unsigned char *cgrib, g2int *lencurr, int verbose)
27 {
28  unsigned char G = 0x47; /* 'G' */
29  unsigned char R = 0x52; /* 'R' */
30  unsigned char I = 0x49; /* 'I' */
31  unsigned char B = 0x42; /* 'B' */
32  unsigned char seven = 0x37; /* '7' */
33 
34  assert(cgrib && lencurr);
35 
36  /* Check to see if beginning of GRIB message exists. */
37  if (cgrib[0] != G || cgrib[1] != R || cgrib[2] != I || cgrib[3] != B)
38  {
39  if (verbose)
40  printf("GRIB not found in given message. A call to routine g2_create() "
41  "is required to to initialize GRIB messge.\n");
42  return G2C_NOT_GRIB;
43  }
44 
45  /* Get current length of GRIB message. */
46  gbit(cgrib, lencurr, 96, 32);
47 
48  /* Check to see if GRIB message is already complete. */
49  if (cgrib[*lencurr - 4] == seven && cgrib[*lencurr - 3] == seven &&
50  cgrib[*lencurr - 2] == seven && cgrib[*lencurr - 1] == seven)
51  {
52  if (verbose)
53  printf("GRIB message already complete. Cannot add new section.\n");
54  return G2C_MSG_COMPLETE;
55  }
56 
57  return G2C_NO_ERROR;
58 }
59 
60 #ifdef LOGGING
61 /* This is the severity level of messages which will be logged. Use
62  severity 0 for errors, 1 for important log messages, 2 for less
63  important, etc. */
64 int g2_log_level = -1;
65 
66 /* This function prints out a message, if the severity of
67  * the message is lower than the global g2_log_level. To use it, do
68  * something like this:
69  *
70  * g2_log(0, "this computer will explode in %d seconds", i);
71  *
72  * After the first arg (the severity), use the rest like a normal
73  * printf statement. Output will appear on stderr.
74  *
75  * This function is not included in the build unless NCEPLIBS-g2c was
76  * built with -DLOGGING.
77  *
78  * Ed Hartnett
79  */
80 void
81 g2_log(int severity, const char *fmt, ...)
82 {
83  va_list argp;
84  int t;
85  FILE *f = stderr;
86 
87  /* If the severity is greater than the log level, we don't print
88  * this message. */
89  if (severity > g2_log_level)
90  return;
91 
92  /* If the severity is zero, this is an error. Otherwise insert that
93  many tabs before the message. */
94  if (!severity)
95  fprintf(f, "ERROR: ");
96  for (t = 0; t < severity; t++)
97  fprintf(f, "\t");
98 
99  /* Print out the variable list of args with vprintf. */
100  va_start(argp, fmt);
101  vfprintf(f, fmt, argp);
102  va_end(argp);
103 
104  /* Put on a final linefeed. */
105  fprintf(f, "\n");
106  fflush(f);
107 }
108 #endif /* LOGGING */
109 
123 int
124 g2c_set_log_level(int new_level)
125 {
126 #ifdef LOGGING
127  /* Remember the new level. */
128  g2_log_level = new_level;
129 
130  LOG((1, "log_level changed to %d", g2_log_level));
131 #endif
132  return G2C_NO_ERROR;
133 }
g2c_set_log_level
int g2c_set_log_level(int new_level)
Use this to set the global log level.
Definition: util.c:124
G2C_NO_ERROR
#define G2C_NO_ERROR
No error.
Definition: grib2.h:328
LOG
#define LOG(e)
Ignore logging to stdout.
Definition: grib2_int.h:138
grib2_int.h
Header file with internal function prototypes NCEPLIBS-g2c library.
gbit
void gbit(unsigned char *in, g2int *iout, g2int iskip, g2int nbits)
Get bits - unpack bits: Extract arbitrary size values from a packed bit string, right justifying each...
Definition: gbits.c:20
g2int
int64_t g2int
Long integer type.
Definition: grib2.h:28
G2C_MSG_COMPLETE
#define G2C_MSG_COMPLETE
GRIB message already complete.
Definition: grib2.h:330
g2c_check_msg
int g2c_check_msg(unsigned char *cgrib, g2int *lencurr, int verbose)
Check for 'GRIB' at the beginning of a GRIB message, and check to see if the message is already termi...
Definition: util.c:26
G2C_NOT_GRIB
#define G2C_NOT_GRIB
GRIB header not found.
Definition: grib2.h:329