Annotation of freem/src/log.c, revision 1.10
1.1 snw 1: /*
1.10 ! snw 2: * $Id: log.c,v 1.9 2025/04/15 02:24:43 snw Exp $
1.4 snw 3: * freem error logging
1.1 snw 4: *
5: *
1.3 snw 6: * Author: Serena Willis <snw@coherent-logic.com>
1.1 snw 7: * Copyright (C) 1998 MUG Deutschland
1.4 snw 8: * Copyright (C) 2020, 2025 Coherent Logic Development LLC
1.1 snw 9: *
10: *
11: * This file is part of FreeM.
12: *
13: * FreeM is free software: you can redistribute it and/or modify
14: * it under the terms of the GNU Affero Public License as published by
15: * the Free Software Foundation, either version 3 of the License, or
16: * (at your option) any later version.
17: *
18: * FreeM is distributed in the hope that it will be useful,
19: * but WITHOUT ANY WARRANTY; without even the implied warranty of
20: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21: * GNU Affero Public License for more details.
22: *
23: * You should have received a copy of the GNU Affero Public License
24: * along with FreeM. If not, see <https://www.gnu.org/licenses/>.
25: *
1.5 snw 26: * $Log: log.c,v $
1.10 ! snw 27: * Revision 1.9 2025/04/15 02:24:43 snw
! 28: * Improve FreeM logging capabilities
! 29: *
1.9 snw 30: * Revision 1.8 2025/04/04 02:12:25 snw
31: * Bump to 0.63.0-rc5 and make sure m_log function is never empty
32: *
1.8 snw 33: * Revision 1.7 2025/04/04 01:18:21 snw
34: * Remove vestigial logging code and bump to 0.63.0-rc4
35: *
1.7 snw 36: * Revision 1.6 2025/04/01 23:21:45 snw
37: * fmadm commands for stopping, starting, and restarting environments now functional
38: *
1.6 snw 39: * Revision 1.5 2025/04/01 20:11:46 snw
40: * Further work on fmadm
41: *
1.5 snw 42: * Revision 1.4 2025/03/09 19:50:47 snw
43: * Second phase of REUSE compliance and header reformat
44: *
1.4 snw 45: *
46: * SPDX-FileCopyrightText: (C) 2025 Coherent Logic Development LLC
47: * SPDX-License-Identifier: AGPL-3.0-or-later
1.1 snw 48: **/
49:
50: #include <stdio.h>
51: #include <string.h>
1.2 snw 52: #if defined(__linux__) | defined(__FreeBSD__) | defined(__sun__)
1.1 snw 53: # include <syslog.h>
54: #endif
55: #include <stdlib.h>
1.9 snw 56: #include <stdarg.h>
57:
58: #if defined(USE_SYS_TIME_H) && !defined(MSDOS) && !defined(__osf__)
59: # include <sys/time.h>
60: #else
61: # include <time.h>
62: #endif
1.1 snw 63:
64: #include "mpsdef.h"
65:
1.10 ! snw 66: short log_threshold_file;
! 67: short log_threshold_syslog;
! 68: short log_threshold_stderr;
! 69:
! 70:
! 71: void init_log(void)
! 72: {
! 73: log_threshold_file = LOG_INFO;
! 74: log_threshold_syslog = LOG_INFO;
! 75: log_threshold_stderr = LOG_WARNING;
! 76: }
! 77:
! 78: void log_level_to_text(int level, char *buf)
! 79: {
! 80: switch (level) {
! 81:
! 82: case LOG_DEBUG:
! 83: sprintf (buf, "DEBUG");
! 84: break;
! 85:
! 86: case LOG_INFO:
! 87: sprintf (buf, "INFO");
! 88: break;
! 89:
! 90: case LOG_WARNING:
! 91: sprintf (buf, "WARNING");
! 92: break;
! 93:
! 94: case LOG_ERROR:
! 95: sprintf (buf, "ERROR");
! 96: break;
! 97:
! 98: case LOG_FATAL:
! 99: sprintf (buf, "FATAL");
! 100: break;
! 101:
! 102: default:
! 103: sprintf (buf, "LEVEL %d", level);
! 104: break;
! 105:
! 106: }
! 107: }
1.9 snw 108:
1.10 ! snw 109: void m_log(int level, const char *msg)
1.1 snw 110: {
111:
1.9 snw 112: FILE *fp;
113: time_t unix_epoch;
114: char timeval[255];
115: char filename[4096];
1.10 ! snw 116: char lvl[20];
1.9 snw 117: struct tm *logtime;
118:
1.10 ! snw 119: log_level_to_text (level, lvl);
1.9 snw 120:
1.10 ! snw 121: if (level >= log_threshold_file) {
! 122: snprintf (filename, sizeof (filename) - 1, "/var/log/freem/%s.log", shm_env);
! 123:
! 124: if ((fp = fopen (filename, "a+")) != NULL) {
! 125: unix_epoch = time (0L);
! 126: logtime = localtime (&unix_epoch);
! 127: strftime (timeval, sizeof (timeval) - 1, "%F %T", logtime);
! 128: fprintf (fp, "%s [LEVEL %s PID %ld]: %s\n", timeval, lvl, pid, msg);
! 129: fclose (fp);
! 130: }
1.9 snw 131: }
1.5 snw 132:
1.2 snw 133: #if defined(__linux__) | defined(__FreeBSD__) | defined(__sun__)
1.10 ! snw 134: if (level >= log_threshold_syslog) {
! 135: syslog (level, "%s", msg);
! 136: }
1.1 snw 137: #endif
1.10 ! snw 138:
! 139: if (level >= log_threshold_stderr) fprintf (stderr, "%s\r\n", msg);
! 140:
! 141: if (level >= LOG_FATAL) {
! 142: cleanup ();
! 143: exit (LOG_FATAL);
! 144: }
1.9 snw 145:
1.10 ! snw 146: return;
1.9 snw 147: }
148:
149: void logprintf(int level, char *fmt, ...)
150: {
151: va_list ptr;
152: va_start (ptr, fmt);
153:
154: char logmsg[BIGSTR];
155: char tmps[BIGSTR];
156:
157: char ch;
158: char typ;
159: char subtyp;
160:
161: register int i;
162:
163: for (i = 0; fmt[i] != '\0'; i++) {
164: ch = fmt[i];
165:
166: switch (ch) {
167:
168: case '%':
169: typ = fmt[++i];
170:
171: switch (typ) {
172:
173: case '%': /* literal percent sign */
174: strcat (logmsg, "%");
175: break;
176:
177: case 'c': /* char */
178: sprintf (tmps, "%c", va_arg (ptr, int));
179: strcat (logmsg, tmps);
180: break;
181:
182: case 's': /* C string */
183: strcat (logmsg, va_arg (ptr, char *));
184: break;
185:
186: case 'S': /* FreeM string */
187: stcpy (tmps, va_arg (ptr, char *));
188: stcnv_m2c (tmps);
189: strcat (logmsg, tmps);
190: break;
191:
192: case 'd': /* int */
193: sprintf (tmps, "%d", va_arg (ptr, int));
194: strcat (logmsg, tmps);
195: break;
196:
197: case 'l': /* long... */
198: subtyp = fmt[++i];
199: switch (subtyp) {
200:
201: case 'd': /* long int */
202: sprintf (tmps, "%ld", va_arg (ptr, long));
203: strcat (logmsg, tmps);
204: break;
205:
206: case 'f': /* float */
207: sprintf (tmps, "%lf", va_arg (ptr, double));
208: strcat (logmsg, tmps);
209: break;
210:
211: }
212: break;
213: }
214:
215: case '\\':
216: typ = fmt[++i];
217: switch (typ) {
218: case 'n':
219: sprintf (tmps, "\n");
220: strcat (logmsg, tmps);
221: break;
222:
223: case 'r':
224: sprintf (tmps, "\r");
225: strcat (logmsg, tmps);
226: break;
227:
228: case 't':
229: sprintf (tmps, "\t");
230: strcat (logmsg, tmps);
231: break;
232:
233: case '\\':
234: sprintf (tmps, "\\");
235: strcat (logmsg, tmps);
236: break;
237: }
238:
239: default:
240: sprintf (tmps, "%c", ch);
241: strcat (logmsg, tmps);
242: break;
243: }
244: }
245:
246: m_log (level, logmsg);
247:
248: va_end (ptr);
1.8 snw 249: return;
1.1 snw 250: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>