Skip to content

Commit 5ef5715

Browse files
committed
Add some function-like macros for log/warn/both
WARN_ONCE() is like fprintf_color() but only once per session. WARN_LOG_ONCE() is the same but also logs the same message. Also add error color to error(), pexit() and similar stuff in misc.c
1 parent dfbbe98 commit 5ef5715

File tree

6 files changed

+64
-11
lines changed

6 files changed

+64
-11
lines changed

src/color.c

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

12-
char *color_error, *color_notice, *color_warning, *color_end;
12+
char *color_none, *color_error, *color_notice, *color_warning, *color_end;
1313

1414
/*
1515
* Translate ^ to Esc, in place.
@@ -34,11 +34,13 @@ char *parse_esc(const char *string)
3434

3535
void color_init()
3636
{
37+
color_none = "";
38+
3739
if (cfg_get_bool(SECTION_OPTIONS, NULL, "UseColors", 0)) {
3840
color_error = parse_esc(cfg_get_param(SECTION_OPTIONS, NULL, "ColorError"));
3941
color_notice = parse_esc(cfg_get_param(SECTION_OPTIONS, NULL, "ColorNotice"));
4042
color_warning = parse_esc(cfg_get_param(SECTION_OPTIONS, NULL, "ColorWarning"));
4143
color_end = parse_esc(cfg_get_param(SECTION_OPTIONS, NULL, "ColorEnd"));
4244
} else
43-
color_error = color_notice = color_warning = color_end = "";
45+
color_error = color_notice = color_warning = color_end = color_none;
4446
}

src/color.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@
88
#ifndef _JOHN_COLOR_H
99
#define _JOHN_COLOR_H
1010

11+
#include <stdio.h>
12+
#include <unistd.h> /* isatty */
13+
1114
extern char *parse_esc(const char *string);
1215
extern void color_init();
1316

1417
/* Color escape sequences as strings */
15-
extern char *color_error, *color_notice, *color_warning, *color_end;
18+
extern char *color_none, *color_error, *color_notice, *color_warning, *color_end;
1619

1720
#define printf_color(color, ...) fprintf_color(color, stdout, __VA_ARGS__);
1821

src/logger.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ void log_guess(char *login, char *uid, char *ciphertext, char *rep_plain,
620620
write_loop(fileno(stderr), "\007", 1);
621621
}
622622

623-
void log_event(const char *format, ...)
623+
void log_event_ex(int add_nl, const char *format, ...)
624624
{
625625
va_list args;
626626
int count1, count2;
@@ -658,7 +658,8 @@ void log_event(const char *format, ...)
658658
va_start(args, format);
659659
vfprintf(stderr, format, args);
660660
va_end(args);
661-
fprintf(stderr, "\n");
661+
if (add_nl)
662+
fprintf(stderr, "\n");
662663
if (options.flags & FLG_NOLOG)
663664
return;
664665
}

src/logger.h

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@
1919

2020
#include <fcntl.h>
2121

22+
#ifndef TRUE
23+
#define TRUE 1
24+
#endif
25+
#ifndef FALSE
26+
#define FALSE 0
27+
#endif
28+
2229
#if defined(F_SETLK) && defined(F_SETLKW) && defined(F_UNLCK) \
2330
&& defined(F_RDLCK) && defined(F_WRLCK)
2431
/*
@@ -50,6 +57,13 @@ extern int log_lock(int fd, int cmd, int type, const char *name,
5057
#endif /* #if defined(F_SETLK) && defined(F_SETLKW) && defined(F_UNLCK)
5158
&& defined(F_RDLCK) && defined(F_WRLCK) */
5259

60+
/*
61+
* log_event_ex() first argument is whether to add a newline or not.
62+
* Using FALSE is for when the same string is to be sent to log and
63+
* screen, so it already contains one.
64+
*/
65+
#define log_event(...) log_event_ex(TRUE, __VA_ARGS__)
66+
5367
/*
5468
* Macro to only log something once per session.
5569
*/
@@ -82,11 +96,11 @@ extern void log_guess(char *login, char *uid, char *ciphertext, char *rep_plain,
8296
*/
8397
extern
8498
#if (__GNUC__ == 4 && __GNUC_MINOR >= 4) || __GNUC__ > 4
85-
__attribute__ ((format (gnu_printf, 1, 2)))
99+
__attribute__ ((format (gnu_printf, 2, 3)))
86100
#elif __GNUC__
87-
__attribute__ ((format (printf, 1, 2)))
101+
__attribute__ ((format (printf, 2, 3)))
88102
#endif
89-
void log_event(const char *format, ...);
103+
void log_event_ex(int add_nl, const char *format, ...);
90104

91105
/*
92106
* Discards any buffered log data.

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: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818
#define _JOHN_MISC_H
1919

2020
#include <stdio.h>
21+
2122
#include "jumbo.h"
23+
#include "color.h"
24+
#include "logger.h"
2225

2326
#if !AC_BUILT
2427
#include <string.h>
@@ -37,6 +40,31 @@
3740
#endif
3841
#endif
3942

43+
/*
44+
* Macro for warning/notice (stderr) only once per session.
45+
*/
46+
#define WARN_ONCE(color, ...) \
47+
do { \
48+
static int warned; \
49+
if (!warned) { \
50+
fprintf_color(color, __VA_ARGS__); \
51+
warned++; \
52+
} \
53+
} while (0)
54+
55+
/*
56+
* Macro for warning/notice (stderr) AND logging, but only once per session.
57+
*/
58+
#define WARN_LOG_ONCE(color, stream, ...) \
59+
do { \
60+
static int warned; \
61+
if (!warned) { \
62+
fprintf_color(color, stream, __VA_ARGS__); \
63+
log_event_ex(FALSE, __VA_ARGS__); \
64+
warned++; \
65+
} \
66+
} while (0)
67+
4068
/*
4169
* Exit on error. Logs the event, closes john.pot and the log file, and
4270
* terminates the process with non-zero exit status.

0 commit comments

Comments
 (0)