logger_support_test.cc 5.84 KB
Newer Older
1
// Copyright (C) 2011  Internet Systems Consortium, Inc. ("ISC")
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.

/// \brief Example Program
///
/// Simple example program showing how to use the logger.

19
#include <stdlib.h>
20
21
22
#include <unistd.h>
#include <string.h>

Stephen Morris's avatar
Stephen Morris committed
23
24
#include <boost/lexical_cast.hpp>

25
#include <iostream>
Stephen Morris's avatar
Stephen Morris committed
26
#include <string>
27

28
29
#include <util/strutil.h>

30
#include <log/logger.h>
31
#include <log/logger_level.h>
Stephen Morris's avatar
Stephen Morris committed
32
33
34
#include <log/logger_manager.h>
#include <log/logger_specification.h>
#include <log/macros.h>
35
36
37
38
39
40
#include <log/root_logger_name.h>

// Include a set of message definitions.
#include <log/messagedef.h>

using namespace isc::log;
Stephen Morris's avatar
Stephen Morris committed
41
42
43
44
45
46
47
using namespace std;


// Print usage information

void usage() {
    cout <<
Stephen Morris's avatar
Stephen Morris committed
48
49
"logger_support_test [-h] [-c stream] [-d dbglevel] [-f file]\n"
"                    [-s severity] [localfile]\n"
Stephen Morris's avatar
Stephen Morris committed
50
51
"\n"
"   -h              Print this message and exit\n"
Stephen Morris's avatar
Stephen Morris committed
52
"\n"
Stephen Morris's avatar
Stephen Morris committed
53
54
55
56
57
"   -d dbglevel     Debug level.  Only interpreted if the severity is 'debug'\n"
"                   this is a number between 0 and 99.\n"
"   -c stream       Send output to the console.  'stream' is one of 'stdout'\n"
"                   of 'stderr'.  The '-c' switch is incompatible with '-f'\n"
"                   and '-l'\n"
Stephen Morris's avatar
Stephen Morris committed
58
59
60
61
62
"   -f file         Send output to specified file, appending to existing file\n"
"                   if one exists.  Incompatible with -c and -l switches.\n"
"   -s severity     Set the severity of messages output.  'severity' is one\n"
"                   of 'debug', 'info', 'warn', 'error', 'fatal', the default\n"
"                   being 'info'.\n"
Stephen Morris's avatar
Stephen Morris committed
63
64
65
"\n"
"If none of -c, -f or -l is given, by default, output is sent to stdout\n";
}
66
67


68
69
// The program sets the attributes on the root logger and logs a set of
// messages.  Looking at the output determines whether the program worked.
70
71

int main(int argc, char** argv) {
Stephen Morris's avatar
Stephen Morris committed
72
    const string ROOT_NAME = "alpha";
73

Stephen Morris's avatar
Stephen Morris committed
74
75
76
77
78
79
80
81
82
83
84
85
86
87
    bool                c_found = false;    // Set true if "-c" found
    bool                f_found = false;    // Set true if "-f" found
    bool                l_found = false;    // Set true if "-l" found

    const char*         localfile = NULL;   // Local message file
    int                 option;             // For getopt() processing

    LoggerSpecification spec(ROOT_NAME);    // Logger specification
    OutputOption        outopt;             // Logger output option

    // Initialize loggers (to set the root name and initialize logging);
    // We'll reset them later.
    setRootLoggerName(ROOT_NAME);
    Logger rootLogger(ROOT_NAME);
88
89

    // Parse options
Stephen Morris's avatar
Stephen Morris committed
90
    while ((option = getopt(argc, argv, "hc:d:f:s:")) != -1) {
91
        switch (option) {
Stephen Morris's avatar
Stephen Morris committed
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
        case 'c':
            if (f_found || l_found) {
                cerr << "Cannot specify -c with -f or -l\n";
                return (1);
            }

            c_found = true;
            outopt.destination = OutputOption::DEST_CONSOLE;

            if (strcmp(optarg, "stdout") == 0) {
                outopt.stream = OutputOption::STR_STDOUT;

            } else if (strcmp(optarg, "stderr") == 0) {
                outopt.stream = OutputOption::STR_STDERR;

            } else {
                cerr << "Unrecognised console option: " << optarg << "\n";
                return (1);
            }
            break;

        case 'd':
            spec.setDbglevel(boost::lexical_cast<int>(optarg));
            break;

Stephen Morris's avatar
Stephen Morris committed
117
118
119
120
121
122
123
124
125
126
127
128
        case 'f':
            if (c_found || l_found) {
                cerr << "Cannot specify -f with -c or -l\n";
                return (1);
            }

            f_found = true;

            outopt.destination = OutputOption::DEST_FILE;
            outopt.filename = optarg;
            break;

Stephen Morris's avatar
Stephen Morris committed
129
130
131
132
133
        case 'h':
            usage();
            return (0);

        case 's':
134
135
136
137
            {
                string severity(optarg);
                isc::util::str::uppercase(severity);
                spec.setSeverity(getSeverity(severity));
Stephen Morris's avatar
Stephen Morris committed
138
139
140
141
142
143
144
            }
            break;

        default:
            std::cerr << "Unrecognised option: " <<
                static_cast<char>(option) << "\n";
            return (1);
145
146
147
148
149
150
151
152
        }
    }

    if (optind < argc) {
        localfile = argv[optind];
    }

    // Update the logging parameters
153
    LoggerManager::init(ROOT_NAME, localfile, isc::log::INFO, 0);
Stephen Morris's avatar
Stephen Morris committed
154
155
156
157
158
159
160
161
162
163

    // Set an output option if we have not done so already.
    if (! (c_found || f_found || l_found)) {
        outopt.destination = OutputOption::DEST_CONSOLE;
        outopt.stream = OutputOption::STR_STDOUT;
    }
    spec.addOutputOption(outopt);

    // Set the logging options for the root logger.
    LoggerManager manager;
164
    manager.process(spec);
Stephen Morris's avatar
Stephen Morris committed
165

166
167

    // Log a few messages
Stephen Morris's avatar
Stephen Morris committed
168
169
    isc::log::Logger logger_dlm("dlm");
    isc::log::Logger logger_ex("example");
170
171
172
173
174
175
176
177
178
    LOG_FATAL(logger_ex, MSG_WRITERR).arg("test1").arg("42");
    LOG_ERROR(logger_ex, MSG_RDLOCMES).arg("dummy/file");
    LOG_WARN(logger_dlm, MSG_READERR).arg("a.txt").arg("dummy reason");
    LOG_INFO(logger_dlm, MSG_OPENIN).arg("example.msg").arg("dummy reason");
    LOG_DEBUG(logger_ex, 0, MSG_RDLOCMES).arg("dummy/0");
    LOG_DEBUG(logger_ex, 24, MSG_RDLOCMES).arg("dummy/24");
    LOG_DEBUG(logger_ex, 25, MSG_RDLOCMES).arg("dummy/25");
    LOG_DEBUG(logger_ex, 26, MSG_RDLOCMES).arg("dummy/26");

179
    return (0);
180
}