[icinga-checkins] icinga.org: icinga2/master: Implemented the LONGDATETIME, SHORTDATETIME, DATE and TIME macros.

git at icinga.org git at icinga.org
Thu Feb 28 10:27:48 CET 2013


Module: icinga2
Branch: master
Commit: b674d46557378a2590d3b37e75bf6bad7332c6ff
URL:    https://git.icinga.org/?p=icinga2.git;a=commit;h=b674d46557378a2590d3b37e75bf6bad7332c6ff

Author: Gunnar Beutner <gunnar.beutner at netways.de>
Date:   Thu Feb 28 10:27:33 2013 +0100

Implemented the LONGDATETIME, SHORTDATETIME, DATE and TIME macros.

---

 lib/base/i2-base.h               |    1 +
 lib/base/streamlogger.cpp        |   19 +------------------
 lib/base/utility.cpp             |   24 ++++++++++++++++++++++++
 lib/base/utility.h               |    2 ++
 lib/icinga/icingaapplication.cpp |    8 +++++++-
 5 files changed, 35 insertions(+), 19 deletions(-)

diff --git a/lib/base/i2-base.h b/lib/base/i2-base.h
index a0c33dc..56a75e1 100644
--- a/lib/base/i2-base.h
+++ b/lib/base/i2-base.h
@@ -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;
diff --git a/lib/base/streamlogger.cpp b/lib/base/streamlogger.cpp
index 74afa94..27cf774 100644
--- a/lib/base/streamlogger.cpp
+++ b/lib/base/streamlogger.cpp
@@ -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);
 
diff --git a/lib/base/utility.cpp b/lib/base/utility.cpp
index f98ce18..ad7726a 100644
--- a/lib/base/utility.cpp
+++ b/lib/base/utility.cpp
@@ -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;
+}
+
diff --git a/lib/base/utility.h b/lib/base/utility.h
index 3b99f54..f901765 100644
--- a/lib/base/utility.h
+++ b/lib/base/utility.h
@@ -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
diff --git a/lib/icinga/icingaapplication.cpp b/lib/icinga/icingaapplication.cpp
index e1b92a6..66b6e09 100644
--- a/lib/icinga/icingaapplication.cpp
+++ b/lib/icinga/icingaapplication.cpp
@@ -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;
 }





More information about the icinga-checkins mailing list