Skip to content

Commit 1b8c8d2

Browse files
committed
Logging tweaks
Add some function-like macros for log/warn/both WARN_AND_LOG() is like fprintf_color() then log_event(), with same message. WARN_ONCE() is like fprintf_color() but only once per session. LOG_ONCE() is like log_event() but only once per session. WARN_AND_LOG_ONCE() does both, with same message. Also add error color to error(), pexit() and similar stuff in misc.c
1 parent a0938a2 commit 1b8c8d2

File tree

7 files changed

+77
-12
lines changed

7 files changed

+77
-12
lines changed

run/john.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ TerminalReset = ^[0m
113113
# which define red for errors, green for notices and yellow for warnings).
114114
# Note that you don't strictly need to use ANSI sequences - other things are
115115
# fine too.
116-
# Set this to N or comment it out to disable all color stuff.
116+
# Set this to N to disable all color stuff.
117117
UseColors = Y
118118
ColorError = ^[0;31m
119119
ColorNotice = ^[0;32m

src/color.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "config.h"
1010
#include "options.h"
1111

12+
const char* const color_none = "";
1213
char *color_error, *color_notice, *color_warning, *color_end;
1314

1415
/*
@@ -34,11 +35,11 @@ char *parse_esc(const char *string)
3435

3536
void color_init()
3637
{
37-
if (cfg_get_bool(SECTION_OPTIONS, NULL, "UseColors", 0)) {
38+
if (cfg_get_bool(SECTION_OPTIONS, NULL, "UseColors", 1)) {
3839
color_error = parse_esc(cfg_get_param(SECTION_OPTIONS, NULL, "ColorError"));
3940
color_notice = parse_esc(cfg_get_param(SECTION_OPTIONS, NULL, "ColorNotice"));
4041
color_warning = parse_esc(cfg_get_param(SECTION_OPTIONS, NULL, "ColorWarning"));
4142
color_end = parse_esc(cfg_get_param(SECTION_OPTIONS, NULL, "ColorEnd"));
4243
} else
43-
color_error = color_notice = color_warning = color_end = "";
44+
color_error = color_notice = color_warning = color_end = (char*)color_none;
4445
}

src/color.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,8 @@
88
#ifndef _JOHN_COLOR_H
99
#define _JOHN_COLOR_H
1010

11-
extern char *parse_esc(const char *string);
12-
extern void color_init();
13-
14-
/* Color escape sequences as strings */
15-
extern char *color_error, *color_notice, *color_warning, *color_end;
11+
#include <stdio.h>
12+
#include <unistd.h> /* isatty */
1613

1714
#define printf_color(color, ...) fprintf_color(color, stdout, __VA_ARGS__);
1815

@@ -40,4 +37,11 @@ extern char *color_error, *color_notice, *color_warning, *color_end;
4037
fputs(color_end, handle); \
4138
} while (0)
4239

40+
extern char *parse_esc(const char *string);
41+
extern void color_init();
42+
43+
/* Color escape sequences as strings */
44+
extern const char* const color_none;
45+
extern char *color_error, *color_notice, *color_warning, *color_end;
46+
4347
#endif /* _JOHN_COLOR_H */

src/john.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2093,6 +2093,12 @@ int main(int argc, char **argv)
20932093
return base64conv(argc, argv);
20942094
}
20952095

2096+
/*
2097+
* For having these as empty strings in case a color warning/error is
2098+
* printed before we init colors.
2099+
*/
2100+
color_error = color_notice = color_warning = color_end = (char*)color_none;
2101+
20962102
#if !(CPU_FALLBACK || OMP_FALLBACK || defined(CPU_FALLBACK_BINARY) || defined(OMP_FALLBACK_BINARY))
20972103
path_init(argv);
20982104
#endif

src/logger.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,41 @@ extern int log_lock(int fd, int cmd, int type, const char *name,
5050
#endif /* #if defined(F_SETLK) && defined(F_SETLKW) && defined(F_UNLCK)
5151
&& defined(F_RDLCK) && defined(F_WRLCK) */
5252

53+
/*
54+
* Macro for warning/notice AND logging. Arguments are like to fprintf_color
55+
* but with no ending NL.
56+
*/
57+
#define WARN_AND_LOG(color, stream, ...) \
58+
do { \
59+
fprintf_color(color, stream, __VA_ARGS__); \
60+
fputc('\n', stream); \
61+
log_event(__VA_ARGS__); \
62+
} while (0)
63+
64+
/*
65+
* Macro to only log something once per session.
66+
*/
67+
#define LOG_ONCE(...) \
68+
do { \
69+
static int warned; \
70+
if (!warned) { \
71+
log_event(__VA_ARGS__); \
72+
warned = 1; \
73+
} \
74+
} while (0)
75+
76+
/*
77+
* Like WARN_AND_LOG, but once per session.
78+
*/
79+
#define WARN_AND_LOG_ONCE(color, stream, ...) \
80+
do { \
81+
static int warned; \
82+
if (!warned) { \
83+
WARN_AND_LOG(color, stream, __VA_ARGS__); \
84+
warned = 1; \
85+
} \
86+
} while (0)
87+
5388
/*
5489
* Initializes the logger (opens john.pot and a log file).
5590
*/

src/misc.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "logger.h"
3333
#include "params.h"
3434
#include "misc.h"
35+
#include "color.h"
3536
#include "options.h"
3637

3738
#include "john_mpi.h"
@@ -49,17 +50,20 @@ void real_error(const char *file, int line)
4950
exit(1);
5051
}
5152

52-
void real_error_msg(const char *file, int line, const char *format, ...)
53+
void real_error_msg(const char *file, int line, const char *format, ...)
5354
{
5455
va_list args;
5556

56-
#if defined(HAVE_MPI) && !defined(_JOHN_MISC_NO_LOG)
57+
#if !defined(_JOHN_MISC_NO_LOG)
58+
fputs(color_error, stderr);
59+
#ifdef HAVE_MPI
5760
if (mpi_p > 1)
5861
fprintf(stderr, "%u@%s: ", mpi_id + 1, mpi_name);
5962
else
60-
#elif OS_FORK && !defined(_JOHN_MISC_NO_LOG)
63+
#elif OS_FORK
6164
if (options.fork)
6265
fprintf(stderr, "%u: ", options.node_min);
66+
#endif
6367
#endif
6468
va_start(args, format);
6569
vfprintf(stderr, format, args);
@@ -73,6 +77,7 @@ void real_pexit(const char *file, int line, const char *format, ...)
7377
va_list args;
7478

7579
#if !defined(_JOHN_MISC_NO_LOG)
80+
fputs(color_error, stderr);
7681
#if HAVE_MPI
7782
if (mpi_p > 1)
7883
fprintf(stderr, "%u@%s: ", mpi_id + 1, mpi_name);

src/misc.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
#define _JOHN_MISC_H
1919

2020
#include <stdio.h>
21+
2122
#include "jumbo.h"
23+
#include "color.h"
2224

2325
#if !AC_BUILT
2426
#include <string.h>
@@ -37,6 +39,18 @@
3739
#endif
3840
#endif
3941

42+
/*
43+
* Like fprintf_color but only once per session.
44+
*/
45+
#define WARN_ONCE(...) \
46+
do { \
47+
static int warned; \
48+
if (!warned) { \
49+
fprintf_color(__VA_ARGS__); \
50+
warned = 1; \
51+
} \
52+
} while (0)
53+
4054
/*
4155
* Exit on error. Logs the event, closes john.pot and the log file, and
4256
* terminates the process with non-zero exit status.
@@ -62,7 +76,7 @@ extern void real_error_msg(const char *file, int line, const char *format, ...)
6276
;
6377
#endif
6478

65-
#define error_msg(...) real_error_msg(__FILE__, __LINE__, __VA_ARGS__)
79+
#define error_msg(...) real_error_msg(__FILE__, __LINE__, __VA_ARGS__)
6680

6781
/*
6882
* Similar to perror(), but supports formatted output, and calls error().

0 commit comments

Comments
 (0)