Commit 4b0b1704 authored by 957dd's avatar 957dd

创建日志文件

parent eae6494c
#define PROJECT_VERSION_MAJOR 1 #define PROJECT_VERSION_MAJOR 1
#define PROJECT_VERSION_MINOR 0 #define PROJECT_VERSION_MINOR 0
#define PROJECT_VERSION_PATCH 1 #define PROJECT_VERSION_PATCH 1
#define GIT_HASH "2d4b24d" #define GIT_HASH "eae6494"
#define BUILD_TIMESTAMP "2025-04-14T10:22:21" #define BUILD_TIMESTAMP "2025-04-14T11:06:17"
#define BUILD_USER "orangepi" #define BUILD_USER "orangepi"
No preview for this file type
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
int logprject_Init(); int logprject_Init();
void my_log_close(); void my_log_close();
int logconf_create();
// 封装 zlog_debug // 封装 zlog_debug
void my_zlog_debug(const char *format, ...) ; void my_zlog_debug(const char *format, ...) ;
// 封装 zlog_info // 封装 zlog_info
......
...@@ -24,7 +24,7 @@ int ipaddr() {//获取ip ...@@ -24,7 +24,7 @@ int ipaddr() {//获取ip
} }
//printf("ip:%s\n",ip_address); //printf("ip:%s\n",ip_address);
my_zlog_debug("ip:%s\n",ip_address); my_zlog_debug("ip:%s",ip_address);
freeifaddrs(ifap); freeifaddrs(ifap);
......
...@@ -5,10 +5,44 @@ ...@@ -5,10 +5,44 @@
#include<zlog.h> #include<zlog.h>
#include "log.h" #include "log.h"
const char *config_content = "[global]\n"
"strict init = true\n"
"buffer min = 1024\n"
"buffer max = 2MB\n"
"default format = \"%d(%Y-%m-%d %H:%M:%S.%ms) %-6V (%c:%F:%L) - %m%n\"\n"
"file perms = 600 # 设置文件权限\n"
"\n"
"[formats]\n"
"millisecond = \"%d(%Y-%m-%d %H:%M:%S.%ms) [%V] %m%n\"\n"
"\n"
"[rules]\n"
"# 日志文件路径,30天轮询,自动创建目录\n"
"my_log.* \"/home/orangepi/car/master/log/log.log\", 30D; millisecond\n";
const char *LOG_FILE = "/home/orangepi/car/master/log/log.log";//日志文件 const char *LOG_FILE = "/home/orangepi/car/master/log/log.log";//日志文件
static zlog_category_t *zlog_c = NULL; static zlog_category_t *zlog_c = NULL;
int logconf_create() {
const char *config_file = "/home/orangepi/car/master/zlog.conf";
// 检查文件是否存在
if (access(config_file, F_OK) != 0) {
// 文件不存在,创建并写入内容
FILE *fp = fopen(config_file, "w");
if (fp == NULL) {
perror("Failed to create zlog.conf");
return 1;
}
// 写入配置内容
fwrite(config_content, strlen(config_content), 1, fp);
fclose(fp);
printf("zlog.conf has been created.\n");
} else {
printf("zlog.conf already exists.\n");
}
return 0;
}
// 检查并创建日志目录 // 检查并创建日志目录
static int create_log_dir_if_not_exist(const char *path) { static int create_log_dir_if_not_exist(const char *path) {
char dir_path[256] = {0}; char dir_path[256] = {0};
......
...@@ -43,7 +43,7 @@ int mqtt_init() { ...@@ -43,7 +43,7 @@ int mqtt_init() {
mosq = mosquitto_new(NULL, true, NULL); mosq = mosquitto_new(NULL, true, NULL);
if (!mosq) { if (!mosq) {
fprintf(stderr, "Failed to create Mosquitto client\n"); //fprintf(stderr, "Failed to create Mosquitto client\n");
my_zlog_fatal("Failed to create Mosquitto client"); my_zlog_fatal("Failed to create Mosquitto client");
return -1; return -1;
} }
...@@ -266,7 +266,7 @@ void mqtt_create(struct mosquitto *mosq) {//创建mqtt客服端 ...@@ -266,7 +266,7 @@ void mqtt_create(struct mosquitto *mosq) {//创建mqtt客服端
rc = mosquitto_connect(mosq, BROKER_ADDRESS, BROKER_PORT, 60); rc = mosquitto_connect(mosq, BROKER_ADDRESS, BROKER_PORT, 60);
if (rc != MOSQ_ERR_SUCCESS) { if (rc != MOSQ_ERR_SUCCESS) {
//fprintf(stderr, "Failed to connect to broker: %s\n", mosquitto_strerror(rc)); //fprintf(stderr, "Failed to connect to broker: %s\n", mosquitto_strerror(rc));
my_zlog_warn("Failed to connect to broker: %s\n", mosquitto_strerror(rc)); my_zlog_warn("Failed to connect to broker: %s", mosquitto_strerror(rc));
mosquitto_destroy(mosq); mosquitto_destroy(mosq);
//return EXIT_FAILURE; //return EXIT_FAILURE;
} }
......
...@@ -2,6 +2,10 @@ ...@@ -2,6 +2,10 @@
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
if(logconf_create() != 0) {
return 6;
}
if(logprject_Init() != 0) { if(logprject_Init() != 0) {
my_zlog_fatal( "日志初始化失败"); my_zlog_fatal( "日志初始化失败");
//printf("日志初始化失败"); //printf("日志初始化失败");
...@@ -54,7 +58,7 @@ int main(int argc, char *argv[]) { ...@@ -54,7 +58,7 @@ int main(int argc, char *argv[]) {
thread_end(); //关闭线程 thread_end(); //关闭线程
my_zlog_info("程序关闭成功"); my_zlog_info("程序关闭成功");
my_log_close();//关闭日志 my_log_close();//关闭日志
//printf("程序关闭成功\n"); //printf("程序关闭成功\n");
......
...@@ -10,4 +10,4 @@ millisecond = "%d(%Y-%m-%d %H:%M:%S.%ms) [%V] %m%n" ...@@ -10,4 +10,4 @@ millisecond = "%d(%Y-%m-%d %H:%M:%S.%ms) [%V] %m%n"
[rules] [rules]
# 日志文件路径,30天轮询,自动创建目录 # 日志文件路径,30天轮询,自动创建目录
my_log.* "/home/orangepi/car/master/log/log.log", 30D; millisecond my_log.* "/home/orangepi/car/master/log/log.log", 30D; millisecond
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment