summaryrefslogtreecommitdiff
path: root/Build/source/texk/gregorio/gregorio-4.0.0-beta2/src/messages.c
blob: f5dc0c53d1a62e87d89d124036de8c88140d3c33 (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
173
/*
 * Copyright (C) 2009-2015 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 <stdlib.h>             /* for exit() */
#include <stdarg.h>             /* for exit() */
#include "bool.h"
#include "messages.h"

static FILE *error_out;
static const char *file_name = NULL;
static gregorio_verbosity verbosity_mode = 0;
static bool debug_messages = false;
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_file_name(const char *const new_name)
{
    file_name = new_name;
}

void gregorio_set_verbosity_mode(const gregorio_verbosity verbosity)
{
    verbosity_mode = verbosity;
}

#if 0 /* unused */
static void gregorio_set_debug_messages(bool debug)
{
    debug_messages = debug;
}
#endif

static const char *verbosity_to_str(const gregorio_verbosity verbosity)
{
    const char *str;
    switch (verbosity) {
    case VERBOSITY_WARNING:
        str = _("warning:");
        break;
    case VERBOSITY_DEPRECATION:
        str = _("deprecation:");
        break;
    case VERBOSITY_ERROR:
        str = _("error:");
        break;
    case VERBOSITY_FATAL:
        str = _("fatal error:");
        break;
    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) {
        line_number = 0;
        function_name = NULL;
    }

    if (!error_out) {
        fprintf(stderr, _("warning: error_out not set in gregorio_messages, "
                    "assumed stderr\n"));
        error_out = stderr;
    }
    if (!verbosity_mode) {
        fprintf(stderr, _("warning: verbosity mode not set in "
                    "gregorio_messages, assumed warnings\n"));
        verbosity_mode = VERBOSITY_WARNING;
    }
    if (verbosity < verbosity_mode) {
        return;
    }
    verbosity_str = verbosity_to_str(verbosity);
    if (line_number) {
        if (function_name) {
            if (!file_name) {
                fprintf(error_out, "line %d: in function `%s': %s",
                        line_number, function_name, verbosity_str);
            } else {
                fprintf(error_out, "%d: in function `%s': %s", line_number,
                        function_name, verbosity_str);
            }
        } else {
            /* no function_name specified */
            if (!file_name) {
                fprintf(error_out, "line %d: %s", line_number, verbosity_str);
            } else {
                fprintf(error_out, "%d: %s", line_number, verbosity_str);
            }
        }
    } else {
        if (function_name) {
            /*
             * if (!file_name) {
             *     fprintf (error_out, "in function `%s': %s",
             *     function_name, verbosity_str);
             *     return;
             * } else {
             */
            fprintf(error_out, "in function `%s': %s", function_name,
                    verbosity_str);
            /* } */
        } else {
            /* no function_name specified */
            /*
             * if (!file_name) {
             *     fprintf (error_out, "%s", verbosity_str);
             *     return;
             * } 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_ERROR:
        return_value = 1;
        break;
    case VERBOSITY_FATAL:
        exit(1);
        break;
    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);
}