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