summaryrefslogtreecommitdiff
path: root/Build/source/texk/gregorio/gregorio-src/src/messages.c
blob: 25e1266d68d2a3dc6df99c0ee518b6282d61bc29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
 * Gregorio is a program that translates gabc files to GregorioTeX
 * This file contains functions for logging messages, warnings, and errors.
 *
 * 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
 * (at your option) any later version.
 *
 * Gregorio is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Gregorio.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"
#include <stdio.h>
#include <assert.h>
#include <stdarg.h>
#include "bool.h"
#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;
static int return_value = 0;

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;
}

void gregorio_set_debug_messages(bool debug)
{
    debug_messages = debug;
}

void gregorio_set_deprecation_errors(bool deprecation_errors)
{
    deprecation_is_warning = !deprecation_errors;
}

static const char *verbosity_to_str(const gregorio_verbosity verbosity)
{
    const char *str;
    switch (verbosity) {
    case VERBOSITY_WARNING:
        str = _("warning:");
        break;
    case VERBOSITY_DEPRECATION:
        /* if there is no deprecation, these lines will not be hit */
        /* LCOV_EXCL_START */
        str = _("deprecation:");
        break;
        /* LCOV_EXCL_STOP */
    case VERBOSITY_ERROR:
        str = _("error:");
        break;
    case VERBOSITY_ASSERTION:
        /* not reachable unless there's a programming error */
        /* LCOV_EXCL_START */
        str = _("assertion:");
        break;
        /* LCOV_EXCL_STOP */
    case VERBOSITY_FATAL:
        /* all fatal errors should not be reasonably testable */
        /* LCOV_EXCL_START */
        str = _("fatal error:");
        break;
        /* LCOV_EXCL_STOP */
    default:
        /* INFO, for example */
        str = " ";
        break;
    }
    return str;
}

void gregorio_messagef(const char *function_name,
        gregorio_verbosity verbosity, int line_number,
        const char *format, ...)
{
    va_list args;
    const char *verbosity_str;

    if (!debug_messages && verbosity != VERBOSITY_ASSERTION) {
        line_number = 0;
        function_name = NULL;
    }

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

    if (verbosity < verbosity_mode) {
        return;
    }
    if (verbosity == VERBOSITY_ASSERTION && return_value) {
        /* if something has already caused the system to fail, demote any
         * assertions coming after to warnings */
        verbosity = VERBOSITY_WARNING;
    }
    verbosity_str = verbosity_to_str(verbosity);
    if (line_number) {
        /* 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,
                    function_name, verbosity_str);
        }
    } else {
        if (function_name) {
            fprintf(error_out, "in function `%s': %s", function_name,
                    verbosity_str);
        } else {
            fprintf(error_out, "%s", verbosity_str);
        }
    }
    va_start(args, format);
    vfprintf(error_out, format, args);
    va_end(args);
    fprintf(error_out, "\n");

    switch (verbosity) {
    case VERBOSITY_DEPRECATION:
        /* if there is no deprecation, these lines will not be hit */
        /* LCOV_EXCL_START */
        if (deprecation_is_warning) {
            break;
        }
        /* LCOV_EXCL_STOP */
        /* else fall through */
    case VERBOSITY_ERROR:
    case VERBOSITY_ASSERTION:
        return_value = 1;
        break;
    case VERBOSITY_FATAL:
        /* all fatal errors should not be reasonably testable */
        /* LCOV_EXCL_START */
        gregorio_exit(1);
        break;
        /* LCOV_EXCL_STOP */
    default:
        break;
    }
}

void gregorio_message(const char *string, const char *function_name,
        gregorio_verbosity verbosity, int line_number)
{
    gregorio_messagef(function_name, verbosity, line_number, "%s", string);
}