Skip to content

Commit

Permalink
fs/fatfs: Add modlog to fatfs
Browse files Browse the repository at this point in the history
This adds logging to fatfs.

Signed-off-by: Jerzy Kasenberg <[email protected]>
  • Loading branch information
kasjer committed Nov 27, 2024
1 parent d431da8 commit 83acd0a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
33 changes: 33 additions & 0 deletions fs/fatfs/src/mynewt_glue.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <stdlib.h>
#include <string.h>
#include "os/mynewt.h"
#include <modlog/modlog.h>
#include <hal/hal_flash.h>
#include <disk/disk.h>
#include <flash_map/flash_map.h>
Expand Down Expand Up @@ -261,6 +262,8 @@ fatfs_open(const char *path, uint8_t access_flags, struct fs_file **out_fs_file)
char *fatfs_path = NULL;
int rc;

FATFS_LOG_DEBUG("Open file %s\n", path);

file = malloc(sizeof(struct fatfs_file));
if (!file) {
rc = FS_ENOMEM;
Expand Down Expand Up @@ -307,8 +310,12 @@ fatfs_open(const char *path, uint8_t access_flags, struct fs_file **out_fs_file)
out:
free(fatfs_path);
if (rc != FS_EOK) {
FATFS_LOG_ERROR("File %s open failed %d\n", path, rc);

if (file) free(file);
if (out_file) free(out_file);
} else {
FATFS_LOG_DEBUG("File %s opened %p\n", path, *out_fs_file);
}
return rc;
}
Expand All @@ -319,6 +326,8 @@ fatfs_close(struct fs_file *fs_file)
FRESULT res = FR_OK;
FIL *file = ((struct fatfs_file *) fs_file)->file;

FATFS_LOG_DEBUG("Open file %p\n", fs_file);

if (file != NULL) {
res = f_close(file);
free(file);
Expand All @@ -334,6 +343,8 @@ fatfs_seek(struct fs_file *fs_file, uint32_t offset)
FRESULT res;
FIL *file = ((struct fatfs_file *) fs_file)->file;

FATFS_LOG_DEBUG("File %p seek %u\n", fs_file, offset);

res = f_lseek(file, offset);
return fatfs_to_vfs_error(res);
}
Expand All @@ -355,6 +366,8 @@ fatfs_file_len(const struct fs_file *fs_file, uint32_t *out_len)

*out_len = (uint32_t) f_size(file);

FATFS_LOG_DEBUG("File %p len %u\n", fs_file, *out_len);

return FS_EOK;
}

Expand All @@ -366,6 +379,8 @@ fatfs_read(struct fs_file *fs_file, uint32_t len, void *out_data,
FIL *file = ((struct fatfs_file *) fs_file)->file;
UINT uint_len;

FATFS_LOG_DEBUG("File %p read %u\n", fs_file, len);

res = f_read(file, out_data, len, &uint_len);
*out_len = uint_len;
return fatfs_to_vfs_error(res);
Expand All @@ -378,6 +393,8 @@ fatfs_write(struct fs_file *fs_file, const void *data, int len)
UINT out_len;
FIL *file = ((struct fatfs_file *) fs_file)->file;

FATFS_LOG_DEBUG("File %p write %u\n", fs_file, len);

res = f_write(file, data, len, &out_len);
if (len != out_len) {
return FS_EFULL;
Expand All @@ -391,6 +408,8 @@ fatfs_flush(struct fs_file *fs_file)
FRESULT res;
FIL *file = ((struct fatfs_file *)fs_file)->file;

FATFS_LOG_DEBUG("Flush %p\n", fs_file);

res = f_sync(file);

return fatfs_to_vfs_error(res);
Expand All @@ -402,6 +421,8 @@ fatfs_unlink(const char *path)
FRESULT res;
char *fatfs_path;

FATFS_LOG_INFO("Unlink %s\n", path);

fatfs_path = fatfs_path_from_fs_path(path);

if (fatfs_path == NULL) {
Expand All @@ -420,6 +441,8 @@ fatfs_rename(const char *from, const char *to)
char *fatfs_src_path;
char *fatfs_dst_path;

FATFS_LOG_INFO("Rename %s to %s\n", from, to);

fatfs_src_path = fatfs_path_from_fs_path(from);
fatfs_dst_path = fatfs_path_from_fs_path(to);

Expand All @@ -442,6 +465,8 @@ fatfs_mkdir(const char *path)
FRESULT res;
char *fatfs_path;

FATFS_LOG_INFO("Mkdir %s\n", path);

fatfs_path = fatfs_path_from_fs_path(path);

if (fatfs_path == NULL) {
Expand Down Expand Up @@ -486,9 +511,13 @@ fatfs_opendir(const char *path, struct fs_dir **out_fs_dir)
*out_fs_dir = (struct fs_dir *)dir;
rc = FS_EOK;

FATFS_LOG_INFO("Open dir %s -> %p\n", path, *out_fs_dir);

out:
free(fatfs_path);
if (rc != FS_EOK) {
FATFS_LOG_ERROR("Open dir %s failed %d\n", path, rc);

if (dir) free(dir);
if (out_dir) free(out_dir);
}
Expand All @@ -501,6 +530,8 @@ fatfs_readdir(struct fs_dir *fs_dir, struct fs_dirent **out_fs_dirent)
FRESULT res;
FATFS_DIR *dir = ((struct fatfs_dir *) fs_dir)->dir;

FATFS_LOG_DEBUG("Read dir %p\n", fs_dir);

dirent.fops = &fatfs_ops;
res = f_readdir(dir, &dirent.filinfo);
if (res != FR_OK) {
Expand All @@ -520,6 +551,8 @@ fatfs_closedir(struct fs_dir *fs_dir)
FRESULT res;
FATFS_DIR *dir = ((struct fatfs_dir *) fs_dir)->dir;

FATFS_LOG_INFO("Close dir %p\n", fs_dir);

res = f_closedir(dir);
free(dir);
free(fs_dir);
Expand Down
12 changes: 12 additions & 0 deletions fs/fatfs/syscfg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,15 @@ syscfg.defs:
description: >
Sysinit stage for FATFS functionality.
value: 200

FATFS_LOG_MODULE:
description: 'Numeric module ID to use for FATFS log messages.'
value: 253
FATFS_LOG_LVL:
description: 'Minimum level for the FATFS log.'
value: 2

syscfg.logs:
FATFS_LOG:
module: MYNEWT_VAL(FATFS_LOG_MODULE)
level: MYNEWT_VAL(FATFS_LOG_LVL)

0 comments on commit 83acd0a

Please sign in to comment.