Log.cpp
Go to the documentation of this file.
1 // (C) Copyright Renaud Detry 2007-2015.
2 // Distributed under the GNU General Public License and under the
3 // BSD 3-Clause License (See accompanying file LICENSE.txt).
4 
5 /** @file */
6 
7 #include <nuklei/Log.h>
8 #include <nuklei/Common.h>
9 
10 namespace nuklei {
11 
12  std::ostream *Log::out = &std::cerr;
13 
14  std::ostream *Log::outInstance = NULL;
15 
16  boost::mutex Log::mutex_;
17 
18  std::string Log::msgColor = NUKLEI_LBLUE;
19  std::string Log::errorColor = NUKLEI_LRED;
20  std::string Log::nocolor = NUKLEI_NOCOLOR;
21 
22  const std::string Log::TypeNames[] = { "NEVER", "FATAL", "ERROR", "WARN", "INFO", "LOG", "DEBUG" };
23 
24  void Log::setOutput(const std::string &filename)
25  {
26  delete outInstance;
27  outInstance = NULL;
28 
29  outInstance = new std::ofstream(filename.c_str(), std::ios::out);
30  out = outInstance;
31  msgColor = "";
32  errorColor = "";
33  nocolor = "";
34  }
35 
36  void Log::setOutput(std::ostream *stream)
37  {
38  delete outInstance;
39  outInstance = NULL;
40  out = stream;
41  }
42 
43  void Log::log(const char* file,
44  int line,
45  unsigned level,
46  const std::string &s)
47  {
48  boost::unique_lock<boost::mutex> lock(mutex_);
49  LAST_OUTPUT_LINE_IS_PROGRESS = false;
50  std::string color_start, color_stop;
51  if (INTERACTIVE_SHELL)
52  {
53  if (level <= WARN) color_start = errorColor;
54  else if (level <= LOG) color_start = msgColor;
55  else color_start = nocolor;
56  color_stop = nocolor;
57  }
58  stream() << color_start << file << ":" << line <<
59  " " << color_stop << TypeNames[level] << " - " <<
60  color_start << breakLines(s) <<
61  color_stop << std::endl;
62  }
63 
64  std::string Log::breakLines(const std::string &s)
65  {
66  std::string input = "\n" + s + "\n";
67  std::string output, line, token;
68 
69  output.reserve(input.size());
70 
71  // Paragraph indent (ideally set to the width of the log annoucement)
72  const std::string lineBreak = "\n ";
73 
74  // Context indent (indent defined in the message)
75  std::string ctxIndent;
76 
77  std::string indent;
78 
79  const unsigned int width = 80;
80 
81  for (std::string::const_iterator i = input.begin();
82  i != input.end();)
83  {
84  if (*i == ' ')
85  {
86  if (token.size() == 0)
87  {
88  }
89  else if (line.size() == 0)
90  {
91  line = token;
92  token = "";
93  }
94  else if (line.size() + 1 + token.size() < width - indent.size())
95  {
96  line.append( " " + token );
97  token = "";
98  }
99  else if (line.size() + 1 + token.size() == width - indent.size())
100  {
101  output.append( indent + line + " " + token);
102  line = "";
103  token = "";
104  }
105  else if (line.size() + 1 + token.size() > width - indent.size())
106  {
107  if (line.size() > 0)
108  {
109  output.append( indent + line );
110  line = "";
111  }
112  if (token.size() >= width)
113  {
114  output.append( indent + token );
115  token = "";
116  }
117  else
118  {
119  line = token;
120  token = "";
121  }
122  }
123  }
124  else if (*i == '\n')
125  {
126  if (line.size() == 0 && token.size() != 0)
127  output.append(indent + token);
128  else if (line.size() + 1 + token.size() <= width - indent.size())
129  {
130  output.append( indent + line + " " + token );
131  }
132  else
133  {
134  output.append( indent + line + indent + token );
135  }
136  line = "";
137  token = "";
138  ctxIndent = "";
139  for (i++; i != input.end(); i++)
140  {
141  if (*i == ' ') ctxIndent.append(" ");
142  else
143  {
144  indent = lineBreak + ctxIndent;
145  break;
146  }
147  }
148  line = "";
149  continue;
150  }
151  else
152  {
153  token += *i;
154  }
155  i++;
156  }
157 
158  return output;
159  }
160 
161 }
162 
Public namespace.
Definition: Color.cpp:9
© Copyright 2007-2013 Renaud Detry.
Distributed under the terms of the GNU General Public License (GPL).
(See accompanying file LICENSE.txt or copy at http://www.gnu.org/copyleft/gpl.html.)
Revised Sun Sep 13 2020 19:10:06.