Logger.cpp
Go to the documentation of this file.
1 /*
2  * Karto (tm) Robot Navigation Software - Software Development Kit
3  * Release 2.1
4  *
5  * Copyright (C) 2006-2011, SRI International (R)
6  *
7  * The material contained in this release is copyrighted. It may not be copied,
8  * reproduced, translated, reverse engineered, modified or reduced to any electronic
9  * medium or machine-readable form without the prior written consent of
10  * SRI International (R).
11  *
12  * Portions of files in this release may be unpublished work
13  * containing SRI International (R) CONFIDENTIAL AND PROPRIETARY INFORMATION.
14  * Disclosure, use, reverse engineering, modification, or reproduction without
15  * written authorization of SRI International (R) is likewise prohibited.
16  *
17  * Karto (tm) is a Trademark of SRI International (R).
18  *
19  * Author(s): Michael A. Eriksen (eriksen@ai.sri.com)
20  */
21 
22 #include <iostream>
23 
24 #ifdef USE_POCO
25 #include <Poco/Logger.h>
26 #include <Poco/LogStream.h>
27 #include <Poco/PatternFormatter.h>
28 #include <Poco/FormattingChannel.h>
29 #include <Poco/ConsoleChannel.h>
30 #include <Poco/FileChannel.h>
31 #include <Poco/TemporaryFile.h>
32 #include <Poco/Process.h>
33 #include <Poco/Path.h>
34 #endif
35 
36 #include <OpenKarto/Logger.h>
37 
38 namespace karto
39 {
40 
41  static kt_bool s_FileLoggingEnabled = false;
43 
45 
46  void InitializeLogger(const String& rApplicationName, const String& rLogLevel)
47  {
48  LogLevel logLevel = LOG_NONE;
49 
50  if (rLogLevel.Find("NONE") != -1)
51  {
52  logLevel = LOG_NONE;
53  }
54  else if (rLogLevel.Find("FATAL") != -1)
55  {
56  logLevel = LOG_FATAL;
57  }
58  else if (rLogLevel.Find("ERROR") != -1)
59  {
60  logLevel = LOG_ERROR;
61  }
62  else if (rLogLevel.Find("WARNING") != -1)
63  {
64  logLevel = LOG_WARNING;
65  }
66  else if (rLogLevel.Find("INFORMATION") != -1)
67  {
68  logLevel = LOG_INFORMATION;
69  }
70  else if (rLogLevel.Find("DEBUG") != -1)
71  {
72  logLevel = LOG_DEBUG;
73  }
74  else
75  {
76  std::cerr << "Warning: invalid KARTO_LOG_LEVEL [" << rLogLevel << "] using NONE as default!"<<std::endl;
77  }
78 
79  karto::String logName;
80  if (logLevel != LOG_NONE)
81  {
82  if (s_FileLoggingEnabled == true)
83  {
84  // create log filename
85  logName = rApplicationName + ".log";
86 
87 #ifdef USE_POCO
88  {
89  // test if we have write permission!!
90  try
91  {
92  std::ostringstream name;
93  unsigned long n = 1;
94 
95  name << "tmp" << Poco::Process::id();
96  for (int i = 0; i < 6; ++i)
97  {
98  name << char('a' + (n % 26));
99  n /= 26;
100  }
101 
102  Poco::File dummyFile(Poco::Path::current() + name.str());
103 
104  // check that we actually can write!
105  dummyFile.createFile();
106  dummyFile.remove();
107  s_FileLoggingEnabled = true;
108  }
109  catch (Poco::FileAccessDeniedException error)
110  {
111  s_FileLoggingEnabled = false;
112  }
113  }
114  }
115 
116  // create loggers
117  Poco::FormattingChannel* pFCConsole = new Poco::FormattingChannel(new Poco::PatternFormatter("%t"));
118  pFCConsole->setChannel(new Poco::ConsoleChannel);
119  pFCConsole->open();
120 
121  Poco::FormattingChannel* pFCFile = NULL;
122  if (s_FileLoggingEnabled == true)
123  {
124  Poco::PatternFormatter* pFormatter = new Poco::PatternFormatter("%Y-%m-%d %H:%M:%S.%c %N[%P]:%q:%t");
125  pFormatter->setProperty("times", "local");
126  pFCFile = new Poco::FormattingChannel(pFormatter);
127  pFCFile->setChannel(new Poco::FileChannel(logName.ToCString()));
128  pFCFile->open();
129  }
130 
131 #ifdef _DEBUG
132  Poco::Logger::create("ConsoleLogger", pFCConsole, Poco::Message::PRIO_DEBUG);
133  if (pFCFile)
134  {
135  Poco::Logger::create("FileLogger", pFCFile, Poco::Message::PRIO_DEBUG);
136  }
137 #else
138  Poco::Logger::create("ConsoleLogger", pFCConsole, Poco::Message::PRIO_INFORMATION);
139  if (pFCFile)
140  {
141  Poco::Logger::create("FileLogger", pFCFile, Poco::Message::PRIO_INFORMATION);
142  }
143 #endif // _DEBUG
144 #else
145  }
146 #endif // USE_POCO
147  }
148 
149  SetLogLevel(logLevel);
150 
151  if (logLevel != LOG_NONE)
152  {
153  if (s_FileLoggingEnabled)
154  {
155  Log(LOG_DEBUG, "Karto created log file: " + logName);
156  }
157  else
158  {
159  if (logName != "")
160  Log(LOG_INFORMATION, "Karto unable to create log file [" + logName + "]. No write permission to log directory.");
161  }
162  }
163  }
164 
165  void SetLogLevel(LogLevel level)
166  {
167  s_LogLevel = level;
168 
169  if (s_LogLevel != LOG_NONE)
170  {
171 #ifdef USE_POCO
172  Poco::Logger::get("ConsoleLogger").setLevel(level);
173  Poco::Logger::get("FileLogger").setLevel(level);
174 #endif // USE_POCO
175  }
176  }
177 
179  {
180  return s_LogLevel;
181  }
182 
183  void Log(LogLevel level, const karto::String& rMessage)
184  {
185  if (s_LogLevel != LOG_NONE)
186  {
187 #ifdef USE_POCO
188  if (level == LOG_FATAL)
189  {
190  Poco::Logger::get("ConsoleLogger").fatal(rMessage.ToCString());
191  Poco::Logger::get("FileLogger").fatal(rMessage.ToCString());
192  }
193  else if (level == LOG_ERROR)
194  {
195  Poco::Logger::get("ConsoleLogger").error(rMessage.ToCString());
196  Poco::Logger::get("FileLogger").error(rMessage.ToCString());
197  }
198  else if (level == LOG_WARNING)
199  {
200  Poco::Logger::get("ConsoleLogger").warning(rMessage.ToCString());
201  Poco::Logger::get("FileLogger").warning(rMessage.ToCString());
202  }
203  else if (level == LOG_INFORMATION)
204  {
205  Poco::Logger::get("ConsoleLogger").information(rMessage.ToCString());
206  Poco::Logger::get("FileLogger").information(rMessage.ToCString());
207  }
208  else if (level == LOG_DEBUG)
209  {
210  Poco::Logger::get("ConsoleLogger").debug(rMessage.ToCString());
211  Poco::Logger::get("FileLogger").debug(rMessage.ToCString());
212  }
213  else
214  {
215  Poco::Logger::get("ConsoleLogger").information(rMessage.ToCString());
216  Poco::Logger::get("FileLogger").information(rMessage.ToCString());
217  }
218 #else
219  std::cout << "Warning OpenKarto is compiled without POCO, so no logging enabled! Compile with POCO and define USE_POCO to enable logging." << std::endl;
220  std::cout << rMessage << std::endl;
221 #endif // USE_POCO
222 
223  LogMessageArguments eventArguments(level, rMessage);
224  LogMessage.Notify(NULL, eventArguments);
225  }
226  }
227 
229  {
230  if (s_LogLevel != LOG_NONE)
231  {
232 #ifdef USE_POCO
233  Poco::Logger::get("ConsoleLogger").close();
234 
235  Poco::Channel* pChannel = Poco::Logger::get("FileLogger").getChannel();
236  if (pChannel != NULL)
237  {
238  pChannel->close();
239  }
240 
241  Poco::Logger::get("ConsoleLogger").shutdown();
242  Poco::Logger::get("FileLogger").shutdown();
243  Poco::Logger::shutdown();
244 #endif // USE_POCO
245  }
246  }
247 
248 }
LogLevel GetLogLevel()
Definition: Logger.cpp:178
bool kt_bool
Definition: Types.h:145
void Log(LogLevel level, const karto::String &rMessage)
Definition: Logger.cpp:183
LogLevel
Definition: Logger.h:43
void TerminateLogger()
Definition: Logger.cpp:228
static LogLevel s_LogLevel
Definition: Logger.cpp:42
BasicEvent< LogMessageArguments > LogMessage
Definition: Logger.cpp:44
void SetLogLevel(LogLevel level)
Definition: Logger.cpp:165
void InitializeLogger(const String &rApplicationName, const String &rLogLevel)
Definition: Logger.cpp:46
kt_size_t Find(const String &rValue) const
Definition: String.cpp:97
Definition: Any.cpp:20
const char * ToCString() const
Definition: String.cpp:72
static kt_bool s_FileLoggingEnabled
Definition: Logger.cpp:41


nav2d_karto
Author(s): Sebastian Kasperski
autogenerated on Tue Nov 7 2017 06:02:36