| 1 | // |
| 2 | // Logger.cpp |
| 3 | // |
| 4 | // This class demonstrates the Logger, PatternFormatter, FormattingChannel, |
| 5 | // ConsoleChannel and FileChannel classes. |
| 6 | // |
| 7 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
| 8 | // and Contributors. |
| 9 | // |
| 10 | // SPDX-License-Identifier: BSL-1.0 |
| 11 | // |
| 12 | |
| 13 | |
| 14 | #include "Poco/Logger.h" |
| 15 | #include "Poco/AutoPtr.h" |
| 16 | #include "Poco/PatternFormatter.h" |
| 17 | #include "Poco/FormattingChannel.h" |
| 18 | #include "Poco/ConsoleChannel.h" |
| 19 | #include "Poco/FileChannel.h" |
| 20 | #include "Poco/Message.h" |
| 21 | |
| 22 | |
| 23 | using Poco::Logger; |
| 24 | using Poco::AutoPtr; |
| 25 | using Poco::PatternFormatter; |
| 26 | using Poco::FormattingChannel; |
| 27 | using Poco::ConsoleChannel; |
| 28 | using Poco::FileChannel; |
| 29 | using Poco::Message; |
| 30 | |
| 31 | |
| 32 | int main(int argc, char** argv) |
| 33 | { |
| 34 | // set up two channel chains - one to the |
| 35 | // console and the other one to a log file. |
| 36 | AutoPtr<PatternFormatter> pPatternFormatter(new PatternFormatter("[%O] %s: %p: %t" )); |
| 37 | AutoPtr<FormattingChannel> pFCConsole(new FormattingChannel(pPatternFormatter)); |
| 38 | AutoPtr<ConsoleChannel> pConsoleChannel(new ConsoleChannel()); |
| 39 | pFCConsole->setChannel(pConsoleChannel); |
| 40 | pFCConsole->open(); |
| 41 | |
| 42 | AutoPtr<PatternFormatter> pPatternFormatter2(new PatternFormatter("%Y-%m-%d %H:%M:%S.%c %N[%P]:%s:%q:%t" )); |
| 43 | AutoPtr<FormattingChannel> pFCFile(new FormattingChannel(pPatternFormatter2)); |
| 44 | AutoPtr<FileChannel> pFileChannel(new FileChannel("sample.log" )); |
| 45 | pFCFile->setChannel(pFileChannel); |
| 46 | pFCFile->open(); |
| 47 | |
| 48 | // create two Logger objects - one for |
| 49 | // each channel chain. |
| 50 | Logger& consoleLogger = Logger::create("ConsoleLogger" , pFCConsole, Message::PRIO_INFORMATION); |
| 51 | Logger& fileLogger = Logger::create("FileLogger" , pFCFile, Message::PRIO_WARNING); |
| 52 | |
| 53 | // log some messages |
| 54 | consoleLogger.error("An error message" ); |
| 55 | fileLogger.error("An error message" ); |
| 56 | |
| 57 | consoleLogger.warning("A warning message" ); |
| 58 | fileLogger.error("A warning message" ); |
| 59 | |
| 60 | consoleLogger.information("An information message" ); |
| 61 | fileLogger.information("An information message" ); |
| 62 | |
| 63 | poco_information(consoleLogger, "Another informational message" ); |
| 64 | poco_warning_f2(consoleLogger, "A warning message with arguments: %d, %d" , 1, 2); |
| 65 | |
| 66 | Logger::get("ConsoleLogger" ).error("Another error message" ); |
| 67 | Logger::shutdown(); |
| 68 | |
| 69 | return 0; |
| 70 | } |
| 71 | |