Skip to content

Commit

Permalink
Add LogHelper2.h for non-plugin projects.
Browse files Browse the repository at this point in the history
  • Loading branch information
rhcad committed Nov 8, 2011
1 parent 0986229 commit 270116e
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 4 deletions.
1 change: 1 addition & 0 deletions ChangeLog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ x3c - C++ Plugin Framework
* Auto call x3UninitializePlugin() when a plugin is unloading.
* LogWriter.plugin: Can work regardless no PluginManager, FileUtility or TextUtility plugins.
* LogHelper.h: Add X3LOG_GROUP(name) and X3LOG_GROUP2(name, extra).
* Add LogHelper2.h for non-plugin projects.

2011-11-01

Expand Down
12 changes: 8 additions & 4 deletions code/pkg_Core/Interface/Log/LogHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,16 @@ inline void UnRegisterLogObserver(Ix_LogObserver* observer)
\ingroup _GROUP_PLUGIN_LOG_
\param name UNICODE string, the first char is '@' and leading as 'Module:StrID' format.
\param extra additional context info, can be UNICODE string or other type number (not ANSI string).
\see X3LOG_GROUP2, X3LOG_ERROR
\see X3LOG_GROUP, X3LOG_ERROR
*/
#define X3LOG_GROUP(name, extra) \
X3LOG_GROUP2(name, NULL)
#define X3LOG_GROUP2(name, extra) \
X3LogGroup group##__LINE__(name, extra, __FILE__, __LINE__)
std::wostringstream _buf; \
_buf << extra; \
X3LogGroup group##__LINE__(name, _buf.str().c_str(), __FILE__, __LINE__)

//! \copydoc X3LOG_GROUP2
#define X3LOG_GROUP(name) \
X3LogGroup group##__LINE__(name, NULL, __FILE__, __LINE__)

//! Helper class for logging group, auto begin and end group.
/*! Use this class to define variable in local function, eg:
Expand Down
168 changes: 168 additions & 0 deletions code/pkg_Core/Interface/Log/LogHelper2.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
/*! \file LogHelper2.h
* \brief Define logging macros such as LOG_GROUP2 and LOG_INFO for non-plugin projects.
* \note Just need LogWriter.plugin for non-plugin system to use logging function.
* \author Zhang Yungui, X3 C++ PluginFramework
* \date 2011.11.08
*/
#ifndef X3_LOG_LOGHELPER2_H_
#define X3_LOG_LOGHELPER2_H_

// msg: UNICODE string. eg: L"some text".
// extra: additional context info, can be UNICODE string or aother type number (not ANSI string).
// eg: L"message" << intvalue << L", " << floatnum);
//
// LOG_GROUP0;
// LOG_GROUP1(msg);
// LOG_GROUP2(msg, extra);
// LOG_INFO(msg);
// LOG_INFO2(msg, extra);
// LOG_WARNING(msg);
// LOG_WARNING2(msg, extra);
// LOG_ERROR(msg);
// LOG_ERROR2(msg, extra);
//

#include <sstream>

#define LOG_GROUP1(msg) \
CLogGroup log##__LINE__(msg, L"", __FILE__, __LINE__)

#define LOG_GROUP0 LOG_GROUP1(L"")

#define LOG_GROUP2(name, extra) \
std::wostringstream _buf; \
_buf << extra; \
CLogGroup log##__LINE__(name, _buf.str().c_str(), __FILE__, __LINE__)

#define LOG_INFO(msg) \
LOG_EVENT_(1, msg, __FILE__, __LINE__)

#define LOG_INFO2(name, extra) \
LOG_EVENT_2(1, name, extra, __FILE__, __LINE__)

#define LOG_WARNING(msg) \
LOG_EVENT_(2, msg, __FILE__, __LINE__)

#define LOG_WARNING2(name, extra) \
LOG_EVENT_2(2, name, extra, __FILE__, __LINE__)

#define LOG_ERROR(msg) \
LOG_EVENT_(3, msg, __FILE__, __LINE__)

#define LOG_ERROR2(name, extra) \
LOG_EVENT_2(3, name, extra, __FILE__, __LINE__)

//-------------------------------

void PushLogGroup(const wchar_t* name, const wchar_t* extra, const char* file, int line);
void PopLogGroup();
void WriteLogItem(int type, const wchar_t* name, const wchar_t* extra, const char*, int);

class CLogGroup
{
public:
CLogGroup(const wchar_t* name, const wchar_t* extra, const char* file, int line)
{
PushLogGroup(name, extra, file, line);
}
~CLogGroup()
{
PopLogGroup();
}
};

#define LOG_EVENT_(type, msg, file, line) \
do { \
std::wostringstream _buf; \
_buf << msg; \
WriteLogItem(type, _buf.str().c_str(), L"", file, line); \
} while (0)

#define LOG_EVENT_2(type, name, extra, file, line) \
do { \
std::wostringstream _buf; \
_buf << extra; \
WriteLogItem(type, name, _buf.str().c_str(), file, line); \
} while (0)

//-------------------------------

#include "Ix_LogObserver.h"

class LogPluginHelper
{
private:
HMODULE dll;
Ix_LogObserver* observer;

LogPluginHelper() : dll(NULL), observer(NULL)
{
}

public:
~LogPluginHelper()
{
if (dll)
{
FreeLibrary(dll);
}
}

Ix_LogObserver* GetObserver(bool load = false)
{
if (load && !observer)
{
typedef Ix_LogObserver* (*FUNC)();
dll = LoadLibraryW(L"LogWriter.plugin.dll");
FUNC func = (FUNC)GetProcAddress(dll, "GetLogObserver");
observer = func ? func() : NULL;
}

return observer;
}

static LogPluginHelper& Instance()
{
static LogPluginHelper obj;
obj.GetObserver(true);
return obj;
}

static long& Level()
{
static long level = 0;
return level;
}
};

inline void PushLogGroup(const wchar_t* name, const wchar_t* extra,
const char* file, int line)
{
Ix_LogObserver* observer = LogPluginHelper::Instance().GetObserver();
if (observer)
{
observer->OnPushGroup(
++LogPluginHelper::Instance().Level(), name, extra, L"", L"", file, line);
}
}

inline void PopLogGroup()
{
Ix_LogObserver* observer = LogPluginHelper::Instance().GetObserver();
if (observer)
{
observer->OnPopGroup(LogPluginHelper::Instance().Level()--);
}
}

inline void WriteLogItem(int type, const wchar_t* name, const wchar_t* extra,
const char* file, int line)
{
Ix_LogObserver* observer = LogPluginHelper::Instance().GetObserver();
if (observer)
{
observer->OnWriteLog(type, name, extra, L"", L"", file, line);
}
}

#endif // X3_LOG_LOGHELPER2_H_

0 comments on commit 270116e

Please sign in to comment.