Annotation of freem/src/log.c, revision 1.11

1.1       snw         1: /*
1.11    ! snw         2:  *   $Id: log.c,v 1.10 2025/04/15 14:39:06 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.11    ! snw        27:  *   Revision 1.10  2025/04/15 14:39:06  snw
        !            28:  *   Further improvements to logging
        !            29:  *
1.10      snw        30:  *   Revision 1.9  2025/04/15 02:24:43  snw
                     31:  *   Improve FreeM logging capabilities
                     32:  *
1.9       snw        33:  *   Revision 1.8  2025/04/04 02:12:25  snw
                     34:  *   Bump to 0.63.0-rc5 and make sure m_log function is never empty
                     35:  *
1.8       snw        36:  *   Revision 1.7  2025/04/04 01:18:21  snw
                     37:  *   Remove vestigial logging code and bump to 0.63.0-rc4
                     38:  *
1.7       snw        39:  *   Revision 1.6  2025/04/01 23:21:45  snw
                     40:  *   fmadm commands for stopping, starting, and restarting environments now functional
                     41:  *
1.6       snw        42:  *   Revision 1.5  2025/04/01 20:11:46  snw
                     43:  *   Further work on fmadm
                     44:  *
1.5       snw        45:  *   Revision 1.4  2025/03/09 19:50:47  snw
                     46:  *   Second phase of REUSE compliance and header reformat
                     47:  *
1.4       snw        48:  *
                     49:  * SPDX-FileCopyrightText:  (C) 2025 Coherent Logic Development LLC
                     50:  * SPDX-License-Identifier: AGPL-3.0-or-later
1.1       snw        51:  **/
                     52: 
                     53: #include <stdio.h>
                     54: #include <string.h>
1.2       snw        55: #if defined(__linux__) | defined(__FreeBSD__) | defined(__sun__)
1.1       snw        56: # include <syslog.h>
                     57: #endif
                     58: #include <stdlib.h>
1.9       snw        59: #include <stdarg.h>
                     60: 
                     61: #if defined(USE_SYS_TIME_H) && !defined(MSDOS) && !defined(__osf__)
                     62: # include <sys/time.h>
                     63: #else
                     64: # include <time.h> 
                     65: #endif
1.1       snw        66: 
                     67: #include "mpsdef.h"
1.11    ! snw        68: #include "log.h" /* for logging constants */
        !            69: #include "init.h" /* for cleanup() */
1.1       snw        70: 
1.10      snw        71: short log_threshold_file;
                     72: short log_threshold_syslog;
                     73: short log_threshold_stderr;
                     74: 
                     75: 
                     76: void init_log(void)
                     77: {
                     78:     log_threshold_file = LOG_INFO;
                     79:     log_threshold_syslog = LOG_INFO;
                     80:     log_threshold_stderr = LOG_WARNING;
                     81: }
                     82: 
                     83: void log_level_to_text(int level, char *buf)
                     84: {
                     85:     switch (level) {
                     86: 
                     87:         case LOG_DEBUG:
                     88:             sprintf (buf, "DEBUG");
                     89:             break;
                     90: 
                     91:         case LOG_INFO:
                     92:             sprintf (buf, "INFO");
                     93:             break;
                     94: 
                     95:         case LOG_WARNING:
                     96:             sprintf (buf, "WARNING");
                     97:             break;
                     98: 
                     99:         case LOG_ERROR:
                    100:             sprintf (buf, "ERROR");
                    101:             break;
                    102: 
                    103:         case LOG_FATAL:
                    104:             sprintf (buf, "FATAL");
                    105:             break;
                    106: 
                    107:         default:
                    108:             sprintf (buf, "LEVEL %d", level);
                    109:             break;
                    110: 
                    111:     }
                    112: }
1.9       snw       113: 
1.10      snw       114: void m_log(int level, const char *msg)
1.1       snw       115: {
                    116: 
1.9       snw       117:     FILE *fp;
                    118:     time_t unix_epoch;
                    119:     char timeval[255];
                    120:     char filename[4096];    
1.10      snw       121:     char lvl[20];
1.9       snw       122:     struct tm *logtime;
                    123: 
1.10      snw       124:     log_level_to_text (level, lvl);
1.9       snw       125:     
1.10      snw       126:     if (level >= log_threshold_file) {
                    127:         snprintf (filename, sizeof (filename) - 1, "/var/log/freem/%s.log", shm_env);
                    128:         
                    129:         if ((fp = fopen (filename, "a+")) != NULL) {
                    130:             unix_epoch = time (0L);
                    131:             logtime = localtime (&unix_epoch);
                    132:             strftime (timeval, sizeof (timeval) - 1, "%F %T", logtime);
                    133:             fprintf (fp, "%s [LEVEL %s PID %ld]:  %s\n", timeval, lvl, pid, msg);
                    134:             fclose (fp);
                    135:         }
1.9       snw       136:     }
1.5       snw       137:     
1.2       snw       138: #if defined(__linux__) | defined(__FreeBSD__) | defined(__sun__)
1.10      snw       139:     if (level >= log_threshold_syslog) {
                    140:         syslog (level, "%s", msg);
                    141:     }
1.1       snw       142: #endif
1.10      snw       143: 
                    144:     if (level >= log_threshold_stderr) fprintf (stderr, "%s\r\n", msg);
                    145: 
                    146:     if (level >= LOG_FATAL) {
                    147:         cleanup ();
                    148:         exit (LOG_FATAL);
                    149:     }
1.9       snw       150:     
1.10      snw       151:     return;    
1.9       snw       152: }
                    153: 
                    154: void logprintf(int level, char *fmt, ...)
                    155: {
                    156:     va_list ptr;
                    157:     va_start (ptr, fmt);
                    158: 
                    159:     char logmsg[BIGSTR];
                    160:     char tmps[BIGSTR];
                    161:     
                    162:     char ch;
                    163:     char typ;
                    164:     char subtyp;
                    165: 
                    166:     register int i;
                    167: 
                    168:     for (i = 0; fmt[i] != '\0'; i++) {
                    169:         ch = fmt[i];
                    170: 
                    171:         switch (ch) {
                    172:             
                    173:             case '%':
                    174:                 typ = fmt[++i];
                    175: 
                    176:                 switch (typ) {
                    177:                     
                    178:                     case '%': /* literal percent sign */
                    179:                         strcat (logmsg, "%");
                    180:                         break;
                    181: 
                    182:                     case 'c': /* char */
                    183:                         sprintf (tmps, "%c", va_arg (ptr, int));
                    184:                         strcat (logmsg, tmps);
                    185:                         break;
                    186: 
                    187:                     case 's': /* C string */
                    188:                         strcat (logmsg, va_arg (ptr, char *));
                    189:                         break;
                    190: 
                    191:                     case 'S': /* FreeM string */
                    192:                         stcpy (tmps, va_arg (ptr, char *));
                    193:                         stcnv_m2c (tmps);
                    194:                         strcat (logmsg, tmps);
                    195:                         break;
                    196: 
                    197:                     case 'd': /* int */
                    198:                         sprintf (tmps, "%d", va_arg (ptr, int));
                    199:                         strcat (logmsg, tmps);
                    200:                         break;
                    201: 
                    202:                     case 'l': /* long... */
                    203:                         subtyp = fmt[++i];
                    204:                         switch (subtyp) {
                    205:                             
                    206:                             case 'd': /* long int */
                    207:                                 sprintf (tmps, "%ld", va_arg (ptr, long));
                    208:                                 strcat (logmsg, tmps);
                    209:                                 break;
                    210: 
                    211:                             case 'f': /* float */
                    212:                                 sprintf (tmps, "%lf", va_arg (ptr, double));
                    213:                                 strcat (logmsg, tmps);
                    214:                                 break;
                    215:                                 
                    216:                         }
                    217:                         break;
                    218:                 }
                    219:                 
                    220:             case '\\':
                    221:                 typ = fmt[++i];
                    222:                 switch (typ) {
                    223:                     case 'n':
                    224:                         sprintf (tmps, "\n");
                    225:                         strcat (logmsg, tmps);
                    226:                         break;
                    227:                         
                    228:                     case 'r':
                    229:                         sprintf (tmps, "\r");
                    230:                         strcat (logmsg, tmps);
                    231:                         break;
                    232: 
                    233:                     case 't':
                    234:                         sprintf (tmps, "\t");
                    235:                         strcat (logmsg, tmps);
                    236:                         break;
                    237:                         
                    238:                     case '\\':
                    239:                         sprintf (tmps, "\\");
                    240:                         strcat (logmsg, tmps);
                    241:                         break;
                    242:                 }
                    243:                 
                    244:             default:
                    245:                 sprintf (tmps, "%c", ch);
                    246:                 strcat (logmsg, tmps);
                    247:                 break;
                    248:         }
                    249:     }
                    250: 
                    251:     m_log (level, logmsg);
                    252:         
                    253:     va_end (ptr);
1.8       snw       254:     return;
1.1       snw       255: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>