Implemented the LONGDATETIME, SHORTDATETIME, DATE and TIME macros.

This commit is contained in:
Gunnar Beutner 2013-02-28 10:27:33 +01:00
parent 5300bf8473
commit b674d46557
5 changed files with 35 additions and 19 deletions

View File

@ -143,6 +143,7 @@ using std::type_info;
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/key_extractors.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
using boost::shared_ptr;
using boost::weak_ptr;

View File

@ -76,24 +76,7 @@ void StreamLogger::OpenFile(const String& filename)
*/
void StreamLogger::ProcessLogEntry(ostream& stream, bool tty, const LogEntry& entry)
{
char timestamp[100];
time_t ts = entry.Timestamp;
tm tmnow;
#ifdef _WIN32
tm *temp = localtime(&ts);
if (temp == NULL)
BOOST_THROW_EXCEPTION(PosixException("localtime() failed", errno));
tmnow = *temp;
#else /* _WIN32 */
if (localtime_r(&ts, &tmnow) == NULL)
BOOST_THROW_EXCEPTION(PosixException("localtime_r() failed.", errno));
#endif /* _WIN32 */
strftime(timestamp, sizeof(timestamp), "%Y/%m/%d %H:%M:%S %z", &tmnow);
String timestamp = Utility::FormatDateTime("%Y/%m/%d %H:%M:%S %z", entry.Timestamp);
boost::mutex::scoped_lock lock(m_Mutex);

View File

@ -553,3 +553,27 @@ void Utility::QueueAsyncCallback(const boost::function<void (void)>& callback)
{
Application::GetEQ().Post(callback);
}
String Utility::FormatDateTime(const char *format, double ts)
{
char timestamp[128];
time_t tempts = (time_t)ts; /* We don't handle sub-second timestamp here just yet. */
tm tmthen;
#ifdef _MSC_VER
tm *temp = localtime(&tempts);
if (temp == NULL)
BOOST_THROW_EXCEPTION(PosixException("localtime() failed", errno));
tmthen = *temp;
#else /* _MSC_VER */
if (localtime_r(&tempts, &tmthen) == NULL)
BOOST_THROW_EXCEPTION(PosixException("localtime_r() failed.", errno));
#endif /* _MSC_VER */
strftime(timestamp, sizeof(timestamp), format, &tmthen);
return timestamp;
}

View File

@ -60,6 +60,8 @@ public:
static void QueueAsyncCallback(const boost::function<void (void)>& callback);
static String FormatDateTime(const char *format, double ts);
static
#ifdef _WIN32
HMODULE

View File

@ -157,7 +157,13 @@ Dictionary::Ptr IcingaApplication::CalculateDynamicMacros(const IcingaApplicatio
{
Dictionary::Ptr macros = boost::make_shared<Dictionary>();
macros->Set("TIMET", (long)Utility::GetTime());
double now = Utility::GetTime();
macros->Set("TIMET", (long)now);
macros->Set("LONGDATETIME", Utility::FormatDateTime("%Y-%m-%d %H:%M:%S %z", now));
macros->Set("SHORTDATETIME", Utility::FormatDateTime("%Y-%m-%d %H:%M:%S", now));
macros->Set("DATE", Utility::FormatDateTime("%Y-%m-%d", now));
macros->Set("TIME", Utility::FormatDateTime("%H:%M:%S %z", now));
return macros;
}