/*
- Copyright (c) HiSilicon (Shanghai) Technologies Co., Ltd. 2024-2024. All rights reserved.
- Description: Log storage manager based on littlefs.
- This module provides timestamp-based log file management with automatic rotation. */
#ifndef LOG_MANAGER_H #define LOG_MANAGER_H
#ifdef __cplusplus #if __cplusplus extern "C" { #endif #endif
#include <stddef.h> #include <time.h>
// ==================== Macro Definitions ====================
/** Maximum log file size (1MB) */ #define LOG_MAX_FILE_SIZE (1024 * 1024)
/** Maximum number of log files to keep */ #define LOG_MAX_FILES 20
/** Maximum log filename length */ #define LOG_MAX_FILENAME_SIZE 256
/** Log buffer size for reading */ #define LOG_BUFFER_SIZE 4096
/** Log directory path */ #define LOG_DIR_PATH "/logs"
// ==================== Data Structures ====================
/**
- @brief Log file metadata information */ typedef struct { char filename[LOG_MAX_FILENAME_SIZE]; // File name with timestamp unsigned int size; // File size in bytes time_t created_time; // Creation timestamp time_t last_modified; // Last modification timestamp } log_file_info_t;
/**
- @brief Log manager context structure */ typedef struct { int initialized; // Initialization flag int current_fd; // Current file descriptor char current_filename[LOG_MAX_FILENAME_SIZE]; // Current file name unsigned int current_file_size; // Current file size log_file_info_t file_list[LOG_MAX_FILES]; // List of log files int file_count; // Number of log files } log_manager_t;
// ==================== Function Declarations ====================
/**
- @brief Initialize log storage system
- @return 0 on success, -1 on failure
- Mounts littlefs, creates /logs directory, and scans existing log files.
- Must be called once before other log operations.
- @example
-
int ret = log_manager_init(); -
if (ret != 0) { -
printf("Log manager initialization failed\n"); -
}
*/ int log_manager_init(void);
/**
- @brief Cleanup log storage system
- @return 0 on success, -1 on failure
- Closes current log file and unmounts littlefs.
- Should be called before system shutdown. */ int log_manager_cleanup(void);
/**
- @brief Generate timestamp-based log filename
- @param[out] filename Output filename (must be at least LOG_MAX_FILENAME_SIZE bytes)
- @param[in] max_len Maximum length of output buffer
- @return Pointer to filename on success, NULL on failure
- Generates filename in format: /logs/YYYYMMDD_HHMMSS.log / char log_manager_generate_filename(char *filename, size_t max_len);
/**
- @brief Write log message to file with automatic rotation
- @param[in] log_message Log message content (null-terminated string)
- @return Number of bytes written, -1 on error
- Features:
-
- Appends message to current log file
-
- Automatically creates new file if current exceeds 1MB
-
- Syncs data to flash storage after write
-
- Adds newline character after message
- @example
-
int ret = log_manager_write("Device initialized successfully"); -
if (ret < 0) { -
printf("Log write failed\n"); -
}
*/ int log_manager_write(const char *log_message);
/**
- @brief Write formatted log message (printf-style)
- @param[in] format Format string
- @param[in] ... Variable arguments
- @return Number of bytes written, -1 on error
- @example
-
log_manager_printf("Temperature: %d°C, Humidity: %d%%\n", temp, humidity);
*/ int log_manager_printf(const char *format, ...);
/**
- @brief Write log message with log level prefix
- @param[in] level Log level: "INFO", "WARN", "ERROR", "DEBUG"
- @param[in] message Log message content
- @return Number of bytes written, -1 on error
- @example
-
log_manager_write_level("ERROR", "Failed to read sensor");
*/ int log_manager_write_level(const char *level, const char *message);
/**
- @brief Read entire log file by filename
- @param[in] filename Log filename to read
- @param[out] buffer Output buffer for file content
- @param[in] max_len Maximum buffer size
- @return Number of bytes read, -1 on error, 0 if file not found
- Reads the entire file content into the provided buffer.
- @example
-
char log_content[4096]; -
ssize_t ret = log_manager_read("20250206_143022.log", -
log_content, sizeof(log_content)); -
if (ret > 0) { -
printf("Log content:\n%s\n", log_content); -
}
*/ int log_manager_read(const char *filename, char *buffer, size_t max_len);
/**
- @brief Read partial log file content with offset
- @param[in] filename Log filename to read
- @param[out] buffer Output buffer for file content
- @param[in] max_len Maximum buffer size
- @param[in] offset Starting offset in file
- @return Number of bytes read, -1 on error
- @example
-
// Read last 1KB of a large log file -
char buffer[1024]; -
unsigned int file_size = 0; -
log_manager_get_file_size("20250206_143022.log", &file_size); -
if (file_size > 1024) { -
log_manager_read_offset("20250206_143022.log", buffer, -
sizeof(buffer), file_size - 1024); -
}
*/ int log_manager_read_offset(const char *filename, char *buffer, size_t max_len, unsigned int offset);
/**
- @brief Get size of a specific log file
- @param[in] filename Log filename
- @param[out] file_size File size in bytes
- @return 0 on success, -1 on failure
- @example
-
unsigned int size = 0; -
if (log_manager_get_file_size("20250206_143022.log", &size) == 0) { -
printf("File size: %u bytes\n", size); -
}
*/ int log_manager_get_file_size(const char *filename, unsigned int *file_size);
/**
- @brief Delete a specific log file
- @param[in] filename Log filename to delete
- @return 0 on success, -1 on failure
- @example
-
if (log_manager_delete_file("old_log.log") == 0) { -
printf("Log file deleted\n"); -
}
*/ int log_manager_delete_file(const char *filename);
/**
- @brief Get list of all log files
- @param[out] files Array to store file information
- @param[in] max_count Maximum number of files to return
- @param[out] actual_count Actual number of files found (can be NULL)
- @return 0 on success, -1 on failure
- @example
-
log_file_info_t files[20]; -
int count = 0; -
if (log_manager_list_files(files, 20, &count) == 0) { -
printf("Found %d log files\n", count); -
for (int i = 0; i < count; i++) { -
printf(" %s - %u bytes\n", files[i].filename, files[i].size); -
} -
}
*/ int log_manager_list_files(log_file_info_t *files, int max_count, int *actual_count);
/**
- @brief Delete oldest log files to maintain maximum file count
- @param[in] max_files Maximum number of files to keep
- @return Number of files deleted, -1 on error
- Automatically deletes oldest files when file count exceeds max_files.
- Useful for automatic cleanup to prevent disk full.
- @example
-
// Keep only 10 most recent log files -
log_manager_cleanup_old_files(10);
*/ int log_manager_cleanup_old_files(int max_files);
/**
- @brief Get total size of all log files
- @param[out] total_size Total size in bytes
- @return 0 on success, -1 on failure
- @example
-
unsigned int total = 0; -
if (log_manager_get_total_size(&total) == 0) { -
printf("Total log size: %u bytes\n", total); -
}
*/ int log_manager_get_total_size(unsigned int *total_size);
/**
- @brief Get information about current log file
- @param[out] current_file Current file information
- @return 0 on success, -1 on failure */ int log_manager_get_current_file_info(log_file_info_t *current_file);
/**
- @brief Clear all log files
- @return Number of files deleted, -1 on error
- Deletes all files in the log directory.
- Use with caution! */ int log_manager_clear_all(void);
/**
- @brief Get Flash storage information
- @param[out] start_addr Flash partition start address (in bytes)
- @param[out] total_size Flash partition total size (in bytes)
- @param[out] free_size Flash free space (in bytes)
- @return 0 on success, -1 on failure
- Retrieves Flash memory information about the littlefs partition:
-
- start_addr: The beginning address of the Flash partition
-
- total_size: Total capacity of the partition
-
- free_size: Remaining available space
- This can be used to:
-
- Monitor disk usage
-
- Calculate log file capacity
-
- Implement storage warnings
- @example
-
unsigned int start_addr, total_size, free_size; -
if (log_manager_get_flash_info(&start_addr, &total_size, &free_size) == 0) { -
printf("Flash start address: 0x%X\n", start_addr); -
printf("Flash total size: %u bytes (%.2f KB)\n", -
total_size, total_size / 1024.0); -
printf("Flash free space: %u bytes (%.2f KB)\n", -
free_size, free_size / 1024.0); -
printf("Storage usage: %.1f%%\n", -
100.0 * (total_size - free_size) / total_size); -
}
*/ int log_manager_get_flash_info(unsigned int *start_addr, unsigned int *total_size, unsigned int *free_size);
#ifdef __cplusplus #if __cplusplus } #endif #endif
#endif // LOG_MANAGER_H