maxon::LoggerTypes::File()



  • Does someone have an example how to use the file logger?

    Specifically how to create one and then get a reference to the maxon::FileLoggerTypeInterface so that I can call its Init() method to set the URL.

    This is not covered in the LoggerInterface Manual.

    Thanks,
    Kent



  • Hello,

    Note : this log file seem to be only created when Cinema 4D shutdown only.

    You need three functions, one to create the logger, one to delete it, and one to use it.
    This example create a logger that will both show the message in the console and write it to the file (except for the method that only write to the file)

    When you use AddLoggerType you have to define a LOGGERTYPEINITCALLBACK it can be a lambda. There you can use the Init() function with the Url you want.

    You have to use the MAXON_INITIALIZATION macro like so to register the first two.

    namespace maxon
    {
    MAXON_INITIALIZATION(CreateLogger, FreeExampleLogger);
    } // end of maxon namespace
    

    Create the logger :

    // Global logger reference
    static maxon::LoggerRef g_exampleLoggerToFile;
    // define logger ID
    static maxon::Id g_loggerToFileID("net.maxonexample.loggertofile");
    
    
    static maxon::Result<void> CreateLogger()
    {
    
    	iferr_scope;
    
    	// check if a logger with that ID already exists
    	if (!maxon::Loggers::Get(g_loggerToFileID))
    	{
    		// 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_exampleLoggerToFile = maxon::LoggerRef::Create())
    		{
    			// if the logger could not be created, use the default logger instead
    			g_exampleLoggerToFile = maxon::Loggers::Default();
    		}
    		else
    		{
    			// if the logger could be created
    			// add the logger type "Application" to display logged messages in the "Console" window
    			g_exampleLoggerToFile.AddLoggerType(maxon::TARGETAUDIENCE::ALL, maxon::LoggerTypes::File(), [](maxon::LoggerTypeRef& logger) -> maxon::Result<void>
    				{
    					iferr_scope;
    
    					maxon::FileLoggerTypeRef fl = maxon::Cast<maxon::FileLoggerTypeRef>(logger);
    
    					// Creates the Url where the log will be write
    					maxon::Url temp = maxon::Application::GetUrl(maxon::APPLICATION_URLTYPE::PREFS_DIR) iferr_return;
    					temp.Append("plugins"_s).Append("test.log"_s) iferr_return;
    
    					fl.Set(maxon::LOGGERTYPEFLAGS::ALWAYSFLUSH, false) iferr_return;
    					fl.Set(maxon::LOGGERTYPEFLAGS::ENQUEUEWRITE, false) iferr_return;
    					fl.Init(temp) iferr_return;
    
    					return maxon::OK;
    				}
    			) iferr_return;
    			
    			// Add this logger to the application (console)
    			g_exampleLoggerToFile.AddLoggerType(maxon::TARGETAUDIENCE::ALL, maxon::LoggerTypes::Application()) iferr_return;
    
    
    			// Defines logger name
    			g_exampleLoggerToFile.SetName("Logging to a file"_s);
    			// Inserts the logger in the "Loggers" registry
    			maxon::Loggers::Insert(g_loggerToFileID, g_exampleLoggerToFile) iferr_return;
    
    		}
    
    	}
    
    	return maxon::OK;
    }
    
    

    free the logger:

    static void FreeExampleLogger()
    {
    	g_exampleLoggerToFile = nullptr;  // reference must be freed
    	
    	// make sure to also remove the entry of your logger from Loggers.
    	maxon::Loggers::Erase(g_loggerToFileID) iferr_ignore("Logger termination failure on shutdown");
    	
    }
    

    There's three way to retrieve a reference and write something:

    iferr_scope;
    
    	// Retrieves a ref to the logger so you can print something in the file that will not show on the console.
    	maxon::FileLoggerTypeRef fileLoggerRef;
    	for (const maxon::Pair<maxon::LoggerTypeRef, maxon::TARGETAUDIENCE>& pair : g_exampleLoggerToFile.GetLoggerTypes())
    	{
    		auto loggerRef = pair.GetFirst();
    		if (loggerRef.IsInstanceOf<maxon::FileLoggerTypeRef>())
    		{
    			fileLoggerRef = maxon::Cast<maxon::FileLoggerTypeRef>(loggerRef);
    			break;
    		}
    	}
    
    	if (fileLoggerRef == nullptr)
    	{
    		return maxon::UnknownError(MAXON_SOURCE_LOCATION, "Logger has no file logger type"_s);
    	}
    	// this Foo will not appear in the console.
    	fileLoggerRef.Write("Foo\n"_s, MAXON_SOURCE_LOCATION, maxon::WRITEMETA::DEFAULT) iferr_return;
    
    
    	if (g_exampleLoggerToFile == nullptr)
    		return maxon::NullptrError(MAXON_SOURCE_LOCATION);
    	// Writes Foo in the logger using the global variable. This will appear in both console and log file.
    	g_exampleLoggerToFile.Write(maxon::TARGETAUDIENCE::ALL, "Bar"_s, MAXON_SOURCE_LOCATION, maxon::WRITEMETA::CRITICAL) iferr_return;
    	
    	// Retrieve a loggerRef.
    	const maxon::LoggerRef logger = maxon::Loggers::Get(g_loggerToFileID);
    	// This last one message will be print out in both console and logfile.
    	logger.Write(maxon::TARGETAUDIENCE::ALL, "Last one"_s, MAXON_SOURCE_LOCATION, maxon::WRITEMETA::DEFAULT) iferr_return;
    
    	return maxon::OK;
    

    Cheers,
    Manuel



  • Hello,

    Note : this log file seem to be only created when Cinema 4D shutdown only.

    You need three functions, one to create the logger, one to delete it, and one to use it.
    This example create a logger that will both show the message in the console and write it to the file (except for the method that only write to the file)

    When you use AddLoggerType you have to define a LOGGERTYPEINITCALLBACK it can be a lambda. There you can use the Init() function with the Url you want.

    You have to use the MAXON_INITIALIZATION macro like so to register the first two.

    namespace maxon
    {
    MAXON_INITIALIZATION(CreateLogger, FreeExampleLogger);
    } // end of maxon namespace
    

    Create the logger :

    // Global logger reference
    static maxon::LoggerRef g_exampleLoggerToFile;
    // define logger ID
    static maxon::Id g_loggerToFileID("net.maxonexample.loggertofile");
    
    
    static maxon::Result<void> CreateLogger()
    {
    
    	iferr_scope;
    
    	// check if a logger with that ID already exists
    	if (!maxon::Loggers::Get(g_loggerToFileID))
    	{
    		// 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_exampleLoggerToFile = maxon::LoggerRef::Create())
    		{
    			// if the logger could not be created, use the default logger instead
    			g_exampleLoggerToFile = maxon::Loggers::Default();
    		}
    		else
    		{
    			// if the logger could be created
    			// add the logger type "Application" to display logged messages in the "Console" window
    			g_exampleLoggerToFile.AddLoggerType(maxon::TARGETAUDIENCE::ALL, maxon::LoggerTypes::File(), [](maxon::LoggerTypeRef& logger) -> maxon::Result<void>
    				{
    					iferr_scope;
    
    					maxon::FileLoggerTypeRef fl = maxon::Cast<maxon::FileLoggerTypeRef>(logger);
    
    					// Creates the Url where the log will be write
    					maxon::Url temp = maxon::Application::GetUrl(maxon::APPLICATION_URLTYPE::PREFS_DIR) iferr_return;
    					temp.Append("plugins"_s).Append("test.log"_s) iferr_return;
    
    					fl.Set(maxon::LOGGERTYPEFLAGS::ALWAYSFLUSH, false) iferr_return;
    					fl.Set(maxon::LOGGERTYPEFLAGS::ENQUEUEWRITE, false) iferr_return;
    					fl.Init(temp) iferr_return;
    
    					return maxon::OK;
    				}
    			) iferr_return;
    			
    			// Add this logger to the application (console)
    			g_exampleLoggerToFile.AddLoggerType(maxon::TARGETAUDIENCE::ALL, maxon::LoggerTypes::Application()) iferr_return;
    
    
    			// Defines logger name
    			g_exampleLoggerToFile.SetName("Logging to a file"_s);
    			// Inserts the logger in the "Loggers" registry
    			maxon::Loggers::Insert(g_loggerToFileID, g_exampleLoggerToFile) iferr_return;
    
    		}
    
    	}
    
    	return maxon::OK;
    }
    
    

    free the logger:

    static void FreeExampleLogger()
    {
    	g_exampleLoggerToFile = nullptr;  // reference must be freed
    	
    	// make sure to also remove the entry of your logger from Loggers.
    	maxon::Loggers::Erase(g_loggerToFileID) iferr_ignore("Logger termination failure on shutdown");
    	
    }
    

    There's three way to retrieve a reference and write something:

    iferr_scope;
    
    	// Retrieves a ref to the logger so you can print something in the file that will not show on the console.
    	maxon::FileLoggerTypeRef fileLoggerRef;
    	for (const maxon::Pair<maxon::LoggerTypeRef, maxon::TARGETAUDIENCE>& pair : g_exampleLoggerToFile.GetLoggerTypes())
    	{
    		auto loggerRef = pair.GetFirst();
    		if (loggerRef.IsInstanceOf<maxon::FileLoggerTypeRef>())
    		{
    			fileLoggerRef = maxon::Cast<maxon::FileLoggerTypeRef>(loggerRef);
    			break;
    		}
    	}
    
    	if (fileLoggerRef == nullptr)
    	{
    		return maxon::UnknownError(MAXON_SOURCE_LOCATION, "Logger has no file logger type"_s);
    	}
    	// this Foo will not appear in the console.
    	fileLoggerRef.Write("Foo\n"_s, MAXON_SOURCE_LOCATION, maxon::WRITEMETA::DEFAULT) iferr_return;
    
    
    	if (g_exampleLoggerToFile == nullptr)
    		return maxon::NullptrError(MAXON_SOURCE_LOCATION);
    	// Writes Foo in the logger using the global variable. This will appear in both console and log file.
    	g_exampleLoggerToFile.Write(maxon::TARGETAUDIENCE::ALL, "Bar"_s, MAXON_SOURCE_LOCATION, maxon::WRITEMETA::CRITICAL) iferr_return;
    	
    	// Retrieve a loggerRef.
    	const maxon::LoggerRef logger = maxon::Loggers::Get(g_loggerToFileID);
    	// This last one message will be print out in both console and logfile.
    	logger.Write(maxon::TARGETAUDIENCE::ALL, "Last one"_s, MAXON_SOURCE_LOCATION, maxon::WRITEMETA::DEFAULT) iferr_return;
    
    	return maxon::OK;
    

    Cheers,
    Manuel



  • Thanks Manuel.Adding that lambda did the trick. Working fine now.