LoggerInterface Manual

内容表

关于

The logger interface allows to create new custom loggers for specific tasks and to access existing loggers. Loggers are presented in the "Console" window in Cinema 4D and registered at the maxon::Loggers registry.

Messages can be sent to the default logger using ApplicationOutput() .

Custom Loggers

A new custom logger can be created with maxon::LoggerRef::Create() and is then registered at maxon::Loggers . A logger type defines the target of the logger (see Logger Types ).

A custom logger is configured with:

// This example creates a new custom logger and adds it to the registered loggers. // g_exampleLogger is a maxon::LoggerRef instance.

// define logger ID const maxon::Id loggerID { "net.maxonexample.logger" };

// check if a logger with that ID already exists if (! maxon::Loggers::Get (loggerID)) { // create the new logger and store it in a global variable for later access // make sure to set this object to nullptr when Cinema shuts down iferr (g_exampleLogger = maxon::LoggerRef::Create()) { // if the logger could not be created, use the default logger instead g_exampleLogger = maxon::Loggers::Default(); } else { // if the logger could be created // add the logger type "Application" to display logged messages in the "Console" window g_exampleLogger.AddLoggerType( maxon::TARGETAUDIENCE::ALL , maxon::LoggerTypes::Application()) iferr_return ;

// define logger name g_exampleLogger.SetName( "Example Logger" _s); // insert the logger in the "Loggers" registry maxon::Loggers::Insert(loggerID, g_exampleLogger) iferr_return ;

// this message must be send to update the console window const maxon::Int32 BFM_REBUILDCONSOLETREE = 334295845; SpecialEventAdd (BFM_REBUILDCONSOLETREE); } }

// This example shows how to free the logger reference.

static void FreeExampleLogger() { g_exampleLogger = nullptr ; // reference must be freed

// make sure to also remove the entry of your logger from Loggers. maxon::Loggers::Erase( maxon::Id { "net.maxonexample.logger" }) iferr_ignore ( "" ); }

MAXON_INITIALIZATION ( nullptr , FreeExampleLogger);

Using Custom Loggers

A message is sent to a custom logger using the Write() function:

// This example writes a message to a custom logger.

// write to a custom logger referenced in g_exampleLogger g_exampleLogger.Write( maxon::TARGETAUDIENCE::ALL , "Foo" _s, MAXON_SOURCE_LOCATION , maxon::WRITEMETA::CRITICAL ) iferr_return ;

// or write to the logger by getting the logger using the logger ID

// get logger const maxon::Id loggerId { "net.maxonexample.logger" }; const maxon::LoggerRef logger = maxon::Loggers::Get (loggerId); logger.Write( maxon::TARGETAUDIENCE::ALL , "Bar" _s, MAXON_SOURCE_LOCATION , maxon::WRITEMETA::DEFAULT ) iferr_return ;

Built-In Loggers

All existing loggers are registered at maxon::Loggers . These three loggers are there for public use:

注意
The default logger is used with ApplicationOutput() .
// This example sends a message to the default logger. ApplicationOutput ( "Some Message." _s);

Logger Types

A logger type implements the actual processing of a given message e.g. if that message is displayed in the GUI or written to a file.

Logger types are based on maxon::LoggerTypeInterface :

// This example adds an observer to the given logger. This observer // will be called if a message is sent to that logger.

for ( const auto & loggerTypeAudience : logger.GetLoggerTypes()) { const maxon::LoggerTypeRef loggerType = loggerTypeAudience.GetFirst(); // get first element of the pair loggerType.ObservableLoggerNotify().AddObserver(LoggerCallback) iferr_return ; }

maxon::UserLoggerTypeInterface is the logger type that displays the message in Cinema 4D 's "Console" window:

maxon::FileLoggerTypeInterface is the logger type that writes the message to a file:

// This example accesses the default logger and gets all displayed lines.

// get default logger const maxon::LoggerRef defaultLogger = maxon::Loggers::Default(); for ( const auto & logger : defaultLogger.GetLoggerTypes()) { const maxon::LoggerTypeRef loggerType = logger.GetFirst(); // get first element of the pair

if (loggerType.IsInstanceOf<maxon::UserLoggerTypeRef>()) { const maxon::UserLoggerTypeRef userLoggerType = maxon::Cast<maxon::UserLoggerTypeRef>(loggerType);

// iterate over all lines sent to the logger userLoggerType.Iterate([]( maxon::LoggerLine & line) -> maxon::Result<void> { DiagnosticOutput ( "Line: @" , line. _str ); return maxon::OK ; }) iferr_return ; } }

延伸阅读

maxon::LoggerLine
Each object of LoggerLine represents an entry in the logger with several meta information....
定义: logger.h:55
maxon::WRITEMETA::CRITICAL
@ CRITICAL
maxon::OK
return OK
定义: apibase.h:2532
SpecialEventAdd
void SpecialEventAdd(Int32 messageid, UInt p1=0, UInt p2=0)
maxon::Id
定义: apibaseid.h:250
iferr_return
#define iferr_return
定义: resultbase.h:1434
MAXON_SOURCE_LOCATION
#define MAXON_SOURCE_LOCATION
定义: memoryallocationbase.h:66
DiagnosticOutput
#define DiagnosticOutput(formatString,...)
定义: debugdiagnostics.h:166
MAXON_INITIALIZATION
#define MAXON_INITIALIZATION(...)
定义: module.h:735
maxon::TARGETAUDIENCE::ALL
@ ALL
maxon::Result< void >
iferr_ignore
#define iferr_ignore(...)
定义: resultbase.h:1399
maxon::Int32
int32_t Int32
32 bit signed integer datatype.
定义: apibase.h:172
maxon::Classes::Get
const Class< R > & Get(const Id &cls)
定义: objectbase.h:1903
ApplicationOutput
#define ApplicationOutput(formatString,...)
定义: debugdiagnostics.h:207
iferr
#define iferr(...)
定义: errorbase.h:380
maxon::LoggerLine::_str
String _str
定义: logger.h:58
maxon::WRITEMETA::DEFAULT
@ DEFAULT

Copyright  © 2014-2025 乐数软件    

工业和信息化部: 粤ICP备14079481号-1