Slog
为MetaOS提供的c/c++日志库,负责日志写入
C使用示例,C++使用示例
// C style
#include "slog/slog.h"
int main() {
const int int_value = 42;
const float float_value = 3.14159;
const char char_value = 'A';
const char* string_value = "Hello, world!";
SLOG_DEFAULT_LEVEL_C(SLOG_TRACE);
## 模块(标签)
const char* tag = "cstyle.print_demo";
## 打开/关闭输出控制台
SLOG_SET_QUIET_C(false); // disable stdout output
SLOG_FATAL_C(tag, "Integer: %d", int_value);
SLOG_ERROR_C(tag, "Float: %f", float_value);
## 日志输出类型(与日志目录绑定)
SLOG_SYSLOG_ENABLE_C(APPLICATION); // syslog output
SLOG_DEBUG_C(tag, "Character: %c", char_value);
SLOG_INFO_C(tag, "String: %s", string_value);
SLOG_TRACE_C(tag, "Formatted integer: %5d", int_value);
SLOG_TRACE_C(tag, "Formatted float: %.2f", float_value);
## 关闭输出到syslog
SLOG_SYSLOG_DISABLE_C();
return 0;
}
C 日志控制台输出内容
2024-09-25 17:24:19.473 DEBUG /home/sinsegye/workspace/apps/src/logger/logger.cc:53 slog.syslog: enabled
2024-09-25 17:24:19.473 FATAL /home/sinsegye/workspace/apps/test/selftest/test.c:22 cstyle.print_demo: Integer: 42
2024-09-25 17:24:19.473 ERROR /home/sinsegye/workspace/apps/test/selftest/test.c:23 cstyle.print_demo: Float: 3.141590
2024-09-25 17:24:19.473 DEBUG /home/sinsegye/workspace/apps/src/logger/logger.cc:53 slog.syslog: enabled
2024-09-25 17:24:19.473 DEBUG /home/sinsegye/workspace/apps/test/selftest/test.c:27 cstyle.print_demo: Character: A
2024-09-25 17:24:19.473 INFO /home/sinsegye/workspace/apps/test/selftest/test.c:28 cstyle.print_demo: String: Hello, world!
2024-09-25 17:24:19.473 TRACE /home/sinsegye/workspace/apps/test/selftest/test.c:30 cstyle.print_demo: Formatted integer: 42
2024-09-25 17:24:19.473 TRACE /home/sinsegye/workspace/apps/test/selftest/test.c:31 cstyle.print_demo: Formatted float: 3.14
2024-09-25 17:24:19.473 DEBUG /home/sinsegye/workspace/apps/src/logger/logger.cc:59 slog.syslog: disabled
C 日志syslog输出内容
cat /log/application
/application.log (路径与代码syslog_enable指定类型有关系)
[2024-09-25 17:24:19.4743] [sinsegye-gj] [debug] slog_c_style[2336069]: slog.syslog: enabled
[2024-09-25 17:24:19.4746] [sinsegye-gj] [crit] slog_c_style[2336069]: cstyle.print_demo: Integer: 42
[2024-09-25 17:24:19.4747] [sinsegye-gj] [err] slog_c_style[2336069]: cstyle.print_demo: Float: 3.141590
[2024-09-25 17:24:19.4747] [sinsegye-gj] [debug] slog_c_style[2336069]: slog.syslog: enabled
[2024-09-25 17:24:19.4748] [sinsegye-gj] [debug] slog_c_style[2336069]: cstyle.print_demo: Character: A
[2024-09-25 17:24:19.4749] [sinsegye-gj] [info] slog_c_style[2336069]: cstyle.print_demo: String: Hello, world!
[2024-09-25 17:24:19.4749] [sinsegye-gj] [debug] slog_c_style[2336069]: cstyle.print_demo: Formatted integer: 42
[2024-09-25 17:24:19.4750] [sinsegye-gj] [debug] slog_c_style[2336069]: cstyle.print_demo: Formatted float: 3.14
[2024-09-25 17:24:19.4751] [sinsegye-gj] [debug] slog_c_style[2336069]: slog.syslog: disable
#include <thread>
#include "slog/slog.hpp"
auto main() -> int {
const int int_value{42};
const float float_value{3.14159};
const char char_value{'A'};
const std::string string_value{"Hello, world!"};
const auto level = slog::SlogLevel::TRACE;
SLOG_DEFAULT_LEVEL(level);
## 模块(标签)
const std::string tag{"cppstyle.print_demo"};
## 打开/关闭输出控制台
SLOG_SET_QUIET(false);
SLOG_FATAL(tag, "Integer: %d", int_value);
SLOG_ERROR(tag, "Float: %f", float_value);
## 日志输出类型(与日志目录绑定)
SLOG_SYSLOG_ENABLE(slog::SyslogType::SERVICE); // syslog output
SLOG_DEBUG(tag, "Character: %c", char_value);
SLOG_INFO(tag, "String: %s", string_value.c_str());
SLOG_TRACE(tag, "Formatted integer: %5d", int_value);
SLOG_TRACE(tag, "Formatted float: %.2f", float_value);
## 关闭输出到syslog
SLOG_SYSLOG_DISABLE();
return 0;
}
C++ 日志输出内容
2024-09-25 17:24:56.527 DEBUG /home/sinsegye/workspace/apps/src/logger/logger.cc:53 slog.syslog: enabled
2024-09-25 17:24:56.527 FATAL /home/sinsegye/workspace/apps/test/selftest/test.cc:17 cppstyle.print_demo: Integer: 42
2024-09-25 17:24:56.527 ERROR /home/sinsegye/workspace/apps/test/selftest/test.cc:18 cppstyle.print_demo: Float: 3.141590
2024-09-25 17:24:56.527 DEBUG /home/sinsegye/workspace/apps/src/logger/logger.cc:53 slog.syslog: enabled
2024-09-25 17:24:56.528 DEBUG /home/sinsegye/workspace/apps/test/selftest/test.cc:22 cppstyle.print_demo: Character: A
2024-09-25 17:24:56.528 INFO /home/sinsegye/workspace/apps/test/selftest/test.cc:23 cppstyle.print_demo: String: Hello, world!
2024-09-25 17:24:56.528 TRACE /home/sinsegye/workspace/apps/test/selftest/test.cc:25 cppstyle.print_demo: Formatted integer: 42
2024-09-25 17:24:56.528 TRACE /home/sinsegye/workspace/apps/test/selftest/test.cc:26 cppstyle.print_demo: Formatted float: 3.14
2024-09-25 17:24:56.528 DEBUG /home/sinsegye/workspace/apps/src/logger/logger.cc:59 slog.syslog: disabled
C++ 日志syslog输出内容
cat /log/service
/service.log (路径与代码syslog_enable指定类型有关系)
[2024-09-25 17:24:56.5286] [sinsegye-gj] [debug] slog_cpp_style[2336567]: slog.syslog: enabled
[2024-09-25 17:24:56.5289] [sinsegye-gj] [crit] slog_cpp_style[2336567]: cppstyle.print_demo: Integer: 42
[2024-09-25 17:24:56.5290] [sinsegye-gj] [err] slog_cpp_style[2336567]: cppstyle.print_demo: Float: 3.141590
[2024-09-25 17:24:56.5290] [sinsegye-gj] [debug] slog_cpp_style[2336567]: slog.syslog: enabled
[2024-09-25 17:24:56.5290] [sinsegye-gj] [debug] slog_cpp_style[2336567]: cppstyle.print_demo: Character: A
[2024-09-25 17:24:56.5290] [sinsegye-gj] [info] slog_cpp_style[2336567]: cppstyle.print_demo: String: Hello, world!
[2024-09-25 17:24:56.5290] [sinsegye-gj] [debug] slog_cpp_style[2336567]: cppstyle.print_demo: Formatted integer: 42
[2024-09-25 17:24:56.5291] [sinsegye-gj] [debug] slog_cpp_style[2336567]: cppstyle.print_demo: Formatted float: 3.14
[2024-09-25 17:24:56.5291] [sinsegye-gj] [debug] slog_cpp_style[2336567]: slog.syslog: disable
tag{"cstyle.print_demo"}; 日志关键信息
tag{"cppstyle.print_demo"}; 日志关键信息