Log API#
Verify the operational status of applications through the Oasis Log feature.
The log output format or log recording destination method can be modified using the log API.
The default settings for Oasis Log are as follows:
| Item | Default |
Description |
|---|---|---|
| Console Output | true | Log messages are printed to the console. The log server is activated. |
| Redirect | false | Call logToRotateFiles to redirect messages to log files. |
| ANSI Color Code | true | Supports terminal ANSI Escape. |
| System Timestamp | true | Prints an incrementally increasing timer value from system startup as the header of each log message. |
| Local Time | false | Does not print local time as the log message header. |
| File Function and File Line Number | true | Prints the corresponding function name and line number when outputting a log message. |
| Flush Interval | 5 seconds | Flushes log messages at intervals of 5 seconds. |
Note
The text color of standard Oasis log messages is white. It is recommended to configure the background color of the console to a dark color (such as black).
Header Files#
OasisAPI.h
OasisLog.h
Log Macros#
The application outputs log messages using the macro functions defined in OasisLog.h.
DLOG(condition, fmt, ...)
Prints a log message including DLOG_TAG if condition is met.DLOG0(condition, fmt, ...)
Prints a log message including DLOG_TAG if condition is met. The function name and line number are not included.TLOG(condition, tag, fmt, ...)
Prints a log message including tag if condition is met.TLOG0(condition, tag, fmt, ...)
Prints a log message including tag if condition is met. The function name and line number are not included.TRACE(fmt, ...)
Prints a log message in white text color.TRACE0(fmt, ...)
Prints only the message passed to TRACE0. Time information and everything else are not included.TRACE1(fmt, ...)
Prints only the message passed to TRACE1 along with time information.ASSERT(condition)
Prints the condition message including DLOG_TAG if condition is not met. The function name and line number are always included.ASSERT0(condition, fmt, ...)
Prints the condition and log message including DLOG_TAG if condition is not met. The function name and line number are always included.DUMP(msg, data, size)
Prints msg on the first line, and prints data in Hex+Ascii format.DUMP2(msg, data, size, realsize)
Prints msg on the first line, and prints data in Hex+Ascii format. size can be smaller than realsize.
The condition check for DLOG, DLOG0, TLOG, and TLOG0 is as follows:
// Define DLOG_FLAGS inside the source file.
if (((condition)&DLOG_FLAGS) != 0) {
// Output log
}
The table below summarizes whether each macro function is affected when items to be applied to log messages via the Log API are enabled/disabled. Blank spaces indicate that the item is not used.
| Macro\Item | System Timestamp | Local Time | ANSI Code | Function Name and Line Number |
|---|---|---|---|---|
| DLOG | ◯ | ◯ | ◯ | ◯ |
| TLOG | ◯ | ◯ | ◯ | ◯ |
| DLOG0 | ◯ | ◯ | ◯ | |
| TLOG0 | ◯ | ◯ | ◯ | |
| TRACE | ◯ | ◯ | △ | ◯ |
| TRACE0 | ||||
| TRACE1 | ◯ | ◯ | ||
| ASSERT | ◯ | ◯ | ◯ | Always |
| ASSERT0 | ◯ | ◯ | ◯ | Always |
Note
In the case of TRACE, if ANSI codes are enabled, the text color is processed as white. If ANSI codes are disabled, the current text color is used. In the case of ASSERT and ASSERT0, the function name and line number are always included in the log message.
Usage of Log Macros#
To use Oasis logs in an application, include the OasisLog.h file.
#include "OasisLog.h"
Define DLOG_FLAGS and DLOG_TAG to output logs by condition or include tags.
#undef DLOG_TAG
#define DLOG_TAG "Desired tag name"
#define DLOG_THIS 0x00080000
#undef DLOG_FLAGS
#define DLOG_FLAGS (DLOG_FLAGS_DEFAULT|DLOG_THIS/**/)
DLOG_FLAGS_DEFAULT consists of the following values:
#define DLOG_COLOR2 0x00000200
#define DLOG_COLOR1 0x00000100
#define DLOG_SIGNAL 0x00000080
#define DLOG_FATAL 0x00000040
#define DLOG_ASSERT 0x00000020
#define DLOG_ERROR 0x00000010
#define DLOG_WARN 0x00000008
#define DLOG_INFO 0x00000004
#define DLOG_DEBUG 0x00000002
#define DLOG_VERBOSE 0x00000001
#define DLOG_F_NOFILEINFO 0x8000000000000000ull
#define DLOG_F_NOFUNCINFO 0x4000000000000000ull
#define DLOG_F_ENAB1BYAPI 0x2000000000000000ull //toggled by api
#define DLOG_F_ENAB2BYAPI 0x1000000000000000ull //toggled by api
#define DLOG_FLAGS_DEFAULT (DLOG_F_ENAB2BYAPI \
| DLOG_F_ENAB1BYAPI \
| DLOG_F_NOFILEINFO \
| DLOG_F_NOFUNCINFO \
| DLOG_INFO \
| DLOG_ERROR \
| DLOG_WARN \
| DLOG_ASSERT \
| DLOG_FATAL/*|DLOG_SIGNAL*/)
The ANSI colors for each flag are as follows:
| DLOG_FLAG | Color |
|---|---|
| DLOG_ERROR | Red |
| DLOG_WARN | Green |
| DLOG_INFO | Yellow |
| DLOG_COLOR1 | Blue |
| DLOG_COLOR2 | Purple |
| DLOG_SIGNAL | Cyan |
| Others | White |
Then use the log macros in the code.
#include "OasisLog.h"
#undef DLOG_TAG
#define DLOG_TAG "MYAPP"
#define DLOG_THIS 0x00080000
#undef DLOG_FLAGS
#define DLOG_FLAGS (DLOG_FLAGS_DEFAULT|DLOG_THIS/**/)
void function()
{
int32_t value = 0;
char data[256] = { 0, };
for(int32_t i=0; i<256; i++) {
data[i] = (i%256);
}
DLOG(DLOG_INFO, "info message...\n");
DLOG(DLOG_THIS, "value = %d\n", value);
DLOG0(DLOG_THIS, "value = %d\n", value);
TLOG(DLOG_THIS, "new tag name", "value = %d\n", value);
TRACE0("always printed\n");
ASSERT(value == 1);
ASSERT0(value == 1, ": %d == 1\n", value);
DUMP2("myapp-data", data, 16, 256);
}
The console log output is as follows:
[89511.647264] Info: [MYAPP] function()@hello.cpp:75: info message...
[89511.647307] [MYAPP] function()@hello.cpp:76: value = 0
[89511.647326] value = 0
[89511.647341] [new tag name] function()@hello.cpp:78: value = 0
[89511.647366] MYAPP: ASSERT hello.cpp@81 function: value == 1
[89511.647380] MYAPP: ASSERT hello.cpp@82 function: value == 1 : 0 == 1
myapp-data, __data=0xffffc47b62f8, __size=16 of 256:
00000000 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f ................
Functions#
oasis::initialize.
<Absolute Path>/<File Name>-%d[.extension]. For example, it is specified in a format like /var/log/oasis-%d.log.
- 0: Success
- -1: Failure
true.
true.
true.
true.
true. Ignored if redirecting to a file is in progress.
true. Returns -1 if it has already been started. Stops the log server if false.
- 0: Success
- -1: Failure
/tmp/oasis-logger-<pid>.
Example#
Below is an example of modifying the log environment configuration.
using namespace oasis;
// Configure Oasis log.
if(run_in_background) {
// Saves logs to files when running in the background.
logToRotateFiles("/var/log/myapp-oasis-%d.log", 128*1024*2, 10);
enableLogAnsiEscapes(true);
enableLogLocalTime(true);
enableLogSourceFileFuncLineInfo(false);
setLogFlushInterval(0);
} else {
setLogOutToStdOut(true);
enableLogLocalTime(true);
enableLogMonolithicTime(false);
}
// Initialize Oasis.
oasis::initialize(parameters);