Whamcloud - gitweb
fa457f581e49b370bb1a134404f088e981e8badc
[fs/lustre-release.git] / lustre / utils / gss / err_util.c
1 /*
2   Copyright (c) 2004 The Regents of the University of Michigan.
3   All rights reserved.
4
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions
7   are met:
8
9   1. Redistributions of source code must retain the above copyright
10      notice, this list of conditions and the following disclaimer.
11   2. Redistributions in binary form must reproduce the above copyright
12      notice, this list of conditions and the following disclaimer in the
13      documentation and/or other materials provided with the distribution.
14   3. Neither the name of the University nor the names of its
15      contributors may be used to endorse or promote products derived
16      from this software without specific prior written permission.
17
18   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
19   WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20   MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21   DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22   FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
25   BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include <stdio.h>
32 #include <stdarg.h>
33 #include <syslog.h>
34 #include <string.h>
35 #include <fcntl.h>
36 #include <ctype.h>
37 #include "err_util.h"
38
39 static int verbosity = 0;
40 static int fg = 0;
41
42 static char message_buf[500];
43
44 void initerr(char *progname, int set_verbosity, int set_fg)
45 {
46         verbosity = set_verbosity;
47         fg = set_fg;
48         if (!fg)
49                 openlog(progname, LOG_PID, LOG_DAEMON);
50 }
51
52
53 void printerr(int priority, char *format, ...)
54 {
55         va_list args;
56         int ret;
57         int buf_used, buf_available;
58         char *buf;
59
60         /* Don't bother formatting a message we're never going to print! */
61         if (priority > verbosity)
62                 return;
63
64         buf_used = strlen(message_buf);
65         /* subtract 4 to leave room for "...\n" if necessary */
66         buf_available = sizeof(message_buf) - buf_used - 4;
67         buf = message_buf + buf_used;
68
69         /*
70          * Aggregate lines: only print buffer when we get to the
71          * end of a line or run out of space
72          */
73         va_start(args, format);
74         ret = vsnprintf(buf, buf_available, format, args);
75         va_end(args);
76
77         if (ret < 0)
78                 goto printit;
79         if (ret >= buf_available) {
80                 /* Indicate we're truncating */
81                 strcat(message_buf, "...\n");
82                 goto printit;
83         }
84         buf_used = strlen(message_buf);
85         if (buf_used > 0 && message_buf[buf_used - 1] == '\n')
86                 goto printit;
87         return;
88 printit:
89         if (fg) {
90                 fprintf(stderr, "%s", message_buf);
91         } else {
92                 syslog(LOG_ERR, "%s", message_buf);
93         }
94         /* reset the buffer */
95         memset(message_buf, 0, sizeof(message_buf));
96 }
97
98 void print_hexl(int pri, unsigned char *cp, int length)
99 {
100         int i, j, jm;
101         unsigned char c;
102
103         printerr(pri, "length %d\n",length);
104         printerr(pri, "\n");
105
106         for (i = 0; i < length; i += 0x10) {
107                 printerr(pri, "  %04x: ", (unsigned int)i);
108                 jm = length - i;
109                 jm = jm > 16 ? 16 : jm;
110
111                 for (j = 0; j < jm; j++) {
112                         if ((j % 2) == 1)
113                                 printerr(pri, "%02x ", (unsigned int)cp[i+j]);
114                         else
115                                 printerr(pri, "%02x", (unsigned int)cp[i+j]);
116                 }
117                 for (; j < 16; j++) {
118                         if ((j % 2) == 1)
119                                 printerr(pri,"   ");
120                         else
121                                 printerr(pri,"  ");
122                 }
123                 printerr(pri," ");
124
125                 for (j = 0; j < jm; j++) {
126                         c = cp[i+j];
127                         c = isprint(c) ? c : '.';
128                         printerr(pri,"%c", c);
129                 }
130                 printerr(pri,"\n");
131         }
132 }
133