Line data Source code
1 : /*
2 : * Copyright (c) 2006-2021 Made to Order Software Corp. All Rights Reserved
3 : *
4 : * https://snapwebsites.org/project/snaplogger
5 : * contact@m2osw.com
6 : *
7 : * This program is free software; you can redistribute it and/or modify
8 : * it under the terms of the GNU General Public License as published by
9 : * the Free Software Foundation; either version 2 of the License, or
10 : * (at your option) any later version.
11 : *
12 : * This program is distributed in the hope that it will be useful,
13 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 : * GNU General Public License for more details.
16 : *
17 : * You should have received a copy of the GNU General Public License along
18 : * with this program; if not, write to the Free Software Foundation, Inc.,
19 : * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 : */
21 :
22 : // self
23 : //
24 : #include "main.h"
25 :
26 :
27 : // snaplogger lib
28 : //
29 : #include <snaplogger/buffer_appender.h>
30 : #include <snaplogger/exception.h>
31 : #include <snaplogger/format.h>
32 : #include <snaplogger/logger.h>
33 : #include <snaplogger/map_diagnostic.h>
34 : #include <snaplogger/message.h>
35 : #include <snaplogger/severity.h>
36 : #include <snaplogger/version.h>
37 :
38 :
39 : // C lib
40 : //
41 : #include <unistd.h>
42 : #include <netdb.h>
43 : #include <sys/param.h>
44 :
45 :
46 :
47 :
48 :
49 3 : CATCH_TEST_CASE("appender", "[appender]")
50 : {
51 2 : CATCH_START_SECTION("Create Appender")
52 : {
53 1 : snaplogger::set_diagnostic(snaplogger::DIAG_KEY_PROGNAME, "appender");
54 :
55 2 : snaplogger::appender::pointer_t unknown(snaplogger::create_appender("unknown", "test-buffer"));
56 1 : CATCH_REQUIRE(unknown == nullptr);
57 :
58 2 : snaplogger::appender::pointer_t buffer(snaplogger::create_appender("buffer", "test-buffer"));
59 1 : CATCH_REQUIRE(buffer != nullptr);
60 :
61 1 : char const * cargv[] =
62 : {
63 : "/usr/bin/daemon",
64 : nullptr
65 : };
66 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
67 1 : char ** argv = const_cast<char **>(cargv);
68 :
69 1 : advgetopt::options_environment environment_options;
70 1 : environment_options.f_project_name = "test-logger";
71 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
72 2 : advgetopt::getopt opts(environment_options);
73 1 : opts.parse_program_name(argv);
74 1 : opts.parse_arguments(argc, argv, advgetopt::option_source_t::SOURCE_COMMAND_LINE);
75 1 : buffer->set_config(opts);
76 :
77 2 : snaplogger::format::pointer_t f(std::make_shared<snaplogger::format>("${severity}: ${message}"));
78 1 : buffer->set_format(f);
79 :
80 2 : snaplogger::logger::pointer_t l(snaplogger::logger::get_instance());
81 1 : l->add_appender(buffer);
82 :
83 1 : SNAP_LOG_FATAL << "Appender created by name" << SNAP_LOG_SEND;
84 1 : CATCH_REQUIRE(std::dynamic_pointer_cast<snaplogger::buffer_appender>(buffer)->str() == "fatal: Appender created by name\n");
85 :
86 1 : l->reset();
87 : }
88 : CATCH_END_SECTION()
89 7 : }
90 :
91 :
92 :
93 : // vim: ts=4 sw=4 et
|