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 <map>
#include <string> #include <string>
#include <boost/format.hpp>
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
static util::level_t static util::level_t
@ -95,45 +93,31 @@ log_level (void)
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
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);
//----------------------------------------------------------------------------- if (0 == strftime (&time_string[0],
void time_len,
util::detail::log (util::level_t level, boost::format &&format) "%Y-%m-%d %H%Mh%S",
{ localtime (&unix_time))) {
static const util::level_t LOG_LEVEL = log_level (); warn ("failed to log time");
if (level > LOG_LEVEL) return;
return; }
static const boost::format LEVEL_FORMAT ("%s [%s] "); std::cerr << time_string << " [" << level << "] " << msg << std::endl;
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;
} }
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
util::scoped_logger::scoped_logger (util::level_t _level, util::scoped_logger::scoped_logger (util::level_t _level,
std::string &&_message): std::string _message):
m_level (_level), m_level (_level),
m_message (std::move (_message)) m_message (std::move (_message))
{ ; } { ; }
@ -148,9 +132,9 @@ util::scoped_logger::~scoped_logger ()
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
util::scoped_timer::scoped_timer (util::level_t _level, util::scoped_timer::scoped_timer (util::level_t _level,
std::string &&_message): std::string _message):
m_level (_level), m_level (_level),
m_message (_message), m_message (std::move (_message)),
m_start (util::nanoseconds ()) 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> template <typename ...tail>
void log (level_t, const std::string &format, tail ..._tail); void log (level_t, const std::string &format, tail ..._tail);
@ -79,7 +79,7 @@ namespace util {
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
class scoped_logger : public nocopy { class scoped_logger : public nocopy {
public: public:
scoped_logger (const level_t, std::string&&); scoped_logger (const level_t, std::string);
~scoped_logger (); ~scoped_logger ();
protected: protected:
@ -91,7 +91,7 @@ namespace util {
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
class scoped_timer : public nocopy { class scoped_timer : public nocopy {
public: public:
scoped_timer (const level_t, std::string&&); scoped_timer (const level_t, std::string);
~scoped_timer (); ~scoped_timer ();
private: private:

27
log.ipp
View File

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