Skip to content

Commit f5e30ee

Browse files
committed
Make logging functions a bit more flexible.
Calls to dsp_log_printf() must now be wrapped in the new dsp_log_acquire() and dsp_log_release() functions. Allows printing lines piecemeal with proper locking.
1 parent b47b8cf commit f5e30ee

File tree

8 files changed

+91
-66
lines changed

8 files changed

+91
-66
lines changed

dsp.c

+12-11
Original file line numberDiff line numberDiff line change
@@ -145,20 +145,18 @@ struct dsp_globals dsp_globals = {
145145
"dsp", /* prog_name */
146146
};
147147

148-
int dsp_log_printf(const char *fmt, ...)
148+
void dsp_log_acquire(void)
149149
{
150-
int r;
151150
pthread_mutex_lock(&log_lock);
152151
if (progress_line && !progress_cleared)
153152
fputs(CLEAR_PROGRESS, stderr);
154-
va_list v;
155-
va_start(v, fmt);
156-
r = vfprintf(stderr, fmt, v);
157-
va_end(v);
153+
}
154+
155+
void dsp_log_release(void)
156+
{
158157
if (progress_line && !progress_cleared)
159158
fputs(progress_line, stderr);
160159
pthread_mutex_unlock(&log_lock);
161-
return r;
162160
}
163161

164162
void ev_queue_push(enum event_type type, int val)
@@ -457,27 +455,28 @@ static void print_progress(struct codec *in, ssize_t pos, int is_paused, int for
457455
static struct timespec then;
458456
if (has_elapsed(&then, 0.1) || force) {
459457
#endif
460-
double chain_delay_s, out_delay_s;
458+
double in_delay_s = 0.0, chain_delay_s, out_delay_s;
461459
get_delay_sec(&chain_delay_s, &out_delay_s);
462460
ssize_t delay = get_delay_frames(in->fs, chain_delay_s, out_delay_s);
463461
ssize_t p = (pos > delay) ? pos - delay : 0;
464462
ssize_t rem = (in->frames > p) ? in->frames - p : 0;
463+
if (verbose_progress)
464+
in_delay_s = (double) codec_read_buf_delay(in_codec_buf) / in->fs;
465+
pthread_mutex_lock(&log_lock);
465466
if (progress_line == NULL)
466467
progress_line = calloc(PROGRESS_MAX_LEN, sizeof(char));
467468
int pl = snprintf(progress_line, PROGRESS_MAX_LEN, "%c %.1f%% "TIME_FMT" -"TIME_FMT" ",
468469
(is_paused) ? '|' : '>', (in->frames != -1) ? (double) p / in->frames * 100.0 : 0,
469470
TIME_FMT_ARGS(p, in->fs), TIME_FMT_ARGS(rem, in->fs));
470471
if (pl < PROGRESS_MAX_LEN - 1 && verbose_progress) {
471-
double in_delay_s = (double) codec_read_buf_delay(in_codec_buf) / in->fs;
472472
pl += snprintf(progress_line + pl, PROGRESS_MAX_LEN - pl, "lat:%.2fms+%.2fms+%.2fms=%.2fms ",
473473
in_delay_s*1000.0, chain_delay_s*1000.0, out_delay_s*1000.0, (in_delay_s+chain_delay_s+out_delay_s)*1000.0);
474474
}
475475
if (pl < PROGRESS_MAX_LEN - 1 && (verbose_progress || clip_count != 0)) {
476476
pl += snprintf(progress_line + pl, PROGRESS_MAX_LEN - pl, "peak:%.2fdBFS clip:%ld ",
477477
20.0*log10(peak), clip_count);
478478
}
479-
pthread_mutex_lock(&log_lock);
480-
fprintf(stderr, "\r%s\033[K", progress_line);
479+
dsp_log_printf("\r%s\033[K", progress_line);
481480
progress_cleared = 0;
482481
pthread_mutex_unlock(&log_lock);
483482
#ifdef HAVE_CLOCK_GETTIME
@@ -782,7 +781,9 @@ int main(int argc, char *argv[])
782781
case EVENT_TYPE_KEY:
783782
switch (ev.val) {
784783
case 'h':
784+
dsp_log_acquire();
785785
dsp_log_printf("\n%s\n", interactive_help);
786+
dsp_log_release();
786787
break;
787788
case ',':
788789
pos = do_seek(in_codecs.head, pos, (ssize_t) in_codecs.head->fs * -5, SEEK_CUR, is_paused);

dsp.h

+19-3
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,23 @@ enum {
3030
LL_VERBOSE,
3131
};
3232
#define LOGLEVEL(l) (dsp_globals.loglevel >= (l))
33-
#define LOG_FMT(l, fmt, ...) do { if (LOGLEVEL(l)) dsp_log_printf("%s: " fmt "\n", dsp_globals.prog_name, __VA_ARGS__); } while (0)
34-
#define LOG_S(l, s) do { if (LOGLEVEL(l)) dsp_log_printf("%s: %s\n", dsp_globals.prog_name, s); } while (0)
33+
#define dsp_log_printf(...) fprintf(stderr, __VA_ARGS__)
34+
#define LOG_FMT(l, fmt, ...) \
35+
do { \
36+
if (LOGLEVEL(l)) { \
37+
dsp_log_acquire(); \
38+
dsp_log_printf("%s: " fmt "\n", dsp_globals.prog_name, __VA_ARGS__); \
39+
dsp_log_release(); \
40+
} \
41+
} while (0)
42+
#define LOG_S(l, s) \
43+
do { \
44+
if (LOGLEVEL(l)) { \
45+
dsp_log_acquire(); \
46+
dsp_log_printf("%s: %s\n", dsp_globals.prog_name, s); \
47+
dsp_log_release(); \
48+
} \
49+
} while (0)
3550

3651
#define DEFAULT_FS 44100
3752
#define DEFAULT_CHANNELS 1
@@ -53,6 +68,7 @@ struct stream_info {
5368
};
5469

5570
extern struct dsp_globals dsp_globals;
56-
extern int dsp_log_printf(const char *, ...);
71+
extern void dsp_log_acquire(void);
72+
extern void dsp_log_release(void);
5773

5874
#endif

effect.c

+6-4
Original file line numberDiff line numberDiff line change
@@ -235,12 +235,14 @@ static int build_effects_chain_block(int argc, const char *const *argv, struct e
235235
}
236236
else {
237237
if (LOGLEVEL(LL_VERBOSE)) {
238-
fprintf(stderr, "%s: effect:", dsp_globals.prog_name);
238+
dsp_log_acquire();
239+
dsp_log_printf("%s: effect:", dsp_globals.prog_name);
239240
for (int j = 0; j < i - k; ++j)
240-
fprintf(stderr, " %s", argv[k + j]);
241-
fprintf(stderr, "; channels=%d [", stream->channels);
241+
dsp_log_printf(" %s", argv[k + j]);
242+
dsp_log_printf("; channels=%d [", stream->channels);
242243
print_selector(channel_selector, stream->channels);
243-
fprintf(stderr, "] fs=%d\n", stream->fs);
244+
dsp_log_printf("] fs=%d\n", stream->fs);
245+
dsp_log_release();
244246
}
245247
e = ei->init(ei, stream, channel_selector, dir, i - k, &argv[k]);
246248
if (e == NULL) {

ladspa_dsp.c

+2-9
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,8 @@ static int n_configs = 0;
5757
static struct ladspa_dsp_config *configs = NULL;
5858
static LADSPA_Descriptor *descriptors = NULL;
5959

60-
int dsp_log_printf(const char *fmt, ...)
61-
{
62-
int r;
63-
va_list v;
64-
va_start(v, fmt);
65-
r = vfprintf(stderr, fmt, v);
66-
va_end(v);
67-
return r;
68-
}
60+
void dsp_log_acquire(void) { /* do nothing */ }
61+
void dsp_log_release(void) { /* do nothing */ }
6962

7063
static void init_config(struct ladspa_dsp_config *config, const char *file_name, const char *dir_path)
7164
{

matrix4.c

+10-3
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,13 @@ sample_t * matrix4_effect_run(struct effect *e, ssize_t *frames, sample_t *ibuf,
123123
}
124124
#ifndef LADSPA_FRONTEND
125125
/* TODO: Implement a proper way for effects to show status lines. */
126-
if (state->show_status)
127-
fprintf(stderr, "\n%s%s: lr: %+06.2f (%+06.2f); cs: %+06.2f (%+06.2f); dir_boost: l:%05.3f r:%05.3f; adj: %05.3f; ord: %zd; diff: %zd; early: %zd\033[K\r\033[A",
126+
if (state->show_status) {
127+
dsp_log_acquire();
128+
dsp_log_printf("\n%s%s: lr: %+06.2f (%+06.2f); cs: %+06.2f (%+06.2f); dir_boost: l:%05.3f r:%05.3f; adj: %05.3f; ord: %zd; diff: %zd; early: %zd\033[K\r\033[A",
128129
e->name, (state->disable) ? " [off]" : "", TO_DEGREES(state->ax.lr), TO_DEGREES(state->ax_ev.lr), TO_DEGREES(state->ax.cs), TO_DEGREES(state->ax_ev.cs),
129130
fl_boost, fr_boost, state->ev.adj, state->ev.ord_count, state->ev.diff_count, state->ev.early_count);
131+
dsp_log_release();
132+
}
130133
#endif
131134

132135
*frames = oframes;
@@ -187,7 +190,11 @@ void matrix4_effect_destroy(struct effect *e)
187190
free(state->bufs);
188191
event_state_cleanup(&state->ev);
189192
#ifndef LADSPA_FRONTEND
190-
if (state->show_status) fprintf(stderr, "\033[K\n\033[K\r\033[A");
193+
if (state->show_status) {
194+
dsp_log_acquire();
195+
dsp_log_printf("\033[K\n\033[K\r\033[A");
196+
dsp_log_release();
197+
}
191198
#endif
192199
free(state);
193200
}

matrix4_mb.c

+9-5
Original file line numberDiff line numberDiff line change
@@ -390,20 +390,22 @@ sample_t * matrix4_mb_effect_run(struct effect *e, ssize_t *frames, sample_t *ib
390390
#ifndef LADSPA_FRONTEND
391391
/* TODO: Implement a proper way for effects to show status lines. */
392392
if (state->show_status) {
393+
dsp_log_acquire();
393394
for (i = 0; i < N_BANDS; ++i) {
394-
fprintf(stderr, "\n%s%s: band %zd: lr: %+06.2f (%+06.2f); cs: %+06.2f (%+06.2f); dir_boost: l:%05.3f r:%05.3f; adj: %05.3f; ord: %zd; diff: %zd; early: %zd\033[K\r",
395+
dsp_log_printf("\n%s%s: band %zd: lr: %+06.2f (%+06.2f); cs: %+06.2f (%+06.2f); dir_boost: l:%05.3f r:%05.3f; adj: %05.3f; ord: %zd; diff: %zd; early: %zd\033[K\r",
395396
e->name, (state->disable) ? " [off]" : "", i,
396397
TO_DEGREES(state->band[i].ax.lr), TO_DEGREES(state->band[i].ax_ev.lr), TO_DEGREES(state->band[i].ax.cs), TO_DEGREES(state->band[i].ax_ev.cs),
397398
state->band[i].fl_boost, state->band[i].fr_boost, state->band[i].ev.adj, state->band[i].ev.ord_count, state->band[i].ev.diff_count, state->band[i].ev.early_count);
398399
}
399-
fprintf(stderr, "\n%s%s: weighted RMS dir_boost: l:%05.3f r:%05.3f\033[K\r",
400+
dsp_log_printf("\n%s%s: weighted RMS dir_boost: l:%05.3f r:%05.3f\033[K\r",
400401
e->name, (state->disable) ? " [off]" : "",
401402
#if DOWNSAMPLE_FACTOR > 1
402403
state->fl_boost[1], state->fr_boost[1]);
403404
#else
404405
state->fl_boost, state->fr_boost);
405406
#endif
406-
fprintf(stderr, "\033[%zdA", i+1);
407+
dsp_log_printf("\033[%zdA", i+1);
408+
dsp_log_release();
407409
}
408410
#endif
409411

@@ -470,8 +472,10 @@ void matrix4_mb_effect_destroy(struct effect *e)
470472
event_state_cleanup(&state->band[i].ev);
471473
#ifndef LADSPA_FRONTEND
472474
if (state->show_status) {
473-
for (int i = 0; i < N_BANDS+1; ++i) fprintf(stderr, "\033[K\n");
474-
fprintf(stderr, "\033[K\r\033[%dA", N_BANDS+1);
475+
dsp_log_acquire();
476+
for (int i = 0; i < N_BANDS+1; ++i) dsp_log_printf("\033[K\n");
477+
dsp_log_printf("\033[K\r\033[%dA", N_BANDS+1);
478+
dsp_log_release();
475479
}
476480
#endif
477481
free(state);

stats.c

+29-27
Original file line numberDiff line numberDiff line change
@@ -58,50 +58,52 @@ void stats_effect_destroy(struct effect *e)
5858
{
5959
ssize_t i;
6060
struct stats_state *state = (struct stats_state *) e->data;
61-
fprintf(stderr, "\n%-18s", "Channel");
61+
dsp_log_acquire();
62+
dsp_log_printf("\n%-18s", "Channel");
6263
for (i = 0; i < e->ostream.channels; ++i)
63-
fprintf(stderr, " %12zd", i);
64-
fprintf(stderr, "\n%-18s", "DC offset");
64+
dsp_log_printf(" %12zd", i);
65+
dsp_log_printf("\n%-18s", "DC offset");
6566
for (i = 0; i < e->ostream.channels; ++i)
66-
fprintf(stderr, " %12.8f", state[i].sum / state[i].samples);
67-
fprintf(stderr, "\n%-18s", "Minimum");
67+
dsp_log_printf(" %12.8f", state[i].sum / state[i].samples);
68+
dsp_log_printf("\n%-18s", "Minimum");
6869
for (i = 0; i < e->ostream.channels; ++i)
69-
fprintf(stderr, " %12.8f", state[i].min);
70-
fprintf(stderr, "\n%-18s", "Maximum");
70+
dsp_log_printf(" %12.8f", state[i].min);
71+
dsp_log_printf("\n%-18s", "Maximum");
7172
for (i = 0; i < e->ostream.channels; ++i)
72-
fprintf(stderr, " %12.8f", state[i].max);
73-
fprintf(stderr, "\n%-18s", "Peak level (dBFS)");
73+
dsp_log_printf(" %12.8f", state[i].max);
74+
dsp_log_printf("\n%-18s", "Peak level (dBFS)");
7475
for (i = 0; i < e->ostream.channels; ++i)
75-
fprintf(stderr, " %12.4f", 20 * log10(MAXIMUM(fabs(state[i].min), fabs(state[i].max))));
76+
dsp_log_printf(" %12.4f", 20 * log10(MAXIMUM(fabs(state[i].min), fabs(state[i].max))));
7677
if (state->ref != -HUGE_VAL) {
77-
fprintf(stderr, "\n%-18s", "Peak level (dBr)");
78+
dsp_log_printf("\n%-18s", "Peak level (dBr)");
7879
for (i = 0; i < e->ostream.channels; ++i)
79-
fprintf(stderr, " %12.4f", state->ref + (20 * log10(MAXIMUM(fabs(state[i].min), fabs(state[i].max)))));
80+
dsp_log_printf(" %12.4f", state->ref + (20 * log10(MAXIMUM(fabs(state[i].min), fabs(state[i].max)))));
8081
}
81-
fprintf(stderr, "\n%-18s", "RMS level (dBFS)");
82+
dsp_log_printf("\n%-18s", "RMS level (dBFS)");
8283
for (i = 0; i < e->ostream.channels; ++i)
83-
fprintf(stderr, " %12.4f", 20 * log10(sqrt(state[i].sum_sq / state[i].samples)));
84+
dsp_log_printf(" %12.4f", 20 * log10(sqrt(state[i].sum_sq / state[i].samples)));
8485
if (state->ref != -HUGE_VAL) {
85-
fprintf(stderr, "\n%-18s", "RMS level (dBr)");
86+
dsp_log_printf("\n%-18s", "RMS level (dBr)");
8687
for (i = 0; i < e->ostream.channels; ++i)
87-
fprintf(stderr, " %12.4f", state->ref + (20 * log10(sqrt(state[i].sum_sq / state[i].samples))));
88+
dsp_log_printf(" %12.4f", state->ref + (20 * log10(sqrt(state[i].sum_sq / state[i].samples))));
8889
}
89-
fprintf(stderr, "\n%-18s", "Crest factor (dB)");
90+
dsp_log_printf("\n%-18s", "Crest factor (dB)");
9091
for (i = 0; i < e->ostream.channels; ++i)
91-
fprintf(stderr, " %12.4f", 20 * log10(MAXIMUM(fabs(state[i].min), fabs(state[i].max)) / sqrt(state[i].sum_sq / state[i].samples)));
92-
fprintf(stderr, "\n%-18s", "Peak count");
92+
dsp_log_printf(" %12.4f", 20 * log10(MAXIMUM(fabs(state[i].min), fabs(state[i].max)) / sqrt(state[i].sum_sq / state[i].samples)));
93+
dsp_log_printf("\n%-18s", "Peak count");
9394
for (i = 0; i < e->ostream.channels; ++i)
94-
fprintf(stderr, " %12zd", state[i].peak_count);
95-
fprintf(stderr, "\n%-18s", "Peak sample");
95+
dsp_log_printf(" %12zd", state[i].peak_count);
96+
dsp_log_printf("\n%-18s", "Peak sample");
9697
for (i = 0; i < e->ostream.channels; ++i)
97-
fprintf(stderr, " %12zd", state[i].peak_frame);
98-
fprintf(stderr, "\n%-18s", "Samples");
98+
dsp_log_printf(" %12zd", state[i].peak_frame);
99+
dsp_log_printf("\n%-18s", "Samples");
99100
for (i = 0; i < e->ostream.channels; ++i)
100-
fprintf(stderr, " %12zd", state[i].samples);
101-
fprintf(stderr, "\n%-18s", "Length (s)");
101+
dsp_log_printf(" %12zd", state[i].samples);
102+
dsp_log_printf("\n%-18s", "Length (s)");
102103
for (i = 0; i < e->ostream.channels; ++i)
103-
fprintf(stderr, " %12.2f", (double) state[i].samples / e->ostream.fs);
104-
fputc('\n', stderr);
104+
dsp_log_printf(" %12.2f", (double) state[i].samples / e->ostream.fs);
105+
dsp_log_printf("\n");
106+
dsp_log_release();
105107
free(state);
106108
}
107109

util.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -178,20 +178,20 @@ void print_selector(const char *b, int n)
178178
if (c && l)
179179
range_start = (range_start == -1) ? i - 1 : range_start;
180180
else if (!c && range_start != -1) {
181-
fprintf(stderr, "%s%d%s%d", (f) ? "" : ",", range_start, (i - range_start == 2) ? "," : "-", i - 1);
181+
dsp_log_printf("%s%d%s%d", (f) ? "" : ",", range_start, (i - range_start == 2) ? "," : "-", i - 1);
182182
range_start = -1;
183183
f = 0;
184184
}
185185
else if (l) {
186-
fprintf(stderr, "%s%d", (f) ? "" : ",", i - 1);
186+
dsp_log_printf("%s%d", (f) ? "" : ",", i - 1);
187187
f = 0;
188188
}
189189
l = c;
190190
}
191191
if (range_start != -1)
192-
fprintf(stderr, "%s%d%s%d", (f) ? "" : ",", range_start, (i - range_start == 2) ? "," : "-", i - 1);
192+
dsp_log_printf("%s%d%s%d", (f) ? "" : ",", range_start, (i - range_start == 2) ? "," : "-", i - 1);
193193
else if (l)
194-
fprintf(stderr, "%s%d", (f) ? "" : ",", n - 1);
194+
dsp_log_printf("%s%d", (f) ? "" : ",", n - 1);
195195
}
196196

197197
int num_bits_set(const char *b, int n)

0 commit comments

Comments
 (0)