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> 50 if (rLogLevel.
Find(
"NONE") != -1)
54 else if (rLogLevel.
Find(
"FATAL") != -1)
58 else if (rLogLevel.
Find(
"ERROR") != -1)
62 else if (rLogLevel.
Find(
"WARNING") != -1)
66 else if (rLogLevel.
Find(
"INFORMATION") != -1)
70 else if (rLogLevel.
Find(
"DEBUG") != -1)
76 std::cerr <<
"Warning: invalid KARTO_LOG_LEVEL [" << rLogLevel <<
"] using NONE as default!"<<std::endl;
82 if (s_FileLoggingEnabled ==
true)
85 logName = rApplicationName +
".log";
92 std::ostringstream name;
95 name <<
"tmp" << Poco::Process::id();
96 for (
int i = 0; i < 6; ++i)
98 name << char(
'a' + (n % 26));
102 Poco::File dummyFile(Poco::Path::current() + name.str());
105 dummyFile.createFile();
107 s_FileLoggingEnabled =
true;
109 catch (Poco::FileAccessDeniedException error)
111 s_FileLoggingEnabled =
false;
117 Poco::FormattingChannel* pFCConsole =
new Poco::FormattingChannel(
new Poco::PatternFormatter(
"%t"));
118 pFCConsole->setChannel(
new Poco::ConsoleChannel);
121 Poco::FormattingChannel* pFCFile = NULL;
122 if (s_FileLoggingEnabled ==
true)
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()));
132 Poco::Logger::create(
"ConsoleLogger", pFCConsole, Poco::Message::PRIO_DEBUG);
135 Poco::Logger::create(
"FileLogger", pFCFile, Poco::Message::PRIO_DEBUG);
138 Poco::Logger::create(
"ConsoleLogger", pFCConsole, Poco::Message::PRIO_INFORMATION);
141 Poco::Logger::create(
"FileLogger", pFCFile, Poco::Message::PRIO_INFORMATION);
153 if (s_FileLoggingEnabled)
160 Log(
LOG_INFORMATION,
"Karto unable to create log file [" + logName +
"]. No write permission to log directory.");
172 Poco::Logger::get(
"ConsoleLogger").setLevel(level);
173 Poco::Logger::get(
"FileLogger").setLevel(level);
190 Poco::Logger::get(
"ConsoleLogger").fatal(rMessage.
ToCString());
191 Poco::Logger::get(
"FileLogger").fatal(rMessage.
ToCString());
195 Poco::Logger::get(
"ConsoleLogger").error(rMessage.
ToCString());
196 Poco::Logger::get(
"FileLogger").error(rMessage.
ToCString());
200 Poco::Logger::get(
"ConsoleLogger").warning(rMessage.
ToCString());
201 Poco::Logger::get(
"FileLogger").warning(rMessage.
ToCString());
205 Poco::Logger::get(
"ConsoleLogger").information(rMessage.
ToCString());
206 Poco::Logger::get(
"FileLogger").information(rMessage.
ToCString());
210 Poco::Logger::get(
"ConsoleLogger").debug(rMessage.
ToCString());
211 Poco::Logger::get(
"FileLogger").debug(rMessage.
ToCString());
215 Poco::Logger::get(
"ConsoleLogger").information(rMessage.
ToCString());
216 Poco::Logger::get(
"FileLogger").information(rMessage.
ToCString());
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;
233 Poco::Logger::get(
"ConsoleLogger").close();
235 Poco::Channel* pChannel = Poco::Logger::get(
"FileLogger").getChannel();
236 if (pChannel != NULL)
241 Poco::Logger::get(
"ConsoleLogger").shutdown();
242 Poco::Logger::get(
"FileLogger").shutdown();
243 Poco::Logger::shutdown();
void Log(LogLevel level, const karto::String &rMessage)
static LogLevel s_LogLevel
BasicEvent< LogMessageArguments > LogMessage
void SetLogLevel(LogLevel level)
void InitializeLogger(const String &rApplicationName, const String &rLogLevel)
kt_size_t Find(const String &rValue) const
const char * ToCString() const
static kt_bool s_FileLoggingEnabled