summaryrefslogtreecommitdiff
path: root/src/map/map.c
diff options
context:
space:
mode:
authorFate <fate-tmw@googlemail.com>2009-09-14 21:12:29 +0000
committerFate <fate-tmw@googlemail.com>2009-09-14 21:12:29 +0000
commite02e6e25938e55b9b8c02bbc4e373ce0ec1a54d9 (patch)
treeabe91ed29c11ca6f72296d63f85f8dc1806e2bd3 /src/map/map.c
parentaf28784c5cba267c49bbda8a7bc796249865be2d (diff)
downloadtmwa-e02e6e25938e55b9b8c02bbc4e373ce0ec1a54d9.tar.gz
tmwa-e02e6e25938e55b9b8c02bbc4e373ce0ec1a54d9.tar.bz2
tmwa-e02e6e25938e55b9b8c02bbc4e373ce0ec1a54d9.tar.xz
tmwa-e02e6e25938e55b9b8c02bbc4e373ce0ec1a54d9.zip
Replace logging mechanism: write out text file every ~20 minutes and try to gzip. If gzip fails, the old file still survives.
Diffstat (limited to 'src/map/map.c')
-rw-r--r--src/map/map.c70
1 files changed, 60 insertions, 10 deletions
diff --git a/src/map/map.c b/src/map/map.c
index b2b9984..b4cb0cb 100644
--- a/src/map/map.c
+++ b/src/map/map.c
@@ -1656,28 +1656,78 @@ int map_delmap(char *mapname) {
extern char *gm_logfile_name;
+
+#define LOGFILE_SECONDS_PER_CHUNK_SHIFT 10
+
FILE *map_logfile = NULL;
+char *map_logfile_name = NULL;
+static long map_logfile_index;
static void
-map_pclose_map_logfile()
+map_close_logfile()
{
- pclose(map_logfile);
+ if (map_logfile) {
+ char *filenameop_buf = malloc(strlen(map_logfile_name) + 50);
+ sprintf(filenameop_buf, "gzip %s.%ld", map_logfile_name, map_logfile_index);
+
+ fclose(map_logfile);
+
+ if (!system(filenameop_buf))
+ perror(filenameop_buf);
+
+ free(filenameop_buf);
+ }
}
+
static void
-map_setlogfile(const char *filename)
+map_start_logfile(long suffix)
{
- char *filename_buf = malloc(strlen (filename) + 50);
- sprintf(filename_buf, "gzip --rsyncable -c > %s", filename);
- map_logfile = popen(filename_buf, "w");
+ char *filename_buf = malloc(strlen(map_logfile_name) + 50);
+ map_logfile_index = suffix >> LOGFILE_SECONDS_PER_CHUNK_SHIFT;
+
+ sprintf(filename_buf, "%s.%ld", map_logfile_name, map_logfile_index);
+ map_logfile = fopen(filename_buf, "w+");
if (!map_logfile)
- perror(filename);
- else
- atexit(map_pclose_map_logfile);
+ perror(map_logfile_name);
+
free(filename_buf);
+}
+
+static void
+map_set_logfile(char *filename)
+{
+ struct timeval tv;
+
+ map_logfile_name = strdup(filename);
+ gettimeofday(&tv, NULL);
+
+ map_start_logfile(tv.tv_sec);
+ atexit(map_close_logfile);
MAP_LOG("log-start");
}
+
+void
+map_write_log(char *format, ...)
+{
+ struct timeval tv;
+ va_list args;
+ va_start(args, format);
+
+ gettimeofday(&tv, NULL);
+
+ if ((tv.tv_sec >> LOGFILE_SECONDS_PER_CHUNK_SHIFT) != map_logfile_index) {
+ map_close_logfile();
+ map_start_logfile(tv.tv_sec);
+ }
+
+ fprintf(map_logfile, "%ld.%06ld ", (long)tv.tv_sec, (long) tv.tv_usec);
+ vfprintf(map_logfile, format, args);
+ fputc('\n', map_logfile);
+}
+
+
/*==========================================
* 設定ファイルを読み込む
*------------------------------------------
@@ -1748,7 +1798,7 @@ int map_config_read(char *cfgName) {
} else if (strcmpi(w1, "gm_log") == 0) {
gm_logfile_name = strdup(w2);
} else if (strcmpi(w1, "log_file") == 0) {
- map_setlogfile(w2);
+ map_set_logfile(w2);
} else if (strcmpi(w1, "import") == 0) {
map_config_read(w2);
}