Skip to content

Commit

Permalink
Merge pull request #1606 from rpspringuel/stderr-fix
Browse files Browse the repository at this point in the history
Stderr fix
  • Loading branch information
rpspringuel authored Jan 28, 2025
2 parents 46304a5 + 4db82c9 commit d7f83e3
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 48 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ As of v3.0.0 this project adheres to [Semantic Versioning](http://semver.org/).
- Fixed a bug that caused a custos to sometimes change into a clef. See [#1373](https://github.com/gregorio-project/gregorio/issues/1373).

### Changed
- Modified gregorio to append to the log file specified as an argument and to send early messages to it. See [#1541](https://github.com/gregorio-project/gregorio/issues/1541).
- Defined an output directory for gtex and glog files. Default is `tmp-gre`. This can be changed using `\gresetoutputdir{...}`. Fixes [#1393](https://github.com/gregorio-project/gregorio/issues/1393), [#1542](https://github.com/gregorio-project/gregorio/issues/1542), and [#1571](https://github.com/gregorio-project/gregorio/issues/1571).
- gabc.vim has been expanded into a proper vim plugin.

Expand Down
68 changes: 35 additions & 33 deletions src/gregorio-utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,8 @@ int main(int argc, char **argv)
bool debug = false;
bool must_print_short_usage = false;
int option_index = 0;
static struct option long_options[] = {
static const char *const options = "o:SF:l:f:shOLVvWDpd";
static const struct option long_options[] = {
{"output-file", 1, 0, 'o'},
{"stdout", 0, 0, 'S'},
{"output-format", 1, 0, 'F'},
Expand All @@ -350,16 +351,41 @@ int main(int argc, char **argv)
gregorio_score *score = NULL;

gregorio_support_init("gregorio", argv[0]);
setlocale(LC_CTYPE, "C");

if (argc == 1) {
fprintf(stderr, "%s: missing file operand.\n", argv[0]);
print_short_usage(argv[0]);
gregorio_exit(0);
/* need to look for the -l option up front */
for (;;) {
c = getopt_long(argc, argv, options,
long_options, &option_index);
if (c == -1)
break;

if (c == 'l') {
if (error_file_name) {
fprintf(stderr,
"warning: several error files declared, %s taken\n",
error_file_name);
must_print_short_usage = true;
} else {
error_file_name = optarg;
}
}
} /* end of for */

if (error_file_name) {
gregorio_check_file_access(write, error_file_name, ERROR,
gregorio_exit(1));
error_file = freopen(error_file_name, "a", stderr);
if (!error_file) {
fprintf(stderr, "error: can't open file %s for writing\n",
error_file_name);
gregorio_exit(1);
}
}
setlocale(LC_CTYPE, "C");

while (1) {
c = getopt_long(argc, argv, "o:SF:l:f:shOLVvWDpd",
optind = 1;
for (;;) {
c = getopt_long(argc, argv, options,
long_options, &option_index);
if (c == -1)
break;
Expand Down Expand Up @@ -420,18 +446,9 @@ int main(int argc, char **argv)
}
break;
case 'l':
if (error_file_name) {
fprintf(stderr,
"warning: several error files declared, %s taken\n",
error_file_name);
must_print_short_usage = true;
break;
}
error_file_name = optarg;
break;
case 'f':
if (input_format) {
gregorio_set_error_out(error_file);
fprintf(stderr,
"warning: several output formats declared, first taken\n");
must_print_short_usage = true;
Expand Down Expand Up @@ -527,7 +544,7 @@ int main(int argc, char **argv)
break;
/* LCOV_EXCL_STOP */
}
} /* end of while */
} /* end of for */
if (optind == argc) {
if (!input_file) { /* input not undefined (could be stdin) */
fprintf(stderr, "%s: missing file operand.\n", argv[0]);
Expand Down Expand Up @@ -638,21 +655,6 @@ int main(int argc, char **argv)
}
}

if (!error_file_name) {
error_file = stderr;
gregorio_set_error_out(error_file);
} else {
gregorio_check_file_access(write, error_file_name, ERROR,
gregorio_exit(1));
error_file = fopen(error_file_name, "wb");
if (!error_file) {
fprintf(stderr, "error: can't open file %s for writing\n",
error_file_name);
gregorio_exit(1);
}
gregorio_set_error_out(error_file);
}

if (!verb_mode) {
verb_mode = VERBOSITY_DEPRECATION;
}
Expand Down
20 changes: 7 additions & 13 deletions src/messages.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Copyright (C) 2009-2021 The Gregorio Project (see CONTRIBUTORS.md)
*
* This file is part of Gregorio.
*
*
* Gregorio is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
Expand All @@ -28,7 +28,6 @@
#include "messages.h"
#include "support.h"

static FILE *error_out;
static gregorio_verbosity verbosity_mode = 0;
static bool debug_messages = false;
static bool deprecation_is_warning = true;
Expand All @@ -39,11 +38,6 @@ int gregorio_get_return_value(void)
return return_value;
}

void gregorio_set_error_out(FILE *const f)
{
error_out = f;
}

void gregorio_set_verbosity_mode(const gregorio_verbosity verbosity)
{
verbosity_mode = verbosity;
Expand Down Expand Up @@ -108,7 +102,7 @@ void gregorio_messagef(const char *function_name,
}

/* if these assertions fail, the program is not using this code correctly */
assert(error_out);
assert(stderr);
assert(verbosity_mode);

if (verbosity < verbosity_mode) {
Expand All @@ -124,21 +118,21 @@ void gregorio_messagef(const char *function_name,
/* if line number is specified, function_name must be specified */
assert(function_name);
if (function_name) {
fprintf(error_out, "%d: in function `%s': %s", line_number,
fprintf(stderr, "%d: in function `%s': %s", line_number,
function_name, verbosity_str);
}
} else {
if (function_name) {
fprintf(error_out, "in function `%s': %s", function_name,
fprintf(stderr, "in function `%s': %s", function_name,
verbosity_str);
} else {
fprintf(error_out, "%s", verbosity_str);
fprintf(stderr, "%s", verbosity_str);
}
}
va_start(args, format);
vfprintf(error_out, format, args);
vfprintf(stderr, format, args);
va_end(args);
fprintf(error_out, "\n");
fprintf(stderr, "\n");

switch (verbosity) {
case VERBOSITY_DEPRECATION:
Expand Down
1 change: 0 additions & 1 deletion src/messages.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ void gregorio_messagef(const char *function_name,
void gregorio_set_verbosity_mode(gregorio_verbosity verbosity);
void gregorio_set_debug_messages(bool debug);
void gregorio_set_deprecation_errors(bool deprecation_errors);
void gregorio_set_error_out(FILE *f);
int gregorio_get_return_value(void);

#define gregorio_assert_only(TEST,FUNCTION,MESSAGE) \
Expand Down
1 change: 0 additions & 1 deletion src/support.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ static kpathsea kpse = NULL;
void gregorio_support_init(const char *const program USED_FOR_KPSE,
const char *const argv0 USED_FOR_KPSE)
{
gregorio_set_error_out(stderr);
gregorio_set_verbosity_mode(VERBOSITY_ERROR);
gregorio_struct_init();
#ifdef USE_KPSE
Expand Down

0 comments on commit d7f83e3

Please sign in to comment.