Skip to content

Commit be23acf

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 be23acf

File tree

5 files changed

+65
-6
lines changed

5 files changed

+65
-6
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.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: 14 additions & 0 deletions
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.

0 commit comments

Comments
 (0)