Complete.Org: Mailing Lists: Archives: freeciv-dev: February 2004:
[Freeciv-Dev] (PR#7565) Server output directory
Home

[Freeciv-Dev] (PR#7565) Server output directory

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: undisclosed-recipients: ;
Subject: [Freeciv-Dev] (PR#7565) Server output directory
From: "Per I. Mathisen" <per@xxxxxxxxxxx>
Date: Fri, 27 Feb 2004 12:11:31 -0800
Reply-to: rt@xxxxxxxxxxx

<URL: http://rt.freeciv.org/Ticket/Display.html?id=7565 >

log1.diff : Style fixes and some defensive programming in log.c
con1.dif  : Ditto for console.c
gam1.diff : Ditto for gamelog.c
dir1.diff : Implements --Dir DIR server cmd option

The last patch allows you to output logs, gamelogs and savegames to a
directory of your choice. I am not very happy with it, though, as it
doesn't allow you to change this output directory while running, and this
is a necessary feature with restart-able servers, I think. Need to think
how I can make this happen elegantly.

The reason for this feature is support for pubservers, so some feedback
on this feature's usefulness from paulz or rp would be nice.

  - Per

Index: server/gamelog.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/gamelog.c,v
retrieving revision 1.30
diff -u -r1.30 gamelog.c
--- server/gamelog.c    12 Oct 2003 10:36:36 -0000      1.30
+++ server/gamelog.c    27 Feb 2004 20:06:43 -0000
@@ -44,7 +44,11 @@
 void gamelog_init(char *filename)
 {
   gamelog_level = GAMELOG_FULL;
-  gamelog_filename = filename;
+  if (filename && strlen(filename) > 0) {
+    gamelog_filename = filename;
+  } else {
+    gamelog_filename = NULL;
+  }
 }
 
 /**************************************************************************
Index: common/log.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/log.c,v
retrieving revision 1.43
diff -u -r1.43 log.c
--- common/log.c        4 Apr 2003 15:47:49 -0000       1.43
+++ common/log.c        27 Feb 2004 19:51:37 -0000
@@ -166,17 +166,20 @@
 }
 
 /**************************************************************************
-Initialise the log module.
-Either 'filename' or 'callback' may be NULL.
-If both are NULL, print to stderr.
-If both are non-NULL, both callback, and fprintf to file.
+  Initialise the log module. Either 'filename' or 'callback' may be NULL.
+  If both are NULL, print to stderr. If both are non-NULL, both callback, 
+  and fprintf to file.
 **************************************************************************/
 void log_init(const char *filename, int initial_level,
              log_callback_fn callback)
 {
-  fc_log_level=initial_level;
-  log_filename=filename;
-  log_callback=callback;
+  fc_log_level = initial_level;
+  if (filename && strlen(filename) > 0) {
+    log_filename = filename;
+  } else {
+    log_filename = NULL;
+  }
+  log_callback = callback;
   freelog(LOG_VERBOSE, "log started");
   freelog(LOG_DEBUG, "LOG_DEBUG test");
 }
Index: common/shared.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/shared.c,v
retrieving revision 1.106
diff -u -r1.106 shared.c
--- common/shared.c     28 Nov 2003 17:37:21 -0000      1.106
+++ common/shared.c     27 Feb 2004 20:01:42 -0000
@@ -58,6 +58,16 @@
 #endif
 #endif
 
+#ifndef PATH_SYMBOL
+#if defined _WIN32 || defined __WIN32__ || defined __EMX__ || defined __DJGPP__
+  /* Win32, OS/2, DOS */
+#define PATH_SYMBOL "\\"
+#else
+  /* Unix */
+#define PATH_SYMBOL "/"
+#endif
+#endif
+
 /* If no default data path is defined use the default default one */
 #ifndef DEFAULT_DATA_PATH
 #define DEFAULT_DATA_PATH "." PATH_SEPARATOR "data" PATH_SEPARATOR \
@@ -943,6 +953,23 @@
 }
 
 /***************************************************************************
+  Insert dirname into filename.
+***************************************************************************/
+void insert_path(char *dirname, char *filename)
+{
+  static char buffer[MAX_LEN_NAME * 3];
+
+  if (filename == NULL || dirname == NULL || strlen(filename) <= 0
+      || strlen(dirname) <= 0) {
+    return;
+  }
+
+  my_snprintf(buffer, sizeof(buffer), "%s%s%s", dirname, 
+              PATH_SYMBOL, filename);
+  mystrlcpy(filename, buffer, MAX_LEN_NAME);
+}
+
+/***************************************************************************
   Returns a filename to access the specified file from a data
   directory by searching all data directories (as specified by
   get_data_dirs) for the file.
Index: common/shared.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/shared.h,v
retrieving revision 1.122
diff -u -r1.122 shared.h
--- common/shared.h     11 Jan 2004 17:45:04 -0000      1.122
+++ common/shared.h     27 Feb 2004 20:01:42 -0000
@@ -236,4 +236,6 @@
 
 char *get_multicast_group(void);
 
+void insert_path(char *dirname, char *filename);
+
 #endif  /* FC__SHARED_H */
Index: server/civserver.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/civserver.c,v
retrieving revision 1.216
diff -u -r1.216 civserver.c
--- server/civserver.c  19 Nov 2003 11:16:52 -0000      1.216
+++ server/civserver.c  27 Feb 2004 20:01:43 -0000
@@ -66,15 +66,17 @@
   inx = 1;
   while (inx < argc) {
     if ((option = get_option("--file", argv, &inx, argc)))
-      srvarg.load_filename = option;
+      sz_strlcpy(srvarg.load_filename, option);
     else if (is_option("--help", argv[inx])) {
       showhelp = TRUE;
       break;
     } else if ((option = get_option("--log", argv, &inx, argc)))
-      srvarg.log_filename = option;
+      sz_strlcpy(srvarg.log_filename, option);
     else if ((option = get_option("--gamelog", argv, &inx, argc)))
-      srvarg.gamelog_filename = option;
-    else if (is_option("--nometa", argv[inx])) {
+      sz_strlcpy(srvarg.gamelog_filename, option);
+    else if ((option = get_option("--Dir", argv, &inx, argc))) {
+      sz_strlcpy(srvarg.directory, option);
+    } else if (is_option("--nometa", argv[inx])) {
       fprintf(stderr, _("Warning: the %s option is obsolete.  "
                        "Use -m to enable the metaserver.\n"), argv[inx]);
       showhelp = TRUE;
@@ -92,7 +94,7 @@
     } else if ((option = get_option("--bind", argv, &inx, argc))) {
       srvarg.bind_addr = option;
     } else if ((option = get_option("--read", argv, &inx, argc)))
-      srvarg.script_filename = option;
+      sz_strlcpy(srvarg.script_filename, option);
     else if ((option = get_option("--quitidle", argv, &inx, argc))) {
       if (sscanf(option, "%d", &srvarg.quitidle) != 1) {
        showhelp = TRUE;
@@ -128,6 +130,7 @@
   if (showhelp) {
     fprintf(stderr, _("Usage: %s [option ...]\nValid options are:\n"), 
argv[0]);
     fprintf(stderr, _("  -b  --bind ADDR\tListen for clients on ADDR\n"));
+    fprintf(stderr, _("  -D  --Dir DIR\tPut all server output in DIR\n"));
 #ifdef DEBUG
     fprintf(stderr, _("  -d, --debug NUM\tSet debug log level (0 to 4,"
                      " or 4:file1,min,max:...)\n"));
Index: server/srv_main.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/srv_main.c,v
retrieving revision 1.154
diff -u -r1.154 srv_main.c
--- server/srv_main.c   19 Feb 2004 21:06:42 -0000      1.154
+++ server/srv_main.c   27 Feb 2004 20:01:44 -0000
@@ -165,10 +165,11 @@
 
   srvarg.loglevel = LOG_NORMAL;
 
-  srvarg.log_filename = NULL;
-  srvarg.gamelog_filename = NULL;
-  srvarg.load_filename = NULL;
-  srvarg.script_filename = NULL;
+  memset(srvarg.directory, '\0', sizeof(srvarg.directory));
+  memset(srvarg.log_filename, '\0', sizeof(srvarg.log_filename));
+  memset(srvarg.gamelog_filename, '\0', sizeof(srvarg.gamelog_filename));
+  memset(srvarg.load_filename, '\0', sizeof(srvarg.load_filename));
+  memset(srvarg.script_filename, '\0', sizeof(srvarg.script_filename));
 
   srvarg.quitidle = 0;
 
@@ -594,10 +595,12 @@
     }
   }
 
-  if(!section_file_save(&file, filename, game.save_compress_level))
+  insert_path(srvarg.directory, filename);
+  if (!section_file_save(&file, filename, game.save_compress_level)) {
     con_write(C_FAIL, _("Failed saving game as %s"), filename);
-  else
+  } else {
     con_write(C_OK, _("Game saved as %s"), filename);
+  }
 
   section_file_free(&file);
 
@@ -1432,10 +1435,11 @@
 
   my_init_network();
 
+  insert_path(srvarg.directory, srvarg.log_filename);
+  insert_path(srvarg.directory, srvarg.gamelog_filename);
   con_log_init(srvarg.log_filename, srvarg.loglevel);
   gamelog_init(srvarg.gamelog_filename);
   gamelog_set_level(GAMELOG_FULL);
-  gamelog(GAMELOG_NORMAL, _("Starting new log"));
   
 #if IS_BETA_VERSION
   con_puts(C_COMMENT, "");
@@ -1453,7 +1457,7 @@
   server_open_socket();
 
   /* load a saved game */
-  if (srvarg.load_filename) {
+  if (strlen(srvarg.load_filename) > 0) {
     load_command(NULL, srvarg.load_filename);
   } 
 
@@ -1469,7 +1473,7 @@
   server_state = PRE_GAME_STATE;
 
   /* load a script file */
-  if (srvarg.script_filename
+  if (strlen(srvarg.script_filename) > 0
       && !read_init_script(NULL, srvarg.script_filename)) {
     exit(EXIT_FAILURE);
   }
Index: server/srv_main.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/srv_main.h,v
retrieving revision 1.17
diff -u -r1.17 srv_main.h
--- server/srv_main.h   28 Nov 2003 17:37:22 -0000      1.17
+++ server/srv_main.h   27 Feb 2004 20:01:44 -0000
@@ -32,10 +32,11 @@
   /* the log level */
   int loglevel;
   /* filenames */
-  char *log_filename;
-  char *gamelog_filename;
-  char *load_filename;
-  char *script_filename;
+  char directory[MAX_LEN_NAME];
+  char log_filename[MAX_LEN_NAME];
+  char gamelog_filename[MAX_LEN_NAME];
+  char load_filename[MAX_LEN_NAME];
+  char script_filename[MAX_LEN_NAME];
   /* extra info for the metaserver */
   char extra_metaserver_info[256];
   /* quit if there no players after a given time interval */
Index: server/console.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/console.c,v
retrieving revision 1.21
diff -u -r1.21 console.c
--- server/console.c    4 Apr 2003 15:47:49 -0000       1.21
+++ server/console.c    27 Feb 2004 19:53:05 -0000
@@ -76,11 +76,13 @@
 }
 
 /************************************************************************
-Initialize logging via console.
+  Initialize logging via console.
 ************************************************************************/
 void con_log_init(const char *log_filename, int log_level)
 {
-  log_init(log_filename, log_level, (log_filename ? NULL : con_handle_log));
+  bool has_file = (log_filename && strlen(log_filename) > 0);
+
+  log_init(log_filename, log_level, has_file ? NULL : con_handle_log);
   logdebug_suppress_warning;
 }
 

[Prev in Thread] Current Thread [Next in Thread]
  • [Freeciv-Dev] (PR#7565) Server output directory, Per I. Mathisen <=