Debian Codegraph @

terps/tads/tads3/vmlog.cpp

raw
1 #ifdef RCSID
2 static char RCSid[] =
3 "$Header$";
4 #endif
5
6 /* Copyright (c) 2011 by Michael J. Roberts. All Rights Reserved. */
7 /*
8 Name
9 vmlog.cpp - system log file
10 Function
11
12 Notes
13
14 Modified
15 12/01/11 MJRoberts - Creation
16 */
17
18 #include <time.h>
19
20 #include "t3std.h"
21 #include "vmglob.h"
22 #include "vmlog.h"
23 #include "vmfile.h"
24 #include "vmdatasrc.h"
25 #include "charmap.h"
26
27
28 /* ------------------------------------------------------------------------ */
29 /*
30 * System log file
31 */
32
33 /*
34 * Log a message with sprintf-style formatting
35 */
36 void vm_log_fmt(VMG_ const char *fmt, ...)
37 {
38 /* format the message */
39 va_list args;
40 va_start(args, fmt);
41 char *str = t3vsprintf_alloc(fmt, args);
42 va_end(args);
43
44 /* log the message */
45 vm_log(vmg_ str, strlen(str));
46
47 /* done with the string */
48 t3free(str);
49 }
50
51 /*
52 * Log a string with a given length
53 */
54 void vm_log(VMG_ const char *str, size_t len)
55 {
56 /* open the system log file */
57 osfildef *fp = osfoprwt(G_syslogfile, OSFTTEXT);
58 if (fp != 0)
59 {
60 /* wrap it in a data source */
61 CVmFileSource ds(fp);
62
63 /* get a printable timestamp */
64 os_time_t timer = os_time(0);
65 struct tm *tblk = os_localtime(&timer);
66 char *tmsg = asctime(tblk);
67
68 /* remove the trailing '\n' from the asctime message */
69 size_t tmsgl = strlen(tmsg);
70 if (tmsgl > 0 && tmsg[tmsgl-1] == '\n')
71 tmsg[--tmsgl] = '\0';
72
73 /* build the full message: [<timestamp>] <message> <newline> */
74 char *msg = t3sprintf_alloc("[%s] %.*s\n", tmsg, (int)len, str);
75 size_t msglen = strlen(msg);
76
77 /* seek to the end of the file */
78 ds.seek(0, OSFSK_END);
79
80 /* if we can convert to a local character set, do so */
81 if (G_cmap_to_log != 0)
82 {
83 /* write the message in the local character set */
84 G_cmap_to_file->write_file(&ds, msg, msglen);
85 }
86 else
87 {
88 /* write the message with no character set conversion */
89 (void)osfwb(fp, msg, msglen);
90 }
91
92 /* done with the formatted text string */
93 t3free(msg);
94 }
95 }
96
97