summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArnon Gilboa <agilboa@redhat.com>2011-11-21 09:48:50 +0200
committerArnon Gilboa <agilboa@redhat.com>2011-11-21 14:10:55 +0200
commit017482d779ed087326f40d6379a990de5d65234d (patch)
tree4d39dffc49ff4cebef3713da6740409774ed69e4
parent28d7028278f8ca061c6dee9934901eaa035ef7bb (diff)
vdlog: change log times to human readable date & time rhbz#672828
-use RHEV log format -add log levels & macros -remove LOG_ENABLED ifdefs
-rw-r--r--common/vdlog.cpp4
-rw-r--r--common/vdlog.h62
2 files changed, 47 insertions, 19 deletions
diff --git a/common/vdlog.cpp b/common/vdlog.cpp
index 1001de3..8ece384 100644
--- a/common/vdlog.cpp
+++ b/common/vdlog.cpp
@@ -40,7 +40,6 @@ VDLog::~VDLog()
VDLog* VDLog::get(TCHAR* path)
{
-#ifdef LOG_ENABLED
if (_log || !path) {
return _log;
}
@@ -64,9 +63,6 @@ VDLog* VDLog::get(TCHAR* path)
}
_log = new VDLog(handle);
return _log;
-#else
- return NULL;
-#endif
}
void VDLog::printf(const char* format, ...)
diff --git a/common/vdlog.h b/common/vdlog.h
index bb2eb28..5a794b6 100644
--- a/common/vdlog.h
+++ b/common/vdlog.h
@@ -22,8 +22,8 @@
#include <tchar.h>
#include <crtdbg.h>
#include <windows.h>
-
-#define LOG_ENABLED
+#include <time.h>
+#include <sys/timeb.h>
class VDLog {
public:
@@ -39,23 +39,55 @@ private:
FILE* _handle;
};
-#ifdef LOG_ENABLED
-#define vd_printf(format, ...) { \
- VDLog* log = VDLog::get(); \
- double secs = GetTickCount() / 1000.0; \
- if (log) { \
- log->printf("%.3f %s: " format "\n", secs, __FUNCTION__, __VA_ARGS__); \
- } else { \
- printf("%.3f %s: " format "\n", secs, __FUNCTION__, __VA_ARGS__); \
- } \
-}
+enum {
+ LOG_DEBUG,
+ LOG_INFO,
+ LOG_WARN,
+ LOG_ERROR,
+ LOG_FATAL
+};
-#define ASSERT(x) _ASSERTE(x)
+#ifdef _DEBUG
+static unsigned int log_level = LOG_DEBUG;
#else
-#define vd_printf(format, ...)
-#define ASSERT(x)
+static unsigned int log_level = LOG_INFO;
#endif
+#define PRINT_LINE(type, format, datetime, ms, ...) \
+ printf("%u::%s::%s,%.3d::%s::" format "\n", GetCurrentThreadId(), type, datetime, ms, \
+ __FUNCTION__, ## __VA_ARGS__);
+
+#define LOG(type, format, ...) if (type >= log_level && type <= LOG_FATAL) { \
+ VDLog* log = VDLog::get(); \
+ const char *type_as_char[] = { "DEBUG", "INFO", "WARN", "ERROR", "FATAL" }; \
+ struct _timeb now; \
+ struct tm today; \
+ char datetime_str[20]; \
+ _ftime_s(&now); \
+ localtime_s(&today, &now.time); \
+ strftime(datetime_str, 20, "%Y-%m-%d %H:%M:%S", &today); \
+ if (log) { \
+ log->PRINT_LINE(type_as_char[type], format, datetime_str, now.millitm, ## __VA_ARGS__); \
+ } else { \
+ PRINT_LINE(type_as_char[type], format, datetime_str, now.millitm, ## __VA_ARGS__); \
+ } \
+}
+
+#define vd_printf(format, ...) LOG(LOG_INFO, format, ## __VA_ARGS__)
+#define LOG_INFO(format, ...) LOG(LOG_INFO, format, ## __VA_ARGS__)
+#define LOG_WARN(format, ...) LOG(LOG_WARN, format, ## __VA_ARGS__)
+#define LOG_ERROR(format, ...) LOG(LOG_ERROR, format, ## __VA_ARGS__)
+
+#define DBGLEVEL 1000
+
+#define DBG(level, format, ...) { \
+ if (level <= DBGLEVEL) { \
+ LOG(LOG_DEBUG, format, ## __VA_ARGS__); \
+ } \
+}
+
+#define ASSERT(x) _ASSERTE(x)
+
void log_version();
#endif