!236 add log utils for c
From: @tong_1001 Reviewed-by: @znzjugod Signed-off-by: @znzjugod
This commit is contained in:
commit
87260fb362
203
add-log-utils-for-c.patch
Normal file
203
add-log-utils-for-c.patch
Normal file
@ -0,0 +1,203 @@
|
|||||||
|
From 0ee8307d556c200733270fdffd8db2d48869724a Mon Sep 17 00:00:00 2001
|
||||||
|
From: shixuantong <shixuantong1@huawei.com>
|
||||||
|
Date: Fri, 14 Mar 2025 14:55:56 +0800
|
||||||
|
Subject: [PATCH] add log utils for c
|
||||||
|
|
||||||
|
---
|
||||||
|
Makefile | 14 ++++++-
|
||||||
|
src/libsentry/c/log/CMakeLists.txt | 7 ++++
|
||||||
|
src/libsentry/c/log/log_utils.c | 45 +++++++++++++++++++++
|
||||||
|
src/libsentry/c/log/log_utils.h | 64 ++++++++++++++++++++++++++++++
|
||||||
|
4 files changed, 129 insertions(+), 1 deletion(-)
|
||||||
|
create mode 100644 src/libsentry/c/log/CMakeLists.txt
|
||||||
|
create mode 100644 src/libsentry/c/log/log_utils.c
|
||||||
|
create mode 100644 src/libsentry/c/log/log_utils.h
|
||||||
|
|
||||||
|
diff --git a/Makefile b/Makefile
|
||||||
|
index 29c4a53..73ada63 100644
|
||||||
|
--- a/Makefile
|
||||||
|
+++ b/Makefile
|
||||||
|
@@ -29,12 +29,16 @@ PKGVEREGG := syssentry-$(VERSION)-py$(PYTHON_VERSION).egg-info
|
||||||
|
|
||||||
|
all: lib ebpf hbm_online_repair
|
||||||
|
|
||||||
|
-lib:libxalarm
|
||||||
|
+lib:libxalarm log
|
||||||
|
|
||||||
|
libxalarm:
|
||||||
|
cd $(CURLIBDIR) && cmake . -DXD_INSTALL_BINDIR=$(LIBINSTALLDIR) -B build
|
||||||
|
cd $(CURLIBDIR)/build && make
|
||||||
|
|
||||||
|
+log:
|
||||||
|
+ cd $(CURSRCDIR)/libsentry/c/log && cmake . -B build
|
||||||
|
+ cd $(CURSRCDIR)/libsentry/c/log/build && make
|
||||||
|
+
|
||||||
|
ebpf:
|
||||||
|
@if [ -d "$(CURSRCDIR)/services/sentryCollector/ebpf_collector/" ]; then \
|
||||||
|
cd $(CURSRCDIR)/services/sentryCollector/ebpf_collector/ && make; \
|
||||||
|
@@ -134,6 +138,11 @@ isentry:
|
||||||
|
|
||||||
|
# pyxalarm
|
||||||
|
install -m 550 src/libs/pyxalarm/register_xalarm.py $(PYDIR)/xalarm
|
||||||
|
+
|
||||||
|
+ # log utils
|
||||||
|
+ install -d -m 700 $(INCLUDEDIR)/libsentry
|
||||||
|
+ install -m 644 $(CURSRCDIR)/libsentry/c/log/log_utils.h $(INCLUDEDIR)/libsentry/
|
||||||
|
+ install -m 550 $(CURSRCDIR)/libsentry/c/log/build/libsentry_log.so $(LIBINSTALLDIR)
|
||||||
|
|
||||||
|
ebpf_clean:
|
||||||
|
cd $(CURSRCDIR)/services/sentryCollector/ebpf_collector && make clean
|
||||||
|
@@ -144,6 +153,7 @@ hbm_clean:
|
||||||
|
clean: ebpf_clean hbm_clean
|
||||||
|
rm -rf $(CURLIBDIR)/build
|
||||||
|
rm -rf $(CURSRCDIR)/build
|
||||||
|
+ rm -rf $(CURSRCDIR)/libsentry/c/log/build
|
||||||
|
rm -rf $(CURSRCDIR)/syssentry.egg-info
|
||||||
|
rm -rf $(CURSRCDIR)/SENTRY_FILES
|
||||||
|
|
||||||
|
@@ -156,6 +166,8 @@ uninstall:
|
||||||
|
rm -rf $(BINDIR)/ebpf_collector
|
||||||
|
rm -rf $(LIBINSTALLDIR)/libxalarm.so
|
||||||
|
rm -rf $(INCLUDEDIR)/xalarm
|
||||||
|
+ rm -rf $(LIBINSTALLDIR)/libsentry_log.so
|
||||||
|
+ rm -rf $(INCLUDEDIR)/libsentry
|
||||||
|
rm -rf $(ETCDIR)/sysSentry
|
||||||
|
rm -rf $(ETCDIR)/hbm_online_repair.env
|
||||||
|
rm -rf $(LOGSAVEDIR)/sysSentry
|
||||||
|
diff --git a/src/libsentry/c/log/CMakeLists.txt b/src/libsentry/c/log/CMakeLists.txt
|
||||||
|
new file mode 100644
|
||||||
|
index 0000000..6488195
|
||||||
|
--- /dev/null
|
||||||
|
+++ b/src/libsentry/c/log/CMakeLists.txt
|
||||||
|
@@ -0,0 +1,7 @@
|
||||||
|
+# Copyright (c) Huawei Technologies Co., Ltd. 2023-2025. All rights reserved.
|
||||||
|
+# Description: cmake file for log_utils
|
||||||
|
+project(sentry_log)
|
||||||
|
+cmake_minimum_required(VERSION 3.22)
|
||||||
|
+add_library(sentry_log SHARED log_utils.c)
|
||||||
|
+set_target_properties(sentry_log PROPERTIES LINK_FLAGS "-Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack -Wtrampolines")
|
||||||
|
+set_target_properties(sentry_log PROPERTIES CMAKE_C_FLAGS "-shared -fPIC -fstack-protector-strong -D_FORTIFY_SOURCE=2 -O2 -Wall -Werror -g")
|
||||||
|
diff --git a/src/libsentry/c/log/log_utils.c b/src/libsentry/c/log/log_utils.c
|
||||||
|
new file mode 100644
|
||||||
|
index 0000000..935e6d6
|
||||||
|
--- /dev/null
|
||||||
|
+++ b/src/libsentry/c/log/log_utils.c
|
||||||
|
@@ -0,0 +1,45 @@
|
||||||
|
+/*
|
||||||
|
+ * Copyright (c) Huawei Technologies Co., Ltd. 2023-2025. All rights reserved.
|
||||||
|
+ * Description: log utils for sysSentry
|
||||||
|
+ * Author: sxt1001
|
||||||
|
+ * Create: 2025-2-16
|
||||||
|
+ */
|
||||||
|
+
|
||||||
|
+#include "log_utils.h"
|
||||||
|
+
|
||||||
|
+static LogLevel currentLogLevel = LOG_INFO;
|
||||||
|
+
|
||||||
|
+void logMessage(LogLevel level, char* file, int line, const char *format, ...)
|
||||||
|
+{
|
||||||
|
+ if (level >= currentLogLevel) {
|
||||||
|
+ PRINT_LOG_PREFIX(level, file, line);
|
||||||
|
+ va_list args;
|
||||||
|
+ va_start(args, format);
|
||||||
|
+ vfprintf(LOG_FD(level), format, args);
|
||||||
|
+ va_end(args);
|
||||||
|
+ fflush(LOG_FD(level));
|
||||||
|
+ }
|
||||||
|
+}
|
||||||
|
+
|
||||||
|
+void setLogLevel()
|
||||||
|
+{
|
||||||
|
+ currentLogLevel = LOG_INFO;
|
||||||
|
+ char* levelStr = getenv(LOG_LEVEL_ENV);
|
||||||
|
+ if (levelStr == NULL) {
|
||||||
|
+ logMessage(LOG_WARN, __FILE__, __LINE__, "getenv('%s') is NULL, use default log level : %s\n", LOG_LEVEL_ENV, LOG_LEVEL_STRING(LOG_INFO));
|
||||||
|
+ } else if (strcmp(levelStr, "info") == 0) {
|
||||||
|
+ currentLogLevel = LOG_INFO;
|
||||||
|
+ logMessage(LOG_INFO, __FILE__, __LINE__, "Set log level : %s\n", LOG_LEVEL_STRING(LOG_INFO));
|
||||||
|
+ } else if (strcmp(levelStr, "warning") == 0) {
|
||||||
|
+ currentLogLevel = LOG_WARN;
|
||||||
|
+ logMessage(LOG_INFO,__FILE__, __LINE__,"Set log level : %s\n", LOG_LEVEL_STRING(LOG_WARN));
|
||||||
|
+ } else if (strcmp(levelStr, "error") == 0) {
|
||||||
|
+ currentLogLevel = LOG_ERROR;
|
||||||
|
+ logMessage(LOG_INFO,__FILE__, __LINE__,"Set log level : %s\n", LOG_LEVEL_STRING(LOG_ERROR));
|
||||||
|
+ } else if (strcmp(levelStr, "debug") == 0) {
|
||||||
|
+ currentLogLevel = LOG_DEBUG;
|
||||||
|
+ logMessage(LOG_INFO,__FILE__, __LINE__,"Set log level : %s\n", LOG_LEVEL_STRING(LOG_DEBUG));
|
||||||
|
+ } else {
|
||||||
|
+ logMessage(LOG_WARN, __FILE__, __LINE__, "unknown log level : %s, use default log level : %s\n", levelStr, LOG_LEVEL_STRING(LOG_INFO));
|
||||||
|
+ }
|
||||||
|
+}
|
||||||
|
diff --git a/src/libsentry/c/log/log_utils.h b/src/libsentry/c/log/log_utils.h
|
||||||
|
new file mode 100644
|
||||||
|
index 0000000..8a56520
|
||||||
|
--- /dev/null
|
||||||
|
+++ b/src/libsentry/c/log/log_utils.h
|
||||||
|
@@ -0,0 +1,64 @@
|
||||||
|
+/*
|
||||||
|
+ * Copyright (c) Huawei Technologies Co., Ltd. 2023-2025. All rights reserved.
|
||||||
|
+ * Description: log utils for sysSentry
|
||||||
|
+ * Author: sxt1001
|
||||||
|
+ * Create: 2025-2-16
|
||||||
|
+ */
|
||||||
|
+
|
||||||
|
+#ifndef _SYSSENTRY_LOG_H
|
||||||
|
+#define _SYSSENTRY_LOG_H
|
||||||
|
+
|
||||||
|
+#include "stdio.h"
|
||||||
|
+#include <stdlib.h>
|
||||||
|
+#include "string.h"
|
||||||
|
+#include "stdarg.h"
|
||||||
|
+#include "time.h"
|
||||||
|
+#include "libgen.h"
|
||||||
|
+
|
||||||
|
+typedef enum {
|
||||||
|
+ LOG_DEBUG = 0,
|
||||||
|
+ LOG_INFO,
|
||||||
|
+ LOG_WARN,
|
||||||
|
+ LOG_ERROR,
|
||||||
|
+} LogLevel;
|
||||||
|
+
|
||||||
|
+#define LOG_FD(level) (level == LOG_ERROR ? stderr : stdout)
|
||||||
|
+
|
||||||
|
+#define LOG_LEVEL_STRING(level) \
|
||||||
|
+ (level == LOG_DEBUG ? "DEBUG": \
|
||||||
|
+ level == LOG_INFO ? "INFO" : \
|
||||||
|
+ level == LOG_WARN ? "WARNING" : \
|
||||||
|
+ level == LOG_ERROR ? "ERROR" : \
|
||||||
|
+ "UNKNOWN_LEVEL")
|
||||||
|
+
|
||||||
|
+#define PRINT_LOG_PREFIX(level, file, line) do { \
|
||||||
|
+ time_t t = time(NULL); \
|
||||||
|
+ struct tm *local_time = localtime(&t); \
|
||||||
|
+ fprintf(LOG_FD(level), "%d-%02d-%02d %02d:%02d:%02d,000 - %s - [%s:%d] - ", \
|
||||||
|
+ local_time->tm_year + 1900, \
|
||||||
|
+ local_time->tm_mon + 1, \
|
||||||
|
+ local_time->tm_mday, \
|
||||||
|
+ local_time->tm_hour, \
|
||||||
|
+ local_time->tm_min, \
|
||||||
|
+ local_time->tm_sec, \
|
||||||
|
+ LOG_LEVEL_STRING(level), \
|
||||||
|
+ basename(file), \
|
||||||
|
+ line); \
|
||||||
|
+} while (0)
|
||||||
|
+
|
||||||
|
+// configure Env for log
|
||||||
|
+#define LOG_LEVEL_ENV "LOG_LEVEL"
|
||||||
|
+
|
||||||
|
+// print msg
|
||||||
|
+void logMessage(LogLevel level, char* file, int line, const char *format, ...);
|
||||||
|
+
|
||||||
|
+// set log level
|
||||||
|
+void setLogLevel();
|
||||||
|
+
|
||||||
|
+// log function
|
||||||
|
+#define logging_debug(...) logMessage(LOG_DEBUG, __FILE__, __LINE__, __VA_ARGS__)
|
||||||
|
+#define logging_info(...) logMessage(LOG_INFO, __FILE__, __LINE__, __VA_ARGS__)
|
||||||
|
+#define logging_warn(...) logMessage(LOG_WARN, __FILE__, __LINE__, __VA_ARGS__)
|
||||||
|
+#define logging_error(...) logMessage(LOG_ERROR, __FILE__, __LINE__, __VA_ARGS__)
|
||||||
|
+
|
||||||
|
+#endif
|
||||||
|
--
|
||||||
|
2.27.0
|
||||||
|
|
||||||
@ -4,7 +4,7 @@
|
|||||||
Summary: System Inspection Framework
|
Summary: System Inspection Framework
|
||||||
Name: sysSentry
|
Name: sysSentry
|
||||||
Version: 1.0.3
|
Version: 1.0.3
|
||||||
Release: 8
|
Release: 9
|
||||||
License: Mulan PSL v2
|
License: Mulan PSL v2
|
||||||
Group: System Environment/Daemons
|
Group: System Environment/Daemons
|
||||||
Source0: https://gitee.com/openeuler/sysSentry/releases/download/v%{version}/%{name}-%{version}.tar.gz
|
Source0: https://gitee.com/openeuler/sysSentry/releases/download/v%{version}/%{name}-%{version}.tar.gz
|
||||||
@ -16,6 +16,7 @@ Patch4: fix-xalarm-log-not-print-and-add-on-iter-problem.patch
|
|||||||
Patch5: add-new-func-for-ebpf-in-the-rq_driver-stage.patch
|
Patch5: add-new-func-for-ebpf-in-the-rq_driver-stage.patch
|
||||||
Patch6: fix-the-sentryCollector-service-can-t-be-stopped-for.patch
|
Patch6: fix-the-sentryCollector-service-can-t-be-stopped-for.patch
|
||||||
Patch7: ai-block-io-exit-when-stage-is-not-supported.patch
|
Patch7: ai-block-io-exit-when-stage-is-not-supported.patch
|
||||||
|
Patch8: add-log-utils-for-c.patch
|
||||||
|
|
||||||
BuildRequires: cmake gcc-c++
|
BuildRequires: cmake gcc-c++
|
||||||
BuildRequires: python3 python3-setuptools
|
BuildRequires: python3 python3-setuptools
|
||||||
@ -171,7 +172,9 @@ rm -rf /var/run/sysSentry | :
|
|||||||
%attr(0550,root,root) %{_bindir}/sentryCollector
|
%attr(0550,root,root) %{_bindir}/sentryCollector
|
||||||
%attr(0600,root,root) %{_sysconfdir}/sysSentry/collector.conf
|
%attr(0600,root,root) %{_sysconfdir}/sysSentry/collector.conf
|
||||||
%attr(0600,root,root) %{_unitdir}/sentryCollector.service
|
%attr(0600,root,root) %{_unitdir}/sentryCollector.service
|
||||||
|
%attr(0550,root,root) %{_libdir}/libsentry_log.so
|
||||||
|
|
||||||
|
%exclude %{_includedir}/libsentry/log_utils.h
|
||||||
%exclude %{_sysconfdir}/sysSentry/tasks/hbm_online_repair.mod
|
%exclude %{_sysconfdir}/sysSentry/tasks/hbm_online_repair.mod
|
||||||
%exclude %{python3_sitelib}/syssentry/bmc_*
|
%exclude %{python3_sitelib}/syssentry/bmc_*
|
||||||
%exclude %{python3_sitelib}/syssentry/*/bmc_*
|
%exclude %{python3_sitelib}/syssentry/*/bmc_*
|
||||||
@ -212,6 +215,12 @@ rm -rf /var/run/sysSentry | :
|
|||||||
%attr(0550,root,root) %{python3_sitelib}/syssentry/bmc_alarm.py
|
%attr(0550,root,root) %{python3_sitelib}/syssentry/bmc_alarm.py
|
||||||
|
|
||||||
%changelog
|
%changelog
|
||||||
|
* Fri Mar 14 2025 shixuantong <shixuantong1@huawei.com> - 1.0.3-9
|
||||||
|
- Type:bugfix
|
||||||
|
- CVE:NA
|
||||||
|
- SUG:NA
|
||||||
|
- DESC:add log utils for c
|
||||||
|
|
||||||
* Thu Mar 13 2025 luckky <guodashun1@huawei.com> - 1.0.3-8
|
* Thu Mar 13 2025 luckky <guodashun1@huawei.com> - 1.0.3-8
|
||||||
- Type:bugfix
|
- Type:bugfix
|
||||||
- CVE:NA
|
- CVE:NA
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user