Left: | ||
Right: |
OLD | NEW |
---|---|
1 /* | 1 /* |
2 This file is part of LilyPond, the GNU music typesetter. | 2 This file is part of LilyPond, the GNU music typesetter. |
3 | 3 |
4 Copyright (C) 1997--2011 Han-Wen Nienhuys <hanwen@xs4all.nl> | 4 Copyright (C) 1997--2011 Han-Wen Nienhuys <hanwen@xs4all.nl> |
5 | 5 |
6 LilyPond is free software: you can redistribute it and/or modify | 6 LilyPond is free software: you can redistribute it and/or modify |
7 it under the terms of the GNU General Public License as published by | 7 it under the terms of the GNU General Public License as published by |
8 the Free Software Foundation, either version 3 of the License, or | 8 the Free Software Foundation, either version 3 of the License, or |
9 (at your option) any later version. | 9 (at your option) any later version. |
10 | 10 |
11 LilyPond is distributed in the hope that it will be useful, | 11 LilyPond is distributed in the hope that it will be useful, |
12 but WITHOUT ANY WARRANTY; without even the implied warranty of | 12 but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 GNU General Public License for more details. | 14 GNU General Public License for more details. |
15 | 15 |
16 You should have received a copy of the GNU General Public License | 16 You should have received a copy of the GNU General Public License |
17 along with LilyPond. If not, see <http://www.gnu.org/licenses/>. | 17 along with LilyPond. If not, see <http://www.gnu.org/licenses/>. |
18 */ | 18 */ |
19 | 19 |
20 #include "warn.hh" | 20 #include "warn.hh" |
21 | 21 |
22 #include <cstdlib> | 22 #include <cstdlib> |
23 #include <cstdio> | 23 #include <cstdio> |
24 | 24 |
25 #include "international.hh" | 25 #include "international.hh" |
26 | 26 |
27 using namespace std; | 27 using namespace std; |
28 | 28 |
29 /* Is progress indication at NEWLINE? */ | 29 /** We have several different loglevels, each with its own message function(s): |
30 static bool progress_newline = true; | 30 ERROR: error, non_fatal_error, programming_error |
31 WARN: warning | |
32 BASIC_PROGRESS: success/... | |
33 PROGRESS: progress_indication, successful | |
34 INFO: message | |
35 DEBUG: debug | |
36 All these functions check whether the corresponding loglevel bit is set | |
37 and print the message only if that's the case | |
38 */ | |
31 | 39 |
32 /* Display user information that is not a full message. */ | 40 /* Define the loglevel (default is PROGRESS) */ |
41 int loglevel = LOGLEVEL_PROGRESS; | |
42 | |
43 bool | |
44 is_loglevel (int level) | |
45 { | |
46 // Check the bitmask containing the loglevel | |
47 return (loglevel & level); | |
48 } | |
49 | |
50 void· | |
51 set_loglevel (int level) | |
52 { | |
53 loglevel = level; | |
54 debug_output (_f ("Log level set to `%d'\n", loglevel)); | |
Keith
2011/08/08 06:52:46
Quotes around the number, `271', confused me
| |
55 } | |
56 | |
57 void· | |
58 set_loglevel (string level) | |
59 { | |
60 /* Convert the loglevel string to lower-case, so we allow | |
61 both upper- and lower-case loglevels */ | |
62 std::transform (level.begin (), level.end (), level.begin (), ::tolower); | |
63 ·· | |
64 /* Compare just the first few characters, so the loglevels | |
65 can be abbreviated */ | |
66 if (level.compare (0, 5, "debug") == 0) // debug | |
67 set_loglevel (LOGLEVEL_DEBUG); | |
68 else if (level.compare (0, 4, "info") == 0) // info | |
69 set_loglevel (LOGLEVEL_INFO); | |
70 else if (level.compare (0, 4, "prog") == 0) // progress | |
71 set_loglevel (LOGLEVEL_PROGRESS); | |
72 else if (level.compare (0, 5, "basic") == 0) // basic progress | |
73 set_loglevel (LOGLEVEL_BASIC); | |
74 else if (level.compare (0, 4, "warn") == 0) // warning | |
75 set_loglevel (LOGLEVEL_WARN); | |
76 else if (level.compare (0, 3, "err") == 0) // error | |
77 set_loglevel (LOGLEVEL_ERROR); | |
78 else if (level.compare (0, 4, "none") == 0) // none | |
79 set_loglevel (LOGLEVEL_NONE); | |
80 else | |
81 { | |
82 int l; | |
83 if (sscanf (level.c_str (), "%d", &l)) | |
84 set_loglevel (l); | |
85 else | |
86 { | |
87 non_fatal_error (_f ("unknown log level `%s' given, using default (PRO GRESS)", level)); | |
88 set_loglevel (LOGLEVEL_INFO); | |
89 » } | |
90 } | |
91 } | |
92 | |
93 | |
94 /** | |
95 * Helper functions: print_message_part (no newline prepended) | |
96 * print_message (always starts on a new line) | |
97 */ | |
98 | |
99 /* Is output message at NEWLINE? */ | |
100 static bool message_newline = true; | |
101 | |
102 /* Display user information as a full message. | |
103 if newline is true, start the message on a new line. | |
104 */ | |
33 void | 105 void |
34 progress_indication (string s) | 106 print_message (int level, string location, string s, bool newline) |
35 { | 107 { |
108 /* Only print the message if the current loglevel allows it: */ | |
109 if (!is_loglevel (level)) | |
110 return; | |
111 if (newline && !message_newline) | |
112 fputc ('\n', stderr); | |
113 | |
36 /* Test if all silly progress_indication ("\n") can be dropped now. */ | 114 /* Test if all silly progress_indication ("\n") can be dropped now. */ |
37 if (s == "\n") | 115 if (s == "\n") |
38 return; | 116 return; |
39 | 117 |
118 if (!location.empty ()) | |
119 s = location + ": " + s; | |
40 fputs (s.c_str (), stderr); | 120 fputs (s.c_str (), stderr); |
41 fflush (stderr); | 121 fflush (stderr); |
42 if (s.length ()) | 122 if (s.length ()) |
43 progress_newline = s[s.length () - 1] == '\n'; | 123 message_newline = s[s.length () - 1] == '\n'; |
44 } | 124 } |
45 | 125 |
46 /* Display a single user message. Always starts on a new line. */ | 126 |
127 /** The actual output functions to be called in lilypond code. | |
128 * Sorted in descending order of importance (errors, warnings, progress, info, | |
129 * debug). Each prints a message on a separate line. | |
130 */ | |
131 | |
132 /* Display a fatal error message. Also exits lilypond. */ | |
Keith
2011/08/08 06:52:46
Can you now remove the function non_fatal_error()
| |
47 void | 133 void |
48 message (string s) | 134 error (string s, string location) |
49 { | 135 { |
50 if (!progress_newline) | 136 non_fatal_error (s, location); |
51 fputc ('\n', stderr); | |
52 progress_indication (s); | |
53 } | |
54 | |
55 /* Display a success message. Always starts on a new line. */ | |
56 void | |
57 successful (string s) | |
58 { | |
59 message (_f ("success: %s", s.c_str ()) + "\n"); | |
60 } | |
61 | |
62 /* Display a warning message. Always starts on a new line. */ | |
63 void | |
64 warning (string s) | |
65 { | |
66 message (_f ("warning: %s", s.c_str ()) + "\n"); | |
67 } | |
68 | |
69 void | |
70 non_fatal_error (string s) | |
71 { | |
72 message (_f ("error: %s", s.c_str ()) + "\n"); | |
73 } | |
74 | |
75 /* Display an error message. Always starts on a new line. */ | |
76 void | |
77 error (string s) | |
78 { | |
79 non_fatal_error (s); | |
80 exit (1); | 137 exit (1); |
81 } | 138 } |
82 | 139 |
140 /* Display a severe programming error message, but don't exit. */ | |
83 void | 141 void |
84 programming_error (string s) | 142 programming_error (string s, string location) |
85 { | 143 { |
86 message (_f ("programming error: %s", s) + "\n"); | 144 print_message (LOG_ERROR, location, _f ("programming error: %s", s) + "\n"); |
87 message (_ ("continuing, cross fingers") + "\n"); | 145 print_message (LOG_ERROR, location, _ ("continuing, cross fingers") + "\n"); |
88 } | 146 } |
89 | 147 |
148 /* Display a non-fatal error message, don't exit. */ | |
149 void | |
150 non_fatal_error (string s, string location) | |
151 { | |
152 print_message (LOG_ERROR, location, _f ("error: %s", s) + "\n"); | |
153 } | |
154 | |
155 /* Display a warning message. */ | |
156 void | |
157 warning (string s, string location) | |
158 { | |
159 print_message (LOG_WARN, location, _f ("warning: %s", s) + "\n"); | |
160 } | |
161 | |
162 /* Display a success message. */ | |
163 void | |
164 successful (string s, string location) | |
165 { | |
166 progress_indication (_f ("success: %s", s) + "\n", true, location); | |
167 } | |
168 | |
169 /* Display information about the progress. */ | |
170 void | |
171 progress_indication (string s, bool newline, string location) | |
172 { | |
173 print_message (LOG_PROGRESS, location, s, newline); | |
174 } | |
175 | |
176 /* Display a single info message. */ | |
177 void | |
178 message (string s, bool newline, string location) | |
179 { | |
180 // Use the progress loglevel for all normal messages (including progress msg) | |
Keith
2011/08/08 06:52:46
Confusing. Input::message() filters with LOG_INFO;
| |
181 print_message (LOG_PROGRESS, location, s, newline); | |
182 } | |
183 | |
184 /* Display a debug information, not necessarily on a new line. */ | |
185 void | |
186 debug_output (string s, bool newline, string location) | |
187 { | |
188 print_message (LOG_DEBUG, location, s, newline); | |
189 } | |
OLD | NEW |