1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
|
#include "swserv.h"
#ifndef LOG_MAX_TIME_STR
# define LOG_MAX_TIME_STR 256
#endif
char *LogGetCurrentTimeString(void);
/*
* Returns a formatted time string of the current time specified
* in global cur_systime.
*/
char *LogGetCurrentTimeString(void)
{
size_t len;
time_t ct;
struct tm *tm_ptr;
static char s[LOG_MAX_TIME_STR];
/* Get current systime from global cur_systime. */
ct = cur_systime;
/* Get current time. */
tm_ptr = localtime(&ct);
if(tm_ptr == NULL)
return("");
/* Format time string. */
len = strftime(
s,
LOG_MAX_TIME_STR,
"%c",
tm_ptr
);
if(len >= LOG_MAX_TIME_STR)
len = LOG_MAX_TIME_STR - 1;
if(len < 0)
len = 0;
/* Null terminate. */
s[len] = '\0';
return(s);
}
/*
* Logs message mesg to the log file filename. The message
* may not contain any new line characters.
*/
int LogAppendLineFormatted(char *filename, char *mesg)
{
int i, len;
FILE *fp;
char *time_str_ptr, *buf;
aux_connection_struct **ac_ptr;
/* Make sure input message is valid and contains data. */
if((mesg == NULL) ? 1 : (*mesg == '\0'))
return(0);
/* Open or create file for appending. */
fp = fopen(filename, "a");
if(fp == NULL)
return(-1);
/* Allocate buffer (for sending to AUX stat connections). */
len = strlen(mesg) + 128;
buf = (char *)malloc(len * sizeof(char));
if(buf == NULL)
{
fclose(fp);
return(-1);
}
/* Get time string (return will never be NULL). */
time_str_ptr = LogGetCurrentTimeString();
/* Print log line to file. */
fprintf(fp, "%s: %s\n", time_str_ptr, mesg);
/* Close file. */
fclose(fp);
/* Write to all AUX connections. */
sprintf(
buf,
"%s: %s: %s\n",
STAT_PFX_MESSAGE,
time_str_ptr,
mesg
);
for(i = 0, ac_ptr = aux_connection;
i < total_aux_connections;
i++, ac_ptr++
)
{
if(*ac_ptr == NULL)
continue;
if(!(*ac_ptr)->logged_in)
continue;
if((*ac_ptr)->socket < 0)
continue;
AUXConSend(*ac_ptr, buf);
}
/* Free buffer. */
free(buf);
return(0);
}
|