2012-04-08 03:59:47 +02:00
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
*
|
2021-01-05 15:39:07 +01:00
|
|
|
* Copyright (C) 2021 Artem Pavlenko
|
2012-04-08 03:59:47 +02:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2012-04-09 03:00:51 +02:00
|
|
|
// mapnik
|
2012-04-08 03:59:47 +02:00
|
|
|
#include <mapnik/debug.hpp>
|
2016-03-10 01:16:14 +01:00
|
|
|
#include <mapnik/stringify_macro.hpp>
|
2012-04-08 03:59:47 +02:00
|
|
|
|
2012-04-09 12:05:49 +02:00
|
|
|
// stl
|
|
|
|
#include <ctime>
|
2013-06-03 05:19:33 +02:00
|
|
|
#include <stdexcept>
|
2014-08-06 21:27:10 +02:00
|
|
|
#include <fstream>
|
2015-08-27 17:08:41 +02:00
|
|
|
#include <cstdlib>
|
2012-04-09 12:05:49 +02:00
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
// clang-format off
|
2012-04-09 12:05:49 +02:00
|
|
|
#ifndef MAPNIK_LOG_FORMAT
|
2015-09-08 12:28:16 +02:00
|
|
|
#define MAPNIK_LOG_FORMAT Mapnik LOG> %Y-%m-%d %H:%M:%S:
|
2012-04-09 12:05:49 +02:00
|
|
|
#endif
|
2022-01-26 10:43:31 +01:00
|
|
|
// clang-format on
|
2012-04-09 12:05:49 +02:00
|
|
|
|
2012-04-10 16:19:51 +02:00
|
|
|
#ifndef MAPNIK_DEFAULT_LOG_SEVERITY
|
2015-09-08 12:28:16 +02:00
|
|
|
#ifdef MAPNIK_DEBUG
|
|
|
|
#define MAPNIK_DEFAULT_LOG_SEVERITY 0
|
|
|
|
#else
|
|
|
|
#define MAPNIK_DEFAULT_LOG_SEVERITY 2
|
|
|
|
#endif
|
2012-04-10 16:19:51 +02:00
|
|
|
#endif
|
|
|
|
|
2012-04-10 10:14:28 +02:00
|
|
|
namespace mapnik {
|
|
|
|
|
|
|
|
#ifdef MAPNIK_THREADSAFE
|
2015-09-30 23:23:21 +02:00
|
|
|
|
2013-10-11 14:20:46 +02:00
|
|
|
std::mutex logger::severity_mutex_;
|
|
|
|
std::mutex logger::format_mutex_;
|
2012-04-10 16:19:51 +02:00
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
std::atomic<bool> logger::severity_env_check_{true};
|
|
|
|
std::atomic<bool> logger::format_env_check_{true};
|
2012-04-10 16:19:51 +02:00
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
std::atomic<logger::severity_type> logger::severity_level_
|
|
|
|
{
|
2015-09-30 23:23:21 +02:00
|
|
|
#else
|
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
bool logger::severity_env_check_{true};
|
|
|
|
bool logger::format_env_check_{true};
|
2015-09-30 23:23:21 +02:00
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
logger::severity_type logger::severity_level_
|
|
|
|
{
|
2015-09-30 23:23:21 +02:00
|
|
|
#endif
|
2015-09-08 12:28:16 +02:00
|
|
|
#if MAPNIK_DEFAULT_LOG_SEVERITY == 0
|
|
|
|
logger::debug
|
|
|
|
#elif MAPNIK_DEFAULT_LOG_SEVERITY == 1
|
|
|
|
logger::warn
|
|
|
|
#elif MAPNIK_DEFAULT_LOG_SEVERITY == 2
|
2022-01-26 10:43:31 +01:00
|
|
|
logger::error
|
2015-09-08 12:28:16 +02:00
|
|
|
#elif MAPNIK_DEFAULT_LOG_SEVERITY == 3
|
2022-01-26 10:43:31 +01:00
|
|
|
logger::none
|
2015-09-08 12:28:16 +02:00
|
|
|
#else
|
|
|
|
#error "Wrong default log severity level specified!"
|
|
|
|
#endif
|
|
|
|
};
|
2012-04-09 03:00:51 +02:00
|
|
|
|
2012-04-10 10:14:28 +02:00
|
|
|
logger::severity_map logger::object_severity_level_ = logger::severity_map();
|
2012-04-10 00:25:31 +02:00
|
|
|
|
2016-03-10 01:16:14 +01:00
|
|
|
std::string logger::format_ = MAPNIK_STRINGIFY(MAPNIK_LOG_FORMAT);
|
2012-04-09 12:05:49 +02:00
|
|
|
|
2012-04-10 10:14:28 +02:00
|
|
|
std::string logger::str()
|
2012-04-09 12:05:49 +02:00
|
|
|
{
|
2015-08-27 17:37:18 +02:00
|
|
|
#ifdef MAPNIK_CHECK_ENV
|
2012-04-10 16:19:51 +02:00
|
|
|
// update the format from getenv if this is the first time
|
|
|
|
if (logger::format_env_check_)
|
|
|
|
{
|
|
|
|
logger::format_env_check_ = false;
|
|
|
|
|
2015-08-27 17:08:41 +02:00
|
|
|
const char* log_format = std::getenv("MAPNIK_LOG_FORMAT");
|
2013-11-27 16:52:47 +01:00
|
|
|
if (log_format != nullptr)
|
2012-04-10 16:19:51 +02:00
|
|
|
{
|
|
|
|
logger::format_ = log_format;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-04-09 12:05:49 +02:00
|
|
|
char buf[256];
|
|
|
|
const time_t tm = time(0);
|
2015-08-27 17:08:41 +02:00
|
|
|
std::strftime(buf, sizeof(buf), logger::format_.c_str(), localtime(&tm));
|
2012-04-09 12:05:49 +02:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2012-04-10 00:25:31 +02:00
|
|
|
// output
|
|
|
|
|
2012-04-10 10:14:28 +02:00
|
|
|
std::ofstream logger::file_output_;
|
|
|
|
std::string logger::file_name_;
|
|
|
|
std::streambuf* logger::saved_buf_ = 0;
|
2012-04-10 00:25:31 +02:00
|
|
|
|
2012-09-03 19:27:48 +02:00
|
|
|
void logger::use_file(std::string const& filepath)
|
2012-04-10 00:25:31 +02:00
|
|
|
{
|
|
|
|
// 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;
|
2013-07-27 01:32:49 +02:00
|
|
|
s << "cannot redirect log to file " << file_name_;
|
2012-04-10 00:25:31 +02:00
|
|
|
throw std::runtime_error(s.str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-10 10:14:28 +02:00
|
|
|
void logger::use_console()
|
2012-04-10 00:25:31 +02:00
|
|
|
{
|
|
|
|
// save clog rdbuf
|
|
|
|
if (saved_buf_ == 0)
|
|
|
|
{
|
|
|
|
saved_buf_ = std::clog.rdbuf();
|
|
|
|
}
|
|
|
|
|
2012-04-10 16:46:11 +02:00
|
|
|
// close the file to force a flush
|
|
|
|
if (file_output_.is_open())
|
|
|
|
{
|
|
|
|
file_output_.close();
|
|
|
|
}
|
|
|
|
|
2012-04-10 00:25:31 +02:00
|
|
|
std::clog.rdbuf(saved_buf_);
|
|
|
|
}
|
|
|
|
|
2012-04-10 16:46:11 +02:00
|
|
|
} // namespace mapnik
|