| 1 |
/* |
| 2 |
* Gregorio is a program that translates gabc files to GregorioTeX |
| 3 |
* This file contains functions for logging messages, warnings, and errors. |
| 4 |
* |
| 5 |
* Copyright (C) 2009-2025 The Gregorio Project (see CONTRIBUTORS.md) |
| 6 |
* |
| 7 |
* This file is part of Gregorio. |
| 8 |
* |
| 9 |
* Gregorio is free software: you can redistribute it and/or modify |
| 10 |
* it under the terms of the GNU General Public License as published by |
| 11 |
* the Free Software Foundation, either version 3 of the License, or |
| 12 |
* (at your option) any later version. |
| 13 |
* |
| 14 |
* Gregorio is distributed in the hope that it will be useful, |
| 15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 |
* GNU General Public License for more details. |
| 18 |
* |
| 19 |
* You should have received a copy of the GNU General Public License |
| 20 |
* along with Gregorio. If not, see <http://www.gnu.org/licenses/>. |
| 21 |
*/ |
| 22 |
|
| 23 |
#include "config.h" |
| 24 |
#include <stdio.h> |
| 25 |
#include <assert.h> |
| 26 |
#include <stdarg.h> |
| 27 |
#include "bool.h" |
| 28 |
#include "messages.h" |
| 29 |
#include "support.h" |
| 30 |
|
| 31 |
static gregorio_verbosity verbosity_mode = 0; |
| 32 |
static bool debug_messages = false; |
| 33 |
static bool deprecation_is_warning = true; |
| 34 |
static int return_value = 0; |
| 35 |
|
| 36 |
int gregorio_get_return_value(void) |
| 37 |
{ |
| 38 |
return return_value; |
| 39 |
} |
| 40 |
|
| 41 |
void gregorio_set_verbosity_mode(const gregorio_verbosity verbosity) |
| 42 |
{ |
| 43 |
verbosity_mode = verbosity; |
| 44 |
} |
| 45 |
|
| 46 |
void gregorio_set_debug_messages(bool debug) |
| 47 |
{ |
| 48 |
debug_messages = debug; |
| 49 |
} |
| 50 |
|
| 51 |
void gregorio_set_deprecation_errors(bool deprecation_errors) |
| 52 |
{ |
| 53 |
deprecation_is_warning = !deprecation_errors; |
| 54 |
} |
| 55 |
|
| 56 |
static const char *verbosity_to_str(const gregorio_verbosity verbosity) |
| 57 |
{ |
| 58 |
const char *str; |
| 59 |
switch (verbosity) { |
| 60 |
case VERBOSITY_WARNING: |
| 61 |
str = _("warning:"); |
| 62 |
break; |
| 63 |
case VERBOSITY_DEPRECATION: |
| 64 |
/* if there is no deprecation, these lines will not be hit */ |
| 65 |
/* LCOV_EXCL_START */ |
| 66 |
str = _("deprecation:"); |
| 67 |
break; |
| 68 |
/* LCOV_EXCL_STOP */ |
| 69 |
case VERBOSITY_ERROR: |
| 70 |
str = _("error:"); |
| 71 |
break; |
| 72 |
case VERBOSITY_ASSERTION: |
| 73 |
/* not reachable unless there's a programming error */ |
| 74 |
/* LCOV_EXCL_START */ |
| 75 |
str = _("assertion:"); |
| 76 |
break; |
| 77 |
/* LCOV_EXCL_STOP */ |
| 78 |
case VERBOSITY_FATAL: |
| 79 |
/* all fatal errors should not be reasonably testable */ |
| 80 |
/* LCOV_EXCL_START */ |
| 81 |
str = _("fatal error:"); |
| 82 |
break; |
| 83 |
/* LCOV_EXCL_STOP */ |
| 84 |
default: |
| 85 |
/* INFO, for example */ |
| 86 |
str = " "; |
| 87 |
break; |
| 88 |
} |
| 89 |
return str; |
| 90 |
} |
| 91 |
|
| 92 |
void gregorio_messagef(const char *function_name, |
| 93 |
gregorio_verbosity verbosity, int line_number, |
| 94 |
const char *format, ...) |
| 95 |
{ |
| 96 |
va_list args; |
| 97 |
const char *verbosity_str; |
| 98 |
|
| 99 |
if (!debug_messages && verbosity != VERBOSITY_ASSERTION) { |
| 100 |
line_number = 0; |
| 101 |
function_name = NULL; |
| 102 |
} |
| 103 |
|
| 104 |
/* if these assertions fail, the program is not using this code correctly */ |
| 105 |
assert(stderr); |
| 106 |
assert(verbosity_mode); |
| 107 |
|
| 108 |
if (verbosity < verbosity_mode) { |
| 109 |
return; |
| 110 |
} |
| 111 |
if (verbosity == VERBOSITY_ASSERTION && return_value) { |
| 112 |
/* if something has already caused the system to fail, demote any |
| 113 |
* assertions coming after to warnings */ |
| 114 |
verbosity = VERBOSITY_WARNING; |
| 115 |
} |
| 116 |
verbosity_str = verbosity_to_str(verbosity); |
| 117 |
if (line_number) { |
| 118 |
/* if line number is specified, function_name must be specified */ |
| 119 |
assert(function_name); |
| 120 |
if (function_name) { |
| 121 |
fprintf(stderr, "%d: in function `%s': %s", line_number, |
| 122 |
function_name, verbosity_str); |
| 123 |
} |
| 124 |
} else { |
| 125 |
if (function_name) { |
| 126 |
fprintf(stderr, "in function `%s': %s", function_name, |
| 127 |
verbosity_str); |
| 128 |
} else { |
| 129 |
fprintf(stderr, "%s", verbosity_str); |
| 130 |
} |
| 131 |
} |
| 132 |
va_start(args, format); |
| 133 |
vfprintf(stderr, format, args); |
| 134 |
va_end(args); |
| 135 |
fprintf(stderr, "\n"); |
| 136 |
|
| 137 |
switch (verbosity) { |
| 138 |
case VERBOSITY_DEPRECATION: |
| 139 |
/* if there is no deprecation, these lines will not be hit */ |
| 140 |
/* LCOV_EXCL_START */ |
| 141 |
if (deprecation_is_warning) { |
| 142 |
break; |
| 143 |
} |
| 144 |
/* LCOV_EXCL_STOP */ |
| 145 |
/* else fall through */ |
| 146 |
case VERBOSITY_ERROR: |
| 147 |
case VERBOSITY_ASSERTION: |
| 148 |
return_value = 1; |
| 149 |
break; |
| 150 |
case VERBOSITY_FATAL: |
| 151 |
/* all fatal errors should not be reasonably testable */ |
| 152 |
/* LCOV_EXCL_START */ |
| 153 |
gregorio_exit(1); |
| 154 |
break; |
| 155 |
/* LCOV_EXCL_STOP */ |
| 156 |
default: |
| 157 |
break; |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
void gregorio_message(const char *string, const char *function_name, |
| 162 |
gregorio_verbosity verbosity, int line_number) |
| 163 |
{ |
| 164 |
gregorio_messagef(function_name, verbosity, line_number, "%s", string); |
| 165 |
} |
| 166 |
|