log: remove boost::format to reduce complexity

This commit is contained in:
Danny Robson 2016-01-20 16:40:42 +11:00
parent 0fd72f0893
commit aee9d8ef36
3 changed files with 30 additions and 57 deletions

54
log.cpp
View File

@ -27,8 +27,6 @@
#include <map>
#include <string>
#include <boost/format.hpp>
///////////////////////////////////////////////////////////////////////////////
static util::level_t
@ -95,45 +93,31 @@ log_level (void)
///////////////////////////////////////////////////////////////////////////////
void
util::log (util::level_t l, const std::string &format)
util::log (util::level_t level, const std::string &msg)
{
detail::log (l, boost::format (format));
}
static const util::level_t LOG_LEVEL = log_level ();
if (level > LOG_LEVEL)
return;
static const size_t time_len = strlen("YYYY-mm-dd HHMMhSS") + 1;
std::string time_string (time_len - 1, '\0');
time_t unix_time = time (nullptr);
//-----------------------------------------------------------------------------
void
util::detail::log (util::level_t level, boost::format &&format)
{
static const util::level_t LOG_LEVEL = log_level ();
if (level > LOG_LEVEL)
return;
if (0 == strftime (&time_string[0],
time_len,
"%Y-%m-%d %H%Mh%S",
localtime (&unix_time))) {
warn ("failed to log time");
return;
}
static const boost::format LEVEL_FORMAT ("%s [%s] ");
static const size_t time_len = strlen("YYYY-mm-dd HHMMhSS") + 1;
std::string time_string (time_len - 1, '\0');
time_t unix_time = time (nullptr);
if (0 == strftime (&time_string[0],
time_len,
"%Y-%m-%d %H%Mh%S",
localtime (&unix_time))) {
unusual ();
return;
}
std::cerr << boost::format (LEVEL_FORMAT)
% time_string
% level
<< format
<< std::endl;
std::cerr << time_string << " [" << level << "] " << msg << std::endl;
}
///////////////////////////////////////////////////////////////////////////////
util::scoped_logger::scoped_logger (util::level_t _level,
std::string &&_message):
util::scoped_logger::scoped_logger (util::level_t _level,
std::string _message):
m_level (_level),
m_message (std::move (_message))
{ ; }
@ -148,9 +132,9 @@ util::scoped_logger::~scoped_logger ()
///////////////////////////////////////////////////////////////////////////////
util::scoped_timer::scoped_timer (util::level_t _level,
std::string &&_message):
std::string _message):
m_level (_level),
m_message (_message),
m_message (std::move (_message)),
m_start (util::nanoseconds ())
{ ; }

View File

@ -54,7 +54,7 @@ namespace util {
///////////////////////////////////////////////////////////////////////////
void log (level_t, const std::string &format);
void log (level_t, const std::string &msg);
template <typename ...tail>
void log (level_t, const std::string &format, tail ..._tail);
@ -79,7 +79,7 @@ namespace util {
///////////////////////////////////////////////////////////////////////////
class scoped_logger : public nocopy {
public:
scoped_logger (const level_t, std::string&&);
scoped_logger (const level_t, std::string);
~scoped_logger ();
protected:
@ -91,7 +91,7 @@ namespace util {
///////////////////////////////////////////////////////////////////////////
class scoped_timer : public nocopy {
public:
scoped_timer (const level_t, std::string&&);
scoped_timer (const level_t, std::string);
~scoped_timer ();
private:

27
log.ipp
View File

@ -11,35 +11,24 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2012 Danny Robson <danny@nerdcruft.net>
* Copyright 2012-2016 Danny Robson <danny@nerdcruft.net>
*/
#ifdef __UTIL_LOG_IPP
#error Double inclusion of util/log.ipp
#error
#endif
#define __UTIL_LOG_IPP
//-----------------------------------------------------------------------------
#include <boost/format.hpp>
#include "format.hpp"
//-----------------------------------------------------------------------------
namespace util {
namespace detail {
void
log (level_t l, boost::format &&format);
template <typename T, typename ...tail>
void
log (level_t l, boost::format &&format, const T &val, tail ..._tail) {
::util::detail::log (l, std::move (format.operator% (val)), _tail...);
}
}
template <typename ...tail>
void log (level_t l, const std::string &format, tail ..._tail)
{ detail::log (l, std::move (boost::format (format)), _tail...); }
void
log (level_t l, const std::string &format, tail ..._tail)
{
log (l, format::render (format, std::forward<tail> (_tail)...));
}
}