Skip to content

Commit

Permalink
libs/log.[ch]: Implement stderr logging.
Browse files Browse the repository at this point in the history
* Export log_level as global variable; remove getter and setter.
* Fix memory leak in log_open().
* Move FVWM3_LOGFILE_DEFAULT to defaults.h.
  • Loading branch information
domivogt authored and ThomasAdam committed Nov 20, 2021
1 parent 42df1c8 commit 3eadfa1
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 36 deletions.
8 changes: 7 additions & 1 deletion doc/fvwm3_manpage_source.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
[*-c* _config-command_]
[*-d* _displayname_]
[*-f* _config-file_]
[*-o* _logfile_]
[*-s* [_screen_num_]]
[*-v*]
[other options]
Expand Down Expand Up @@ -81,6 +82,11 @@ Causes fvwm to read _config-file_ instead of _~/.fvwm/config_ as its
initialization file. _$FVWM_USERDIR_ can also be used to change location
of default user directory _~/.fvwm_.

*-o* _logfile_::

Write log messages to _logfile_. If _logfile_ is '-', log to the
console. (Does not turn on logging, see the *-v* option.)

*-h* | *--help*::

A short usage description is printed.
Expand Down Expand Up @@ -241,7 +247,7 @@ save a few bits of memory.
Enables stack ring debugging. This option is only intended for internal
debugging and should only be used by developers.

*-v*::
*-v* | *--verbose*::

Enables debug logging. Writes in append mode to fvwm log file, which is
~/.fvwm/fvwm3-output.log by default. See ENVIRONMENT section on how to
Expand Down
2 changes: 1 addition & 1 deletion fvwm/expand.c
Original file line number Diff line number Diff line change
Expand Up @@ -1140,7 +1140,7 @@ static signed int expand_vars_extended(
break;
case VAR_DEBUG_LOG_STATE:
is_numeric = True;
val = log_get_level();
val = lib_log_level;
break;
default:
/* unknown variable - try to find it in the environment */
Expand Down
21 changes: 18 additions & 3 deletions fvwm/fvwm3.c
Original file line number Diff line number Diff line change
Expand Up @@ -1242,10 +1242,12 @@ static void usage(int is_verbose)
" -I vis-id: use visual <vis-id>\n"
" -l colors: try to use no more than <colors> colors\n"
" -L: strict color limit\n"
" -o logfile: output file or '-' for stderr\n"
" -P: visual palette\n"
" -r: replace running window manager\n"
" -s [screen]: manage a single screen\n"
" -S: static palette\n"
" -v: verbose log output\n"
" -V: print version information\n"
);
fprintf(stderr, "Try 'man %s' for more information.\n",
Expand Down Expand Up @@ -1802,6 +1804,7 @@ int main(int argc, char **argv)
fvwm_userdir);
}

set_log_file(NULL);
for (i = 1; i < argc; i++)
{
if (strcmp(argv[i], "-debug_stack_ring") == 0 ||
Expand Down Expand Up @@ -2043,10 +2046,21 @@ int main(int argc, char **argv)
free(Fvwm_SupportInfo);
exit(0);
}
else if (strcmp(argv[i], "-v") == 0)
else if (
strcmp(argv[i], "-v") == 0 ||
strcmp(argv[i], "--verbose") == 0)
{
log_set_level(1);
log_open(fvwm_userdir);
lib_log_level = 1;
}
else if (strcmp(argv[i], "-o") == 0 ||
strcmp(argv[i], "--output-file") == 0)
{
if (++i >= argc)
{
usage(0);
exit(1);
}
set_log_file(argv[i]);
}
else
{
Expand All @@ -2056,6 +2070,7 @@ int main(int argc, char **argv)
exit(1);
}
}
log_open(fvwm_userdir);

InstallSignals();

Expand Down
3 changes: 3 additions & 0 deletions libs/defaults.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
#ifndef FVWMLIB_DEFAULTS_H
#define FVWMLIB_DEFAULTS_H

/*** logging ***/
#define FVWM3_LOGFILE_DEFAULT "fvwm3-output.log"

/*** event handling ***/
#define CLOCK_SKEW_MS 30000 /* ms */

Expand Down
69 changes: 41 additions & 28 deletions libs/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,51 +31,64 @@
#include "getpwuid.h"
#include "log.h"

static char *log_file_name;
static FILE *log_file;
static int log_level;
int lib_log_level = 0;

static void log_vwrite(const char *, const char *, va_list);

/* Set log level. */
void
log_set_level(int ll)
set_log_file(char *name)
{
log_level = ll;
}
if (log_file_name != NULL)
{
free(log_file_name);
log_file_name = NULL;
}
if (name != NULL)
{
log_file_name = fxstrdup(name);
}

/* Get log level. */
int log_get_level(void)
{
return log_level;
}

/* Open logging to file. */
void
log_open(const char *fvwm_userdir)
{
char *path, *logfile_env;

xasprintf(&path, "%s/%s", fvwm_userdir, FVWM3_LOGFILE_DEFAULT);
char *path, *file_name;

logfile_env = getenv("FVWM3_LOGFILE");
if (logfile_env != NULL) {
const char *expanded_path;
if (lib_log_level == 0)
return;
if (log_file_name != NULL &&
log_file_name[0] == '-' && log_file_name[1] == 0) {
log_file = stderr;
return;
}
if ((file_name = log_file_name) == NULL)
file_name = getenv("FVWM3_LOGFILE");

expanded_path = expand_path(logfile_env);
if (file_name != NULL)
{
char *expanded_path;

expanded_path = expand_path(file_name);
if (expanded_path[0] == '/')
path = fxstrdup(expanded_path);
{
path = expanded_path;
}
else
{
xasprintf(&path, "%s/%s", fvwm_userdir, expanded_path);
free((char *)expanded_path);
free((char *)expanded_path);
}
}
else
{
xasprintf(&path, "%s/%s", fvwm_userdir, FVWM3_LOGFILE_DEFAULT);
}

if (log_level == 0)
return;
log_close();

log_file = fopen(path, "a");
if (log_file == NULL) {
if ((log_file = fopen(path, "a")) == NULL) {
free(path);
return;
}
Expand All @@ -89,13 +102,13 @@ log_open(const char *fvwm_userdir)
void
log_toggle(const char *fvwm_userdir)
{
if (log_level == 0) {
log_level = 1;
if (lib_log_level == 0) {
lib_log_level = 1;
log_open(fvwm_userdir);
fvwm_debug(NULL, "log opened (because of SIGUSR2)");
} else {
fvwm_debug(NULL, "log closed (because of SIGUSR2)");
log_level = 0;
lib_log_level = 0;
log_close();
}
}
Expand All @@ -104,7 +117,7 @@ log_toggle(const char *fvwm_userdir)
void
log_close(void)
{
if (log_file != NULL)
if (log_file != NULL && log_file != stderr)
fclose(log_file);
log_file = NULL;
}
Expand Down
6 changes: 3 additions & 3 deletions libs/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
#define FVWMLIB_LOG_H

#define printflike(a, b) __attribute__ ((format (printf, a, b)))
#define FVWM3_LOGFILE_DEFAULT "fvwm3-output.log"

void log_set_level(int);
int log_get_level(void);
extern int lib_log_level;

void set_log_file(char *name);
void log_open(const char *);
void log_toggle(const char *);
void log_close(void);
Expand Down

0 comments on commit 3eadfa1

Please sign in to comment.