Skip to content

Commit 72fab82

Browse files
committed
Added API level logging and logging configuration options
1 parent 5e4ef9f commit 72fab82

File tree

3 files changed

+113
-3
lines changed

3 files changed

+113
-3
lines changed

LittleFileSystem.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
#include "mbed.h"
2323
#include "LittleFileSystem.h"
2424
#include "errno.h"
25+
extern "C" {
26+
#include "lfs.h"
27+
#include "lfs_util.h"
28+
}
2529

2630

2731
////// Conversion functions //////
@@ -124,9 +128,11 @@ LittleFileSystem::~LittleFileSystem() {
124128

125129
int LittleFileSystem::mount(BlockDevice *bd) {
126130
_mutex.lock();
131+
LFS_INFO("mount(%p)", bd);
127132
_bd = bd;
128133
int err = _bd->init();
129134
if (err) {
135+
LFS_INFO("mount -> %d", err);
130136
_mutex.unlock();
131137
return err;
132138
}
@@ -156,37 +162,45 @@ int LittleFileSystem::mount(BlockDevice *bd) {
156162
}
157163

158164
err = lfs_mount(&_lfs, &_config);
165+
LFS_INFO("mount -> %d", lfs_toerror(err));
159166
_mutex.unlock();
160167
return lfs_toerror(err);
161168
}
162169

163170
int LittleFileSystem::unmount() {
164171
_mutex.lock();
172+
LFS_INFO("unmount()", "");
165173
if (_bd) {
166174
int err = lfs_unmount(&_lfs);
167175
if (err) {
176+
LFS_INFO("unmount -> %d", lfs_toerror(err));
168177
_mutex.unlock();
169178
return lfs_toerror(err);
170179
}
171180

172181
err = _bd->deinit();
173182
if (err) {
183+
LFS_INFO("unmount -> %d", err);
174184
_mutex.unlock();
175185
return err;
176186
}
177187

178188
_bd = NULL;
179189
}
180190

191+
LFS_INFO("unmount -> %d", 0);
181192
_mutex.unlock();
182193
return 0;
183194
}
184195

185196
int LittleFileSystem::format(BlockDevice *bd,
186197
lfs_size_t read_size, lfs_size_t prog_size,
187198
lfs_size_t block_size, lfs_size_t lookahead) {
199+
LFS_INFO("format(%p, %d, %d, %d, %d)",
200+
bd, read_size, prog_size, block_size, lookahead);
188201
int err = bd->init();
189202
if (err) {
203+
LFS_INFO("format -> %d", err);
190204
return err;
191205
}
192206

@@ -219,78 +233,95 @@ int LittleFileSystem::format(BlockDevice *bd,
219233

220234
err = lfs_format(&_lfs, &_config);
221235
if (err) {
236+
LFS_INFO("format -> %d", lfs_toerror(err));
222237
return lfs_toerror(err);
223238
}
224239

225240
err = bd->deinit();
226241
if (err) {
242+
LFS_INFO("format -> %d", err);
227243
return err;
228244
}
229245

246+
LFS_INFO("format -> %d", 0);
230247
return 0;
231248
}
232249

233250
int LittleFileSystem::reformat(BlockDevice *bd) {
234251
_mutex.lock();
252+
LFS_INFO("reformat(%p)", bd);
235253
if (_bd) {
236254
if (!bd) {
237255
bd = _bd;
238256
}
239257

240258
int err = unmount();
241259
if (err) {
260+
LFS_INFO("reformat -> %d", err);
242261
_mutex.unlock();
243262
return err;
244263
}
245264
}
246265

247266
if (!bd) {
267+
LFS_INFO("reformat -> %d", -ENODEV);
248268
_mutex.unlock();
249269
return -ENODEV;
250270
}
251271

252272
int err = LittleFileSystem::format(bd,
253273
_read_size, _prog_size, _block_size, _lookahead);
254274
if (err) {
275+
LFS_INFO("reformat -> %d", err);
255276
_mutex.unlock();
256277
return err;
257278
}
258279

259280
err = mount(bd);
260281
if (err) {
282+
LFS_INFO("reformat -> %d", err);
261283
_mutex.unlock();
262284
return err;
263285
}
264286

287+
LFS_INFO("reformat -> %d", 0);
265288
_mutex.unlock();
266289
return 0;
267290
}
268291

269292
int LittleFileSystem::remove(const char *filename) {
270293
_mutex.lock();
294+
LFS_INFO("remove(\"%s\")", filename);
271295
int err = lfs_remove(&_lfs, filename);
296+
LFS_INFO("remove -> %d", lfs_toerror(err));
272297
_mutex.unlock();
273298
return lfs_toerror(err);
274299
}
275300

276301
int LittleFileSystem::rename(const char *oldname, const char *newname) {
277302
_mutex.lock();
303+
LFS_INFO("rename(\"%s\", \"%s\")", oldname, newname);
278304
int err = lfs_rename(&_lfs, oldname, newname);
305+
LFS_INFO("rename -> %d", lfs_toerror(err));
279306
_mutex.unlock();
280307
return lfs_toerror(err);
281308
}
282309

283310
int LittleFileSystem::mkdir(const char *name, mode_t mode) {
284311
_mutex.lock();
312+
LFS_INFO("mkdir(\"%s\", 0x%x)", name, mode);
285313
int err = lfs_mkdir(&_lfs, name);
314+
LFS_INFO("mkdir -> %d", lfs_toerror(err));
286315
_mutex.unlock();
287316
return lfs_toerror(err);
288317
}
289318

290319
int LittleFileSystem::stat(const char *name, struct stat *st) {
291320
struct lfs_info info;
292321
_mutex.lock();
322+
LFS_INFO("stat(\"%s\", %p)", name, st);
293323
int err = lfs_stat(&_lfs, name, &info);
324+
LFS_INFO("stat -> %d", lfs_toerror(err));
294325
_mutex.unlock();
295326
st->st_size = info.size;
296327
st->st_mode = lfs_tomode(info.type);
@@ -303,15 +334,19 @@ int LittleFileSystem::file_open(fs_file_t *file, const char *path, int flags) {
303334
lfs_file_t *f = new lfs_file_t;
304335
*file = f;
305336
_mutex.lock();
337+
LFS_INFO("file_open(%p, \"%s\", 0x%x)", *file, path, flags);
306338
int err = lfs_file_open(&_lfs, f, path, lfs_fromflags(flags));
339+
LFS_INFO("file_open -> %d", lfs_toerror(err));
307340
_mutex.unlock();
308341
return lfs_toerror(err);
309342
}
310343

311344
int LittleFileSystem::file_close(fs_file_t file) {
312345
lfs_file_t *f = (lfs_file_t *)file;
313346
_mutex.lock();
347+
LFS_INFO("file_close(%p)", file);
314348
int err = lfs_file_close(&_lfs, f);
349+
LFS_INFO("file_close -> %d", lfs_toerror(err));
315350
_mutex.unlock();
316351
delete f;
317352
return lfs_toerror(err);
@@ -320,47 +355,59 @@ int LittleFileSystem::file_close(fs_file_t file) {
320355
ssize_t LittleFileSystem::file_read(fs_file_t file, void *buffer, size_t len) {
321356
lfs_file_t *f = (lfs_file_t *)file;
322357
_mutex.lock();
358+
LFS_INFO("file_read(%p, %p, %d)", file, buffer, len);
323359
lfs_ssize_t res = lfs_file_read(&_lfs, f, buffer, len);
360+
LFS_INFO("file_read -> %d", lfs_toerror(res));
324361
_mutex.unlock();
325362
return lfs_toerror(res);
326363
}
327364

328365
ssize_t LittleFileSystem::file_write(fs_file_t file, const void *buffer, size_t len) {
329366
lfs_file_t *f = (lfs_file_t *)file;
330367
_mutex.lock();
368+
LFS_INFO("file_write(%p, %p, %d)", file, buffer, len);
331369
lfs_ssize_t res = lfs_file_write(&_lfs, f, buffer, len);
370+
LFS_INFO("file_write -> %d", lfs_toerror(res));
332371
_mutex.unlock();
333372
return lfs_toerror(res);
334373
}
335374

336375
int LittleFileSystem::file_sync(fs_file_t file) {
337376
lfs_file_t *f = (lfs_file_t *)file;
338377
_mutex.lock();
378+
LFS_INFO("file_sync(%p)", file);
339379
int err = lfs_file_sync(&_lfs, f);
380+
LFS_INFO("file_sync -> %d", lfs_toerror(err));
340381
_mutex.unlock();
341382
return lfs_toerror(err);
342383
}
343384

344385
off_t LittleFileSystem::file_seek(fs_file_t file, off_t offset, int whence) {
345386
lfs_file_t *f = (lfs_file_t *)file;
346387
_mutex.lock();
388+
LFS_INFO("file_seek(%p, %d, %d)", file, offset, whence);
347389
off_t res = lfs_file_seek(&_lfs, f, offset, lfs_fromwhence(whence));
390+
LFS_INFO("file_seek -> %d", lfs_toerror(res));
348391
_mutex.unlock();
349392
return lfs_toerror(res);
350393
}
351394

352395
off_t LittleFileSystem::file_tell(fs_file_t file) {
353396
lfs_file_t *f = (lfs_file_t *)file;
354397
_mutex.lock();
398+
LFS_INFO("file_tell(%p)", file);
355399
off_t res = lfs_file_tell(&_lfs, f);
400+
LFS_INFO("file_tell -> %d", lfs_toerror(res));
356401
_mutex.unlock();
357402
return lfs_toerror(res);
358403
}
359404

360405
off_t LittleFileSystem::file_size(fs_file_t file) {
361406
lfs_file_t *f = (lfs_file_t *)file;
362407
_mutex.lock();
408+
LFS_INFO("file_size(%p)", file);
363409
off_t res = lfs_file_size(&_lfs, f);
410+
LFS_INFO("file_size -> %d", lfs_toerror(res));
364411
_mutex.unlock();
365412
return lfs_toerror(res);
366413
}
@@ -371,15 +418,19 @@ int LittleFileSystem::dir_open(fs_dir_t *dir, const char *path) {
371418
lfs_dir_t *d = new lfs_dir_t;
372419
*dir = d;
373420
_mutex.lock();
421+
LFS_INFO("dir_open(%p, \"%s\")", *dir, path);
374422
int err = lfs_dir_open(&_lfs, d, path);
423+
LFS_INFO("dir_open -> %d", lfs_toerror(err));
375424
_mutex.unlock();
376425
return lfs_toerror(err);
377426
}
378427

379428
int LittleFileSystem::dir_close(fs_dir_t dir) {
380429
lfs_dir_t *d = (lfs_dir_t *)dir;
381430
_mutex.lock();
431+
LFS_INFO("dir_close(%p)", dir);
382432
int err = lfs_dir_close(&_lfs, d);
433+
LFS_INFO("dir_close -> %d", lfs_toerror(err));
383434
_mutex.unlock();
384435
delete d;
385436
return lfs_toerror(err);
@@ -389,7 +440,9 @@ ssize_t LittleFileSystem::dir_read(fs_dir_t dir, struct dirent *ent) {
389440
lfs_dir_t *d = (lfs_dir_t *)dir;
390441
struct lfs_info info;
391442
_mutex.lock();
443+
LFS_INFO("dir_read(%p, %p)", dir, ent);
392444
int res = lfs_dir_read(&_lfs, d, &info);
445+
LFS_INFO("dir_read -> %d", lfs_toerror(res));
393446
_mutex.unlock();
394447
if (res == 1) {
395448
ent->d_type = lfs_totype(info.type);
@@ -401,22 +454,28 @@ ssize_t LittleFileSystem::dir_read(fs_dir_t dir, struct dirent *ent) {
401454
void LittleFileSystem::dir_seek(fs_dir_t dir, off_t offset) {
402455
lfs_dir_t *d = (lfs_dir_t *)dir;
403456
_mutex.lock();
457+
LFS_INFO("dir_seek(%p, %d)", dir, offset);
404458
lfs_dir_seek(&_lfs, d, offset);
459+
LFS_INFO("dir_seek -> void", "");
405460
_mutex.unlock();
406461
}
407462

408463
off_t LittleFileSystem::dir_tell(fs_dir_t dir) {
409464
lfs_dir_t *d = (lfs_dir_t *)dir;
410465
_mutex.lock();
466+
LFS_INFO("dir_tell(%p)", dir);
411467
lfs_soff_t res = lfs_dir_tell(&_lfs, d);
468+
LFS_INFO("dir_tell -> %d", lfs_toerror(res));
412469
_mutex.unlock();
413470
return lfs_toerror(res);
414471
}
415472

416473
void LittleFileSystem::dir_rewind(fs_dir_t dir) {
417474
lfs_dir_t *d = (lfs_dir_t *)dir;
418475
_mutex.lock();
476+
LFS_INFO("dir_rewind(%p)", dir);
419477
lfs_dir_rewind(&_lfs, d);
478+
LFS_INFO("dir_rewind -> void", "");
420479
_mutex.unlock();
421480
}
422481

littlefs/lfs_util.h

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,43 @@ void lfs_crc(uint32_t *crc, const void *buffer, size_t size);
9292
#ifdef __MBED__
9393
#include "mbed_debug.h"
9494
#else
95-
#define debug printf
95+
#define MBED_LFS_ENABLE_INFO false
96+
#define MBED_LFS_ENABLE_DEBUG true
97+
#define MBED_LFS_ENABLE_WARN true
98+
#define MBED_LFS_ENABLE_ERROR true
9699
#endif
97100

98-
#define LFS_DEBUG(fmt, ...) debug("lfs debug: " fmt "\n", __VA_ARGS__)
101+
#if MBED_LFS_ENABLE_INFO == true
102+
#define LFS_INFO(fmt, ...) printf("lfs info: " fmt "\n", __VA_ARGS__)
103+
#elif !defined(MBED_LFS_ENABLE_INFO)
104+
#define LFS_INFO(fmt, ...) debug("lfs info: " fmt "\n", __VA_ARGS__)
105+
#else
106+
#define LFS_INFO(fmt, ...)
107+
#endif
108+
109+
#if MBED_LFS_ENABLE_DEBUG == true
110+
#define LFS_DEBUG(fmt, ...) printf("lfs debug: " fmt "\n", __VA_ARGS__)
111+
#elif !defined(MBED_LFS_ENABLE_DEBUG)
112+
#define LFS_DEBUG(fmt, ...) debug("lfs debug: " fmt "\n", __VA_ARGS__)
113+
#else
114+
#define LFS_DEBUG(fmt, ...)
115+
#endif
116+
117+
#if MBED_LFS_ENABLE_WARN == true
118+
#define LFS_WARN(fmt, ...) printf("lfs warn: " fmt "\n", __VA_ARGS__)
119+
#elif !defined(MBED_LFS_ENABLE_WARN)
99120
#define LFS_WARN(fmt, ...) debug("lfs warn: " fmt "\n", __VA_ARGS__)
100-
#define LFS_ERROR(fmt, ...) debug("lfs error: " fmt "\n", __VA_ARGS__)
121+
#else
122+
#define LFS_WARN(fmt, ...)
123+
#endif
124+
125+
#if MBED_LFS_ENABLE_ERROR == true
126+
#define LFS_ERROR(fmt, ...) printf("lfs error: " fmt "\n", __VA_ARGS__)
127+
#elif !defined(MBED_LFS_ENABLE_ERROR)
128+
#define LFS_ERROR(fmt, ...) debug("lfs error: " fmt "\n", __VA_ARGS__)
129+
#else
130+
#define LFS_ERROR(fmt, ...)
131+
#endif
101132

102133

103134
#endif

mbed_lib.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,26 @@
2020
"macro_name": "MBED_LFS_LOOKAHEAD",
2121
"value": 512,
2222
"help": "Number of blocks to lookahead during block allocation. A larger lookahead reduces the number of passes required to allocate a block. The lookahead buffer requires only 1 bit per block so it can be quite large with little ram impact. Should be a multiple of 32."
23+
},
24+
"enable_info": {
25+
"macro_name": "MBED_LFS_ENABLE_INFO",
26+
"value": false,
27+
"help": "Enables info logging, true = enabled, false = disabled, null = disabled only in release builds"
28+
},
29+
"enable_debug": {
30+
"macro_name": "MBED_LFS_ENABLE_DEBUG",
31+
"value": null,
32+
"help": "Enables debug logging, true = enabled, false = disabled, null = disabled only in release builds"
33+
},
34+
"enable_warn": {
35+
"macro_name": "MBED_LFS_ENABLE_WARN",
36+
"value": null,
37+
"help": "Enables warn logging, true = enabled, false = disabled, null = disabled only in release builds"
38+
},
39+
"enable_error": {
40+
"macro_name": "MBED_LFS_ENABLE_ERROR",
41+
"value": null,
42+
"help": "Enables error logging, true = enabled, false = disabled, null = disabled only in release builds"
2343
}
2444
}
2545
}

0 commit comments

Comments
 (0)