File: log.c

package info (click to toggle)
ctdb 1.12%2Bgit20120201-5
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 6,656 kB
  • sloc: ansic: 61,736; sh: 18,367; xml: 3,887; python: 1,220; makefile: 554; perl: 319; awk: 118
file content (233 lines) | stat: -rw-r--r-- 5,418 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
/*

This file is taken from nfsim (http://ozlabs.org/~jk/projects/nfsim/)

Copyright (c) 2003,2004 Jeremy Kerr & Rusty Russell

This file is part of nfsim.

nfsim is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

nfsim is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with nfsim; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
#include "log.h"
#include "tui.h"
#include "utils.h"
#include "expect.h"
#include <string.h>
#include <talloc.h>
#include <err.h>

static struct {
	enum log_type	type;
	char *		name;
} log_names[] = {
	{ LOG_WRITE,	"write" },
	{ LOG_READ,	"read" },
	{ LOG_LIB,	"lib" },
	{ LOG_VERBOSE,	"verbose" },
};

static int typemask = ~LOG_VERBOSE;

bool log_line(enum log_type type, const char *format, ...)
{
	va_list ap;
	char *line;
	bool ret;

	va_start(ap, format);
	line = talloc_vasprintf(NULL, format, ap);
	va_end(ap);

	if (!type || (type & typemask)) {
		printf("%s\n", line);
		fflush(stdout);
	}

	ret = expect_log_hook(line);
	talloc_free(line);
	return ret;
}

static void log_partial_v(enum log_type type,
			  char *buf,
			  unsigned bufsize,
			  const char *format,
			  va_list ap)
{
	char *ptr;
	int len = strlen(buf);

	/* write to the end of buffer */
	if (vsnprintf(buf + len, bufsize - len - 1, format, ap)
	    > bufsize - len - 1) {
		errx(1, "log_line_partial buffer is full!");
	}

	ptr = buf;

	/* print each bit that ends in a newline */
	for (len = strcspn(ptr, "\n"); *(ptr + len);
			ptr += len, len = strcspn(ptr, "\n")) {
		log_line(type, "%.*s", len++, ptr);
	}

	/* if we've printed, copy any remaining (non-newlined)
	   parts (including the \0) to the front of buf */
	memmove(buf, ptr, strlen(ptr) + 1);
}

void log_partial(enum log_type type, char *buf, unsigned bufsize,
		 const char *format, ...)
{
	va_list ap;

	va_start(ap, format);
	log_partial_v(type, buf, bufsize, format, ap);
	va_end(ap);
}

static inline int parsetype(const char *type)
{
	int i;

	for (i = 0; i < ARRAY_SIZE(log_names); i++)
		if (streq(log_names[i].name, type))
			return log_names[i].type;

	return 0;
}

static bool log_admin(int argc, char **argv)
{
	int i;
	int newtypemask = 0;

	if (argc == 1) {
		log_line(LOG_UI, "current log types:", typemask);

		for (i = 0; i < ARRAY_SIZE(log_names); i++) {
			if (typemask & log_names[i].type)
				log_line(LOG_UI, "\t%s", log_names[i].name);
		}
		return true;
	}

	if (argc == 2) {
		log_line(LOG_ALWAYS, "Expected =, + or - then args");
		return false;
	}

	for (i = 2; i < argc; i++) {
		int type;

		if (!(type = parsetype(argv[i]))) {
			log_line(LOG_ALWAYS, "no such type %s", argv[i]);
			return false;
		}
		newtypemask |= type;
	}

	switch (*argv[1]) {
	case '=':
		typemask = newtypemask;
		break;
	case '-':
		typemask &= ~newtypemask;
		break;
	case '+':
		typemask |= newtypemask;
		break;
	default:
		log_line(LOG_ALWAYS, "unknown modifer: %c", *argv[1]);
		return false;
	}

	return true;
}

static void log_admin_help(int agc, char **argv)
{
#include "generated-log-help:log"
/*** XML Help:
    <section id="c:log">
     <title><command>log</command></title>
     <para>Manage logging settings</para>
     <cmdsynopsis>
      <command>log</command>
      <group choice="opt">
       <arg choice="plain">=</arg>
       <arg choice="plain">+</arg>
       <arg choice="plain">-</arg>
      </group>
      <arg choice="req"><replaceable>type, ...</replaceable></arg>
     </cmdsynopsis>
     <para>Each log message is classified into one of the following
     types:</para>
      <varlistentry>
       <term>UI</term>
       <listitem>
        <para>Normal response from command lines.</para>
       </listitem>
      </varlistentry>
      <varlistentry>
       <term>LIB</term>
       <listitem>
        <para>Logging output from libctdb</para>
       </listitem>
      </varlistentry>
     <variablelist>
      <varlistentry>
       <term>READ</term>
       <listitem>
        <para>Messages from ctdbd</para>
       </listitem>
      </varlistentry>
      <varlistentry>
       <term>WRITE</term>
       <listitem>
        <para>Messages to ctdbd</para>
       </listitem>
      </varlistentry>
      <varlistentry>
       <term>VERBOSE</term>
       <listitem>
        <para>Verbose debug output</para>
       </listitem>
      </varlistentry>
     </variablelist>

     <para>The <command>log</command> command allows you to select
      which messages are displayed. By default, all messages except
      debug will be shown.</para>

     <para>Without any arguments, the current logged types are listed.</para>

     <para>With +, - or = character, those types will be added,
      removed or set as the current types of messages to be logged
      (repectively).</para>

      <para>Messages generated as a result of user input are always
      logged.  </para> </section>
*/
}

static void log_init(void)
{
	if (tui_quiet)
		typemask = 0;
	tui_register_command("log", log_admin, log_admin_help);
}

init_call(log_init);