11 #include "command-line.h"
14 #include "vlog-socket.h"
17 usage(char *prog_name, int exit_code)
19 printf("Usage: %s [TARGET] [ACTION...]\n"
21 " -a, --all Apply to all targets (default)\n"
22 " -t, --target=TARGET Specify target program, as a pid or an\n"
23 " absolute path to a Unix domain socket\n"
25 " -l, --list List current settings\n"
26 " -s, --set=MODULE:FACILITY:LEVEL\n"
27 " Set MODULE and FACILITY log level to LEVEL\n"
28 " MODULE may be any valid module name or 'ANY'\n"
29 " FACILITY may be 'syslog' or 'console' or 'ANY'\n"
30 " LEVEL may be 'emer', 'err', 'warn', or 'dbg'\n"
31 " -h, --help Print this helpful information\n",
37 transact(struct vlog_client *client, const char *request, bool *ok)
40 int error = vlog_client_transact(client, request, &reply);
42 fprintf(stderr, "%s: transaction error: %s\n",
43 vlog_client_target(client), strerror(error));
46 return reply ? reply : xstrdup("");
50 transact_ack(struct vlog_client *client, const char* request, bool *ok)
53 int error = vlog_client_transact(client, request, &reply);
55 fprintf(stderr, "%s: transaction error: %s\n",
56 vlog_client_target(client), strerror(error));
58 } else if (strcmp(reply, "ack")) {
59 fprintf(stderr, "Received unexpected reply from %s: %s\n",
60 vlog_client_target(client), reply);
67 add_target(struct vlog_client ***clients, size_t *n_clients,
68 const char *path, bool *ok)
70 struct vlog_client *client;
71 int error = vlog_client_connect(path, &client);
73 fprintf(stderr, "Error connecting to \"%s\": %s\n",
74 path, strerror(error));
77 *clients = xrealloc(*clients, sizeof *clients * (*n_clients + 1));
78 (*clients)[*n_clients] = client;
84 add_all_targets(struct vlog_client ***clients, size_t *n_clients, bool *ok)
89 directory = opendir("/tmp");
91 fprintf(stderr, "/tmp: opendir: %s\n", strerror(errno));
94 while ((de = readdir(directory)) != NULL) {
95 if (!strncmp(de->d_name, "vlogs.", 5)) {
96 char *path = xasprintf("/tmp/%s", de->d_name);
97 add_target(clients, n_clients, path, ok);
105 int main(int argc, char *argv[])
107 static const struct option long_options[] = {
108 /* Target options must come first. */
109 {"all", no_argument, NULL, 'a'},
110 {"target", required_argument, NULL, 't'},
111 {"help", no_argument, NULL, 'h'},
113 /* Action options come afterward. */
114 {"list", no_argument, NULL, 'l'},
115 {"set", required_argument, NULL, 's'},
120 /* Determine targets. */
123 struct vlog_client **clients = NULL;
124 size_t n_clients = 0;
126 set_program_name(argv[0]);
128 short_options = long_options_to_short_options(long_options);
133 option = getopt_long(argc, argv, short_options, long_options, NULL);
137 if (!strchr("ath", option) && n_clients == 0) {
138 fatal(0, "no targets specified (use --help for help)");
144 add_all_targets(&clients, &n_clients, &ok);
148 add_target(&clients, &n_clients, optarg, &ok);
152 for (i = 0; i < n_clients; i++) {
153 struct vlog_client *client = clients[i];
156 printf("%s:\n", vlog_client_target(client));
157 reply = transact(client, "list", &ok);
158 fputs(reply, stdout);
164 for (i = 0; i < n_clients; i++) {
165 struct vlog_client *client = clients[i];
166 char *request = xasprintf("set %s", optarg);
167 transact_ack(client, request, &ok);
173 usage(argv[0], EXIT_SUCCESS);
182 "warning: no actions specified (use --help for help)\n");