1 /******************************************************************************
2 Copyright (c) 1992, 1995, 1996 Xerox Corporation. All rights reserved.
3 Portions of this code were written by Stephen White, aka ghond.
4 Use and copying of this software and preparation of derivative works based
5 upon this software are permitted. Any distribution of this software or
6 derivative works must comply with all applicable United States export
7 control laws. This software is made available AS IS, and Xerox Corporation
8 makes no warranty about the software, its performance or its conformity to
9 any specification. Any person obtaining a copy of this software is requested
10 to send their name and post office or electronic mail address to:
11 Pavel Curtis
12 Xerox PARC
13 3333 Coyote Hill Rd.
14 Palo Alto, CA 94304
15 Pavel@Xerox.Com
16 *****************************************************************************/
17
18 #include <errno.h>
19 #include "my-stdarg.h"
20 #include "my-stdio.h"
21 #include "my-string.h"
22 #include "my-time.h"
23
24 #include "bf_register.h"
25 #include "config.h"
26 #include "functions.h"
27 #include "log.h"
28 #include "options.h"
29 #include "storage.h"
30 #include "streams.h"
31 #include "utils.h"
32
33 static FILE *log_file = 0;
34
35 void
set_log_file(FILE * f)36 set_log_file(FILE * f)
37 {
38 log_file = f;
39 }
40
41 static void
do_log(const char * fmt,va_list args,const char * prefix)42 do_log(const char *fmt, va_list args, const char *prefix)
43 {
44 FILE *f;
45
46 if (log_file) {
47 time_t now = time(0);
48 char *nowstr = ctime(&now);
49
50 nowstr[19] = '\0'; /* kill the year and newline at the end */
51 f = log_file;
52 fprintf(f, "%s: %s", nowstr + 4, prefix); /* skip the day of week */
53 } else
54 f = stderr;
55
56 vfprintf(f, fmt, args);
57 fflush(f);
58 }
59
60 void
oklog(const char * fmt,...)61 oklog(const char *fmt,...)
62 {
63 va_list args;
64
65 va_start(args, fmt);
66 do_log(fmt, args, "");
67 va_end(args);
68 }
69
70 void
errlog(const char * fmt,...)71 errlog(const char *fmt,...)
72 {
73 va_list args;
74
75 va_start(args, fmt);
76 do_log(fmt, args, "*** ");
77 va_end(args);
78 }
79
80 void
log_perror(const char * what)81 log_perror(const char *what)
82 {
83 errlog("%s: %s\n", what, strerror(errno));
84 }
85
86
87 #ifdef LOG_COMMANDS
88 static Stream *command_history = 0;
89 #endif
90
91 void
reset_command_history()92 reset_command_history()
93 {
94 #ifdef LOG_COMMANDS
95 if (command_history == 0)
96 command_history = new_stream(1024);
97 else
98 reset_stream(command_history);
99 #endif
100 }
101
102 void
log_command_history()103 log_command_history()
104 {
105 #ifdef LOG_COMMANDS
106 errlog("COMMAND HISTORY:\n%s", stream_contents(command_history));
107 #endif
108 }
109
110 void
add_command_to_history(Objid player,const char * command)111 add_command_to_history(Objid player, const char *command)
112 {
113 #ifdef LOG_COMMANDS
114 time_t now = time(0);
115 char *nowstr = ctime(&now);
116
117 nowstr[19] = '\0'; /* kill the year and newline at the end */
118 stream_printf(command_history, "%s: #%d: %s\n",
119 nowstr + 4, /* skip day of week */
120 player, command);
121 #endif /* LOG_COMMANDS */
122 }
123
124 /**** built in functions ****/
125
126 static package
bf_server_log(Var arglist,Byte next,void * vdata,Objid progr)127 bf_server_log(Var arglist, Byte next, void *vdata, Objid progr)
128 {
129 if (!is_wizard(progr)) {
130 free_var(arglist);
131 return make_error_pack(E_PERM);
132 } else {
133 int is_error = (arglist.v.list[0].v.num == 2
134 && is_true(arglist.v.list[2]));
135
136 if (is_error)
137 errlog("> %s\n", arglist.v.list[1].v.str);
138 else
139 oklog("> %s\n", arglist.v.list[1].v.str);
140
141 free_var(arglist);
142 return no_var_pack();
143 }
144 }
145
146 void
register_log(void)147 register_log(void)
148 {
149 register_function("server_log", 1, 2, bf_server_log, TYPE_STR, TYPE_ANY);
150 }
151
152 char rcsid_log[] = "$Id: log.c,v 1.3 1998/12/14 13:17:59 nop Exp $";
153
154 /*
155 * $Log: log.c,v $
156 * Revision 1.3 1998/12/14 13:17:59 nop
157 * Merge UNSAFE_OPTS (ref fixups); fix Log tag placement to fit CVS whims
158 *
159 * Revision 1.2 1997/03/03 04:18:48 nop
160 * GNU Indent normalization
161 *
162 * Revision 1.1.1.1 1997/03/03 03:45:00 nop
163 * LambdaMOO 1.8.0p5
164 *
165 * Revision 2.2 1996/04/08 01:06:25 pavel
166 * Added `set_log_file()' entry point. Made logging print undated messages to
167 * stderr if they arrive before a log file has been set. Release 1.8.0p3.
168 *
169 * Revision 2.1 1996/02/08 07:00:50 pavel
170 * Renamed err/logf() to errlog/oklog(). Cleaned up bf_server_log() code.
171 * Updated copyright notice for 1996. Release 1.8.0beta1.
172 *
173 * Revision 2.0 1995/11/30 04:26:56 pavel
174 * New baseline version, corresponding to release 1.8.0alpha1.
175 *
176 * Revision 1.10 1992/10/23 23:03:47 pavel
177 * Added copyright notice.
178 *
179 * Revision 1.9 1992/10/21 03:02:35 pavel
180 * Converted to use new automatic configuration system.
181 *
182 * Revision 1.8 1992/09/22 22:48:15 pavel
183 * Added missing #include of "config.h".
184 *
185 * Revision 1.7 1992/09/08 22:04:45 pjames
186 * changed `register_bf_log()' to `register_log()'
187 *
188 * Revision 1.6 1992/08/20 17:27:13 pavel
189 * Added the prefix `> ' to all log messages generated by the server_log()
190 * built-in function, so that they can be distinguished from server-generated
191 * messages.
192 *
193 * Revision 1.5 1992/08/14 00:23:37 pavel
194 * Commented text after #endif.
195 *
196 * Revision 1.4 1992/08/11 17:27:58 pjames
197 * Changed server_log to return 0 instead of 1.
198 *
199 * Revision 1.3 1992/08/10 17:14:00 pjames
200 * Updated #includes. Added bf_server_log to write messages to server
201 * log output. Added registration procedure.
202 *
203 * Revision 1.2 1992/07/21 00:04:15 pavel
204 * Added rcsid_<filename-root> declaration to hold the RCS ident. string.
205 *
206 * Revision 1.1 1992/07/20 23:23:12 pavel
207 * Initial RCS-controlled version.
208 */
209