linux下找到指定目录下最新日期log文件

发布于:2025-08-19 ⋅ 阅读:(15) ⋅ 点赞:(0)

以下是一个完整的C函数,用于在指定目录下自动查找最近更新的日志文件(根据文件名中的时间戳选择最新的文件):

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <regex.h>

// 检查文件名是否符合日志格式: YYYY-MM-DD_HH-MM-SS.log
int is_valid_log_filename(const char *filename) {
    regex_t regex;
    int ret;
    
    // 编译正则表达式
    char *pattern = "^[0-9]{4}-[0-9]{2}-[0-9]{2}_[0-9]{2}-[0-9]{2}-[0-9]{2}\\.log$";
    if (regcomp(&regex, pattern, REG_EXTENDED) != 0) {
        return 0; // 正则编译失败
    }
    
    // 执行匹配
    ret = regexec(&regex, filename, 0, NULL, 0);
    regfree(&regex);
    
    return (ret == 0) ? 1 : 0;
}

// 查找最新日志文件
char *find_latest_log(const char *log_dir) {
    DIR *dir;
    struct dirent *entry;
    char *latest_file = NULL;
    
    // 打开日志目录
    if ((dir = opendir(log_dir)) == NULL) {
        perror("无法打开日志目录");
        return NULL;
    }
    
    // 遍历目录中的文件
    while ((entry = readdir(dir)) != NULL) {
        // 跳过.和..目录
        if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
            continue;
        }
        
        // 检查文件名格式
        if (is_valid_log_filename(entry->d_name)) {
            // 如果是第一个有效文件或比当前最新文件更新
            if (latest_file == NULL || strcmp(entry->d_name, latest_file) > 0) {
                // 释放之前的内存(如果有)
                if (latest_file != NULL) {
                    free(latest_file);
                }
                
                // 分配内存并复制新文件名
                latest_file = strdup(entry->d_name);
                if (latest_file == NULL) {
                    perror("内存分配失败");
                    closedir(dir);
                    return NULL;
                }
            }
        }
    }
    
    closedir(dir);
    
    // 如果找到最新文件,构建完整路径
    if (latest_file != NULL) {
        // 计算完整路径长度
        size_t path_len = strlen(log_dir) + strlen(latest_file) + 2; // +2 用于'/'和空字符
        char *full_path = malloc(path_len);
        if (full_path == NULL) {
            perror("内存分配失败");
            free(latest_file);
            return NULL;
        }
        
        // 构建完整路径
        snprintf(full_path, path_len, "%s/%s", log_dir, latest_file);
        free(latest_file);
        return full_path;
    }
    
    return NULL; // 未找到符合条件的日志文件
}

// 使用示例:
int main() {
    const char *log_dir = "/appdata/log";
    char *latest_log = find_latest_log(log_dir);
    
    if (latest_log != NULL) {
        printf("最新的日志文件是: %s\n", latest_log);
        free(latest_log);
    } else {
        printf("未找到符合条件的日志文件\n");
    }
    
    return 0;
}

函数说明:

  1. is_valid_log_filename 函数

    • 使用正则表达式验证文件名是否符合 YYYY-MM-DD_HH-MM-SS.log 格式

    • 确保只处理符合命名规范的日志文件

  2. find_latest_log 函数

    • 输入:日志目录路径 (如 "/appdata/log")

    • 输出:最新日志文件的完整路径(动态分配的内存,需调用者释放)

    • 处理流程

      1. 打开指定目录

      2. 遍历所有文件,跳过 "." 和 ".."

      3. 验证文件名格式是否符合日志文件规范

      4. 通过字符串比较找到时间戳最新的文件(文件名越大表示时间越新)

      5. 构建完整路径并返回

  3. 内存管理

    • 使用 strdup 和 malloc 动态分配内存

    • 调用者负责使用 free() 释放返回的字符串

  4. 错误处理

    • 检查目录打开是否成功

    • 处理内存分配失败的情况

    • 未找到文件时返回 NULL

编译说明:

  1. 需要链接正则表达式库:

    bash

    复制

    下载

    gcc -o log_finder log_finder.c -lregex
  2. 运行示例:

    bash

    复制

    下载

    ./log_finder

注意事项:

  1. 文件名格式必须严格遵循 YYYY-MM-DD_HH-MM-SS.log 格式

  2. 函数仅基于文件名判断,不检查文件修改时间

  3. 返回的路径字符串需要调用者释放内存

  4. 如果目录中有大量文件,建议添加最大文件数量限制

  5. 对于嵌入式系统等资源受限环境,可简化正则表达式检查

此实现能正确处理您提供的示例文件列表,并返回最新文件(如 "2025-08-15_11-07-58.log")。


网站公告

今日签到

点亮在社区的每一天
去签到