|
| 1 | +/* |
| 2 | + * Copyright (c) 2021 Florian Becker <[email protected]> (VX APPS). |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions are met: |
| 7 | + * |
| 8 | + * 1. Redistributions of source code must retain the above copyright notice, this |
| 9 | + * list of conditions and the following disclaimer. |
| 10 | + * |
| 11 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 12 | + * this list of conditions and the following disclaimer in the documentation |
| 13 | + * and/or other materials provided with the distribution. |
| 14 | + * |
| 15 | + * 3. Neither the name of the copyright holder nor the names of its |
| 16 | + * contributors may be used to endorse or promote products derived from |
| 17 | + * this software without specific prior written permission. |
| 18 | + * |
| 19 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 20 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 21 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 22 | + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 23 | + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 24 | + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 25 | + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 26 | + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 27 | + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | + */ |
| 30 | + |
| 31 | +/* stl header */ |
| 32 | +#include <filesystem> |
| 33 | +#include <iostream> |
| 34 | +#include <map> |
| 35 | +#include <string> |
| 36 | + |
| 37 | +/* modern.cpp.logger */ |
| 38 | +#include <LoggerFactory.h> |
| 39 | + |
| 40 | +/** |
| 41 | + * @brief Filename of temporary log file. |
| 42 | + */ |
| 43 | +constexpr auto filename = "example.log"; |
| 44 | + |
| 45 | +/** |
| 46 | + * @brief Log message itself. |
| 47 | + */ |
| 48 | +constexpr auto logMessage = "This is a log message"; |
| 49 | + |
| 50 | +/** |
| 51 | + * @brief Function for log some messages. |
| 52 | + */ |
| 53 | +static void logSomeMessages() { |
| 54 | + |
| 55 | + LogFatal( logMessage ); |
| 56 | + LogError( logMessage ); |
| 57 | + LogWarning( logMessage ); |
| 58 | + LogInfo( logMessage ); |
| 59 | + LogDebug( logMessage ); |
| 60 | + LogVerbose( logMessage ); |
| 61 | +} |
| 62 | + |
| 63 | +int main() { |
| 64 | + |
| 65 | + /* configure logging, if you dont it defaults to standard out logging with colors */ |
| 66 | + std::cout << "Log to std::cout" << std::endl; |
| 67 | + ConfigureLogger( { { "type", "std" }, { "color", "true" } } ); |
| 68 | + |
| 69 | + /* Log some messages */ |
| 70 | + logSomeMessages(); |
| 71 | + |
| 72 | + /* create tmp file */ |
| 73 | + std::filesystem::path tmpPath = std::filesystem::temp_directory_path(); |
| 74 | + tmpPath /= filename; |
| 75 | + std::string tmpFile = tmpPath.string(); |
| 76 | + std::cout << "Create tmp file: " << tmpFile << std::endl; |
| 77 | + |
| 78 | + /* configure logging, if you dont it defaults to standard out logging with colors */ |
| 79 | + std::cout << "Log to file" << std::endl; |
| 80 | + ConfigureLogger( { { "type", "file" }, { "filename", tmpFile }, { "reopen_interval", "1" } } ); |
| 81 | + |
| 82 | + /* Log some messages */ |
| 83 | + logSomeMessages(); |
| 84 | + |
| 85 | + /* remove tmp file */ |
| 86 | + bool removed = std::filesystem::remove( tmpFile ); |
| 87 | + if ( !removed ) { |
| 88 | + |
| 89 | + std::cout << "Tmp file cannot be removed: " << tmpFile << std::endl; |
| 90 | + return EXIT_FAILURE; |
| 91 | + } |
| 92 | + std::cout << "Tmp file was removed: " << tmpFile << std::endl; |
| 93 | + return EXIT_SUCCESS; |
| 94 | + |
| 95 | + return EXIT_SUCCESS; |
| 96 | +} |
0 commit comments