Logging an event or information
libMagAO-X implements a binary logging system, using the co-developed "flatlogs" format and the "flatbuffers" binary protocol from google. Any class derived from MagAOX::app::MagAOXApp has two log methods defined which can be used as follows.
- To log an event, such as a control loop closing:
- To log information, such as a software error:
log<software_error>({__FILE__, __LINE__, errno});
Here we see that the log system constructs a message from a "brace enclosed initializer list". The system is statically typed, and the list of parameters in the "{}" list depends on the type specified as a template parameter to log. All details, such as the time stamp and log priority, are handled automatically but can be specified/overridden if needed.
Steps to adding a new log type:
To add a log type to the MagAO-X flatlogs logging system, perform the following steps. We will assume that this log is named "log_type" in what follows.
- If it is an empty log:
- Create a new file "log_type.hpp" in the logger/types directory. This new file has the following form
#ifndef logger_types_loop_open_hpp
#define logger_types_loop_open_hpp
{
namespace logger
{
struct log_type : public empty_log<log_type>
{
static const char *
msg() {
return "specific log_type message";}
};
}
}
#endif
The MagAO-X logger empty log base type.
uint16_t eventCodeT
The type of an event code (16-bit unsigned int).
int8_t logPrioT
The type of the log priority code.
constexpr static logPrioT LOG_NOTICE
A normal but significant condition.
- If it is not an empty log:
- Create a flatbuffer schema in logger/types/schemas
- Create a new file "log_type.hpp" in the logger/types directory. This new file has the following form
#ifndef logger_types_log_type_hpp
#define logger_types_log_type_hpp
#include "generated/log_type_generated.h"
{
namespace logger
{
struct log_type : public flatbuffer_log
{
struct messageT : public fbMessage
{
messageT(
const char *
msg )
{
auto _msg = builder.CreateString(
msg);
auto gs = CreateString_log_fb(builder, _msg);
builder.Finish(gs);
}
};
{
static_cast<void>(len);
auto rgs = GetString_log_fb(msgBuffer);
if(rgs->message()) return rgs->message()->c_str();
else return "";
}
};
}
}
#endif
The MagAO-X logger flatbuffer log base type.
msgLen2T msgLenT
The type used to refer to the message length, regardless of length.
For more information about how to write flatbuffer schemas and pack and access data, see the flatbuffer documentation.
- Next add the new types/log_type.hpp file to the libMagAOX Makefile INCLUDEDEPS list.
- Now add the log to
"logger/logCodes.dat"
- Place it in an appropriate section of the file, and in numerical order by the code so future additions can be easily managed.
- Choose a code <= 65535, that is not already used.
- Be sure to make the third column the name of the schema (without .fbs attached), which is not necessarily the log type name.
- Re-compile the logging system by, in the libMagAOX directory (above logger), typing make. This will generate a number of files based on the logCodes.dat entry you have made. Correct any errors from the
flatc
compiler or the flatlogs code gerator.
- Now recompile the app using the log type and the logdump utility.