File: logger_config_file.cpp

package info (click to toggle)
mldemos 0.5.1-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 32,224 kB
  • ctags: 46,525
  • sloc: cpp: 306,887; ansic: 167,718; ml: 126; sh: 109; makefile: 2
file content (209 lines) | stat: -rw-r--r-- 7,035 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// Copyright (C) 2007  Davis E. King (davis@dlib.net)
// License: Boost Software License   See LICENSE.txt for the full license.
#ifndef DLIB_LOGGER_CONFIg_FILE_CPP
#define DLIB_LOGGER_CONFIg_FILE_CPP

#include "logger_config_file.h"
#include <string>
#include "../config_reader.h"
#include <fstream>
#include <sstream>
#include "../error.h"
#include "../map.h"
#include "../string.h"

// ----------------------------------------------------------------------------------------

namespace dlib
{

    namespace logger_config_file_helpers 
    {
        typedef config_reader::kernel_1a cr_type;

// ----------------------------------------------------------------------------------------

        std::ostream& get_file_stream (
            const std::string& file_name
        )
        {
            using namespace std;
            static dlib::mutex m;
            auto_mutex M(m);
            static dlib::map<string,ostream*>::kernel_1a_c file_map;

            if (file_map.is_in_domain(file_name) == false)
            {
                // We won't ever delete this output stream.  It should be around for the
                // entire life of the program so just let the OS take care of it.
                ostream* fout = new ofstream(file_name.c_str());
                if (!(*fout))
                {
                    delete fout;
                    throw error("logger_config: unable to open output file " + file_name);
                }

                // add this file to our file map
                string temp(file_name);
                file_map.add(temp,fout);
            }

            return *file_map[file_name];
        }

// ----------------------------------------------------------------------------------------

        log_level string_to_log_level (
            const std::string& level 
        )
        {
            using namespace std;
            if (level == "LALL" || level == "ALL" || level == "all")
                return LALL;
            else if (level == "LNONE" || level == "NONE" || level == "none")
                return LNONE;
            else if (level == "LTRACE" || level == "TRACE" || level == "trace")
                return LTRACE;
            else if (level == "LDEBUG" || level == "DEBUG" || level == "debug")
                return LDEBUG;
            else if (level == "LINFO" || level == "INFO" || level == "info")
                return LINFO;
            else if (level == "LWARN" || level == "WARN" || level == "warn")
                return LWARN;
            else if (level == "LERROR" || level == "ERROR" || level == "error")
                return LERROR;
            else if (level == "LFATAL" || level == "FATAL" || level == "fatal")
                return LFATAL;
            else
            {
                const int priority = string_cast<int>(level);
                return log_level(priority,"CONFIG_FILE_DEFINED");
            }
        }

// ----------------------------------------------------------------------------------------
        
        void configure_sub_blocks (
            const cr_type& cr,
            const std::string& name 
        )
        {
            using namespace std;

            logger dlog(name.c_str());

            if (cr.is_key_defined("logging_level"))
            {
                dlog.set_level(string_to_log_level(cr["logging_level"]));
            }

            if (cr.is_key_defined("output"))
            {
                string output = cr["output"];
                if (output == "cout")
                    dlog.set_output_stream(cout);
                else if (output == "cerr")
                    dlog.set_output_stream(cerr);
                else if (output == "clog")
                    dlog.set_output_stream(clog);
                else
                {
                    istringstream sin(output);
                    string one, two, three;
                    sin >> one;
                    sin >> two;
                    sin >> three;
                    if (one == "file" && three.size() == 0)
                        dlog.set_output_stream(get_file_stream(two));
                    else
                        throw error("logger_config: invalid argument to output option: " + output);
                }

            } // if (cr.is_key_defined("output"))

            // now configure all the sub-blocks
            std_vector_c<std::string> blocks;
            cr.get_blocks(blocks);
            for (unsigned long i = 0; i < blocks.size(); ++i)
            {
                configure_sub_blocks(cr.block(blocks[i]), name + "." + blocks[i]);
            }

        }

// ----------------------------------------------------------------------------------------

    } // namespace

// ----------------------------------------------------------------------------------------

    void configure_loggers_from_file (
        const std::string& file_name
    )
    {
        using namespace logger_config_file_helpers;
        using namespace std;
        ifstream fin(file_name.c_str());

        if (!fin)
            throw logger_config_file_error("logger_config: unable to open config file " + file_name);


        cr_type main_cr;
        main_cr.load_from(fin);


        if (main_cr.is_block_defined("logger_config"))
        {
            const cr_type& cr = main_cr.block("logger_config");

            if (cr.is_key_defined("logging_level"))
            {
                set_all_logging_levels(string_to_log_level(cr["logging_level"]));
            }

            if (cr.is_key_defined("output"))
            {
                string output = cr["output"];
                if (output == "cout")
                    set_all_logging_output_streams(cout);
                else if (output == "cerr")
                    set_all_logging_output_streams(cerr);
                else if (output == "clog")
                    set_all_logging_output_streams(clog);
                else
                {
                    istringstream sin(output);
                    string one, two, three;
                    sin >> one;
                    sin >> two;
                    sin >> three;
                    if (one == "file" && three.size() == 0)
                        set_all_logging_output_streams(get_file_stream(two));
                    else
                        throw logger_config_file_error("logger_config: invalid argument to output option: " + output);
                }

            } // if (cr.is_key_defined("output"))

            // now configure all the sub-blocks
            std_vector_c<std::string> blocks;
            cr.get_blocks(blocks);
            for (unsigned long i = 0; i < blocks.size(); ++i)
            {
                configure_sub_blocks(cr.block(blocks[i]), blocks[i]);
            }

        }
    }

// ----------------------------------------------------------------------------------------

}

// ----------------------------------------------------------------------------------------

#endif // DLIB_LOGGER_CONFIg_FILE_CPP