From 5d94e216a5c37c4444f361bc75cf0d93ee596b8e Mon Sep 17 00:00:00 2001 From: AW-TECH Date: Mon, 25 Jul 2016 20:18:32 +0200 Subject: [PATCH] Add custom log file name function in command line via -f --- config.c | 23 +++++++++++++++++++++++ config.h | 2 ++ direwolf.c | 17 +++++++++++++++-- log.c | 13 ++++++++++++- log.h | 4 ++-- 5 files changed, 54 insertions(+), 5 deletions(-) diff --git a/config.c b/config.c index 8090c85..45e883f 100644 --- a/config.c +++ b/config.c @@ -756,6 +756,7 @@ void config_init (char *fname, struct audio_s *p_audio_config, strlcpy (p_misc_config->gpsnmea_port, "", sizeof(p_misc_config->gpsnmea_port)); strlcpy (p_misc_config->nmea_port, "", sizeof(p_misc_config->nmea_port)); strlcpy (p_misc_config->logdir, "", sizeof(p_misc_config->logdir)); + strlcpy (p_misc_config->logfilename, "", sizeof(p_misc_config->logfilename)); /* @@ -3863,6 +3864,28 @@ void config_init (char *fname, struct audio_s *p_audio_config, } } + +/* + * LOGFILENAME - Log file name. + */ + else if (strcasecmp(t, "logfilename") == 0) { + t = split(NULL,0); + if (t == NULL) { + text_color_set(DW_COLOR_ERROR); + dw_printf ("Config file: Missing name for LogFileName on line %d.\n", line); + continue; + } + else { + strlcpy (p_misc_config->logfilename, t, sizeof(p_misc_config->logfilename)); + } + t = split(NULL,0); + if (t != NULL) { + text_color_set(DW_COLOR_ERROR); + dw_printf ("Config file: LOGFILE on line %d should have logfile name and nothing more.\n", line); + } + } + + /* * BEACON channel delay every message * diff --git a/config.h b/config.h index 4ea2eda..c8f94c8 100644 --- a/config.h +++ b/config.h @@ -56,6 +56,8 @@ struct misc_config_s { char logdir[80]; /* Directory for saving activity logs. */ + char logfilename[80]; /* File name for saving activity logs. */ + int sb_configured; /* TRUE if SmartBeaconing is configured. */ int sb_fast_speed; /* MPH */ int sb_fast_rate; /* seconds */ diff --git a/direwolf.c b/direwolf.c index 3ea54c7..c016f38 100644 --- a/direwolf.c +++ b/direwolf.c @@ -178,6 +178,7 @@ int main (int argc, char *argv[]) int r_opt = 0, n_opt = 0, b_opt = 0, B_opt = 0, D_opt = 0; /* Command line options. */ char P_opt[16]; char l_opt[80]; + char f_opt[80]; char input_file[80]; int t_opt = 1; /* Text color option. */ @@ -194,6 +195,7 @@ int main (int argc, char *argv[]) #endif strlcpy(l_opt, "", sizeof(l_opt)); + strlcpy(f_opt, "", sizeof(f_opt)); strlcpy(P_opt, "", sizeof(P_opt)); #if __WIN32__ @@ -322,7 +324,7 @@ int main (int argc, char *argv[]) /* ':' following option character means arg is required. */ - c = getopt_long(argc, argv, "P:B:D:c:pxr:b:n:d:q:t:Ul:Sa:", + c = getopt_long(argc, argv, "P:B:D:c:pxr:b:n:d:q:t:Ul:Sa:Uf:", long_options, &option_index); if (c == -1) break; @@ -501,6 +503,11 @@ int main (int argc, char *argv[]) strlcpy (l_opt, optarg, sizeof(l_opt)); break; + case 'f': /* -f for log file name */ + + strlcpy (f_opt, optarg, sizeof(f_opt)); + break; + case 'S': /* Print symbol tables and exit. */ symbols_init (); @@ -594,6 +601,11 @@ int main (int argc, char *argv[]) strlcpy (misc_config.logdir, l_opt, sizeof(misc_config.logdir)); } + if (strlen(f_opt) > 0) { + strlcpy (misc_config.logfilename, f_opt, sizeof(misc_config.logfilename)); + } + + misc_config.enable_kiss_pt = enable_pseudo_terminal; if (strlen(input_file) > 0) { @@ -711,7 +723,7 @@ int main (int argc, char *argv[]) * log the tracker beacon transmissions with fake channel 999. */ - log_init(misc_config.logdir); + log_init(misc_config.logdir, misc_config.logfilename); beacon_init (&audio_config, &misc_config); @@ -1062,6 +1074,7 @@ static void usage (char **argv) dw_printf ("Options:\n"); dw_printf (" -c fname Configuration file name.\n"); dw_printf (" -l logdir Directory name for log files. Use . for current.\n"); + dw_printf (" -f logname Name for log file. \n"); dw_printf (" -r n Audio sample rate, per sec.\n"); dw_printf (" -n n Number of audio channels, 1 or 2.\n"); dw_printf (" -b n Bits per audio sample, 8 or 16.\n"); diff --git a/log.c b/log.c index f5448e7..0ef3658 100644 --- a/log.c +++ b/log.c @@ -104,9 +104,10 @@ static void quote_for_csv (char *out, size_t outsize, const char *in) { static char g_log_dir[80]; static FILE *g_log_fp; static char g_open_fname[20]; +static char g_logfilename[80]; -void log_init (char *path) +void log_init (char *path, char *logfilename) { struct stat st; @@ -118,6 +119,11 @@ void log_init (char *path) return; } +if (strlen(logfilename) != 0) { + strlcpy (g_logfilename, logfilename, sizeof(g_logfilename)); + } + + if (stat(path,&st) == 0) { // Exists, but is it a directory? if (S_ISDIR(st.st_mode)) { @@ -191,7 +197,12 @@ void log_write (int chan, decode_aprs_t *A, packet_t pp, alevel_t alevel, retry_ // Microsoft doesn't recognize %F as equivalent to %Y-%m-%d + if (strlen(g_logfilename) != 0) { + strftime (fname, sizeof(fname), g_logfilename, &tm); + } + else { strftime (fname, sizeof(fname), "%Y-%m-%d.log", &tm); + } // Close current file if name has changed diff --git a/log.h b/log.h index 5cc0264..baf7590 100644 --- a/log.h +++ b/log.h @@ -10,8 +10,8 @@ -void log_init (char *path); +void log_init (char *path, char *logfilename); void log_write (int chan, decode_aprs_t *A, packet_t pp, alevel_t alevel, retry_t retries); -void log_term (void); \ No newline at end of file +void log_term (void); \ No newline at end of file