Skip to content

Commit

Permalink
LogWriter.plugin: Can work regardless no PluginManager, FileUtility o…
Browse files Browse the repository at this point in the history
…r TextUtility plugins.

LogHelper.h: Add X3LOG_GROUP(name) and X3LOG_GROUP2(name, extra).
  • Loading branch information
rhcad committed Nov 8, 2011
1 parent 69a1a0e commit 0986229
Show file tree
Hide file tree
Showing 11 changed files with 148 additions and 50 deletions.
2 changes: 2 additions & 0 deletions ChangeLog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ x3c - C++ Plugin Framework

* Add GetLogObserver() export function in LogWriter.plugin
* 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).

2011-11-01

Expand Down
7 changes: 5 additions & 2 deletions code/pkg_Core/Interface/Log/Ix_LogManager.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*! \file Ix_LogManager.h
* \brief Define interface of logging output observer manager: Ix_LogManager
* \author Zhang Yungui, X3 C++ PluginFramework
* \date 2011.06.30
* \date 2011.11.08
*/
#ifndef X3_LOG_ILOGMANAGER_H_
#define X3_LOG_ILOGMANAGER_H_
Expand Down Expand Up @@ -46,10 +46,13 @@ class Ix_LogManager : public Ix_Object
if the first char is '@' and leading as 'Module:StrID' format
then the manager will translate into localization text using Ix_StringTable.
\param extra additional context info.
\param file source file name, __FILE__
\param line code line, __LINE__
\return true if successful.
\see X3LogGroup, PopGroup
*/
virtual bool PushGroup(const wchar_t* msg, const wchar_t* extra) = 0;
virtual bool PushGroup(const wchar_t* msg, const wchar_t* extra,
const char* file, long line) = 0;

//! Ending a logging group.
/*!
Expand Down
16 changes: 10 additions & 6 deletions code/pkg_Core/Interface/Log/Ix_LogObserver.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*! \file Ix_LogObserver.h
* \brief Define logging output observer interface: Ix_LogObserver
* \author Zhang Yungui, X3 C++ PluginFramework
* \date 2010.10.19
* \date 2011.11.08
*/
#ifndef X3_LOG_ILOGOBSERVER_H_
#define X3_LOG_ILOGOBSERVER_H_
Expand All @@ -27,10 +27,13 @@ class Ix_LogObserver
\param extra additional context info.
\param module module name extracting from group message text.
\param idname string id name extracting from group message text.
\param file source file name.
\param line code line in source file.
*/
virtual void OnPushGroup(long level,
const std::wstring& msg, const std::wstring& extra,
const std::wstring& module, const std::wstring& idname) = 0;
const std::wstring& module, const std::wstring& idname,
const char* file, long line) = 0;

//! Notification of ending a logging group.
/*!
Expand All @@ -51,7 +54,7 @@ class Ix_LogObserver
virtual void OnWriteLog(int type,
const std::wstring& msg, const std::wstring& extra,
const std::wstring& module, const std::wstring& idname,
const std::wstring& file, long line) = 0;
const char* file, long line) = 0;
};

//! Default implement of logging output observer.
Expand All @@ -66,9 +69,10 @@ class Cx_LogObserverImpl : public Ix_LogObserver

virtual void OnPushGroup(long level,
const std::wstring& msg, const std::wstring& extra,
const std::wstring& module, const std::wstring& idname)
const std::wstring& module, const std::wstring& idname,
const char* file, long line)
{
level; msg; extra; module; idname;
level; msg; extra; module; idname; file; line;
}

virtual void OnPopGroup(long level)
Expand All @@ -79,7 +83,7 @@ class Cx_LogObserverImpl : public Ix_LogObserver
virtual void OnWriteLog(int type,
const std::wstring& msg, const std::wstring& extra,
const std::wstring& module, const std::wstring& idname,
const std::wstring& file, long line)
const char* file, long line)
{
type; msg; extra; module; idname; file; line;
}
Expand Down
56 changes: 40 additions & 16 deletions code/pkg_Core/Interface/Log/LogHelper.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*! \file LogHelper.h
* \brief Define logging macros such as X3LOG_WARNING and X3LogGroup.
* \brief Define logging macros such as X3LOG_WARNING and X3LOG_GROUP.
* \author Zhang Yungui, X3 C++ PluginFramework
* \date 2011.07.01
* \date 2011.11.08
*/
#ifndef X3_LOG_LOGHELPER_H_
#define X3_LOG_LOGHELPER_H_
Expand All @@ -25,7 +25,8 @@
//
// x3::RegisterLogObserver(Ix_LogObserver* observer)
// x3::UnRegisterLogObserver(Ix_LogObserver* observer)
// X3LogGroup(msg)
// X3LOG_GROUP(name)
// X3LOG_GROUP2(name, extra)


//! Output a debug info.
Expand All @@ -34,7 +35,7 @@
\param msg logging text, can be UNICODE string or other type number (not ANSI string).
if the first char is '@' and leading as 'Module:StrID' format,
then the manager will translate into localization text.
\see X3LOG_DEBUG2, X3LogGroup
\see X3LOG_DEBUG2, X3LOG_GROUP
*/
#define X3LOG_DEBUG(msg) \
X3LOG_EVENT_(msg, x3LogType_Debug, __FILE__, __LINE__)
Expand All @@ -45,7 +46,7 @@
\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_INFO, X3LOG_DEBUG, X3LogGroup
\see X3LOG_INFO, X3LOG_DEBUG, X3LOG_GROUP
*/
#define X3LOG_DEBUG2(name, extra) \
X3LOG_EVENT_2(name, extra, x3LogType_Debug, __FILE__, __LINE__)
Expand All @@ -57,7 +58,7 @@
\param msg logging text, can be UNICODE string or other type number (not ANSI string).
if the first char is '@' and leading as 'Module:StrID' format,
then the manager will translate into localization text.
\see X3LOG_INFO2, X3LogGroup
\see X3LOG_INFO2, X3LOG_GROUP
*/
#define X3LOG_INFO(msg) \
X3LOG_EVENT_(msg, x3LogType_Info, __FILE__, __LINE__)
Expand All @@ -68,7 +69,7 @@
\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_WARNING2, X3LOG_INFO, X3LogGroup
\see X3LOG_WARNING2, X3LOG_INFO, X3LOG_GROUP
*/
#define X3LOG_INFO2(name, extra) \
X3LOG_EVENT_2(name, extra, x3LogType_Info, __FILE__, __LINE__)
Expand All @@ -80,7 +81,7 @@
\param msg logging text, can be UNICODE string or other type number (not ANSI string).
if the first char is '@' and leading as 'Module:StrID' format,
then the manager will translate into localization text.
\see X3LOG_WARNING2, X3LOG_INFO2, X3LogGroup
\see X3LOG_WARNING2, X3LOG_INFO2, X3LOG_GROUP
*/
#define X3LOG_WARNING(msg) \
X3LOG_EVENT_(msg, x3LogType_Warning, __FILE__, __LINE__)
Expand All @@ -91,7 +92,7 @@
\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_ERROR2, X3LogGroup
\see X3LOG_ERROR2, X3LOG_GROUP
*/
#define X3LOG_WARNING2(name, extra) \
X3LOG_EVENT_2(name, extra, x3LogType_Warning, __FILE__, __LINE__)
Expand All @@ -103,7 +104,7 @@
\param msg logging text, can be UNICODE string or other type number (not ANSI string).
if the first char is '@' and leading as 'Module:StrID' format,
then the manager will translate into localization text.
\see X3LOG_ERROR2, X3LogGroup
\see X3LOG_ERROR2, X3LOG_GROUP
*/
#define X3LOG_ERROR(msg) \
X3LOG_EVENT_(msg, x3LogType_Error, __FILE__, __LINE__)
Expand All @@ -114,7 +115,7 @@
\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_WARNING2, X3LogGroup
\see X3LOG_WARNING2, X3LOG_GROUP
*/
#define X3LOG_ERROR2(name, extra) \
X3LOG_EVENT_2(name, extra, x3LogType_Error, __FILE__, __LINE__)
Expand All @@ -126,7 +127,7 @@
\param msg logging text, can be UNICODE string or other type number (not ANSI string).
if the first char is '@' and leading as 'Module:StrID' format,
then the manager will translate into localization text.
\see X3LOG_FATAL2, X3LogGroup
\see X3LOG_FATAL2, X3LOG_GROUP
*/
#define X3LOG_FATAL(msg) \
X3LOG_EVENT_(msg, x3LogType_Fatal, __FILE__, __LINE__)
Expand All @@ -137,7 +138,7 @@
\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_ERROR2, X3LogGroup
\see X3LOG_ERROR2, X3LOG_GROUP
*/
#define X3LOG_FATAL2(name, extra) \
X3LOG_EVENT_2(name, extra, x3LogType_Fatal, __FILE__, __LINE__)
Expand Down Expand Up @@ -176,6 +177,27 @@ inline void UnRegisterLogObserver(Ix_LogObserver* observer)

} // x3

//! Helper macro for logging group, auto begin and end group.
/*! Use this macro in local function, eg:
\code
void MyFunc()
{
X3LOG_GROUP(L"@MyPlugin:GROUP_MYNAME");
X3LOG_INFO2(L"@MyPlugin:IDS_DO_THING", value);
...
X3LOG_GROUP2(L"@MyPlugin:GROUP_MYNAME", "info...");
}
\endcode
\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
*/
#define X3LOG_GROUP(name, extra) \
X3LOG_GROUP2(name, NULL)
#define X3LOG_GROUP2(name, extra) \
X3LogGroup group##__LINE__(name, extra, __FILE__, __LINE__)

//! Helper class for logging group, auto begin and end group.
/*! Use this class to define variable in local function, eg:
\code
Expand All @@ -186,7 +208,6 @@ inline void UnRegisterLogObserver(Ix_LogObserver* observer)
...
}
\endcode
\ingroup _GROUP_PLUGIN_LOG_
*/
class X3LogGroup
{
Expand All @@ -197,13 +218,16 @@ class X3LogGroup
if the first char is '@' and leading as 'Module:StrID' format
then the manager will translate into localization text using Ix_StringTable.
\param extra additional context info.
\param file source file name, __FILE__
\param line code line, __LINE__
*/
X3LogGroup(const wchar_t* msg, const wchar_t* extra = NULL)
X3LogGroup(const wchar_t* msg, const wchar_t* extra = NULL,
const char* file = NULL, long line = 0)
{
Cx_Interface<Ix_LogManager> pIFManager(x3::CLSID_LogManager);
if (pIFManager.IsNotNull())
{
pIFManager->PushGroup(msg, extra);
pIFManager->PushGroup(msg, extra, file, line);
}
}
//! Destructor, auto end the logging group.
Expand Down
21 changes: 11 additions & 10 deletions code/pkg_Core/Modules/LogManager/Cx_LogManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ void Cx_LogManager::FireFirstEvent()
}
}

bool Cx_LogManager::PushGroup(const wchar_t* msg, const wchar_t* extra)
bool Cx_LogManager::PushGroup(const wchar_t* msg, const wchar_t* extra,
const char* file, long line)
{
std::wstring msg2, extra2, module, idname;
CheckMsgParam(msg2, extra2, module, idname, msg, extra);
Expand All @@ -76,7 +77,7 @@ bool Cx_LogManager::PushGroup(const wchar_t* msg, const wchar_t* extra)

for (ObserverIt it = m_observers.begin(); it != m_observers.end(); ++it)
{
it->first->OnPushGroup(m_groupLevel, msg2, extra2, module, idname);
it->first->OnPushGroup(m_groupLevel, msg2, extra2, module, idname, file, line);
}

return true;
Expand All @@ -102,16 +103,16 @@ bool Cx_LogManager::WriteLog(x3LogType type, const wchar_t* msg,
return false;
}

std::wstring wstrFile(x3::a2w(TrimFileName(file)));
std::wstring msg2, extra2, module, idname;

CheckMsgParam(msg2, extra2, module, idname, msg, extra);
file = TrimFileName(file);
FireFirstEvent();

for (ObserverIt it = m_observers.begin(); it != m_observers.end(); ++it)
{
it->first->OnWriteLog(type, msg2, extra2,
module, idname, wstrFile, line);
module, idname, file, line);
}

#if defined(_DEBUG) && defined(OutputDebugString)
Expand Down Expand Up @@ -168,23 +169,23 @@ const char* Cx_LogManager::TrimFileName(const char* file)
{
ASSERT(file && *file);

const char* pszName = PathFindFileNameA(file);
const char* name = PathFindFileNameA(file);
int folder = 0;

while (pszName > file)
while (name > file)
{
pszName--;
if ('\\' == *pszName || '/' == *pszName)
name--;
if ('\\' == *name || '/' == *name)
{
if (++folder > 2)
{
pszName++;
name++;
break;
}
}
}

return pszName;
return name;
}

bool Cx_LogManager::CheckMsgParam(std::wstring& msg2,
Expand Down
3 changes: 2 additions & 1 deletion code/pkg_Core/Modules/LogManager/Cx_LogManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ class Cx_LogManager
private:
virtual bool RegisterObserver(Ix_LogObserver* observer, HMODULE fromdll);
virtual void UnRegisterObserver(Ix_LogObserver* observer);
virtual bool PushGroup(const wchar_t* msg, const wchar_t* extra);
virtual bool PushGroup(const wchar_t* msg, const wchar_t* extra,
const char* file, long line);
virtual bool PopGroup();
virtual bool WriteLog(x3LogType type, const wchar_t* msg,
const wchar_t* extra, const char* file, long line);
Expand Down
Loading

0 comments on commit 0986229

Please sign in to comment.