CC Localization
utils.cc
Go to the documentation of this file.
1 
5 #include "utils.hh"
6 #include <iostream>
7 
9 std::string esc_str(std::string const & s)
10 {
11  std::string t;
12  bool f = false; // flag for previous extensible escape
13  for (auto i:s) {
14  if (f&&i>='0'&&i<'8') { // escaped with hex or oct
15  t += "\"\""; // escape terminating concatenation
16  t += i;
17  f = false;
18  continue;
19  }
20  f = false;
21  if (i=='\\') {t += "\\\\"; continue;}
22  if (i=='"') {t += "\\\""; continue;}
23  if (isprint(i)) {t += i; continue;}
24  if (i=='\n') {t += "\\n"; continue;}
25  if (i=='\r') {t += "\\r"; continue;}
26  if (i=='\t') {t += "\\t"; continue;}
27  int r = (unsigned char) i;
28  t += '\\';
29  if (r>64) t += char('0'+r/64);
30  if (r>8) t += char('0'+r%64/8);
31  t += char('0'+r%8);
32  f = true;
33  }
34  return t;
35 }
36 
38 enum MsgLevel {
39  MSGL_ERROR = 0,
40  MSGL_WARN = 1,
41  MSGL_INFO = 2,
42  MSGL_DEBUG = 9,
43  MSGL_ALL = 10
44 };
45 
46 int msg_level = MSGL_INFO;
47 std::ostream & msg(int l)
49 {
50  static std::ostream null_stream(0);
51  return msg_level>=l ? std::cerr : null_stream;
52 }
53 std::ostream & debug = msg(MSGL_DEBUG);
54 std::ostream & info = msg(MSGL_INFO);
55 std::ostream & warn = msg(MSGL_WARN);
56 
58 void error(std::string const & m)
59 {
60  msg(0) << "Error: " << m << '\n';
61  throw Error(m);
62 }
std::ostream & warn
stream for warning messages
Definition: utils.cc:55
MsgLevel
Message levels.
Definition: utils.cc:38
std::ostream & msg(int l)
message output stream
Definition: utils.cc:48
std::ostream & info
stream for informative messages
Definition: utils.cc:54
std::string esc_str(std::string const &s)
Produce an escaped string that, when double quoted, produces the original.
Definition: utils.cc:9
Error exception.
Definition: utils.hh:14
void error(std::string const &m)
throw an Error with given message string
Definition: utils.cc:58
int msg_level
level below which messages are shown
Definition: utils.cc:46
std::ostream & debug
stream for debug messages
Definition: utils.cc:53
Miscellaneous utilities.