Log File System#

Functions

struct logfs *logfs_create(struct fs *fs, const char *base_path, const size_t max_size, const size_t max_logs, size_t cache_size)#

Creates a log filesystem structure.

This function initializes and returns a pointer to a log filesystem structure. The log filesystem is used to manage log files with a specified maximum size and maximum number of logs.

Note

Logs are sorted in ascending order by filename.

Note

If the total size of the logs exceeds max_size, logs are deleted in sorted order until the total size is less than max_size. For example, if there are files “1” and “2”, the file “1” will be deleted first.

Note

If the number of stored logs exceeds max_logs, logs are deleted in sorted order until the the number of logs is less than max_logs.

Note

If max_size or max_logs is 0, it is considered unlimited.

Parameters:
  • fs[in] Pointer to the underlying filesystem structure.

  • base_path[in] The base path where log files will be stored.

  • max_size[in] The maximum size (in bytes) for the log filesystem.

  • max_logs[in] The maximum number of log files to be maintained.

  • cache_size[in] The size of the cache (in bytes) for buffering log data.

Returns:

Pointer to the created log filesystem structure.

void logfs_destroy(struct logfs *self)#

Destroys the log filesystem structure.

This function releases all resources associated with the log filesystem structure.

Parameters:

self[in] Pointer to the log filesystem structure to be destroyed.

int logfs_write(struct logfs *self, const time_t timestamp, const void *log, const size_t logsize)#

Writes data to a log file.

This function writes the specified data to the given log file within the log filesystem.

Note

If the log file does not exist, it will be created.

Note

If the log file exists, the data will be appended to the end of the file.

Parameters:
  • self[in] Pointer to the log filesystem structure.

  • timestamp[in] The timestamp used as the log file name.

  • log[in] Pointer to the data to be written.

  • logsize[in] The size of the data to be written in bytes.

Returns:

A positive value indicating the number of bytes actually written on success, or a negative error code on failure.

int logfs_read(struct logfs *self, const time_t timestamp, const size_t offset, void *buf, const size_t bufsize)#

Reads data from a log file.

This function reads data from the specified log file within the log filesystem.

Parameters:
  • self[in] Pointer to the log filesystem structure.

  • timestamp[in] The timestamp used as the log file name.

  • offset[in] The offset within the file to start reading from.

  • buf[out] Pointer to the buffer where the read data will be stored.

  • bufsize[in] The size of the buffer in bytes.

Returns:

The number of bytes read on success, or a negative error code on failure.

size_t logfs_size(struct logfs *self, const time_t timestamp)#

Gets the size of a log file or the total size of all log files.

This function returns the size of the specified log file within the log filesystem.

Parameters:
  • self[in] Pointer to the log filesystem structure.

  • timestamp[in] The timestamp used as the log file name. If 0, the total size of all log files is returned.

Returns:

The size of the specified log file in bytes, or the total size of all log files if timestamp is 0. If the return value is 0, it may indicate an error or that the file does not exist.

size_t logfs_count(struct logfs *self)#

Get the count of log entries in the log file system.

Parameters:

self[in] Pointer to the log file system instance.

Returns:

The number of log entries.

int logfs_dir(struct logfs *self, logfs_dir_cb_t cb, void *cb_ctx, const size_t max_files)#

Lists log files in the log filesystem up to a specified maximum number.

This function iterates over log files in the log filesystem and calls the provided callback function for each file, up to the specified maximum number of files.

Note

If max_files is 0, all log files will be listed.

Parameters:
  • self[in] Pointer to the log filesystem structure.

  • cb[in] Callback function to be called for each log file.

  • cb_ctx[in] User-defined context to be passed to the callback function.

  • max_files[in] The maximum number of log files to list. If 0, all log files will be listed.

Returns:

0 on success, or a negative error code on failure.

int logfs_delete(struct logfs *self, const time_t timestamp)#

Deletes a log file.

This function deletes the specified log file from the log filesystem.

Parameters:
  • self[in] Pointer to the log filesystem structure.

  • timestamp[in] The timestamp used as the log file name.

Returns:

0 on success, or a negative error code on failure.

int logfs_clear(struct logfs *self)#

Clears all log files.

This function deletes all log files from the log filesystem.

Parameters:

self[in] Pointer to the log filesystem structure.

Returns:

0 on success, or a negative error code on failure.

int logfs_flush(struct logfs *self)#

Flushes the log filesystem.

This function flushes any buffered data to the log files within the log filesystem. It ensures that all pending writes are completed and the data is written to the disk.

Parameters:

self[in] Pointer to the log filesystem structure.

Returns:

0 on success, or a negative error code on failure.

Typedefs

typedef void (*logfs_dir_cb_t)(struct logfs *fs, const time_t timestamp, void *ctx)#

Callback function type for iterating over log files.

This callback function is called for each log file in the log filesystem when using the logfs_dir function.

Param fs:

[in] Pointer to the log filesystem structure.

Param timestamp:

[in] The timestamp used as the log file name.

Param ctx:

[in] User-defined context passed to the callback function.

Defines

LOGFS_MIN_CACHE_SIZE 4096#