My Project
Public Types | Public Member Functions | Static Public Member Functions | List of all members
ParaEngine::CServiceLogger Class Reference

CServiceLogger is a more advanced multi-threaded logger than the simple CLogger it supports log level, formatting, log time output, etc. More...

#include <LogService.h>

Inheritance diagram for ParaEngine::CServiceLogger:
ParaEngine::CLogger ParaEngine::intrusive_ptr_thread_safe_base

Public Types

typedef ParaIntrusivePtr< CServiceLoggerCServiceLogger_ptr_type
 

Public Member Functions

 CServiceLogger (const char *filename=NULL, bool bAppendMode=true)
 
void WriteServiceFormated (const char *,...)
 it will write an ordinary formated message to service log with current date time prepended. More...
 
- Public Member Functions inherited from ParaEngine::CLogger
void AddLogStr (const char *pStr)
 [thread safe]
 
void AddLogStr (const wchar_t *pStr)
 
void AddLogStr_st (const char *pStr)
 single threaded version
 
void AddLogStr_st (const wchar_t *pStr)
 
void SetLogFile (const std::string &sFile)
 change the log file. More...
 
const std::string & GetLogFile ()
 
void WriteFormated (const char *,...)
 output to log file a formatted string [thread safe]
 
void WriteFormatedVarList (const char *, va_list args)
 
void WriteFormated (const wchar_t *,...)
 
void WriteFormated_WithTime (const char *,...)
 
void WriteFormated_st (const char *,...)
 single threaded version
 
void WriteFormated_st (const wchar_t *,...)
 
int Write (const char *buf, int nLength)
 write byte array [thread safe] More...
 
int Write_st (const char *buf, int nLength)
 single threaded version
 
int GetPos ()
 get the current log file position. More...
 
const char * GetLog (int fromPos, int nCount)
 get log text between two Log positions. More...
 
void CloseLog ()
 close the log file handle
 
FILE * GetLogFileHandle ()
 get the log file handle, and seek to end of file for immediate writing. More...
 
void WriteDebugStr (const char *zFormat, const char *str, const char *sFile, int nLine)
 output a string to log file with a given file name and line number in a given format,when compiled in debug mode e.g. More...
 
const std::string & GetName ()
 Get the logger name.
 
void SetAppendMode (bool bAppendToExistingFile)
 by default, append mode is enabled in server mode, and disabled in client build. More...
 
void SetForceFlush (bool bForceFlush)
 if true we will flush the new log to file immediately. More...
 
int GetLevel ()
 Returns the assigned Level. More...
 
void SetLevel (const int level1)
 set level of this logger. More...
 
bool IsEnabledFor (int level)
 Check whether this logger is enabled for a given Level passed as parameter. More...
 
void log (const int level, const std::string &message, const ParaEngine::LogDetail::LocationInfo &location)
 This is the most generic printing method. More...
 
void log (const int level, const char *message, const ParaEngine::LogDetail::LocationInfo &location)
 
void log (const int level, const std::string &message)
 This is the most generic printing method. More...
 
void log (const int level, const char *message)
 
void ForcedLog (const int level, const std::string &message, const ParaEngine::LogDetail::LocationInfo &location)
 This method creates a new logging event and logs the event without further checks. More...
 
void ForcedLog (const int level, const char *message, const ParaEngine::LogDetail::LocationInfo &location)
 
void ForcedLog (const int level, const std::string &message)
 This method creates a new logging event and logs the event without further checks. More...
 
void ForcedLog (const int level, const char *message)
 

Static Public Member Functions

static CServiceLogger_ptr_type GetLogger (const std::string &name)
 Retrieve a logger by name in current encoding. More...
 
static CServiceLogger_ptr_type GetLogger (const char *const name)
 Retrieve a logger by name in current encoding. More...
 
- Static Public Member Functions inherited from ParaEngine::CLogger
static CLoggerGetSingleton ()
 get the default application logger. More...
 

Additional Inherited Members

- Public Attributes inherited from ParaEngine::intrusive_ptr_thread_safe_base
boost::detail::atomic_count m_ref_count
 
- Protected Member Functions inherited from ParaEngine::intrusive_ptr_thread_safe_base
 intrusive_ptr_thread_safe_base (const intrusive_ptr_thread_safe_base &)
 
intrusive_ptr_thread_safe_baseoperator= (const intrusive_ptr_thread_safe_base &)
 
- Protected Attributes inherited from ParaEngine::CLogger
std::string m_log_file_name
 
FILE * m_file_handle
 
char m_buffer [MAX_DEBUG_STRING_LENGTH *2+2]
 
mutex m_mutex
 
bool m_is_first_time_open
 
bool m_bForceFlush
 if true we will flush the new log to file immediately. More...
 
std::string m_name
 The name of this logger. More...
 
int m_level
 The assigned level of this logger. More...
 

Detailed Description

CServiceLogger is a more advanced multi-threaded logger than the simple CLogger it supports log level, formatting, log time output, etc.

Please use CLogService to create an instance of CServiceLogger

Note
: All server daemon can GetLogger on startup (only once), keep the CServiceLogger_ptr and use it for logging thereafterwards. Example: keep the g_router_logger, and use it for logging, do not call GetLogger again. CServiceLogger_ptr g_router_logger = CLogService::GetLogger("router"); create an unnamed logger. CServiceLogger_ptr logger(new CServiceLogger("mylog.txt", false));

SERVICE_LOG(g_router_logger, 0, "some message1 here"); SERVICE_LOG(g_router_logger, 0, "some message2 here");

Another note: service log do not provide formatted string like application log. Such design is deliberate because snprintf is a slow function and should be used sparingly on server side. If you do need to use formated string, use

    SERVICE_LOG1(g_router_logger, "Hello");
    SERVICE_LOG1(g_router_logger, "Hello %s", "ParaEngine");

Constructor & Destructor Documentation

§ CServiceLogger()

ParaEngine::CServiceLogger::CServiceLogger ( const char *  filename = NULL,
bool  bAppendMode = true 
)
Parameters
filenamethe file name, such as "mylog.txt"
bAppendModeif true, we will append log to the existing one.

Member Function Documentation

§ GetLogger() [1/2]

ParaEngine::CServiceLogger_ptr ParaEngine::CServiceLogger::GetLogger ( const std::string &  name)
static

Retrieve a logger by name in current encoding.

Parameters
namelogger name.

§ GetLogger() [2/2]

ParaEngine::CServiceLogger_ptr ParaEngine::CServiceLogger::GetLogger ( const char *const  name)
static

Retrieve a logger by name in current encoding.

Parameters
namelogger name.

§ WriteServiceFormated()

void ParaEngine::CServiceLogger::WriteServiceFormated ( const char *  zFormat,
  ... 
)

it will write an ordinary formated message to service log with current date time prepended.

Please note internally it uses snprintf. However, snprintf is a slow function (which needs to retrieve the current system locale) and should be used sparingly on server side.


The documentation for this class was generated from the following files: