BRL-CAD
log.c
Go to the documentation of this file.
1 /* L O G . C
2  * BRL-CAD
3  *
4  * Copyright (c) 2008-2014 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; see the file named COPYING for more
18  * information.
19  */
20 /** @file libged/log.c
21  *
22  * The log command.
23  *
24  */
25 
26 #include "common.h"
27 
28 #include <string.h>
29 
30 #include "ged.h"
31 
32 
33 /**
34  * Get the output from bu_log and append it to clientdata vls.
35  */
36 static int
37 log_hook(void *clientdata,
38  void *str)
39 {
40  struct bu_vls *vp = (struct bu_vls *)clientdata;
41  int len;
42 
43  BU_CK_VLS(vp);
44  len = bu_vls_strlen(vp);
45  bu_vls_strcat(vp, (const char *)str);
46  len = bu_vls_strlen(vp) - len;
47 
48  return len;
49 }
50 
51 
52 int
53 ged_log(struct ged *gedp, int argc, const char *argv[])
54 {
55  static char *usage = "get|start|stop";
56 
59  GED_CHECK_ARGC_GT_0(gedp, argc, GED_ERROR);
60 
61  /* initialize result */
62  bu_vls_trunc(gedp->ged_result_str, 0);
63 
64  /* must be wanting help */
65  if (argc == 1) {
66  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
67  return GED_HELP;
68  }
69 
70  if (argc != 2) {
71  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
72  return GED_ERROR;
73  }
74 
75  if (argv[1][0] == 'g' && BU_STR_EQUAL(argv[1], "get")) {
77  return GED_OK;
78  }
79 
80  if (argv[1][0] == 's' && BU_STR_EQUAL(argv[1], "start")) {
81  bu_log_add_hook(log_hook, (void *)gedp->ged_log);
82  return GED_OK;
83  }
84 
85  if (argv[1][0] == 's' && BU_STR_EQUAL(argv[1], "stop")) {
86  bu_log_delete_hook(log_hook, (void *)gedp->ged_log);
87  return GED_OK;
88  }
89 
90  bu_log("Usage: %s %s ", argv[0], usage);
91  return GED_ERROR;
92 }
93 
94 
95 /*
96  * Local Variables:
97  * tab-width: 8
98  * mode: C
99  * indent-tabs-mode: t
100  * c-file-style: "stroustrup"
101  * End:
102  * ex: shiftwidth=4 tabstop=8
103  */
void usage(struct ged *gedp)
Definition: coil.c:315
void bu_log_add_hook(bu_hook_t func, void *clientdata)
Definition: log.c:69
#define GED_OK
Definition: ged.h:55
void bu_log(const char *fmt,...)
Definition: log.c:176
Definition: ged.h:338
void bu_vls_strcat(struct bu_vls *vp, const char *s)
Definition: vls.c:368
void bu_vls_trunc(struct bu_vls *vp, int len)
Definition: vls.c:198
#define GED_CHECK_ARGC_GT_0(_gedp, _argc, _flags)
Definition: ged.h:202
Header file for the BRL-CAD common definitions.
struct bu_vls * ged_log
Definition: ged.h:343
#define GED_ERROR
Definition: ged.h:61
#define BU_CK_VLS(_vp)
Definition: vls.h:69
#define GED_CHECK_DATABASE_OPEN(_gedp, _flags)
Definition: ged.h:114
size_t bu_vls_strlen(const struct bu_vls *vp)
Definition: vls.c:189
struct bu_vls * ged_result_str
Definition: ged.h:357
void bu_vls_printf(struct bu_vls *vls, const char *fmt,...) _BU_ATTR_PRINTF23
Definition: vls.c:694
#define GED_HELP
Definition: ged.h:62
int ged_log(struct ged *gedp, int argc, const char *argv[])
Definition: log.c:53
#define GED_CHECK_READ_ONLY(_gedp, _flags)
Definition: ged.h:181
Definition: vls.h:56
void bu_log_delete_hook(bu_hook_t func, void *clientdata)
Definition: log.c:76
void bu_vls_vlscatzap(struct bu_vls *dest, struct bu_vls *src)
Definition: vls.c:433
#define BU_STR_EQUAL(s1, s2)
Definition: str.h:126