Mercurial > hg > batmud > maputils
changeset 1863:60785ece855b
Implement simplistic logging to mapsearch server.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 02 Nov 2017 19:54:13 +0200 |
parents | 53214478fa6e |
children | e5b1e2e75fdc |
files | mapsearch.c |
diffstat | 1 files changed, 120 insertions(+), 31 deletions(-) [+] |
line wrap: on
line diff
--- a/mapsearch.c Thu Nov 02 15:42:28 2017 +0200 +++ b/mapsearch.c Thu Nov 02 19:54:13 2017 +0200 @@ -7,6 +7,7 @@ #include "th_config.h" #include "liblocfile.h" #include "libmaputils.h" +#include <stdarg.h> #include <libwebsockets.h> #include <sys/types.h> #include <pwd.h> @@ -91,6 +92,8 @@ struct lws_context *setLWSContext = NULL; int optWorldXC = 0, optWorldYC = 0; char *optTest = NULL; +char *optLogFilename = NULL; +FILE *setLogFH = NULL; unsigned char *setLWSBuffer = NULL; int optUID = -1, optGID = -1; @@ -108,6 +111,7 @@ { 6, 'T', "test", "Test search with given file input", OPT_ARGREQ }, { 7, 'U', "uid", "Run as UID", OPT_ARGREQ }, { 8, 'G', "gid", "Run as GID", OPT_ARGREQ }, + { 9, 'L', "log-file", "Log to specified file", OPT_ARGREQ }, }; static const int optListN = sizeof(optList) / sizeof(optList[0]); @@ -141,6 +145,82 @@ } +void mapLogTimestamp(void) +{ + if (setLogFH != NULL) + { + char str[64] = ""; + time_t stamp = time(NULL); + struct tm *stamp_tm; + + if ((stamp_tm = localtime(&stamp)) != NULL) + strftime(str, sizeof(str), "%c", stamp_tm); + + fprintf(setLogFH, "[%s] ", str); + } +} + + +void mapMSG_V(const int level, const char *fmt, va_list ap) +{ + if (setLogFH == NULL) + THMSG_V(level, fmt, ap); + else + { + mapLogTimestamp(); + vfprintf(setLogFH, fmt, ap); + fflush(setLogFH); + } +} + + +void mapMSG(const int level, const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + mapMSG_V(level, fmt, ap); + va_end(ap); +} + + +void mapERR_V(const char *fmt, va_list ap) +{ + if (setLogFH == NULL) + THERR_V(fmt, ap); + else + { + mapLogTimestamp(); + vfprintf(setLogFH, fmt, ap); + fflush(setLogFH); + } +} + + +void mapERR(const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + mapERR_V(fmt, ap); + va_end(ap); +} + + +void mapBlockPrintDo(const MapBlock *block) +{ + (void) block; +} + + +void mapLogStr(int level, const char *line) +{ + if (setLogFH != NULL) + mapMSG(level, "%s", line); + else + if (level <= (1 << th_verbosityLevel)) + THMSG(level, "%s", line); +} + + BOOL mapParseCoordPair(const char *str, int *xc, int *yc) { char *piece, *tmp, *fmt = th_strdup(str); @@ -449,6 +529,10 @@ } break; + case 9: + optLogFilename = optArg; + break; + default: THERR("Unknown option '%s'.\n", currArg); return FALSE; @@ -687,12 +771,12 @@ data[offs++] = 0; maxMatches = atoi((char *) data); - THMSG(2, "Requested %d matches.\n", maxMatches); + mapMSG(2, "Requested %d matches.\n", maxMatches); if (maxMatches < 1 || maxMatches > SET_MAX_MATCHES) maxMatches = SET_MAX_MATCHES; - THMSG(2, "Limiting to %d matches.\n", maxMatches); + mapMSG(2, "Limiting to %d matches.\n", maxMatches); // Get active map list nmapList = 0; @@ -755,7 +839,7 @@ goto out; } - THMSG(2, "Block size %d x %d\n", width, height); + mapMSG(2, "Block size %d x %d\n", width, height); if (width * height < 3) { @@ -779,14 +863,12 @@ goto out; } - THPRINT(2, "--------------------------------\n"); if (th_verbosityLevel >= 2) - mapBlockPrint(stdout, pattern); - THPRINT(2, "--------------------------------\n"); + mapBlockPrintDo(pattern); // Entropy check int entropy = mapBlockGetEntropy(pattern, optCleanChars, strlen(optCleanChars)); - THMSG(2, "Block entropy %d\n", entropy); + mapMSG(2, "Block entropy %d\n", entropy); if ((entropy < 2 && width < 10 && height < 10) || (entropy < 3 && width * height < 4)) @@ -799,7 +881,7 @@ centerFound = mapBlockFindCenter(pattern, (unsigned char*) "*@", 2, ¢erX, ¢erY, 20); if (centerFound) - THMSG(2, "Center @ %d, %d\n", centerX, centerY); + mapMSG(2, "Center @ %d, %d\n", centerX, centerY); // Clean the pattern from characters we do not want to match mapBlockClean(pattern, optCleanChars); @@ -867,7 +949,7 @@ } th_strbuf_puts(&buf, &bufSize, &bufLen, "]"); - THMSG(2, "%s\n", buf); + mapMSG(2, "%s\n", buf); mapLWSWrite(wsi, (unsigned char *) buf, bufLen); @@ -884,7 +966,7 @@ switch (reason) { case LWS_CALLBACK_ESTABLISHED: - THMSG(2, "[%p] Client connection established.\n", wsi); + mapMSG(2, "[%p] Client connection established.\n", wsi); break; case LWS_CALLBACK_RECEIVE: @@ -896,7 +978,7 @@ // Check what the request is about? if (len >= 10 && strncmp(data, "MAPSEARCH:", 10) == 0) { - THMSG(1, "[%p] Map search query.\n", wsi); + mapMSG(1, "[%p] Map search query.\n", wsi); // A map search query! if (len <= 10 + 1) @@ -911,7 +993,7 @@ char *buf = NULL; size_t bufLen = 0, bufSize = 0; - THMSG(1, "[%p] Sending map information.\n", wsi); + mapMSG(1, "[%p] Sending map information.\n", wsi); th_strbuf_puts(&buf, &bufSize, &bufLen, "MAPS:["); @@ -985,14 +1067,6 @@ }; - -void mapLogStr(int level, const char *line) -{ - if (level <= (1 << th_verbosityLevel)) - THMSG(1, "%s", line); -} - - #define BUF_SIZE_INITIAL (1024 * 4) #define BUF_SIZE_GROW (512) @@ -1052,7 +1126,7 @@ BOOL mapLoadMaps(void) { - THMSG(1, "Trying to load %d map specs. World origin at [%d, %d].\n", + mapMSG(1, "Trying to load %d map specs. World origin at [%d, %d].\n", optNMaps, optWorldXC, optWorldYC); for (int n = 0; n < optNMaps; n++) @@ -1060,7 +1134,7 @@ MAPInfoCtx *info = &optMaps[n]; FILE *fh; - THMSG(1, "Map ID '%s', data '%s', locations '%s' at [%d, %d]\n", + mapMSG(1, "Map ID '%s', data '%s', locations '%s' at [%d, %d]\n", info->locFile.continent, info->mapFilename, info->locFile.filename, @@ -1152,6 +1226,18 @@ goto exit; } + // Initialize logging + if (optLogFilename != NULL && optTest == NULL) + { + if ((setLogFH = fopen(optLogFilename, "a")) == NULL) + { + int err = th_get_error(); + THERR("Could not open log file '%s' for writing (%d): %s\n", + err, th_error_str(err)); + goto exit; + } + } + // Load maps if (!mapLoadMaps()) goto exit; @@ -1159,7 +1245,7 @@ // Check for test mode if (optTest != NULL) { - THMSG(1, "Running in test mode, input '%s'.\n", optTest); + mapMSG(1, "Running in test mode, input '%s'.\n", optTest); uint8_t *buf = NULL; size_t bufSize; @@ -1178,11 +1264,11 @@ } // Initialize libwebsockets and create context - THMSG(1, "Creating libwebsockets context.\n"); + mapMSG(1, "Creating libwebsockets context.\n"); if ((setLWSBuffer = th_malloc(SET_LWS_BUF_SIZE)) == NULL) { - THERR("Could not allocate %d bytes of memory for LWS buffer.\n", + mapERR("Could not allocate %d bytes of memory for LWS buffer.\n", SET_LWS_BUF_SIZE); goto exit; @@ -1225,7 +1311,7 @@ if (ctx->useSSL) { - THMSG(1, "Listen to %s:%d (wss) [vhost='%s', cert='%s', key='%s', ca='%s']\n", + mapMSG(1, "Listen to %s:%d (wss) [vhost='%s', cert='%s', key='%s', ca='%s']\n", ctx->interface != NULL ? ctx->interface : "*", ctx->port, ctx->vhostname, ctx->sslCertFile, ctx->sslKeyFile, ctx->sslCAFile); @@ -1237,7 +1323,7 @@ } else { - THMSG(1, "Listen to %s:%d (ws)\n", + mapMSG(1, "Listen to %s:%d (ws)\n", ctx->interface != NULL ? ctx->interface : "*", ctx->port); info.vhost_name = NULL; @@ -1260,13 +1346,13 @@ lws_finalize_startup(setLWSContext); // Set up signal handlers - THMSG(1, "Setting up signal handlers.\n"); + mapMSG(1, "Setting up signal handlers.\n"); lws_uv_sigint_cfg(setLWSContext, 1, mapSigHandler); - THMSG(1, "Waiting for connections...\n"); + mapMSG(1, "Waiting for connections...\n"); if (lws_uv_initloop(setLWSContext, NULL, 0)) { - THERR("lws_uv_initloop() failed.\n"); + mapERR("lws_uv_initloop() failed.\n"); goto exit; } @@ -1275,7 +1361,7 @@ exit: // Shutdown sequence - THMSG(1, "Server shutting down.\n"); + mapMSG(1, "Server shutting down.\n"); if (setLWSContext != NULL) lws_context_destroy(setLWSContext); @@ -1296,5 +1382,8 @@ th_free(setLWSBuffer); + if (setLogFH) + fclose(setLogFH); + return 0; }