-
Notifications
You must be signed in to change notification settings - Fork 0
/
python.cpp
102 lines (89 loc) · 3.06 KB
/
python.cpp
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
/*------------------------------------------------------------------------------
*
* Python wrapper for logging library.
*
* Author: [email protected]
*
* Copyright 2010-1 Mapquest, Inc. All Rights reserved.
*
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*-----------------------------------------------------------------------------*/
#include <boost/python.hpp>
#include <boost/noncopyable.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/ini_parser.hpp>
#include <string>
#include <iostream>
#include "logger.hpp"
using namespace boost::python;
using std::string;
namespace {
template<typename T>
bool try_set(boost::property_tree::ptree &pt, const string &key, object &val) {
extract<T> ex(val);
if (ex.check()) {
pt.put<T>(key, ex());
return true;
}
return false;
}
void configure_from_dict(const dict &d) {
boost::property_tree::ptree pt;
boost::python::list keys=d.keys();
for (int i = 0; i < len(keys); ++i) {
string key = extract<string>(keys[i]);
object val = d[key];
try_set<string>(pt, key, val)
|| try_set<int>(pt, key, val)
|| try_set<double>(pt, key, val)
;
}
logging::log::configure(pt);
}
void configure_from_file(const std::string &filename) {
try
{
boost::property_tree::ptree logging_config;
boost::property_tree::read_ini(filename, logging_config);
logging::log::configure(logging_config);
}
catch (const std::exception &e)
{
std::cerr << "Error while setting up logging from: " << filename << "\n";
throw;
}
}
} // anonymous namespace
BOOST_PYTHON_MODULE(mq_logging)
{
// resolve the overloads - don't want to expose boost::format to python,
// as it's already quite capable of doing its own string interpolation.
void (*finer) (const string &) = &logging::log::finer;
void (*debug) (const string &) = &logging::log::debug;
void (*info) (const string &) = &logging::log::info;
void (*warning)(const string &) = &logging::log::warning;
void (*error) (const string &) = &logging::log::error;
// note we're not exposing log as a class - no need, since all the
// access methods are static.
def("finer", finer);
def("debug", debug);
def("info", info);
def("warning", warning);
def("error", error);
def("configure", &configure_from_dict);
def("configure_file", &configure_from_file);
}