Logger.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. //
  2. // Logger.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/src/Logger.cpp#4 $
  5. //
  6. // Library: Foundation
  7. // Package: Logging
  8. // Module: Logger
  9. //
  10. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #include "Poco/Logger.h"
  16. #include "Poco/Formatter.h"
  17. #include "Poco/LoggingRegistry.h"
  18. #include "Poco/Exception.h"
  19. #include "Poco/NumberFormatter.h"
  20. #include "Poco/NumberParser.h"
  21. #include "Poco/String.h"
  22. namespace Poco {
  23. Logger::LoggerMap* Logger::_pLoggerMap = 0;
  24. Mutex Logger::_mapMtx;
  25. const std::string Logger::ROOT;
  26. Logger::Logger(const std::string& name, Channel* pChannel, int level): _name(name), _pChannel(pChannel), _level(level)
  27. {
  28. if (pChannel) pChannel->duplicate();
  29. }
  30. Logger::~Logger()
  31. {
  32. if (_pChannel) _pChannel->release();
  33. }
  34. void Logger::setChannel(Channel* pChannel)
  35. {
  36. if (_pChannel) _pChannel->release();
  37. _pChannel = pChannel;
  38. if (_pChannel) _pChannel->duplicate();
  39. }
  40. Channel* Logger::getChannel() const
  41. {
  42. return _pChannel;
  43. }
  44. void Logger::setLevel(int level)
  45. {
  46. _level = level;
  47. }
  48. void Logger::setLevel(const std::string& level)
  49. {
  50. setLevel(parseLevel(level));
  51. }
  52. void Logger::setProperty(const std::string& name, const std::string& value)
  53. {
  54. if (name == "channel")
  55. setChannel(LoggingRegistry::defaultRegistry().channelForName(value));
  56. else if (name == "level")
  57. setLevel(value);
  58. else
  59. Channel::setProperty(name, value);
  60. }
  61. void Logger::log(const Message& msg)
  62. {
  63. if (_level >= msg.getPriority() && _pChannel)
  64. {
  65. _pChannel->log(msg);
  66. }
  67. }
  68. void Logger::log(const Exception& exc)
  69. {
  70. error(exc.displayText());
  71. }
  72. void Logger::log(const Exception& exc, const char* file, int line)
  73. {
  74. error(exc.displayText(), file, line);
  75. }
  76. void Logger::dump(const std::string& msg, const void* buffer, std::size_t length, Message::Priority prio)
  77. {
  78. if (_level >= prio && _pChannel)
  79. {
  80. std::string text(msg);
  81. formatDump(text, buffer, length);
  82. _pChannel->log(Message(_name, text, prio));
  83. }
  84. }
  85. void Logger::setLevel(const std::string& name, int level)
  86. {
  87. Mutex::ScopedLock lock(_mapMtx);
  88. if (_pLoggerMap)
  89. {
  90. std::string::size_type len = name.length();
  91. for (LoggerMap::iterator it = _pLoggerMap->begin(); it != _pLoggerMap->end(); ++it)
  92. {
  93. if (len == 0 ||
  94. (it->first.compare(0, len, name) == 0 && (it->first.length() == len || it->first[len] == '.')))
  95. {
  96. it->second->setLevel(level);
  97. }
  98. }
  99. }
  100. }
  101. void Logger::setChannel(const std::string& name, Channel* pChannel)
  102. {
  103. Mutex::ScopedLock lock(_mapMtx);
  104. if (_pLoggerMap)
  105. {
  106. std::string::size_type len = name.length();
  107. for (LoggerMap::iterator it = _pLoggerMap->begin(); it != _pLoggerMap->end(); ++it)
  108. {
  109. if (len == 0 ||
  110. (it->first.compare(0, len, name) == 0 && (it->first.length() == len || it->first[len] == '.')))
  111. {
  112. it->second->setChannel(pChannel);
  113. }
  114. }
  115. }
  116. }
  117. void Logger::setProperty(const std::string& loggerName, const std::string& propertyName, const std::string& value)
  118. {
  119. Mutex::ScopedLock lock(_mapMtx);
  120. if (_pLoggerMap)
  121. {
  122. std::string::size_type len = loggerName.length();
  123. for (LoggerMap::iterator it = _pLoggerMap->begin(); it != _pLoggerMap->end(); ++it)
  124. {
  125. if (len == 0 ||
  126. (it->first.compare(0, len, loggerName) == 0 && (it->first.length() == len || it->first[len] == '.')))
  127. {
  128. it->second->setProperty(propertyName, value);
  129. }
  130. }
  131. }
  132. }
  133. std::string Logger::format(const std::string& fmt, const std::string& arg)
  134. {
  135. std::string args[] =
  136. {
  137. arg
  138. };
  139. return format(fmt, 1, args);
  140. }
  141. std::string Logger::format(const std::string& fmt, const std::string& arg0, const std::string& arg1)
  142. {
  143. std::string args[] =
  144. {
  145. arg0,
  146. arg1
  147. };
  148. return format(fmt, 2, args);
  149. }
  150. std::string Logger::format(const std::string& fmt, const std::string& arg0, const std::string& arg1, const std::string& arg2)
  151. {
  152. std::string args[] =
  153. {
  154. arg0,
  155. arg1,
  156. arg2
  157. };
  158. return format(fmt, 3, args);
  159. }
  160. std::string Logger::format(const std::string& fmt, const std::string& arg0, const std::string& arg1, const std::string& arg2, const std::string& arg3)
  161. {
  162. std::string args[] =
  163. {
  164. arg0,
  165. arg1,
  166. arg2,
  167. arg3
  168. };
  169. return format(fmt, 4, args);
  170. }
  171. std::string Logger::format(const std::string& fmt, int argc, std::string argv[])
  172. {
  173. std::string result;
  174. std::string::const_iterator it = fmt.begin();
  175. while (it != fmt.end())
  176. {
  177. if (*it == '$')
  178. {
  179. ++it;
  180. if (*it == '$')
  181. {
  182. result += '$';
  183. }
  184. else if (*it >= '0' && *it <= '9')
  185. {
  186. int i = *it - '0';
  187. if (i < argc)
  188. result += argv[i];
  189. }
  190. else
  191. {
  192. result += '$';
  193. result += *it;
  194. }
  195. }
  196. else result += *it;
  197. ++it;
  198. }
  199. return result;
  200. }
  201. void Logger::formatDump(std::string& message, const void* buffer, std::size_t length)
  202. {
  203. const int BYTES_PER_LINE = 16;
  204. message.reserve(message.size() + length*6);
  205. if (!message.empty()) message.append("\n");
  206. unsigned char* base = (unsigned char*) buffer;
  207. int addr = 0;
  208. while (addr < length)
  209. {
  210. if (addr > 0) message.append("\n");
  211. message.append(NumberFormatter::formatHex(addr, 4));
  212. message.append(" ");
  213. int offset = 0;
  214. while (addr + offset < length && offset < BYTES_PER_LINE)
  215. {
  216. message.append(NumberFormatter::formatHex(base[addr + offset], 2));
  217. message.append(offset == 7 ? " " : " ");
  218. ++offset;
  219. }
  220. if (offset < 7) message.append(" ");
  221. while (offset < BYTES_PER_LINE) { message.append(" "); ++offset; }
  222. message.append(" ");
  223. offset = 0;
  224. while (addr + offset < length && offset < BYTES_PER_LINE)
  225. {
  226. unsigned char c = base[addr + offset];
  227. message += (c >= 32 && c < 127) ? (char) c : '.';
  228. ++offset;
  229. }
  230. addr += BYTES_PER_LINE;
  231. }
  232. }
  233. Logger& Logger::get(const std::string& name)
  234. {
  235. Mutex::ScopedLock lock(_mapMtx);
  236. return unsafeGet(name);
  237. }
  238. Logger& Logger::unsafeGet(const std::string& name)
  239. {
  240. Logger* pLogger = find(name);
  241. if (!pLogger)
  242. {
  243. if (name == ROOT)
  244. {
  245. pLogger = new Logger(name, 0, Message::PRIO_INFORMATION);
  246. }
  247. else
  248. {
  249. Logger& par = parent(name);
  250. pLogger = new Logger(name, par.getChannel(), par.getLevel());
  251. }
  252. add(pLogger);
  253. }
  254. return *pLogger;
  255. }
  256. Logger& Logger::create(const std::string& name, Channel* pChannel, int level)
  257. {
  258. Mutex::ScopedLock lock(_mapMtx);
  259. if (find(name)) throw ExistsException();
  260. Logger* pLogger = new Logger(name, pChannel, level);
  261. add(pLogger);
  262. return *pLogger;
  263. }
  264. Logger& Logger::root()
  265. {
  266. Mutex::ScopedLock lock(_mapMtx);
  267. return unsafeGet(ROOT);
  268. }
  269. Logger* Logger::has(const std::string& name)
  270. {
  271. Mutex::ScopedLock lock(_mapMtx);
  272. return find(name);
  273. }
  274. void Logger::shutdown()
  275. {
  276. Mutex::ScopedLock lock(_mapMtx);
  277. if (_pLoggerMap)
  278. {
  279. for (LoggerMap::iterator it = _pLoggerMap->begin(); it != _pLoggerMap->end(); ++it)
  280. {
  281. it->second->release();
  282. }
  283. delete _pLoggerMap;
  284. _pLoggerMap = 0;
  285. }
  286. }
  287. Logger* Logger::find(const std::string& name)
  288. {
  289. if (_pLoggerMap)
  290. {
  291. LoggerMap::iterator it = _pLoggerMap->find(name);
  292. if (it != _pLoggerMap->end())
  293. return it->second;
  294. }
  295. return 0;
  296. }
  297. void Logger::destroy(const std::string& name)
  298. {
  299. Mutex::ScopedLock lock(_mapMtx);
  300. if (_pLoggerMap)
  301. {
  302. LoggerMap::iterator it = _pLoggerMap->find(name);
  303. if (it != _pLoggerMap->end())
  304. {
  305. it->second->release();
  306. _pLoggerMap->erase(it);
  307. }
  308. }
  309. }
  310. void Logger::names(std::vector<std::string>& names)
  311. {
  312. Mutex::ScopedLock lock(_mapMtx);
  313. names.clear();
  314. if (_pLoggerMap)
  315. {
  316. for (LoggerMap::const_iterator it = _pLoggerMap->begin(); it != _pLoggerMap->end(); ++it)
  317. {
  318. names.push_back(it->first);
  319. }
  320. }
  321. }
  322. Logger& Logger::parent(const std::string& name)
  323. {
  324. std::string::size_type pos = name.rfind('.');
  325. if (pos != std::string::npos)
  326. {
  327. std::string pname = name.substr(0, pos);
  328. Logger* pParent = find(pname);
  329. if (pParent)
  330. return *pParent;
  331. else
  332. return parent(pname);
  333. }
  334. else return unsafeGet(ROOT);
  335. }
  336. int Logger::parseLevel(const std::string& level)
  337. {
  338. if (icompare(level, "none") == 0)
  339. return 0;
  340. else if (icompare(level, "fatal") == 0)
  341. return Message::PRIO_FATAL;
  342. else if (icompare(level, "critical") == 0)
  343. return Message::PRIO_CRITICAL;
  344. else if (icompare(level, "error") == 0)
  345. return Message::PRIO_ERROR;
  346. else if (icompare(level, "warning") == 0)
  347. return Message::PRIO_WARNING;
  348. else if (icompare(level, "notice") == 0)
  349. return Message::PRIO_NOTICE;
  350. else if (icompare(level, "information") == 0)
  351. return Message::PRIO_INFORMATION;
  352. else if (icompare(level, "debug") == 0)
  353. return Message::PRIO_DEBUG;
  354. else if (icompare(level, "trace") == 0)
  355. return Message::PRIO_TRACE;
  356. else
  357. {
  358. int numLevel;
  359. if (Poco::NumberParser::tryParse(level, numLevel))
  360. {
  361. if (numLevel > 0 && numLevel < 9)
  362. return numLevel;
  363. else
  364. throw InvalidArgumentException("Log level out of range ", level);
  365. }
  366. else
  367. throw InvalidArgumentException("Not a valid log level", level);
  368. }
  369. }
  370. class AutoLoggerShutdown
  371. {
  372. public:
  373. AutoLoggerShutdown()
  374. {
  375. }
  376. ~AutoLoggerShutdown()
  377. {
  378. try
  379. {
  380. Logger::shutdown();
  381. }
  382. catch (...)
  383. {
  384. poco_unexpected();
  385. }
  386. }
  387. };
  388. namespace
  389. {
  390. static AutoLoggerShutdown als;
  391. }
  392. void Logger::add(Logger* pLogger)
  393. {
  394. if (!_pLoggerMap)
  395. _pLoggerMap = new LoggerMap;
  396. _pLoggerMap->insert(LoggerMap::value_type(pLogger->name(), pLogger));
  397. }
  398. } // namespace Poco