- added logging to file

- started working on the logger bindings
- cleanups some error reporting with cerr/clog in the library
This commit is contained in:
kunitoki 2012-04-10 00:25:31 +02:00
parent efc823a372
commit a902a08aab
9 changed files with 227 additions and 104 deletions

View file

@ -23,43 +23,111 @@
#include <boost/python.hpp> #include <boost/python.hpp>
#include <mapnik/debug.hpp> #include <mapnik/debug.hpp>
using mapnik::logger::severity;
using mapnik::logger::format;
using mapnik::logger::output;
void set_severity(const severity::type& s)
{
severity::set(s);
}
severity::type get_severity()
{
return severity::get();
}
void set_object_severity(const std::string& object_name, const severity::type& s)
{
severity::set_object(object_name, s);
}
severity::type get_object_severity(const std::string& object_name)
{
return severity::get_object(object_name);
}
void export_logger() void export_logger()
{ {
// TODO - expose the logger global class
/*
using namespace boost::python; using namespace boost::python;
enum_<mapnik::logger::severity::type>("severity") enum_<mapnik::logger::severity::type>("SeverityType")
.value("Info", mapnik::logger::severity::info) .value("Info", severity::info)
.value("Debug", mapnik::logger::severity::debug) .value("Debug", severity::debug)
.value("Warn", mapnik::logger::severity::warn) .value("Warn", severity::warn)
.value("Error", mapnik::logger::severity::error) .value("Error", severity::error)
.value("Fatal", mapnik::logger::severity::fatal) .value("Fatal", severity::fatal)
.value("None", mapnik::logger::severity::none) .value("None", severity::none)
; ;
*/
/* /*
using mapnik::freetype_engine;
using mapnik::singleton; using mapnik::singleton;
using mapnik::CreateStatic; using mapnik::CreateStatic;
using namespace boost::python; using namespace boost::python;
class_<singleton<freetype_engine,CreateStatic>,boost::noncopyable>("Singleton",no_init)
.def("instance",&singleton<freetype_engine,CreateStatic>::instance, class_<singleton<severity,CreateStatic>,boost::noncopyable>("Singleton",no_init)
.def("instance",&singleton<severity,CreateStatic>::instance,
return_value_policy<reference_existing_object>()) return_value_policy<reference_existing_object>())
.staticmethod("instance") .staticmethod("instance")
; ;
class_<freetype_engine,bases<singleton<freetype_engine,CreateStatic> >, class_<severity,bases<singleton<severity,CreateStatic> >,
boost::noncopyable>("FontEngine",no_init) boost::noncopyable>("Severity",no_init)
.def("register_font",&freetype_engine::register_font) .def("get",&severity::get)
.def("register_fonts",&freetype_engine::register_fonts) .def("set",&severity::set)
.def("face_names",&freetype_engine::face_names) .def("get_object",&severity::get_object)
.staticmethod("register_font") .def("set_object",&severity::set_object)
.staticmethod("register_fonts") .staticmethod("get")
.staticmethod("face_names") .staticmethod("set")
.staticmethod("get_object")
.staticmethod("set_object")
; ;
*/
def("set_severity", &set_severity,
"\n"
"Set global logger severity.\n"
"\n"
"Usage:\n"
">>> from mapnik import SeverityType, set_severity\n"
">>> set_severity(SeverityType.None)\n"
">>> set_severity(SeverityType.Info)\n"
">>> set_severity(SeverityType.Debug)\n"
">>> set_severity(SeverityType.Warn)\n"
">>> set_severity(SeverityType.Error)\n"
">>> set_severity(SeverityType.Fatal)\n"
);
def("get_severity", &get_severity,
"\n"
"Get global logger severity.\n"
"\n"
"Usage:\n"
">>> from mapnik import get_severity\n"
">>> get_severity()\n"
);
def("set_object_severity", &set_object_severity,
"\n"
"Set logger severity for a single object.\n"
"\n"
"Usage:\n"
">>> from mapnik import SeverityType, set_object_severity\n"
">>> set_object_severity('ogr', SeverityType.None)\n"
">>> set_object_severity('gdal', SeverityType.Info)\n"
">>> set_object_severity('cairo_renderer', SeverityType.Debug)\n"
">>> set_object_severity('agg_renderer', SeverityType.Warn)\n"
">>> set_object_severity('bindings', SeverityType.Error)\n"
);
def("get_object_severity", &get_object_severity,
"\n"
"Get logger severity for a single object.\n"
"\n"
"Usage:\n"
">>> from mapnik import get_object_severity"
">>> get_object_severity('ogr')\n"
);
*/
} }

View file

@ -23,8 +23,9 @@
#ifndef MAPNIK_DEBUG_HPP #ifndef MAPNIK_DEBUG_HPP
#define MAPNIK_DEBUG_HPP #define MAPNIK_DEBUG_HPP
// mapnik (should not depend on anything else) // mapnik (should not depend on anything that need to use this)
#include <mapnik/config.hpp> #include <mapnik/config.hpp>
#include <mapnik/utils.hpp>
// boost // boost
#include <boost/utility.hpp> #include <boost/utility.hpp>
@ -35,7 +36,6 @@
// std // std
#include <iostream> #include <iostream>
#include <cassert>
#include <sstream> #include <sstream>
#include <ostream> #include <ostream>
#include <fstream> #include <fstream>
@ -45,7 +45,9 @@
namespace mapnik { namespace mapnik {
namespace logger { namespace logger {
class severity class MAPNIK_DECL severity :
public singleton<severity,CreateStatic>,
private boost::noncopyable
{ {
public: public:
enum type enum type
@ -113,7 +115,9 @@ namespace mapnik {
}; };
class format class MAPNIK_DECL format :
public singleton<format,CreateStatic>,
private boost::noncopyable
{ {
public: public:
@ -141,8 +145,23 @@ namespace mapnik {
}; };
class MAPNIK_DECL output :
public singleton<output,CreateStatic>,
private boost::noncopyable
{
public:
static void use_file(const std::string& filepath);
static void use_console();
private:
static std::ofstream file_output_;
static std::string file_name_;
static std::streambuf* saved_buf_;
};
template<class Ch, class Tr, class A> template<class Ch, class Tr, class A>
class output_to_clog class clog_sink
{ {
public: public:
typedef std::basic_ostringstream<Ch, Tr, A> stream_buffer; typedef std::basic_ostringstream<Ch, Tr, A> stream_buffer;
@ -210,55 +229,43 @@ namespace mapnik {
std::string object_name_; std::string object_name_;
#endif #endif
}; };
typedef base_log<clog_sink, severity::info> base_log_info;
typedef base_log<clog_sink, severity::debug> base_log_debug;
typedef base_log<clog_sink, severity::warn> base_log_warn;
typedef base_log<clog_sink, severity::error> base_log_error;
typedef base_log<clog_sink, severity::fatal> base_log_fatal;
} }
class MAPNIK_DECL log : public logger::base_log<logger::output_to_clog, class MAPNIK_DECL info : public logger::base_log_info {
logger::severity::debug> {
public: public:
typedef logger::base_log<logger::output_to_clog, logger::severity::debug> base_class; info() : logger::base_log_info() {}
log(const char* object_name) : base_class(object_name) {} info(const char* object_name) : logger::base_log_info(object_name) {}
log() : base_class() {}
}; };
class MAPNIK_DECL info : public logger::base_log<logger::output_to_clog, class MAPNIK_DECL debug : public logger::base_log_debug {
logger::severity::info> {
public: public:
typedef logger::base_log<logger::output_to_clog, logger::severity::info> base_class; debug() : logger::base_log_debug() {}
info(const char* object_name) : base_class(object_name) {} debug(const char* object_name) : logger::base_log_debug(object_name) {}
info() : base_class() {}
}; };
class MAPNIK_DECL debug : public logger::base_log<logger::output_to_clog, class MAPNIK_DECL warn : public logger::base_log_warn {
logger::severity::debug> {
public: public:
typedef logger::base_log<logger::output_to_clog, logger::severity::debug> base_class; warn() : logger::base_log_warn() {}
debug(const char* object_name) : base_class(object_name) {} warn(const char* object_name) : logger::base_log_warn(object_name) {}
debug() : base_class() {}
}; };
class MAPNIK_DECL warn : public logger::base_log<logger::output_to_clog, class MAPNIK_DECL error : public logger::base_log_error {
logger::severity::warn> {
public: public:
typedef logger::base_log<logger::output_to_clog, logger::severity::warn> base_class; error() : logger::base_log_error() {}
warn(const char* object_name) : base_class(object_name) {} error(const char* object_name) : logger::base_log_error(object_name) {}
warn() : base_class() {}
}; };
class MAPNIK_DECL error : public logger::base_log<logger::output_to_clog, class MAPNIK_DECL fatal : public logger::base_log_fatal {
logger::severity::error> {
public: public:
typedef logger::base_log<logger::output_to_clog, logger::severity::error> base_class; fatal() : logger::base_log_fatal() {}
error(const char* object_name) : base_class(object_name) {} fatal(const char* object_name) : logger::base_log_fatal(object_name) {}
error() : base_class() {}
};
class MAPNIK_DECL fatal : public logger::base_log<logger::output_to_clog,
logger::severity::fatal> {
public:
typedef logger::base_log<logger::output_to_clog, logger::severity::fatal> base_class;
fatal(const char* object_name) : base_class(object_name) {}
fatal() : base_class() {}
}; };
@ -267,7 +274,6 @@ namespace mapnik {
#define MAPNIK_LOG_WARN(s) mapnik::warn(#s) #define MAPNIK_LOG_WARN(s) mapnik::warn(#s)
#define MAPNIK_LOG_ERROR(s) mapnik::error(#s) #define MAPNIK_LOG_ERROR(s) mapnik::error(#s)
#define MAPNIK_LOG_FATAL(s) mapnik::fatal(#s) #define MAPNIK_LOG_FATAL(s) mapnik::fatal(#s)
} }
#endif // MAPNIK_DEBUG_HPP #endif // MAPNIK_DEBUG_HPP

View file

@ -306,11 +306,12 @@ public:
if (face_ptr face = get_face(*name)) if (face_ptr face = get_face(*name))
{ {
face_set->add(face); face_set->add(face);
} else { }
#ifdef MAPNIK_DEBUG else
// TODO - handle with mapnik::log {
std::cerr << "Failed to find face '" << *name << "' in font set '" << fset.get_name() << "'\n"; MAPNIK_LOG_ERROR(font_engine_freetype)
#endif << "Failed to find face '" << *name
<< "' in font set '" << fset.get_name() << "'\n";
} }
} }
return face_set; return face_set;

View file

@ -266,12 +266,12 @@ struct feature_grammar :
// //
; ;
coordinates = eps(_r2 == 1) > point_coordinates(extract_geometry_(_r1)) coordinates = (eps(_r2 == 1) > point_coordinates(extract_geometry_(_r1)))
| eps(_r2 == 2) > linestring_coordinates(extract_geometry_(_r1)) | (eps(_r2 == 2) > linestring_coordinates(extract_geometry_(_r1)))
| eps(_r2 == 3) > polygon_coordinates(extract_geometry_(_r1)) | (eps(_r2 == 3) > polygon_coordinates(extract_geometry_(_r1)))
| eps(_r2 == 4) > multipoint_coordinates(extract_geometry_(_r1)) | (eps(_r2 == 4) > multipoint_coordinates(extract_geometry_(_r1)))
| eps(_r2 == 5) > multilinestring_coordinates(extract_geometry_(_r1)) | (eps(_r2 == 5) > multilinestring_coordinates(extract_geometry_(_r1)))
| eps(_r2 == 6) > multipolygon_coordinates(extract_geometry_(_r1)) | (eps(_r2 == 6) > multipolygon_coordinates(extract_geometry_(_r1)))
; ;
point_coordinates = eps[ _a = new_<geometry_type>(Point) ] point_coordinates = eps[ _a = new_<geometry_type>(Point) ]

View file

@ -28,9 +28,6 @@
#include <boost/thread/mutex.hpp> #include <boost/thread/mutex.hpp>
#endif #endif
// mapnik
#include <mapnik/debug.hpp>
// stl // stl
#include <stdexcept> #include <stdexcept>
#include <cstdlib> #include <cstdlib>
@ -123,9 +120,7 @@ template <typename T,
{ {
CreatePolicy<T>::destroy(pInstance_); CreatePolicy<T>::destroy(pInstance_);
pInstance_ = 0; pInstance_ = 0;
destroyed_=true; destroyed_ = true;
MAPNIK_LOG_DEBUG(utils) << "singleton: Destroyed instance";
} }
protected: protected:
@ -151,8 +146,6 @@ public:
{ {
pInstance_ = CreatePolicy<T>::create(); pInstance_ = CreatePolicy<T>::create();
MAPNIK_LOG_DEBUG(utils) << "singleton: Created instance";
// register destruction // register destruction
std::atexit(&DestroySingleton); std::atexit(&DestroySingleton);
} }

View file

@ -63,30 +63,26 @@ using mapnik::filter_at_point;
DATASOURCE_PLUGIN(geos_datasource) DATASOURCE_PLUGIN(geos_datasource)
void geos_notice(const char* fmt, ...) void geos_notice(const char* format, ...)
{ {
// TODO - handle with mapnik::log char buffer[512];
va_list ap; va_list args;
fprintf( stdout, "Mapnik LOG> geos_datasource: (GEOS NOTICE) "); va_start(args, fmt);
vsprintf(buffer, format, args);
va_end(args);
va_start (ap, fmt); MAPNIK_LOG_WARN(geos) << "geos_datasource: " << buffer;
vfprintf( stdout, fmt, ap);
va_end(ap);
fprintf( stdout, "\n" );
} }
void geos_error(const char* fmt, ...) void geos_error(const char* format, ...)
{ {
// TODO - handle with mapnik::log va_list args;
va_start(args, fmt);
vsprintf(buffer, format, args);
va_end(args);
va_list ap; MAPNIK_LOG_ERROR(geos) << "geos_datasource: " << buffer;
fprintf( stdout, "Mapnik LOG> geos_datasource: (GEOS ERROR) ");
va_start (ap, fmt);
vfprintf( stdout, fmt, ap);
va_end(ap);
fprintf( stdout, "\n" );
} }

View file

@ -196,7 +196,8 @@ void agg_renderer<T>::process(markers_symbolizer const& sym,
tr.transform(&x1,&y1); tr.transform(&x1,&y1);
tr.transform(&x2,&y2); tr.transform(&x2,&y2);
extent.init(x1,y1,x2,y2); extent.init(x1,y1,x2,y2);
//mapnik::log() << "agg_renderer: " << x1 << " " << y1 << " " << x2 << " " << y2 << "\n";
//MAPNIK_LOG_DEBUG(agg_renderer) << "agg_renderer: " << x1 << " " << y1 << " " << x2 << " " << y2 << "\n";
} }
else else
{ {
@ -207,7 +208,8 @@ void agg_renderer<T>::process(markers_symbolizer const& sym,
tr.transform(&x1,&y1); tr.transform(&x1,&y1);
tr.transform(&x2,&y2); tr.transform(&x2,&y2);
extent.init(x1,y1,x2,y2); extent.init(x1,y1,x2,y2);
//mapnik::log() << "agg_renderer: " << x1 << " " << y1 << " " << x2 << " " << y2 << "\n";
//MAPNIK_LOG_DEBUG(agg_renderer) << "agg_renderer: " << x1 << " " << y1 << " " << x2 << " " << y2 << "\n";
} }

View file

@ -32,6 +32,7 @@
namespace mapnik { namespace logger { namespace mapnik { namespace logger {
// severity // severity
severity::type severity::severity_level_ = severity::type severity::severity_level_ =
@ -44,17 +45,23 @@ severity::type severity::severity_level_ =
severity::severity_map severity::object_severity_level_ = severity::severity_map(); severity::severity_map severity::object_severity_level_ = severity::severity_map();
#ifdef MAPNIK_THREADSAFE
boost::mutex severity::mutex_;
#endif
// format // format
#define __xstr__(s) __str__(s) #define __xstr__(s) __str__(s)
#define __str__(s) #s #define __str__(s) #s
std::string format::format_ = __xstr__(MAPNIK_LOG_FORMAT); std::string format::format_ = __xstr__(MAPNIK_LOG_FORMAT);
#undef __xstr__ #undef __xstr__
#undef __str__ #undef __str__
#ifdef MAPNIK_THREADSAFE
boost::mutex format::mutex_;
#endif
std::string format::str() std::string format::str()
{ {
char buf[256]; char buf[256];
@ -64,5 +71,55 @@ std::string format::str()
} }
// output
std::ofstream output::file_output_;
std::string output::file_name_;
std::streambuf* output::saved_buf_ = 0;
void output::use_file(const std::string& filepath)
{
// save clog rdbuf
if (saved_buf_ == 0)
{
saved_buf_ = std::clog.rdbuf();
}
// use a file to output as clog rdbuf
if (file_name_ != filepath)
{
file_name_ = filepath;
if (file_output_.is_open())
{
file_output_.close();
}
file_output_.open(file_name_.c_str(), std::ios::out | std::ios::app);
if (file_output_)
{
std::clog.rdbuf(file_output_.rdbuf());
}
else
{
std::stringstream s;
s << "cannot redirect log to file " << file_output_;
throw std::runtime_error(s.str());
}
}
}
void output::use_console()
{
// save clog rdbuf
if (saved_buf_ == 0)
{
saved_buf_ = std::clog.rdbuf();
}
std::clog.rdbuf(saved_buf_);
}
} }
} }

View file

@ -100,7 +100,7 @@ public:
/* /*
if ( ! ctx->valid ) if ( ! ctx->valid )
{ {
mapnik::log() << "libxml2_loader: Failed to validate DTD."; MAPNIK_LOG_WARN(libxml2_loader) << "libxml2_loader: Failed to validate DTD.";
} }
*/ */
load(doc, node); load(doc, node);