1 // formatter_time.hpp 2 3 // Boost Logging library 4 // 5 // Author: John Torjo, www.torjo.com 6 // 7 // Copyright (C) 2007 John Torjo (see www.torjo.com for email) 8 // 9 // Distributed under the Boost Software License, Version 1.0. 10 // (See accompanying file LICENSE_1_0.txt or copy at 11 // http://www.boost.org/LICENSE_1_0.txt) 12 // 13 // See http://www.boost.org for updates, documentation, and revision history. 14 // See http://www.torjo.com/log2/ for more details 15 16 17 #ifndef JT28092007_formatter_time_strf_HPP_DEFINED 18 #define JT28092007_formatter_time_strf_HPP_DEFINED 19 20 #include <hpx/util/logging/detail/fwd.hpp> 21 #include <hpx/util/logging/format/formatter/convert_format.hpp> 22 #include <hpx/util/logging/detail/manipulator.hpp> // is_generic 23 #include <string> 24 #include <stdio.h> 25 #include <time.h> 26 27 namespace hpx { namespace util { namespace logging { namespace formatter { 28 29 30 /** 31 @brief Prefixes the message with the time, by using strftime function. 32 You pass the format string at construction. 33 34 @param msg_type The type that holds your logged message. 35 36 @param convert [optional] In case there needs to be a conversion between 37 std::(w)string and the string that holds your logged message. See convert_format. 38 For instance, you might use @ref hpx::util::logging::optimize::cache_string_one_str 39 "a cached_string class" (see @ref hpx::util::logging::optimize "optimize namespace"). 40 */ 41 template<class convert = do_convert_format::prepend> struct time_strf_t : is_generic { 42 typedef convert convert_type; 43 44 /** 45 constructs a time_strf object 46 47 @param format the time format , strftime-like 48 @param localtime if true, use localtime, otherwise global time 49 */ time_strf_thpx::util::logging::formatter::time_strf_t50 time_strf_t(const std::string & format, bool localtime) 51 : m_format (format), m_localtime (localtime) 52 {} 53 operator ()hpx::util::logging::formatter::time_strf_t54 void operator()(msg_type & msg) const { 55 char buffer[64]; 56 ::time_t t = ::time (nullptr); 57 ::tm t_details = m_localtime ? *localtime( &t) : *gmtime( &t); 58 if (0 != strftime (buffer, sizeof (buffer), m_format.c_str (), &t_details)) 59 convert::write(buffer, msg); 60 } 61 operator ==hpx::util::logging::formatter::time_strf_t62 bool operator==(const time_strf_t & other) const { 63 return m_format == other.m_format; 64 } 65 66 private: 67 std::string m_format; 68 bool m_localtime; 69 70 }; 71 72 73 74 /** @brief time_strf_t with default values. See time_strf_t 75 76 @copydoc time_strf_t 77 */ 78 typedef time_strf_t<> time_strf; 79 80 }}}} 81 82 #endif 83