version 1.10, 2025/04/15 14:39:06
|
version 1.14, 2025/05/14 12:22:04
|
Line 24
|
Line 24
|
* along with FreeM. If not, see <https://www.gnu.org/licenses/>. |
* along with FreeM. If not, see <https://www.gnu.org/licenses/>. |
* |
* |
* $Log$ |
* $Log$ |
|
* Revision 1.14 2025/05/14 12:22:04 snw |
|
* Further work on shared memory |
|
* |
|
* Revision 1.13 2025/04/16 17:36:12 snw |
|
* Add FreeBSD shm cleanup script |
|
* |
|
* Revision 1.12 2025/04/15 16:49:36 snw |
|
* Make use of logprintf throughout codebase |
|
* |
|
* Revision 1.11 2025/04/15 14:56:20 snw |
|
* Fix broken build due to missing declarations |
|
* |
* Revision 1.10 2025/04/15 14:39:06 snw |
* Revision 1.10 2025/04/15 14:39:06 snw |
* Further improvements to logging |
* Further improvements to logging |
* |
* |
Line 57
|
Line 69
|
#endif |
#endif |
#include <stdlib.h> |
#include <stdlib.h> |
#include <stdarg.h> |
#include <stdarg.h> |
|
#include <unistd.h> |
|
|
#if defined(USE_SYS_TIME_H) && !defined(MSDOS) && !defined(__osf__) |
#if defined(USE_SYS_TIME_H) && !defined(MSDOS) && !defined(__osf__) |
# include <sys/time.h> |
# include <sys/time.h> |
Line 65
|
Line 78
|
#endif |
#endif |
|
|
#include "mpsdef.h" |
#include "mpsdef.h" |
|
#include "log.h" /* for logging constants */ |
|
#include "init.h" /* for cleanup() */ |
|
#include "iniconf.h" /* for read_profile_string() */ |
|
#include "shmmgr.h" /* for shm_exit() */ |
|
|
short log_threshold_file; |
short log_threshold_file; |
short log_threshold_syslog; |
short log_threshold_syslog; |
Line 73 short log_threshold_stderr;
|
Line 90 short log_threshold_stderr;
|
|
|
void init_log(void) |
void init_log(void) |
{ |
{ |
log_threshold_file = LOG_INFO; |
char buf[4096]; |
log_threshold_syslog = LOG_INFO; |
char *env_value; |
log_threshold_stderr = LOG_WARNING; |
|
|
if (read_profile_string (env_config_file, shm_env, "log_threshold_file", buf) == TRUE) { |
|
if (strcmp (buf, "debug") == 0) { |
|
log_threshold_file = FM_LOG_DEBUG; |
|
} |
|
else if (strcmp (buf, "info") == 0) { |
|
log_threshold_file = FM_LOG_INFO; |
|
} |
|
else if (strcmp (buf, "warning") == 0) { |
|
log_threshold_file = FM_LOG_WARNING; |
|
} |
|
else if (strcmp (buf, "error") == 0) { |
|
log_threshold_file = FM_LOG_ERROR; |
|
} |
|
else if (strcmp (buf, "fatal") == 0) { |
|
log_threshold_file = FM_LOG_FATAL; |
|
} |
|
else { |
|
log_threshold_file = FM_LOG_INFO; |
|
} |
|
} |
|
else { |
|
log_threshold_file = FM_LOG_INFO; |
|
} |
|
|
|
if (read_profile_string (env_config_file, shm_env, "log_threshold_syslog", buf) == TRUE) { |
|
if (strcmp (buf, "debug") == 0) { |
|
log_threshold_syslog = FM_LOG_DEBUG; |
|
} |
|
else if (strcmp (buf, "info") == 0) { |
|
log_threshold_syslog = FM_LOG_INFO; |
|
} |
|
else if (strcmp (buf, "warning") == 0) { |
|
log_threshold_syslog = FM_LOG_WARNING; |
|
} |
|
else if (strcmp (buf, "error") == 0) { |
|
log_threshold_syslog = FM_LOG_ERROR; |
|
} |
|
else if (strcmp (buf, "fatal") == 0) { |
|
log_threshold_syslog = FM_LOG_FATAL; |
|
} |
|
else { |
|
log_threshold_syslog = FM_LOG_INFO; |
|
} |
|
} |
|
else { |
|
log_threshold_syslog = FM_LOG_INFO; |
|
} |
|
|
|
if (read_profile_string (env_config_file, shm_env, "log_threshold_stderr", buf) == TRUE) { |
|
if (strcmp (buf, "debug") == 0) { |
|
log_threshold_stderr = FM_LOG_DEBUG; |
|
} |
|
else if (strcmp (buf, "info") == 0) { |
|
log_threshold_stderr = FM_LOG_INFO; |
|
} |
|
else if (strcmp (buf, "warning") == 0) { |
|
log_threshold_stderr = FM_LOG_WARNING; |
|
} |
|
else if (strcmp (buf, "error") == 0) { |
|
log_threshold_stderr = FM_LOG_ERROR; |
|
} |
|
else if (strcmp (buf, "fatal") == 0) { |
|
log_threshold_stderr = FM_LOG_FATAL; |
|
} |
|
else { |
|
log_threshold_stderr = FM_LOG_INFO; |
|
} |
|
} |
|
else { |
|
log_threshold_stderr = FM_LOG_WARNING; |
|
} |
|
|
|
if ((env_value = getenv ("FREEM_LOG_THRESHOLD_FILE")) != NULL) { |
|
if (strcmp (env_value, "debug") == 0) { |
|
log_threshold_file = FM_LOG_DEBUG; |
|
} |
|
else if (strcmp (env_value, "info") == 0) { |
|
log_threshold_file = FM_LOG_INFO; |
|
} |
|
else if (strcmp (env_value, "warning") == 0) { |
|
log_threshold_file = FM_LOG_WARNING; |
|
} |
|
else if (strcmp (env_value, "error") == 0) { |
|
log_threshold_file = FM_LOG_ERROR; |
|
} |
|
else if (strcmp (env_value, "fatal") == 0) { |
|
log_threshold_file = FM_LOG_FATAL; |
|
} |
|
else { |
|
log_threshold_file = FM_LOG_INFO; |
|
} |
|
} |
|
|
|
if ((env_value = getenv ("FREEM_LOG_THRESHOLD_SYSLOG")) != NULL) { |
|
if (strcmp (env_value, "debug") == 0) { |
|
log_threshold_syslog = FM_LOG_DEBUG; |
|
} |
|
else if (strcmp (env_value, "info") == 0) { |
|
log_threshold_syslog = FM_LOG_INFO; |
|
} |
|
else if (strcmp (env_value, "warning") == 0) { |
|
log_threshold_syslog = FM_LOG_WARNING; |
|
} |
|
else if (strcmp (env_value, "error") == 0) { |
|
log_threshold_syslog = FM_LOG_ERROR; |
|
} |
|
else if (strcmp (env_value, "fatal") == 0) { |
|
log_threshold_syslog = FM_LOG_FATAL; |
|
} |
|
else { |
|
log_threshold_syslog = FM_LOG_INFO; |
|
} |
|
} |
|
|
|
if ((env_value = getenv ("FREEM_LOG_THRESHOLD_STDERR")) != NULL) { |
|
if (strcmp (env_value, "debug") == 0) { |
|
log_threshold_stderr = FM_LOG_DEBUG; |
|
} |
|
else if (strcmp (env_value, "info") == 0) { |
|
log_threshold_stderr = FM_LOG_INFO; |
|
} |
|
else if (strcmp (env_value, "warning") == 0) { |
|
log_threshold_stderr = FM_LOG_WARNING; |
|
} |
|
else if (strcmp (env_value, "error") == 0) { |
|
log_threshold_stderr = FM_LOG_ERROR; |
|
} |
|
else if (strcmp (env_value, "fatal") == 0) { |
|
log_threshold_stderr = FM_LOG_FATAL; |
|
} |
|
else { |
|
log_threshold_stderr = FM_LOG_INFO; |
|
} |
|
} |
|
|
|
|
|
|
} |
} |
|
|
void log_level_to_text(int level, char *buf) |
void log_level_to_text(int level, char *buf) |
{ |
{ |
switch (level) { |
switch (level) { |
|
|
case LOG_DEBUG: |
case FM_LOG_DEBUG: |
sprintf (buf, "DEBUG"); |
sprintf (buf, "DEBUG"); |
break; |
break; |
|
|
case LOG_INFO: |
case FM_LOG_INFO: |
sprintf (buf, "INFO"); |
sprintf (buf, "INFO"); |
break; |
break; |
|
|
case LOG_WARNING: |
case FM_LOG_WARNING: |
sprintf (buf, "WARNING"); |
sprintf (buf, "WARNING"); |
break; |
break; |
|
|
case LOG_ERROR: |
case FM_LOG_ERROR: |
sprintf (buf, "ERROR"); |
sprintf (buf, "ERROR"); |
break; |
break; |
|
|
case LOG_FATAL: |
case FM_LOG_FATAL: |
sprintf (buf, "FATAL"); |
sprintf (buf, "FATAL"); |
break; |
break; |
|
|
Line 128 void m_log(int level, const char *msg)
|
Line 282 void m_log(int level, const char *msg)
|
unix_epoch = time (0L); |
unix_epoch = time (0L); |
logtime = localtime (&unix_epoch); |
logtime = localtime (&unix_epoch); |
strftime (timeval, sizeof (timeval) - 1, "%F %T", logtime); |
strftime (timeval, sizeof (timeval) - 1, "%F %T", logtime); |
fprintf (fp, "%s [LEVEL %s PID %ld]: %s\n", timeval, lvl, pid, msg); |
fprintf (fp, "%s [LEVEL %s PID %ld]: %s\n", timeval, lvl, getpid (), msg); |
fclose (fp); |
fclose (fp); |
} |
} |
} |
} |
|
|
#if defined(__linux__) | defined(__FreeBSD__) | defined(__sun__) |
#if defined(__linux__) | defined(__FreeBSD__) | defined(__sun__) |
Line 139 void m_log(int level, const char *msg)
|
Line 293 void m_log(int level, const char *msg)
|
} |
} |
#endif |
#endif |
|
|
if (level >= log_threshold_stderr) fprintf (stderr, "%s\r\n", msg); |
if (level >= log_threshold_stderr) fprintf (stderr, "%s [%s]\r\n", msg, lvl); |
|
|
if (level >= LOG_FATAL) { |
if (level >= FM_LOG_FATAL) { |
cleanup (); |
cleanup (); |
exit (LOG_FATAL); |
exit (FM_LOG_FATAL); |
} |
} |
|
|
return; |
return; |
Line 163 void logprintf(int level, char *fmt, ...
|
Line 317 void logprintf(int level, char *fmt, ...
|
|
|
register int i; |
register int i; |
|
|
|
logmsg[0] = '\0'; |
|
|
for (i = 0; fmt[i] != '\0'; i++) { |
for (i = 0; fmt[i] != '\0'; i++) { |
ch = fmt[i]; |
ch = fmt[i]; |
|
|
|
tmps[0] = '\0'; |
|
|
switch (ch) { |
switch (ch) { |
|
|
case '%': |
case '%': |
Line 175 void logprintf(int level, char *fmt, ...
|
Line 333 void logprintf(int level, char *fmt, ...
|
|
|
case '%': /* literal percent sign */ |
case '%': /* literal percent sign */ |
strcat (logmsg, "%"); |
strcat (logmsg, "%"); |
break; |
continue; |
|
|
case 'c': /* char */ |
case 'c': /* char */ |
sprintf (tmps, "%c", va_arg (ptr, int)); |
sprintf (tmps, "%c", va_arg (ptr, int)); |
strcat (logmsg, tmps); |
strcat (logmsg, tmps); |
break; |
continue; |
|
|
case 's': /* C string */ |
case 's': /* C string */ |
strcat (logmsg, va_arg (ptr, char *)); |
strcat (logmsg, va_arg (ptr, char *)); |
break; |
continue; |
|
|
case 'S': /* FreeM string */ |
case 'S': /* FreeM string */ |
stcpy (tmps, va_arg (ptr, char *)); |
stcpy (tmps, va_arg (ptr, char *)); |
stcnv_m2c (tmps); |
stcnv_m2c (tmps); |
strcat (logmsg, tmps); |
strcat (logmsg, tmps); |
break; |
continue; |
|
|
case 'd': /* int */ |
case 'd': /* int */ |
sprintf (tmps, "%d", va_arg (ptr, int)); |
sprintf (tmps, "%d", va_arg (ptr, int)); |
strcat (logmsg, tmps); |
strcat (logmsg, tmps); |
break; |
continue; |
|
|
|
case 'p': /* pointer */ |
|
sprintf (tmps, "%p", va_arg (ptr, void *)); |
|
strcat (logmsg, tmps); |
|
continue; |
|
|
case 'l': /* long... */ |
case 'l': /* long... */ |
subtyp = fmt[++i]; |
subtyp = fmt[++i]; |
switch (subtyp) { |
switch (subtyp) { |
Line 204 void logprintf(int level, char *fmt, ...
|
Line 367 void logprintf(int level, char *fmt, ...
|
case 'd': /* long int */ |
case 'd': /* long int */ |
sprintf (tmps, "%ld", va_arg (ptr, long)); |
sprintf (tmps, "%ld", va_arg (ptr, long)); |
strcat (logmsg, tmps); |
strcat (logmsg, tmps); |
break; |
continue; |
|
|
case 'f': /* float */ |
case 'f': /* float */ |
sprintf (tmps, "%lf", va_arg (ptr, double)); |
sprintf (tmps, "%lf", va_arg (ptr, double)); |
strcat (logmsg, tmps); |
strcat (logmsg, tmps); |
break; |
continue; |
|
|
} |
} |
break; |
continue; |
} |
} |
|
|
case '\\': |
case '\\': |
Line 221 void logprintf(int level, char *fmt, ...
|
Line 384 void logprintf(int level, char *fmt, ...
|
case 'n': |
case 'n': |
sprintf (tmps, "\n"); |
sprintf (tmps, "\n"); |
strcat (logmsg, tmps); |
strcat (logmsg, tmps); |
break; |
continue; |
|
|
case 'r': |
case 'r': |
sprintf (tmps, "\r"); |
sprintf (tmps, "\r"); |
strcat (logmsg, tmps); |
strcat (logmsg, tmps); |
break; |
continue; |
|
|
case 't': |
case 't': |
sprintf (tmps, "\t"); |
sprintf (tmps, "\t"); |
strcat (logmsg, tmps); |
strcat (logmsg, tmps); |
break; |
continue; |
|
|
case '\\': |
case '\\': |
sprintf (tmps, "\\"); |
sprintf (tmps, "\\"); |
strcat (logmsg, tmps); |
strcat (logmsg, tmps); |
break; |
continue; |
} |
} |
|
|
default: |
default: |